go-ethereum/core/state/dump.go
Jeffrey Wilcke 1f8e723821 core, tests, miner, cmd: improved EVM environment
Reduced the overhead caused by all the diffirent implementations of the
vm.Environment interface. Where previously the Environment was an
interface it has now become an implementation it self using several new
easy to implement interfaces such as the Backend that takes care of the
state and CallContext which takes care of the EVM calling conventions.

Most of these interfaces have been implemented in the core package with
the exception of the test suit which uses a combination of the core
package implementations and a mix of its own.

This will reduce the amount of extra overhead when changing an
interface, such as adding a new method or changing a method signature.
2016-10-10 21:48:21 +02:00

98 lines
3.1 KiB
Go

// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
)
type DumpAccount struct {
Balance string `json:"balance"`
Nonce uint64 `json:"nonce"`
Root string `json:"root"`
CodeHash string `json:"codeHash"`
Code string `json:"code"`
Storage map[string]string `json:"storage"`
}
type Dump struct {
Root string `json:"root"`
Accounts map[string]DumpAccount `json:"accounts"`
}
func (self *StateDB) RawDump() Dump {
dump := Dump{
Root: common.Bytes2Hex(self.trie.Root()),
Accounts: make(map[string]DumpAccount),
}
it := self.trie.Iterator()
for it.Next() {
addr := self.trie.GetKey(it.Key)
var data Account
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
panic(err)
}
obj := newObject(nil, common.BytesToAddress(addr), data, nil)
account := DumpAccount{
Balance: data.Balance.String(),
Nonce: data.Nonce,
Root: common.Bytes2Hex(data.Root[:]),
CodeHash: common.Bytes2Hex(data.CodeHash),
Code: common.Bytes2Hex(obj.Code(self.db)),
Storage: make(map[string]string),
}
storageIt := obj.getTrie(self.db).Iterator()
for storageIt.Next() {
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
}
dump.Accounts[common.Bytes2Hex(addr)] = account
}
for addr, stateObject := range self.stateObjects {
account := DumpAccount{
Balance: stateObject.Balance().String(),
Nonce: stateObject.Nonce(),
Root: common.Bytes2Hex(stateObject.data.Root[:]),
CodeHash: common.Bytes2Hex(stateObject.CodeHash()),
Code: common.Bytes2Hex(stateObject.Code(self.db)),
Storage: make(map[string]string),
}
storageIt := stateObject.getTrie(self.db).Iterator()
for storageIt.Next() {
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
}
for storageAddr, value := range stateObject.cachedStorage {
account.Storage[common.Bytes2Hex(storageAddr[:])] = value.Hex()
}
dump.Accounts[common.Bytes2Hex(addr[:])] = account
}
return dump
}
func (self *StateDB) Dump() []byte {
json, err := json.MarshalIndent(self.RawDump(), "", " ")
if err != nil {
fmt.Println("dump err", err)
}
return json
}