From 649e01f4d3e66ee4a30f744a2b397da58c0fc646 Mon Sep 17 00:00:00 2001 From: Delweng Zheng Date: Wed, 16 Oct 2019 21:44:17 +0800 Subject: [PATCH] eth,core: debug_iteratorAccountAt use rpc.BlockNumerOrHash --- core/state/dump.go | 8 ++------ eth/api.go | 39 ++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/core/state/dump.go b/core/state/dump.go index 0ef112fc3d..1ed02ea1c1 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -51,7 +51,7 @@ type iterativeDump struct { *json.Encoder } -// IteratorDump is a +// IterationDump is an implementation for iterating over data type IteratorDump struct { Root string `json:"root"` Accounts map[common.Address]DumpAccount `json:"accounts"` @@ -147,12 +147,8 @@ func (s *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingP } c.onAccount(addr, account) - if maxResults <= 0 { - continue - } - count++ - if count >= maxResults { + if maxResults > 0 && count >= maxResults { if it.Next() { nextKey = it.Key } diff --git a/eth/api.go b/eth/api.go index 3bc7ff85a3..80fa42242e 100644 --- a/eth/api.go +++ b/eth/api.go @@ -305,36 +305,45 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error const IteratorAccountMaxResults = 1000 // IteratorAccountAt enumerates all accounts in the given block and start point in paging request -func (api *PublicDebugAPI) IteratorAccountAt(blockNr rpc.BlockNumber, start []byte, maxResults int, nocode, nostorage, incompletes bool) (state.IteratorDump, error) { +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 blockNr == 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 blockNr == rpc.LatestBlockNumber { - block = api.eth.blockchain.CurrentBlock() + 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 { - block = api.eth.blockchain.GetBlockByNumber(uint64(blockNr)) + 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 #%d not found", blockNr) + 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 { + if maxResults > IteratorAccountMaxResults || maxResults <= 0 { maxResults = IteratorAccountMaxResults } - return stateDb.IteratorDump(nocode, nostorage, incompletes, start, maxResults), nil }