forked from subquery/subql
-
Notifications
You must be signed in to change notification settings - Fork 8
[Tron Support] Do not panic on Invalid state roots #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wizdave97
wants to merge
7
commits into
subquery:main
Choose a base branch
from
polytope-labs:david/tron
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e228368
don't panic on invalid stateroots
Wizdave97 6f7410d
update fix
Wizdave97 9a96a85
fix
Wizdave97 0555700
more fixes for tron
Wizdave97 1e829fa
add comments
Wizdave97 6024ed1
modular approach to tron fixes
Wizdave97 40fc41b
review fixes
Wizdave97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2020-2025 SubQuery Pte Ltd authors & contributors | ||
| // SPDX-License-Identifier: GPL-3.0 | ||
|
|
||
| import { Networkish } from '@ethersproject/networks'; | ||
| import { WebSocketProvider } from '@ethersproject/providers'; | ||
| import { JsonRpcBatchProvider } from '../json-rpc-batch-provider'; | ||
| import { JsonRpcProvider } from '../json-rpc-provider'; | ||
| import { ConnectionInfo } from '../web'; | ||
| import { applyTronParamTransforms } from './tron-utils'; | ||
|
|
||
| /** | ||
| * Tron-specific JsonRpcProvider that applies parameter transformations | ||
| * to handle Tron RPC limitations. | ||
| */ | ||
| export class TronJsonRpcProvider extends JsonRpcProvider { | ||
| constructor(url: string | ConnectionInfo, network?: Networkish) { | ||
| super(url, network); | ||
| } | ||
|
|
||
| async send(method: string, params: Array<any>): Promise<any> { | ||
| const chainId = this.network?.chainId ?? 0; | ||
| const cleanedParams = applyTronParamTransforms(method, params, chainId); | ||
| return super.send(method, cleanedParams); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tron-specific JsonRpcBatchProvider that applies parameter transformations | ||
| * to handle Tron RPC limitations. | ||
| */ | ||
| export class TronJsonRpcBatchProvider extends JsonRpcBatchProvider { | ||
| constructor(url: string | ConnectionInfo, network?: Networkish) { | ||
| super(url, network); | ||
| } | ||
|
|
||
| async send(method: string, params: Array<any>): Promise<any> { | ||
| const chainId = this.network?.chainId ?? 0; | ||
| const cleanedParams = applyTronParamTransforms(method, params, chainId); | ||
| return super.send(method, cleanedParams); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tron-specific WebSocketProvider that applies parameter transformations | ||
| * to handle Tron RPC limitations. | ||
| */ | ||
| export class TronWsProvider extends WebSocketProvider { | ||
| constructor(url: string, network?: Networkish) { | ||
| super(url, network); | ||
| } | ||
|
|
||
| async send(method: string, params: Array<any>): Promise<any> { | ||
| const chainId = this.network?.chainId ?? 0; | ||
| const cleanedParams = applyTronParamTransforms(method, params, chainId); | ||
| return super.send(method, cleanedParams); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2020-2025 SubQuery Pte Ltd authors & contributors | ||
| // SPDX-License-Identifier: GPL-3.0 | ||
|
|
||
| // Tron chain IDs: Mainnet, Shasta testnet, Nile testnet | ||
| export const TRON_CHAIN_IDS = [728126428, 2494104990, 3448148188]; | ||
|
|
||
| // Methods that accept transaction objects as parameters | ||
| export const TRON_TRANSACTION_METHODS = ['eth_call']; | ||
|
|
||
| /** | ||
| * Methods that require block number parameter and the index of that parameter | ||
| * | ||
| * IMPORTANT TRON LIMITATION: | ||
| * Tron RPC rejects numeric block tags with error code -32602 and message: | ||
| * "QUANTITY not supported, just support TAG as latest" | ||
| * | ||
| * We forcibly replace ALL numeric block parameters with 'latest' for Tron chains. | ||
| * This means all state-querying methods (eth_call, eth_getBalance, eth_getCode, | ||
| * eth_getTransactionCount, eth_getStorageAt) will return tip/current state rather | ||
| * than point-in-time state for historical indexing. | ||
| * | ||
| * OPERATIONAL IMPACT: | ||
| * SubQL indexers CANNOT obtain historical state via these calls on Tron chains. | ||
| * All state queries will always return the current/latest state regardless of | ||
| * the block number requested. | ||
| */ | ||
| export const TRON_BLOCK_NUMBER_METHODS: Record<string, number> = { | ||
| eth_call: 1, | ||
| eth_getStorageAt: 2, | ||
| eth_getBalance: 1, | ||
| eth_getCode: 1, | ||
| eth_getTransactionCount: 1, | ||
| }; | ||
|
|
||
| /** | ||
| * Remove type and accessList from transaction objects in params for Tron chains | ||
| */ | ||
| export function cleanParamsForTron( | ||
| params: Array<any>, | ||
| chainId: number, | ||
| ): Array<any> { | ||
| if (!TRON_CHAIN_IDS.includes(chainId)) { | ||
| return params; | ||
| } | ||
|
|
||
| return params.map((param) => { | ||
| if (param && typeof param === 'object' && !Array.isArray(param)) { | ||
| const cleaned = { ...param }; | ||
| delete cleaned.type; | ||
| delete cleaned.accessList; | ||
| return cleaned; | ||
| } | ||
| return param; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Replace block number parameter with 'latest' for Tron chains | ||
| * | ||
| * CRITICAL TRON RPC LIMITATION: | ||
| * The Tron RPC implementation does not support numeric block tags (QUANTITY). | ||
| * When a numeric block number is provided, Tron returns JSON-RPC error -32602 | ||
| * with message: "QUANTITY not supported, just support TAG as latest" | ||
| * | ||
| * This function forcibly replaces any block number parameter with 'latest' to | ||
| * prevent this error. However, this introduces a significant limitation: | ||
| * | ||
| * LIMITATION: | ||
| * All state-querying methods will return the CURRENT/TIP state rather than | ||
| * point-in-time historical state. This means: | ||
| * - eth_call: Will execute against current state, not historical state | ||
| * - eth_getBalance: Returns current balance, not historical balance | ||
| * - eth_getCode: Returns current contract code, not historical code | ||
| * - eth_getTransactionCount: Returns current nonce, not historical nonce | ||
| * - eth_getStorageAt: Returns current storage value, not historical value | ||
| * | ||
| * IMPACT ON INDEXERS: | ||
| * SubQL indexers running on Tron chains CANNOT query historical state via | ||
| * these RPC methods. Indexers must rely solely on event logs and block data | ||
| * for historical information. Any smart contract state queries will always | ||
| * reflect the current state, not the state at the block being indexed. | ||
| */ | ||
| export function replaceBlockNumberForTron( | ||
| method: string, | ||
| params: Array<any>, | ||
| chainId: number, | ||
| ): Array<any> { | ||
| if (!TRON_CHAIN_IDS.includes(chainId)) { | ||
| return params; | ||
| } | ||
|
|
||
| const blockNumberIndex = TRON_BLOCK_NUMBER_METHODS[method]; | ||
| if (blockNumberIndex === undefined || params.length <= blockNumberIndex) { | ||
| return params; | ||
| } | ||
|
|
||
| // Always replace with 'latest' for Tron chains | ||
| // This prevents the -32602 error: "QUANTITY not supported, just support TAG as latest" | ||
| const cleaned = [...params]; | ||
| cleaned[blockNumberIndex] = 'latest'; | ||
| return cleaned; | ||
| } | ||
|
|
||
| /** | ||
| * Apply all Tron-specific parameter transformations for the given method. | ||
| * This combines both transaction object cleaning and block number replacement. | ||
| * | ||
| * NOTE: eth_call intentionally appears in both TRON_TRANSACTION_METHODS and | ||
| * TRON_BLOCK_NUMBER_METHODS, so both cleanParamsForTron and | ||
| * replaceBlockNumberForTron are applied in sequence. This dual-pass behavior | ||
| * is intentional and not a duplication bug. | ||
| */ | ||
| export function applyTronParamTransforms( | ||
| method: string, | ||
| params: Array<any>, | ||
| chainId: number, | ||
| ): Array<any> { | ||
| let result = params; | ||
| if (TRON_TRANSACTION_METHODS.includes(method)) { | ||
| result = cleanParamsForTron(result, chainId); | ||
| } | ||
| if (TRON_BLOCK_NUMBER_METHODS[method] !== undefined) { | ||
| result = replaceBlockNumberForTron(method, result, chainId); | ||
| } | ||
| return result; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.