github.com/devkononov/go-func@v0.0.0-20190722084534-f14392d369a7/core/state/dump.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "encoding/json" 21 "fmt" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/hexutil" 25 "github.com/ethereum/go-ethereum/log" 26 "github.com/ethereum/go-ethereum/rlp" 27 "github.com/ethereum/go-ethereum/trie" 28 ) 29 30 // DumpAccount represents an account in the state 31 type DumpAccount struct { 32 Balance string `json:"balance"` 33 Nonce uint64 `json:"nonce"` 34 Root string `json:"root"` 35 CodeHash string `json:"codeHash"` 36 Code string `json:"code,omitempty"` 37 Storage map[common.Hash]string `json:"storage,omitempty"` 38 Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode 39 SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key 40 41 } 42 43 // Dump represents the full dump in a collected format, as one large map 44 type Dump struct { 45 Root string `json:"root"` 46 Accounts map[common.Address]DumpAccount `json:"accounts"` 47 } 48 49 // iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively 50 type iterativeDump json.Encoder 51 52 // Collector interface which the state trie calls during iteration 53 type collector interface { 54 onRoot(common.Hash) 55 onAccount(common.Address, DumpAccount) 56 } 57 58 func (self *Dump) onRoot(root common.Hash) { 59 self.Root = fmt.Sprintf("%x", root) 60 } 61 62 func (self *Dump) onAccount(addr common.Address, account DumpAccount) { 63 self.Accounts[addr] = account 64 } 65 66 func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) { 67 dumpAccount := &DumpAccount{ 68 Balance: account.Balance, 69 Nonce: account.Nonce, 70 Root: account.Root, 71 CodeHash: account.CodeHash, 72 Code: account.Code, 73 Storage: account.Storage, 74 SecureKey: account.SecureKey, 75 Address: nil, 76 } 77 if addr != (common.Address{}) { 78 dumpAccount.Address = &addr 79 } 80 (*json.Encoder)(&self).Encode(dumpAccount) 81 } 82 func (self iterativeDump) onRoot(root common.Hash) { 83 (*json.Encoder)(&self).Encode(struct { 84 Root common.Hash `json:"root"` 85 }{root}) 86 } 87 88 func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) { 89 emptyAddress := (common.Address{}) 90 missingPreimages := 0 91 c.onRoot(self.trie.Hash()) 92 it := trie.NewIterator(self.trie.NodeIterator(nil)) 93 for it.Next() { 94 var data Account 95 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 96 panic(err) 97 } 98 addr := common.BytesToAddress(self.trie.GetKey(it.Key)) 99 obj := newObject(nil, addr, data) 100 account := DumpAccount{ 101 Balance: data.Balance.String(), 102 Nonce: data.Nonce, 103 Root: common.Bytes2Hex(data.Root[:]), 104 CodeHash: common.Bytes2Hex(data.CodeHash), 105 } 106 if emptyAddress == addr { 107 // Preimage missing 108 missingPreimages++ 109 if excludeMissingPreimages { 110 continue 111 } 112 account.SecureKey = it.Key 113 } 114 if !excludeCode { 115 account.Code = common.Bytes2Hex(obj.Code(self.db)) 116 } 117 if !excludeStorage { 118 account.Storage = make(map[common.Hash]string) 119 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 120 for storageIt.Next() { 121 account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 122 } 123 } 124 c.onAccount(addr, account) 125 } 126 if missingPreimages > 0 { 127 log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages) 128 } 129 } 130 131 // RawDump returns the entire state an a single large object 132 func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump { 133 dump := &Dump{ 134 Accounts: make(map[common.Address]DumpAccount), 135 } 136 self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages) 137 return *dump 138 } 139 140 // Dump returns a JSON string representing the entire state as a single json-object 141 func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte { 142 dump := self.RawDump(excludeCode, excludeStorage, excludeMissingPreimages) 143 json, err := json.MarshalIndent(dump, "", " ") 144 if err != nil { 145 fmt.Println("dump err", err) 146 } 147 return json 148 } 149 150 // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout 151 func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) { 152 self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages) 153 }