eth: add debug_accountRange API (#19645)
This new API allows reading accounts and their content by address range. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
88
eth/api.go
88
eth/api.go
@ -351,70 +351,50 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs,
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// AccountRangeResult returns a mapping from the hash of an account addresses
|
||||
// to its preimage. It will return the JSON null if no preimage is found.
|
||||
// Since a query can return a limited amount of results, a "next" field is
|
||||
// also present for paging.
|
||||
type AccountRangeResult struct {
|
||||
Accounts map[common.Hash]*common.Address `json:"accounts"`
|
||||
Next common.Hash `json:"next"`
|
||||
}
|
||||
|
||||
func accountRange(st state.Trie, start *common.Hash, maxResults int) (AccountRangeResult, error) {
|
||||
if start == nil {
|
||||
start = &common.Hash{0}
|
||||
}
|
||||
it := trie.NewIterator(st.NodeIterator(start.Bytes()))
|
||||
result := AccountRangeResult{Accounts: make(map[common.Hash]*common.Address), Next: common.Hash{}}
|
||||
|
||||
if maxResults > AccountRangeMaxResults {
|
||||
maxResults = AccountRangeMaxResults
|
||||
}
|
||||
|
||||
for i := 0; i < maxResults && it.Next(); i++ {
|
||||
if preimage := st.GetKey(it.Key); preimage != nil {
|
||||
addr := &common.Address{}
|
||||
addr.SetBytes(preimage)
|
||||
result.Accounts[common.BytesToHash(it.Key)] = addr
|
||||
} else {
|
||||
result.Accounts[common.BytesToHash(it.Key)] = nil
|
||||
}
|
||||
}
|
||||
|
||||
if it.Next() {
|
||||
result.Next = common.BytesToHash(it.Key)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AccountRangeMaxResults is the maximum number of results to be returned per call
|
||||
const AccountRangeMaxResults = 256
|
||||
|
||||
// AccountRange enumerates all accounts in the latest state
|
||||
func (api *PrivateDebugAPI) AccountRange(ctx context.Context, start *common.Hash, maxResults int) (AccountRangeResult, error) {
|
||||
var statedb *state.StateDB
|
||||
// AccountRangeAt enumerates all accounts in the given block and start point in paging request
|
||||
func (api *PublicDebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
|
||||
var stateDb *state.StateDB
|
||||
var err error
|
||||
block := api.eth.blockchain.CurrentBlock()
|
||||
|
||||
if len(block.Transactions()) == 0 {
|
||||
statedb, err = api.computeStateDB(block, defaultTraceReexec)
|
||||
if err != nil {
|
||||
return AccountRangeResult{}, err
|
||||
if number, ok := blockNrOrHash.Number(); ok {
|
||||
if number == rpc.PendingBlockNumber {
|
||||
// If we're dumping the pending state, we need to request
|
||||
// both the pending block as well as the pending state from
|
||||
// the miner and operate on those
|
||||
_, stateDb = api.eth.miner.Pending()
|
||||
} else {
|
||||
var block *types.Block
|
||||
if number == rpc.LatestBlockNumber {
|
||||
block = api.eth.blockchain.CurrentBlock()
|
||||
} else {
|
||||
block = api.eth.blockchain.GetBlockByNumber(uint64(number))
|
||||
}
|
||||
if block == nil {
|
||||
return state.IteratorDump{}, fmt.Errorf("block #%d not found", number)
|
||||
}
|
||||
stateDb, err = api.eth.BlockChain().StateAt(block.Root())
|
||||
if err != nil {
|
||||
return state.IteratorDump{}, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, _, statedb, err = api.computeTxEnv(block.Hash(), len(block.Transactions())-1, 0)
|
||||
} else if hash, ok := blockNrOrHash.Hash(); ok {
|
||||
block := api.eth.blockchain.GetBlockByHash(hash)
|
||||
if block == nil {
|
||||
return state.IteratorDump{}, fmt.Errorf("block %s not found", hash.Hex())
|
||||
}
|
||||
stateDb, err = api.eth.BlockChain().StateAt(block.Root())
|
||||
if err != nil {
|
||||
return AccountRangeResult{}, err
|
||||
return state.IteratorDump{}, err
|
||||
}
|
||||
}
|
||||
|
||||
trie, err := statedb.Database().OpenTrie(block.Header().Root)
|
||||
if err != nil {
|
||||
return AccountRangeResult{}, err
|
||||
if maxResults > AccountRangeMaxResults || maxResults <= 0 {
|
||||
maxResults = AccountRangeMaxResults
|
||||
}
|
||||
|
||||
return accountRange(trie, start, maxResults)
|
||||
return stateDb.IteratorDump(nocode, nostorage, incompletes, start, maxResults), nil
|
||||
}
|
||||
|
||||
// StorageRangeResult is the result of a debug_storageRangeAt API call.
|
||||
@ -431,7 +411,7 @@ type storageEntry struct {
|
||||
}
|
||||
|
||||
// StorageRangeAt returns the storage at the given block height and transaction index.
|
||||
func (api *PrivateDebugAPI) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
|
||||
func (api *PrivateDebugAPI) StorageRangeAt(blockHash common.Hash, txIndex int, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) {
|
||||
_, _, statedb, err := api.computeTxEnv(blockHash, txIndex, 0)
|
||||
if err != nil {
|
||||
return StorageRangeResult{}, err
|
||||
|
Reference in New Issue
Block a user