github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/state/dump.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:35</date> 10 //</624450079834509312> 11 12 13 package state 14 15 import ( 16 "encoding/json" 17 "fmt" 18 19 "github.com/ethereum/go-ethereum/common" 20 "github.com/ethereum/go-ethereum/rlp" 21 "github.com/ethereum/go-ethereum/trie" 22 ) 23 24 type DumpAccount struct { 25 Balance string `json:"balance"` 26 Nonce uint64 `json:"nonce"` 27 Root string `json:"root"` 28 CodeHash string `json:"codeHash"` 29 Code string `json:"code"` 30 Storage map[string]string `json:"storage"` 31 } 32 33 type Dump struct { 34 Root string `json:"root"` 35 Accounts map[string]DumpAccount `json:"accounts"` 36 } 37 38 func (self *StateDB) RawDump() Dump { 39 dump := Dump{ 40 Root: fmt.Sprintf("%x", self.trie.Hash()), 41 Accounts: make(map[string]DumpAccount), 42 } 43 44 it := trie.NewIterator(self.trie.NodeIterator(nil)) 45 for it.Next() { 46 addr := self.trie.GetKey(it.Key) 47 var data Account 48 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 49 panic(err) 50 } 51 52 obj := newObject(nil, common.BytesToAddress(addr), data) 53 account := DumpAccount{ 54 Balance: data.Balance.String(), 55 Nonce: data.Nonce, 56 Root: common.Bytes2Hex(data.Root[:]), 57 CodeHash: common.Bytes2Hex(data.CodeHash), 58 Code: common.Bytes2Hex(obj.Code(self.db)), 59 Storage: make(map[string]string), 60 } 61 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 62 for storageIt.Next() { 63 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 64 } 65 dump.Accounts[common.Bytes2Hex(addr)] = account 66 } 67 return dump 68 } 69 70 func (self *StateDB) Dump() []byte { 71 json, err := json.MarshalIndent(self.RawDump(), "", " ") 72 if err != nil { 73 fmt.Println("dump err", err) 74 } 75 76 return json 77 } 78