github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/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  	"math/big"
    23  
    24  	"github.com/FusionFoundation/efsn/common"
    25  	"github.com/FusionFoundation/efsn/rlp"
    26  	"github.com/FusionFoundation/efsn/trie"
    27  )
    28  
    29  type DumpAccount struct {
    30  	Balances         map[common.Hash]*big.Int         `json:"balance"`
    31  	TimeLockBalances map[common.Hash]*common.TimeLock `json:"timelock"`
    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)
    59  		bal := make(map[common.Hash]*big.Int)
    60  		for i, v := range data.BalancesHash {
    61  			bal[v] = data.BalancesVal[i]
    62  		}
    63  		timelocks := make(map[common.Hash]*common.TimeLock)
    64  		for i, v := range data.TimeLockBalancesHash {
    65  			timelock := data.TimeLockBalancesVal[i]
    66  			timelocks[v] = timelock
    67  		}
    68  		account := DumpAccount{
    69  			Balances:         bal,
    70  			TimeLockBalances: timelocks,
    71  			Nonce:            data.Nonce,
    72  			Root:             common.Bytes2Hex(data.Root[:]),
    73  			CodeHash:         common.Bytes2Hex(data.CodeHash),
    74  			Code:             common.Bytes2Hex(obj.Code(self.db)),
    75  			Storage:          make(map[string]string),
    76  		}
    77  		storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
    78  		for storageIt.Next() {
    79  			account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
    80  		}
    81  		dump.Accounts[common.Bytes2Hex(addr)] = account
    82  	}
    83  	return dump
    84  }
    85  
    86  func (self *StateDB) Dump() []byte {
    87  	json, err := json.MarshalIndent(self.RawDump(), "", "    ")
    88  	if err != nil {
    89  		fmt.Println("dump err", err)
    90  	}
    91  
    92  	return json
    93  }