eth: merge debug_iteratorAccountAt into debug_accountRangeAt

This commit is contained in:
Delweng Zheng 2019-11-17 19:18:33 +08:00 committed by Martin Holst Swende
parent 649e01f4d3
commit c9557ed6db
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -301,52 +301,6 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
return stateDb.RawDump(false, false, true), nil return stateDb.RawDump(false, false, true), nil
} }
// IteratorAccountMaxResults is the maximum number of results to be returned per request
const IteratorAccountMaxResults = 1000
// IteratorAccountAt enumerates all accounts in the given block and start point in paging request
func (api *PublicDebugAPI) IteratorAccountAt(blockNrOrHash rpc.BlockNumberOrHash, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
var stateDb *state.StateDB
var err error
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 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 state.IteratorDump{}, err
}
}
if maxResults > IteratorAccountMaxResults || maxResults <= 0 {
maxResults = IteratorAccountMaxResults
}
return stateDb.IteratorDump(nocode, nostorage, incompletes, start, maxResults), nil
}
// PrivateDebugAPI is the collection of Ethereum full node APIs exposed over // PrivateDebugAPI is the collection of Ethereum full node APIs exposed over
// the private debugging endpoint. // the private debugging endpoint.
type PrivateDebugAPI struct { type PrivateDebugAPI struct {
@ -397,70 +351,50 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs,
return results, nil 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 // AccountRangeMaxResults is the maximum number of results to be returned per call
const AccountRangeMaxResults = 256 const AccountRangeMaxResults = 256
// AccountRange enumerates all accounts in the latest state // AccountRangeAt enumerates all accounts in the given block and start point in paging request
func (api *PrivateDebugAPI) AccountRange(ctx context.Context, start *common.Hash, maxResults int) (AccountRangeResult, error) { func (api *PublicDebugAPI) AccountRangeAt(blockNrOrHash rpc.BlockNumberOrHash, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) {
var statedb *state.StateDB var stateDb *state.StateDB
var err error var err error
block := api.eth.blockchain.CurrentBlock()
if len(block.Transactions()) == 0 { if number, ok := blockNrOrHash.Number(); ok {
statedb, err = api.computeStateDB(block, defaultTraceReexec) if number == rpc.PendingBlockNumber {
if err != nil { // If we're dumping the pending state, we need to request
return AccountRangeResult{}, err // both the pending block as well as the pending state from
} // the miner and operate on those
_, stateDb = api.eth.miner.Pending()
} else { } else {
_, _, statedb, err = api.computeTxEnv(block.Hash(), len(block.Transactions())-1, 0) 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 { if err != nil {
return AccountRangeResult{}, err return state.IteratorDump{}, err
}
}
} 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 state.IteratorDump{}, err
} }
} }
trie, err := statedb.Database().OpenTrie(block.Header().Root) if maxResults > AccountRangeMaxResults || maxResults <= 0 {
if err != nil { maxResults = AccountRangeMaxResults
return AccountRangeResult{}, err
} }
return stateDb.IteratorDump(nocode, nostorage, incompletes, start, maxResults), nil
return accountRange(trie, start, maxResults)
} }
// StorageRangeResult is the result of a debug_storageRangeAt API call. // StorageRangeResult is the result of a debug_storageRangeAt API call.