eth: accountRangeAt: only allow enumerating accounts in latest block. make use of preimage db.

This commit is contained in:
Jared Wasinger 2018-12-05 14:11:15 +00:00 committed by Guillaume Ballet
parent 6d85892579
commit 89940c09c1
2 changed files with 15 additions and 15 deletions

View file

@ -28,6 +28,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -334,28 +335,29 @@ func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]*BadBlockArgs,
return results, nil return results, nil
} }
type addressMap map[common.Hash]common.Address
type AccountRangeResult struct { type AccountRangeResult struct {
AddressMap addressMap `json:"addressMap"` Addresses []common.Address `json:"addresses"`
Next common.Address `json:"next"`
} }
func accountRange(st state.Trie, start *common.Hash, maxResult int) (AccountRangeResult, error) { func accountRange(st state.Trie, start *common.Address, maxResult int) (AccountRangeResult, error) {
it := trie.NewIterator(st.NodeIterator(start[:])) it := trie.NewIterator(st.NodeIterator(crypto.Keccak256(start[:])))
result := AccountRangeResult{AddressMap: addressMap{}} result := AccountRangeResult{Addresses: []common.Address{}, Next: common.Address{}}
for i := 0; i < maxResult && it.Next(); i++ { for i := 0; i < maxResult && it.Next(); i++ {
key := st.GetKey(it.Key) if preimage := st.GetKey(it.Key); preimage != nil {
// If key is nil, that means it wasn't found in the preimage database. result.Addresses = append(result.Addresses, common.BytesToAddress(preimage))
// This is not a problem, because we still return the hash of the key together with
// address zero and the client can very easily determine that the hash of the addres zero
// is not matching, which means it wasn't found.
result.AddressMap[common.BytesToHash(it.Key)] = common.BytesToAddress(key)
} }
}
if it.Next() {
result.Next = common.BytesToAddress(st.GetKey(it.Key))
}
return result, nil return result, nil
} }
//block hash or number, tx index, start address hash, max results //block hash or number, tx index, start address hash, max results
func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, blockNr rpc.BlockNumber, txIndex int, startAddr *common.Hash, maxResults int) (AccountRangeResult, error) { func (api *PrivateDebugAPI) AccountRangeAt(ctx context.Context, txIndex int, startAddr *common.Address, maxResults int) (AccountRangeResult, error) {
var statedb *state.StateDB = nil var statedb *state.StateDB = nil
var err error = nil var err error = nil
var block = api.eth.blockchain.CurrentBlock() var block = api.eth.blockchain.CurrentBlock()

View file

@ -19,9 +19,7 @@ package eth
import ( import (
"reflect" "reflect"
"testing" "testing"
"math/big"
"github.com/ethereum/go-ethereum/crypto"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"