github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/dump.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package state 13 14 import ( 15 "encoding/json" 16 "fmt" 17 18 "github.com/Sberex/go-sberex/common" 19 "github.com/Sberex/go-sberex/rlp" 20 "github.com/Sberex/go-sberex/trie" 21 ) 22 23 type DumpAccount struct { 24 Balance string `json:"balance"` 25 Nonce uint64 `json:"nonce"` 26 Root string `json:"root"` 27 CodeHash string `json:"codeHash"` 28 Code string `json:"code"` 29 Storage map[string]string `json:"storage"` 30 } 31 32 type Dump struct { 33 Root string `json:"root"` 34 Accounts map[string]DumpAccount `json:"accounts"` 35 } 36 37 func (self *StateDB) RawDump() Dump { 38 dump := Dump{ 39 Root: fmt.Sprintf("%x", self.trie.Hash()), 40 Accounts: make(map[string]DumpAccount), 41 } 42 43 it := trie.NewIterator(self.trie.NodeIterator(nil)) 44 for it.Next() { 45 addr := self.trie.GetKey(it.Key) 46 var data Account 47 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 48 panic(err) 49 } 50 51 obj := newObject(nil, common.BytesToAddress(addr), data, nil) 52 account := DumpAccount{ 53 Balance: data.Balance.String(), 54 Nonce: data.Nonce, 55 Root: common.Bytes2Hex(data.Root[:]), 56 CodeHash: common.Bytes2Hex(data.CodeHash), 57 Code: common.Bytes2Hex(obj.Code(self.db)), 58 Storage: make(map[string]string), 59 } 60 storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil)) 61 for storageIt.Next() { 62 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 63 } 64 dump.Accounts[common.Bytes2Hex(addr)] = account 65 } 66 return dump 67 } 68 69 func (self *StateDB) Dump() []byte { 70 json, err := json.MarshalIndent(self.RawDump(), "", " ") 71 if err != nil { 72 fmt.Println("dump err", err) 73 } 74 75 return json 76 }