dump: add option to continue even if preimages are missing

This commit is contained in:
Martin Holst Swende 2019-05-21 12:18:47 +02:00 committed by Péter Szilágyi
parent 7515d1d989
commit a9ddecbf00
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
7 changed files with 56 additions and 40 deletions

View file

@ -105,7 +105,7 @@ func stateTestCmd(ctx *cli.Context) error {
// Test failed, mark as so and dump any state to aid debugging // Test failed, mark as so and dump any state to aid debugging
result.Pass, result.Error = false, err.Error() result.Pass, result.Error = false, err.Error()
if ctx.GlobalBool(DumpFlag.Name) && state != nil { if ctx.GlobalBool(DumpFlag.Name) && state != nil {
dump := state.RawDump(false, false) dump := state.RawDump(false, false, true)
result.State = &dump result.State = &dump
} }
} }

View file

@ -165,6 +165,7 @@ Remove blockchain and state databases`,
utils.IterativeOutputFlag, utils.IterativeOutputFlag,
utils.ExcludeCodeFlag, utils.ExcludeCodeFlag,
utils.ExcludeStorageFlag, utils.ExcludeStorageFlag,
utils.IncludeMissingPreimagesFlag,
}, },
Category: "BLOCKCHAIN COMMANDS", Category: "BLOCKCHAIN COMMANDS",
Description: ` Description: `
@ -507,6 +508,7 @@ func dump(ctx *cli.Context) error {
defer stack.Close() defer stack.Close()
chain, chainDb := utils.MakeChain(ctx, stack) chain, chainDb := utils.MakeChain(ctx, stack)
defer chainDb.Close()
for _, arg := range ctx.Args() { for _, arg := range ctx.Args() {
var block *types.Block var block *types.Block
if hashish(arg) { if hashish(arg) {
@ -525,14 +527,18 @@ func dump(ctx *cli.Context) error {
} }
excludeCode := ctx.GlobalBool(utils.ExcludeCodeFlag.Name) excludeCode := ctx.GlobalBool(utils.ExcludeCodeFlag.Name)
excludeStorage := ctx.GlobalBool(utils.ExcludeStorageFlag.Name) excludeStorage := ctx.GlobalBool(utils.ExcludeStorageFlag.Name)
includeMissing := ctx.GlobalBool(utils.IncludeMissingPreimagesFlag.Name)
if ctx.GlobalBool(utils.IterativeOutputFlag.Name) { if ctx.GlobalBool(utils.IterativeOutputFlag.Name) {
state.IterativeDump(excludeCode, excludeStorage, json.NewEncoder(os.Stdout)) state.IterativeDump(excludeCode, excludeStorage, !includeMissing, json.NewEncoder(os.Stdout))
} else { } else {
fmt.Printf("%s\n", state.Dump(excludeCode, excludeStorage)) if includeMissing {
fmt.Printf("If you want to include accounts with missing preimages, you need iterative output, since" +
" otherwise the accounts will overwrite each other in the resulting mapping.")
}
fmt.Printf("%v %s\n", includeMissing, state.Dump(excludeCode, excludeStorage, false))
} }
} }
} }
chainDb.Close()
return nil return nil
} }

View file

@ -193,6 +193,7 @@ var (
utils.IterativeOutputFlag, utils.IterativeOutputFlag,
utils.ExcludeCodeFlag, utils.ExcludeCodeFlag,
utils.ExcludeStorageFlag, utils.ExcludeStorageFlag,
utils.IncludeMissingPreimagesFlag,
} }
) )

View file

@ -204,6 +204,10 @@ var (
Name: "dump.nostorage", Name: "dump.nostorage",
Usage: "When set, exclude storage entries (saves db lookups)", Usage: "When set, exclude storage entries (saves db lookups)",
} }
IncludeMissingPreimagesFlag = cli.BoolFlag{
Name: "dump.includeincomplete",
Usage: "When set, include also those we do not have address of (missing preimage)",
}
ExcludeCodeFlag = cli.BoolFlag{ ExcludeCodeFlag = cli.BoolFlag{
Name: "dump.nocode", Name: "dump.nocode",
Usage: "When set, exclude contract code (saves db lookups)", Usage: "When set, exclude contract code (saves db lookups)",

View file

@ -19,6 +19,7 @@ package state
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -35,6 +36,8 @@ type DumpAccount struct {
Code string `json:"code"` Code string `json:"code"`
Storage map[common.Hash]string `json:"storage"` Storage map[common.Hash]string `json:"storage"`
Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
} }
// Dump represents the full dump in a collected format, as one large map // Dump represents the full dump in a collected format, as one large map
@ -68,6 +71,7 @@ func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
CodeHash: account.CodeHash, CodeHash: account.CodeHash,
Code: account.Code, Code: account.Code,
Storage: account.Storage, Storage: account.Storage,
SecureKey: account.SecureKey,
Address: nil, Address: nil,
} }
if addr != (common.Address{}) { if addr != (common.Address{}) {
@ -81,24 +85,17 @@ func (self iterativeDump) onRoot(root common.Hash) {
}{root}) }{root})
} }
func (self *StateDB) dump(c collector, excludeCode, excludeStorage bool) { func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
emptyAddress := (common.Address{}) emptyAddress := (common.Address{})
missingPreimages := 0 missingPreimages := 0
c.onRoot(self.trie.Hash()) c.onRoot(self.trie.Hash())
it := trie.NewIterator(self.trie.NodeIterator(nil)) it := trie.NewIterator(self.trie.NodeIterator(nil))
for it.Next() { for it.Next() {
addr := common.BytesToAddress(self.trie.GetKey(it.Key))
if emptyAddress == addr {
// We don't have the preimage. All accounts missing preimages
// will be 'mapped' and overwrite the same entry, which is quite useless.
// Make note and continue
missingPreimages++
continue
}
var data Account var data Account
if err := rlp.DecodeBytes(it.Value, &data); err != nil { if err := rlp.DecodeBytes(it.Value, &data); err != nil {
panic(err) panic(err)
} }
addr := common.BytesToAddress(self.trie.GetKey(it.Key))
obj := newObject(nil, addr, data) obj := newObject(nil, addr, data)
account := DumpAccount{ account := DumpAccount{
Balance: data.Balance.String(), Balance: data.Balance.String(),
@ -106,6 +103,14 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage bool) {
Root: common.Bytes2Hex(data.Root[:]), Root: common.Bytes2Hex(data.Root[:]),
CodeHash: common.Bytes2Hex(data.CodeHash), CodeHash: common.Bytes2Hex(data.CodeHash),
} }
if emptyAddress == addr {
// Preimage missing
missingPreimages++
if excludeMissingPreimages {
continue
}
account.SecureKey = it.Key
}
if !excludeCode { if !excludeCode {
account.Code = common.Bytes2Hex(obj.Code(self.db)) account.Code = common.Bytes2Hex(obj.Code(self.db))
} }
@ -124,17 +129,17 @@ func (self *StateDB) dump(c collector, excludeCode, excludeStorage bool) {
} }
// RawDump returns the entire state an a single large object // RawDump returns the entire state an a single large object
func (self *StateDB) RawDump(excludeCode, excludeStorage bool) Dump { func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
dump := &Dump{ dump := &Dump{
Accounts: make(map[common.Address]DumpAccount), Accounts: make(map[common.Address]DumpAccount),
} }
self.dump(dump, excludeCode, excludeStorage) self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
return *dump return *dump
} }
// Dump returns a JSON string representing the entire state as a single json-object // Dump returns a JSON string representing the entire state as a single json-object
func (self *StateDB) Dump(excludeCode, excludeStorage bool) []byte { func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
dump := self.RawDump(excludeCode, excludeStorage) dump := self.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
json, err := json.MarshalIndent(dump, "", " ") json, err := json.MarshalIndent(dump, "", " ")
if err != nil { if err != nil {
fmt.Println("dump err", err) fmt.Println("dump err", err)
@ -143,6 +148,6 @@ func (self *StateDB) Dump(excludeCode, excludeStorage bool) []byte {
} }
// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
func (self *StateDB) IterativeDump(excludeCode, excludeStorage bool, output *json.Encoder) { func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
self.dump(iterativeDump(*output), excludeCode, excludeStorage) self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages)
} }

View file

@ -52,11 +52,11 @@ func (s *StateSuite) TestDump(c *checker.C) {
s.state.Commit(false) s.state.Commit(false)
// check that dump contains the state objects that are in trie // check that dump contains the state objects that are in trie
got := string(s.state.Dump(false, false)) got := string(s.state.Dump(false, false, true))
want := `{ want := `{
"root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2", "root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2",
"accounts": { "accounts": {
"0000000000000000000000000000000000000001": { "0x0000000000000000000000000000000000000001": {
"balance": "22", "balance": "22",
"nonce": 0, "nonce": 0,
"root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
@ -64,7 +64,7 @@ func (s *StateSuite) TestDump(c *checker.C) {
"code": "", "code": "",
"storage": {} "storage": {}
}, },
"0000000000000000000000000000000000000002": { "0x0000000000000000000000000000000000000002": {
"balance": "44", "balance": "44",
"nonce": 0, "nonce": 0,
"root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
@ -72,7 +72,7 @@ func (s *StateSuite) TestDump(c *checker.C) {
"code": "", "code": "",
"storage": {} "storage": {}
}, },
"0000000000000000000000000000000000000102": { "0x0000000000000000000000000000000000000102": {
"balance": "0", "balance": "0",
"nonce": 0, "nonce": 0,
"root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",

View file

@ -266,7 +266,7 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
// both the pending block as well as the pending state from // both the pending block as well as the pending state from
// the miner and operate on those // the miner and operate on those
_, stateDb := api.eth.miner.Pending() _, stateDb := api.eth.miner.Pending()
return stateDb.RawDump(false, false), nil return stateDb.RawDump(false, false, true), nil
} }
var block *types.Block var block *types.Block
if blockNr == rpc.LatestBlockNumber { if blockNr == rpc.LatestBlockNumber {
@ -281,7 +281,7 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
if err != nil { if err != nil {
return state.Dump{}, err return state.Dump{}, err
} }
return stateDb.RawDump(false, false), nil return stateDb.RawDump(false, false, true), nil
} }
// PrivateDebugAPI is the collection of Ethereum full node APIs exposed over // PrivateDebugAPI is the collection of Ethereum full node APIs exposed over