github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/common" 24 "github.com/atheioschain/go-atheios/rlp" 25 ) 26 27 type DumpAccount struct { 28 Balance string `json:"balance"` 29 Nonce uint64 `json:"nonce"` 30 Root string `json:"root"` 31 CodeHash string `json:"codeHash"` 32 Code string `json:"code"` 33 Storage map[string]string `json:"storage"` 34 } 35 36 type Dump struct { 37 Root string `json:"root"` 38 Accounts map[string]DumpAccount `json:"accounts"` 39 } 40 41 func (self *StateDB) RawDump() Dump { 42 dump := Dump{ 43 Root: common.Bytes2Hex(self.trie.Root()), 44 Accounts: make(map[string]DumpAccount), 45 } 46 47 it := self.trie.Iterator() 48 for it.Next() { 49 addr := self.trie.GetKey(it.Key) 50 var data Account 51 if err := rlp.DecodeBytes(it.Value, &data); err != nil { 52 panic(err) 53 } 54 55 obj := newObject(nil, common.BytesToAddress(addr), data, nil) 56 account := DumpAccount{ 57 Balance: data.Balance.String(), 58 Nonce: data.Nonce, 59 Root: common.Bytes2Hex(data.Root[:]), 60 CodeHash: common.Bytes2Hex(data.CodeHash), 61 Code: common.Bytes2Hex(obj.Code(self.db)), 62 Storage: make(map[string]string), 63 } 64 storageIt := obj.getTrie(self.db).Iterator() 65 for storageIt.Next() { 66 account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) 67 } 68 dump.Accounts[common.Bytes2Hex(addr)] = account 69 } 70 return dump 71 } 72 73 func (self *StateDB) Dump() []byte { 74 json, err := json.MarshalIndent(self.RawDump(), "", " ") 75 if err != nil { 76 fmt.Println("dump err", err) 77 } 78 79 return json 80 }