github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/state/dump.go (about) 1 package state 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/quickchainproject/quickchain/common" 8 "github.com/quickchainproject/quickchain/rlp" 9 "github.com/quickchainproject/quickchain/trie" 10 ) 11 12 type DumpAccount struct { 13 Balance string `json:"balance"` 14 Nonce uint64 `json:"nonce"` 15 Root string `json:"root"` 16 CodeHash string `json:"codeHash"` 17 Code string `json:"code"` 18 Storage map[string]string `json:"storage"` 19 } 20 21 type Dump struct { 22 Root string `json:"root"` 23 Accounts map[string]DumpAccount `json:"accounts"` 24 } 25 26 func (self *StateDB) RawDump() Dump { 27 dump := Dump{ 28 Root: fmt.Sprintf("%x", self.trie.Hash()), 29 Accounts: make(map[string]DumpAccount), 30 } 31 32 it := trie.NewIterator(self.trie.NodeIterator(nil)) 33 for it.Next() { 34 addr := self.trie.GetKey(it.Key) 35 var data Account 36 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 37 panic(err) 38 } 39 40 obj := newObject(nil, common.BytesToAddress(addr), data) 41 account := DumpAccount{ 42 Balance: data.Balance.String(), 43 Nonce: data.Nonce, 44 Root: common.Bytes2Hex(data.Root[:]), 45 CodeHash: common.Bytes2Hex(data.CodeHash), 46 Code: common.Bytes2Hex(obj.Code(self.db)), 47 Storage: make(map[string]string), 48 } 49 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 50 for storageIt.Next() { 51 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 52 } 53 dump.Accounts[common.Bytes2Hex(addr)] = account 54 } 55 return dump 56 } 57 58 func (self *StateDB) Dump() []byte { 59 json, err := json.MarshalIndent(self.RawDump(), "", " ") 60 if err != nil { 61 fmt.Println("dump err", err) 62 } 63 64 return json 65 }