github.com/Night-mk/quorum@v21.1.0+incompatible/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 _, content, _, err := rlp.Split(storageIt.Value) 122 if err != nil { 123 log.Error("Failed to decode the value returned by iterator", "error", err) 124 continue 125 } 126 account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content) 127 } 128 } 129 c.onAccount(addr, account) 130 } 131 if missingPreimages > 0 { 132 log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages) 133 } 134 } 135 136 // RawDump returns the entire state an a single large object 137 func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump { 138 dump := &Dump{ 139 Accounts: make(map[common.Address]DumpAccount), 140 } 141 self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages) 142 return *dump 143 } 144 145 // Dump returns a JSON string representing the entire state as a single json-object 146 func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte { 147 dump := self.RawDump(excludeCode, excludeStorage, excludeMissingPreimages) 148 json, err := json.MarshalIndent(dump, "", " ") 149 if err != nil { 150 fmt.Println("dump err", err) 151 } 152 return json 153 } 154 155 func (self *StateDB) DumpAddress(address common.Address) (DumpAccount, bool) { 156 if !self.Exist(address) { 157 return DumpAccount{}, false 158 } 159 160 obj := self.getStateObject(address) 161 account := DumpAccount{ 162 Balance: obj.data.Balance.String(), 163 Nonce: obj.data.Nonce, 164 Root: common.Bytes2Hex(obj.data.Root[:]), 165 CodeHash: common.Bytes2Hex(obj.data.CodeHash), 166 Code: common.Bytes2Hex(obj.Code(self.db)), 167 Storage: make(map[common.Hash]string), 168 } 169 170 noDataIssue := true 171 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 172 for storageIt.Next() { 173 _, content, _, err := rlp.Split(storageIt.Value) 174 if err != nil { 175 noDataIssue = false 176 log.Error("Failed to decode the value returned by iterator", "error", err) 177 break 178 } 179 account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content) 180 } 181 182 return account, noDataIssue 183 } 184 185 // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout 186 func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) { 187 self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages) 188 }