Skip to content

Commit e674dea

Browse files
marcusreinMichaelMacaulayidalithbbenface
authored
Subgraph Best Practices 5 and 6 (#825)
* timeseries included * updated subgraph best practices ... and added links to bottom of each best practice * best practices link changes * formatting * Update website/pages/en/cookbook/avoid-eth-calls.mdx Co-authored-by: Idalith <[email protected]> * Fixing links * Update website/pages/en/cookbook/derivedfrom.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/derivedfrom.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/timeseries.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/timeseries.mdx Co-authored-by: Idalith <[email protected]> * Update website/pages/en/cookbook/grafting-hotfix.mdx Co-authored-by: Idalith <[email protected]> * Ida's feedback * Update website/pages/en/cookbook/grafting-hotfix.mdx * pnpm check * Fix build by reproducing `en/cookbook` file structure in other languages --------- Co-authored-by: Michael Macaulay <[email protected]> Co-authored-by: Idalith <[email protected]> Co-authored-by: benface <[email protected]>
1 parent 1420c3c commit e674dea

File tree

100 files changed

+9230
-6261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+9230
-6261
lines changed

website/pages/ar/cookbook/base-testnet.mdx

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
Subgraph Best Practice 6 - Use Grafting for Quick Hotfix Deployment
3+
---
4+
5+
## TLDR
6+
7+
Grafting is a powerful feature in subgraph development that allows you to build and deploy new subgraphs while reusing the indexed data from existing ones.
8+
9+
### Overview
10+
11+
This feature enables quick deployment of hotfixes for critical issues, eliminating the need to re-index the entire subgraph from scratch. By preserving historical data, grafting minimizes downtime and ensures continuity in data services.
12+
13+
## Benefits of Grafting for Hotfixes
14+
15+
1. **Rapid Deployment**
16+
17+
- **Minimize Downtime**: When a subgraph encounters a critical error and stops indexing, grafting enables you to deploy a fix immediately without waiting for re-indexing.
18+
- **Immediate Recovery**: The new subgraph continues from the last indexed block, ensuring that data services remain uninterrupted.
19+
20+
2. **Data Preservation**
21+
22+
- **Reuse Historical Data**: Grafting copies the existing data from the base subgraph, so you don’t lose valuable historical records.
23+
- **Consistency**: Maintains data continuity, which is crucial for applications relying on consistent historical data.
24+
25+
3. **Efficiency**
26+
- **Save Time and Resources**: Avoids the computational overhead of re-indexing large datasets.
27+
- **Focus on Fixes**: Allows developers to concentrate on resolving issues rather than managing data recovery.
28+
29+
## Best Practices When Using Grafting for Hotfixes
30+
31+
1. **Initial Deployment Without Grafting**
32+
33+
- **Start Clean**: Always deploy your initial subgraph without grafting to ensure that it’s stable and functions as expected.
34+
- **Test Thoroughly**: Validate the subgraph’s performance to minimize the need for future hotfixes.
35+
36+
2. **Implementing the Hotfix with Grafting**
37+
38+
- **Identify the Issue**: When a critical error occurs, determine the block number of the last successfully indexed event.
39+
- **Create a New Subgraph**: Develop a new subgraph that includes the hotfix.
40+
- **Configure Grafting**: Use grafting to copy data up to the identified block number from the failed subgraph.
41+
- **Deploy Quickly**: Publish the grafted subgraph to restore service as soon as possible.
42+
43+
3. **Post-Hotfix Actions**
44+
45+
- **Monitor Performance**: Ensure the grafted subgraph is indexing correctly and the hotfix resolves the issue.
46+
- **Republish Without Grafting**: Once stable, deploy a new version of the subgraph without grafting for long-term maintenance.
47+
> Note: Relying on grafting indefinitely is not recommended as it can complicate future updates and maintenance.
48+
- **Update References**: Redirect any services or applications to use the new, non-grafted subgraph.
49+
50+
4. **Important Considerations**
51+
- **Careful Block Selection**: Choose the graft block number carefully to prevent data loss.
52+
- **Tip**: Use the block number of the last correctly processed event.
53+
- **Use Deployment ID**: Ensure you reference the Deployment ID of the base subgraph, not the Subgraph ID.
54+
- **Note**: The Deployment ID is the unique identifier for a specific subgraph deployment.
55+
- **Feature Declaration**: Remember to declare grafting in the subgraph manifest under features.
56+
57+
## Example: Deploying a Hotfix with Grafting
58+
59+
Suppose you have a subgraph tracking a smart contract that has stopped indexing due to a critical error. Here’s how you can use grafting to deploy a hotfix.
60+
61+
1. **Failed Subgraph Manifest (subgraph.yaml)**
62+
63+
```yaml
64+
specVersion: 1.0.0
65+
schema:
66+
file: ./schema.graphql
67+
dataSources:
68+
- kind: ethereum/contract
69+
name: OldSmartContract
70+
network: sepolia
71+
source:
72+
address: '0xOldContractAddress'
73+
abi: Lock
74+
startBlock: 5000000
75+
mapping:
76+
kind: ethereum/events
77+
apiVersion: 0.0.7
78+
language: wasm/assemblyscript
79+
entities:
80+
- Withdrawal
81+
abis:
82+
- name: Lock
83+
file: ./abis/OldLock.json
84+
eventHandlers:
85+
- event: Withdrawal(uint256,uint256)
86+
handler: handleOldWithdrawal
87+
file: ./src/old-lock.ts
88+
```
89+
90+
2. **New Grafted Subgraph Manifest (subgraph.yaml)**
91+
```yaml
92+
specVersion: 1.0.0
93+
schema:
94+
file: ./schema.graphql
95+
dataSources:
96+
- kind: ethereum/contract
97+
name: NewSmartContract
98+
network: sepolia
99+
source:
100+
address: '0xNewContractAddress'
101+
abi: Lock
102+
startBlock: 6000001 # Block after the last indexed block
103+
mapping:
104+
kind: ethereum/events
105+
apiVersion: 0.0.7
106+
language: wasm/assemblyscript
107+
entities:
108+
- Withdrawal
109+
abis:
110+
- name: Lock
111+
file: ./abis/Lock.json
112+
eventHandlers:
113+
- event: Withdrawal(uint256,uint256)
114+
handler: handleWithdrawal
115+
file: ./src/lock.ts
116+
features:
117+
- grafting
118+
graft:
119+
base: QmBaseDeploymentID # Deployment ID of the failed subgraph
120+
block: 6000000 # Last successfully indexed block
121+
```
122+
123+
**Explanation:**
124+
125+
- **Data Source Update**: The new subgraph points to 0xNewContractAddress, which may be a fixed version of the smart contract.
126+
- **Start Block**: Set to one block after the last successfully indexed block to avoid reprocessing the error.
127+
- **Grafting Configuration**:
128+
- **base**: Deployment ID of the failed subgraph.
129+
- **block**: Block number where grafting should begin.
130+
131+
3. **Deployment Steps**
132+
133+
- **Update the Code**: Implement the hotfix in your mapping scripts (e.g., handleWithdrawal).
134+
- **Adjust the Manifest**: As shown above, update the `subgraph.yaml` with grafting configurations.
135+
- **Deploy the Subgraph**:
136+
- Authenticate with the Graph CLI.
137+
- Deploy the new subgraph using `graph deploy`.
138+
139+
4. **Post-Deployment**
140+
- **Verify Indexing**: Check that the subgraph is indexing correctly from the graft point.
141+
- **Monitor Data**: Ensure that new data is being captured and the hotfix is effective.
142+
- **Plan for Republish**: Schedule the deployment of a non-grafted version for long-term stability.
143+
144+
## Warnings and Cautions
145+
146+
While grafting is a powerful tool for deploying hotfixes quickly, there are specific scenarios where it should be avoided to maintain data integrity and ensure optimal performance.
147+
148+
- **Incompatible Schema Changes**: If your hotfix requires altering the type of existing fields or removing fields from your schema, grafting is not suitable. Grafting expects the new subgraph’s schema to be compatible with the base subgraph’s schema. Incompatible changes can lead to data inconsistencies and errors because the existing data won’t align with the new schema.
149+
- **Significant Mapping Logic Overhauls**: When the hotfix involves substantial modifications to your mapping logic—such as changing how events are processed or altering handler functions—grafting may not function correctly. The new logic might not be compatible with the data processed under the old logic, leading to incorrect data or failed indexing.
150+
- **Deployments to The Graph Network**: Grafting is not recommended for subgraphs intended for The Graph’s decentralized network (mainnet). It can complicate indexing and may not be fully supported by all Indexers, potentially causing unexpected behavior or increased costs. For mainnet deployments, it’s safer to re-index the subgraph from scratch to ensure full compatibility and reliability.
151+
152+
### Risk Management
153+
154+
- **Data Integrity**: Incorrect block numbers can lead to data loss or duplication.
155+
- **Testing**: Always test grafting in a development environment before deploying to production.
156+
157+
## Conclusion
158+
159+
Grafting is an effective strategy for deploying hotfixes in subgraph development, enabling you to:
160+
161+
- **Quickly Recover** from critical errors without re-indexing.
162+
- **Preserve Historical Data**, maintaining continuity for applications and users.
163+
- **Ensure Service Availability** by minimizing downtime during critical fixes.
164+
165+
However, it’s important to use grafting judiciously and follow best practices to mitigate risks. After stabilizing your subgraph with the hotfix, plan to deploy a non-grafted version to ensure long-term maintainability.
166+
167+
## Additional Resources
168+
169+
- **[Grafting Documentation](/cookbook/grafting/)**: Replace a Contract and Keep its History With Grafting
170+
- **[Understanding Deployment IDs](/querying/querying-by-subgraph-id-vs-deployment-id/)**: Learn the difference between Deployment ID and Subgraph ID.
171+
172+
By incorporating grafting into your subgraph development workflow, you can enhance your ability to respond to issues swiftly, ensuring that your data services remain robust and reliable.
173+
174+
## Subgraph Best Practices 1-6
175+
176+
1. [Improve Query Speed with Subgraph Pruning](/cookbook/pruning/)
177+
178+
2. [Improve Indexing and Query Responsiveness by Using @derivedFrom](/cookbook/derivedfrom/)
179+
180+
3. [Improve Indexing and Query Performance by Using Immutable Entities and Bytes as IDs](/cookbook/immutable-entities-bytes-as-ids/)
181+
182+
4. [Improve Indexing Speed by Avoiding `eth_calls`](/cookbook/avoid-eth-calls/)
183+
184+
5. [Simplify and Optimize with Timeseries and Aggregations](/cookbook/timeseries/)
185+
186+
6. [Use Grafting for Quick Hotfix Deployment](/cookbook/grafting-hotfix/)

0 commit comments

Comments
 (0)