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