github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/dump.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2014 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package state 28 29 import ( 30 "encoding/json" 31 "fmt" 32 "time" 33 34 "github.com/MetalBlockchain/subnet-evm/core/types" 35 "github.com/MetalBlockchain/subnet-evm/trie" 36 "github.com/ethereum/go-ethereum/common" 37 "github.com/ethereum/go-ethereum/common/hexutil" 38 "github.com/ethereum/go-ethereum/log" 39 "github.com/ethereum/go-ethereum/rlp" 40 ) 41 42 // DumpConfig is a set of options to control what portions of the statewill be 43 // iterated and collected. 44 type DumpConfig struct { 45 SkipCode bool 46 SkipStorage bool 47 OnlyWithAddresses bool 48 Start []byte 49 Max uint64 50 } 51 52 // DumpCollector interface which the state trie calls during iteration 53 type DumpCollector interface { 54 // OnRoot is called with the state root 55 OnRoot(common.Hash) 56 // OnAccount is called once for each account in the trie 57 OnAccount(common.Address, DumpAccount) 58 } 59 60 // DumpAccount represents an account in the state. 61 type DumpAccount struct { 62 Balance string `json:"balance"` 63 Nonce uint64 `json:"nonce"` 64 Root hexutil.Bytes `json:"root"` 65 CodeHash hexutil.Bytes `json:"codeHash"` 66 Code hexutil.Bytes `json:"code,omitempty"` 67 Storage map[common.Hash]string `json:"storage,omitempty"` 68 Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode 69 SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key 70 71 } 72 73 // Dump represents the full dump in a collected format, as one large map. 74 type Dump struct { 75 Root string `json:"root"` 76 Accounts map[common.Address]DumpAccount `json:"accounts"` 77 } 78 79 // OnRoot implements DumpCollector interface 80 func (d *Dump) OnRoot(root common.Hash) { 81 d.Root = fmt.Sprintf("%x", root) 82 } 83 84 // OnAccount implements DumpCollector interface 85 func (d *Dump) OnAccount(addr common.Address, account DumpAccount) { 86 d.Accounts[addr] = account 87 } 88 89 // IteratorDump is an implementation for iterating over data. 90 type IteratorDump struct { 91 Root string `json:"root"` 92 Accounts map[common.Address]DumpAccount `json:"accounts"` 93 Next []byte `json:"next,omitempty"` // nil if no more accounts 94 } 95 96 // OnRoot implements DumpCollector interface 97 func (d *IteratorDump) OnRoot(root common.Hash) { 98 d.Root = fmt.Sprintf("%x", root) 99 } 100 101 // OnAccount implements DumpCollector interface 102 func (d *IteratorDump) OnAccount(addr common.Address, account DumpAccount) { 103 d.Accounts[addr] = account 104 } 105 106 // iterativeDump is a DumpCollector-implementation which dumps output line-by-line iteratively. 107 type iterativeDump struct { 108 *json.Encoder 109 } 110 111 // OnAccount implements DumpCollector interface 112 func (d iterativeDump) OnAccount(addr common.Address, account DumpAccount) { 113 dumpAccount := &DumpAccount{ 114 Balance: account.Balance, 115 Nonce: account.Nonce, 116 Root: account.Root, 117 CodeHash: account.CodeHash, 118 Code: account.Code, 119 Storage: account.Storage, 120 SecureKey: account.SecureKey, 121 Address: nil, 122 } 123 if addr != (common.Address{}) { 124 dumpAccount.Address = &addr 125 } 126 d.Encode(dumpAccount) 127 } 128 129 // OnRoot implements DumpCollector interface 130 func (d iterativeDump) OnRoot(root common.Hash) { 131 d.Encode(struct { 132 Root common.Hash `json:"root"` 133 }{root}) 134 } 135 136 // DumpToCollector iterates the state according to the given options and inserts 137 // the items into a collector for aggregation or serialization. 138 func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) { 139 // Sanitize the input to allow nil configs 140 if conf == nil { 141 conf = new(DumpConfig) 142 } 143 var ( 144 missingPreimages int 145 accounts uint64 146 start = time.Now() 147 logged = time.Now() 148 ) 149 log.Info("Trie dumping started", "root", s.trie.Hash()) 150 c.OnRoot(s.trie.Hash()) 151 152 it := trie.NewIterator(s.trie.NodeIterator(conf.Start)) 153 for it.Next() { 154 var data types.StateAccount 155 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 156 panic(err) 157 } 158 account := DumpAccount{ 159 Balance: data.Balance.String(), 160 Nonce: data.Nonce, 161 Root: data.Root[:], 162 CodeHash: data.CodeHash, 163 SecureKey: it.Key, 164 } 165 addrBytes := s.trie.GetKey(it.Key) 166 if addrBytes == nil { 167 // Preimage missing 168 missingPreimages++ 169 if conf.OnlyWithAddresses { 170 continue 171 } 172 account.SecureKey = it.Key 173 } 174 addr := common.BytesToAddress(addrBytes) 175 obj := newObject(s, addr, data) 176 if !conf.SkipCode { 177 account.Code = obj.Code(s.db) 178 } 179 if !conf.SkipStorage { 180 account.Storage = make(map[common.Hash]string) 181 storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil)) 182 for storageIt.Next() { 183 _, content, _, err := rlp.Split(storageIt.Value) 184 if err != nil { 185 log.Error("Failed to decode the value returned by iterator", "error", err) 186 continue 187 } 188 account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content) 189 } 190 } 191 c.OnAccount(addr, account) 192 accounts++ 193 if time.Since(logged) > 8*time.Second { 194 log.Info("Trie dumping in progress", "at", it.Key, "accounts", accounts, 195 "elapsed", common.PrettyDuration(time.Since(start))) 196 logged = time.Now() 197 } 198 if conf.Max > 0 && accounts >= conf.Max { 199 if it.Next() { 200 nextKey = it.Key 201 } 202 break 203 } 204 } 205 if missingPreimages > 0 { 206 log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages) 207 } 208 log.Info("Trie dumping complete", "accounts", accounts, 209 "elapsed", common.PrettyDuration(time.Since(start))) 210 211 return nextKey 212 } 213 214 // RawDump returns the entire state an a single large object 215 func (s *StateDB) RawDump(opts *DumpConfig) Dump { 216 dump := &Dump{ 217 Accounts: make(map[common.Address]DumpAccount), 218 } 219 s.DumpToCollector(dump, opts) 220 return *dump 221 } 222 223 // Dump returns a JSON string representing the entire state as a single json-object 224 func (s *StateDB) Dump(opts *DumpConfig) []byte { 225 dump := s.RawDump(opts) 226 json, err := json.MarshalIndent(dump, "", " ") 227 if err != nil { 228 fmt.Println("Dump err", err) 229 } 230 return json 231 } 232 233 // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout 234 func (s *StateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) { 235 s.DumpToCollector(iterativeDump{output}, opts) 236 } 237 238 // IteratorDump dumps out a batch of accounts starts with the given start key 239 func (s *StateDB) IteratorDump(opts *DumpConfig) IteratorDump { 240 iterator := &IteratorDump{ 241 Accounts: make(map[common.Address]DumpAccount), 242 } 243 iterator.Next = s.DumpToCollector(iterator, opts) 244 return *iterator 245 }