-
Notifications
You must be signed in to change notification settings - Fork 21.9k
eth, internal/web3ext: implement storageRangeAt #3407
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -547,3 +547,87 @@ func (api *PrivateDebugAPI) TraceTransaction(ctx context.Context, txHash common. | |
| } | ||
| return nil, errors.New("database inconsistency") | ||
| } | ||
|
|
||
| type StorageRangeAtResult struct { | ||
| Storage map[string]string `json:"storage"` | ||
| Complete bool `json:"complete"` | ||
| } | ||
|
|
||
| // StorageRangeAt returns the storage at the given block height and | ||
| // transaction index (exclusive). It may be limited using the | ||
| // storageAddress, storageAddressEnd (inclusive) and maxResult parameters. | ||
| // | ||
| // StorageRangeAt is currently limited and requires needless iterators | ||
| // due to the limitation of the trie iterator. At present we can't start | ||
| // iterating from any given key, thus we need to loop over any key that's | ||
| // not inclusive in the range of start and end. | ||
| // | ||
| // BUG: Because the state objects make use of the secure storage, iterating | ||
| // trie keys is out of order and will be returned the exact same way. | ||
| func (api *PrivateDebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, storageAddressStart, storageAddressEnd common.Hash, maxResult int) (interface{}, error) { | ||
| block := api.eth.BlockChain().GetBlockByHash(blockHash) | ||
| if block == nil { | ||
| return nil, fmt.Errorf("block %x not found", blockHash) | ||
| } | ||
| // Create the state database to mutate and eventually trace | ||
| parent := api.eth.BlockChain().GetBlock(block.ParentHash(), block.NumberU64()-1) | ||
| if parent == nil { | ||
| return nil, fmt.Errorf("block parent %x not found", block.ParentHash()) | ||
| } | ||
| stateDb, err := api.eth.BlockChain().StateAt(parent.Root()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| result := StorageRangeAtResult{Storage: make(map[string]string)} | ||
|
|
||
| signer := types.MakeSigner(api.config, block.Number()) | ||
| // Mutate the state and trace the selected transaction | ||
| done: | ||
| for idx, tx := range block.Transactions() { | ||
| // Assemble the transaction call message | ||
| msg, err := tx.AsMessage(signer) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("sender retrieval failed: %v", err) | ||
| } | ||
| context := core.NewEVMContext(msg, block.Header(), api.eth.BlockChain()) | ||
|
|
||
| // Mutate the state if we haven't reached the tracing transaction yet | ||
| if idx < txIndex { | ||
| vmenv := vm.NewEnvironment(context, stateDb, api.config, vm.Config{}) | ||
| _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("mutation failed: %v", err) | ||
| } | ||
| stateDb.DeleteSuicides() | ||
| continue | ||
| } | ||
|
|
||
| stateObject := stateDb.GetStateObject(contractAddress) | ||
| // We need to check if the object exists again. It might have been deleted in between the | ||
| // transactions. | ||
| if stateObject != nil { | ||
| trie := stateObject.GetTrie(api.eth.ChainDb()) | ||
| it := trie.Iterator() | ||
|
|
||
| for it.Next() && len(result.Storage) < maxResult { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be better to use ForEachStorage here instead of exposing the trie |
||
| var ( | ||
| value common.Hash | ||
| key = trie.GetKey(it.Key) | ||
| ) | ||
|
|
||
| if bytes.Compare(storageAddressStart[:], key[:]) > 0 { | ||
| continue | ||
| } | ||
| if bytes.Compare(storageAddressEnd[:], key[:]) < 0 { | ||
| break done | ||
| } | ||
|
|
||
| rlp.DecodeBytes(it.Value, &value) | ||
| result.Storage[common.ToHex(key)] = value.Hex() | ||
| } | ||
| result.Complete = !it.Next() | ||
| } | ||
| } | ||
| return result, nil | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract this code into its own function. This basically recomputes the state up to a certain transaction
and is duplicated in the tracing code just above.