From f45aabf843922480ac374285e3242a397b30a200 Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Sun, 19 Nov 2017 14:36:46 +0100 Subject: [PATCH] eth, internal: Changes in response to review --- eth/api.go | 39 +++++++++++++++++++++++++++++++------ internal/web3ext/web3ext.go | 10 +++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/eth/api.go b/eth/api.go index bf06796ce4..81138b7862 100644 --- a/eth/api.go +++ b/eth/api.go @@ -637,17 +637,40 @@ func storageRangeAt(st state.Trie, start []byte, maxResult int) StorageRangeResu return result } -func (api *PrivateDebugAPI) GetDirtyAccountsByNumber(startNum, endNum uint64) ([]common.Address, error) { - return api.getDirtyAccounts(api.eth.blockchain.GetBlockByNumber(startNum), api.eth.blockchain.GetBlockByNumber(endNum)) +// GetModifiedAccountsByumber returns all accounts that have changed between the +// two blocks specified. A change is defined as a difference in nonce, balance, +// code hash, or storage hash. +// With one parameter, returns the list of accounts modified in the specified block. +func (api *PrivateDebugAPI) GetModifiedAccountsByNumber(startNum, endNum *uint64) ([]common.Address, error) { + // With one argument, compare x-1 with x + if endNum == nil { + return api.getModifiedAccounts( + api.eth.blockchain.GetBlockByNumber(*startNum - 1), + api.eth.blockchain.GetBlockByNumber(*startNum)) + } else { + return api.getModifiedAccounts( + api.eth.blockchain.GetBlockByNumber(*startNum), + api.eth.blockchain.GetBlockByNumber(*endNum)) + } } -func (api *PrivateDebugAPI) GetDirtyAccountsByHash(startHash, endHash common.Hash) ([]common.Address, error) { - return api.getDirtyAccounts( +// GetModifiedAccountsByHash returns all accounts that have changed between the +// two blocks specified. A change is defined as a difference in nonce, balance, +// code hash, or storage hash. +func (api *PrivateDebugAPI) GetModifiedAccountsByHash(startHash, endHash common.Hash) ([]common.Address, error) { + return api.getModifiedAccounts( api.eth.blockchain.GetBlockByHash(startHash), api.eth.blockchain.GetBlockByHash(endHash)) } -func (api *PrivateDebugAPI) getDirtyAccounts(startBlock, endBlock *types.Block) ([]common.Address, error) { +func (api *PrivateDebugAPI) getModifiedAccounts(startBlock, endBlock *types.Block) ([]common.Address, error) { + if startBlock == nil || endBlock == nil { + return nil, fmt.Errorf("Both start block and end block are required") + } + if startBlock.Number().Uint64() >= endBlock.Number().Uint64() { + return nil, fmt.Errorf("Start block height (%d) must be less than end block height (%d)", startBlock.Number().Uint64(), endBlock.Number().Uint64()) + } + oldTrie, err := trie.NewSecure(startBlock.Root(), api.eth.chainDb, 0) if err != nil { return nil, err @@ -662,7 +685,11 @@ func (api *PrivateDebugAPI) getDirtyAccounts(startBlock, endBlock *types.Block) iter := trie.NewIterator(diff) var dirty []common.Address for iter.Next() { - dirty = append(dirty, common.BytesToAddress(newTrie.GetKey(iter.Key))) + key := newTrie.GetKey(iter.Key) + if(key == nil) { + return nil, fmt.Errorf("No preimage found for hash %x", iter.Key) + } + dirty = append(dirty, common.BytesToAddress(key)) } return dirty, nil } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index fa81472e2c..ef0d2b4e6e 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -355,16 +355,16 @@ web3._extend({ params: 5, }), new web3._extend.Method({ - name: 'getDirtyAccountsByNumber', - call: 'debug_getDirtyAccountsByNumber', + name: 'getModifiedAccountsByNumber', + call: 'debug_getModifiedAccountsByNumber', params: 2, inputFormatter: [null, null], }), new web3._extend.Method({ - name: 'getDirtyAccountsByHash', - call: 'debug_getDirtyAccountsByHash', + name: 'getModifiedAccountsByHash', + call: 'debug_getModifiedAccountsByHash', params: 2, - inputFormatter:[web3._extend.formatters.inputAddressFormatter, web3._extend.formatters.inputAddressFormatter], + inputFormatter:[null, null], }), ], properties: []