github.com/immesys/bw2bc@v1.1.0/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  )
    25  
    26  type Account struct {
    27  	Balance  string            `json:"balance"`
    28  	Nonce    uint64            `json:"nonce"`
    29  	Root     string            `json:"root"`
    30  	CodeHash string            `json:"codeHash"`
    31  	Storage  map[string]string `json:"storage"`
    32  }
    33  
    34  type World struct {
    35  	Root     string             `json:"root"`
    36  	Accounts map[string]Account `json:"accounts"`
    37  }
    38  
    39  func (self *StateDB) RawDump() World {
    40  	world := World{
    41  		Root:     common.Bytes2Hex(self.trie.Root()),
    42  		Accounts: make(map[string]Account),
    43  	}
    44  
    45  	it := self.trie.Iterator()
    46  	for it.Next() {
    47  		addr := self.trie.GetKey(it.Key)
    48  		stateObject := NewStateObjectFromBytes(common.BytesToAddress(addr), it.Value, self.db)
    49  
    50  		account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
    51  		account.Storage = make(map[string]string)
    52  
    53  		storageIt := stateObject.trie.Iterator()
    54  		for storageIt.Next() {
    55  			account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
    56  		}
    57  		world.Accounts[common.Bytes2Hex(addr)] = account
    58  	}
    59  	return world
    60  }
    61  
    62  func (self *StateDB) Dump() []byte {
    63  	json, err := json.MarshalIndent(self.RawDump(), "", "    ")
    64  	if err != nil {
    65  		fmt.Println("dump err", err)
    66  	}
    67  
    68  	return json
    69  }
    70  
    71  // Debug stuff
    72  func (self *StateObject) CreateOutputForDiff() {
    73  	fmt.Printf("%x %x %x %x\n", self.Address(), self.Root(), self.balance.Bytes(), self.nonce)
    74  	it := self.trie.Iterator()
    75  	for it.Next() {
    76  		fmt.Printf("%x %x\n", it.Key, it.Value)
    77  	}
    78  }