github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/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/rlp" 25 "github.com/ethereum/go-ethereum/trie" 26 ) 27 28 type DumpAccount struct { 29 Balance string `json:"balance"` 30 Nonce uint64 `json:"nonce"` 31 Root string `json:"root"` 32 CodeHash string `json:"codeHash"` 33 Code string `json:"code"` 34 Storage map[string]string `json:"storage"` 35 } 36 37 type Dump struct { 38 Root string `json:"root"` 39 Accounts map[string]DumpAccount `json:"accounts"` 40 } 41 42 func (self *StateDB) RawDump() Dump { 43 dump := Dump{ 44 Root: fmt.Sprintf("%x", self.trie.Hash()), 45 Accounts: make(map[string]DumpAccount), 46 } 47 48 it := trie.NewIterator(self.trie.NodeIterator(nil)) 49 for it.Next() { 50 addr := self.trie.GetKey(it.Key) 51 var data Account 52 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 53 panic(err) 54 } 55 56 obj := newObject(nil, common.BytesToAddress(addr), data) 57 account := DumpAccount{ 58 Balance: data.Balance.String(), 59 Nonce: data.Nonce, 60 Root: common.Bytes2Hex(data.Root[:]), 61 CodeHash: common.Bytes2Hex(data.CodeHash), 62 Code: common.Bytes2Hex(obj.Code(self.db)), 63 Storage: make(map[string]string), 64 } 65 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 66 for storageIt.Next() { 67 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 68 } 69 dump.Accounts[common.Bytes2Hex(addr)] = account 70 } 71 return dump 72 } 73 func (self *StateDB) RawDumpAcccount(address common.Address) Dump { 74 dump := Dump{ 75 Root: fmt.Sprintf("%x", self.trie.Hash()), 76 Accounts: make(map[string]DumpAccount), 77 } 78 79 value,err := self.trie.TryGet(address[:]) 80 if value!= nil && err == nil { 81 var data Account 82 if err := rlp.DecodeBytes(value, &data); err != nil { 83 panic(err) 84 } 85 86 obj := newObject(nil, address, data) 87 account := DumpAccount{ 88 Balance: data.Balance.String(), 89 Nonce: data.Nonce, 90 Root: common.Bytes2Hex(data.Root[:]), 91 CodeHash: common.Bytes2Hex(data.CodeHash), 92 Code: common.Bytes2Hex(obj.Code(self.db)), 93 Storage: make(map[string]string), 94 } 95 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 96 for storageIt.Next() { 97 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 98 } 99 dump.Accounts[address.String()] = account 100 101 } 102 return dump 103 } 104 105 func (self *StateDB) Dump() []byte { 106 json, err := json.MarshalIndent(self.RawDump(), "", " ") 107 if err != nil { 108 fmt.Println("dump err", err) 109 } 110 111 return json 112 }