The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/core/state/dump.go

86 lines
2.6 KiB

// 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"
"github.com/ethereum/go-ethereum/trie"
)
6 years ago
// DumpAccount ...
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"`
}
6 years ago
// Dump is the structure of root hash and associated accounts.
type Dump struct {
Root string `json:"root"`
Accounts map[string]DumpAccount `json:"accounts"`
}
// RawDump returns Dump from given DB.
func (stateDB *DB) RawDump() Dump {
dump := Dump{
6 years ago
Root: fmt.Sprintf("%x", stateDB.trie.Hash()),
Accounts: make(map[string]DumpAccount),
}
6 years ago
it := trie.NewIterator(stateDB.trie.NodeIterator(nil))
for it.Next() {
6 years ago
addr := stateDB.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)
account := DumpAccount{
Balance: data.Balance.String(),
Nonce: data.Nonce,
Root: common.Bytes2Hex(data.Root[:]),
CodeHash: common.Bytes2Hex(data.CodeHash),
6 years ago
Code: common.Bytes2Hex(obj.Code(stateDB.db)),
Storage: make(map[string]string),
}
6 years ago
storageIt := trie.NewIterator(obj.getTrie(stateDB.db).NodeIterator(nil))
for storageIt.Next() {
6 years ago
account.Storage[common.Bytes2Hex(stateDB.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
}
dump.Accounts[common.Bytes2Hex(addr)] = account
}
return dump
}
6 years ago
// Dump dumps into []byte.
func (stateDB *DB) Dump() []byte {
6 years ago
json, err := json.MarshalIndent(stateDB.RawDump(), "", " ")
if err != nil {
fmt.Println("dump err", err)
}
return json
}