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