github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"math/big"
    24  
    25  	"github.com/intfoundation/intchain/common"
    26  	"github.com/intfoundation/intchain/rlp"
    27  	"github.com/intfoundation/intchain/trie"
    28  )
    29  
    30  type DumpAccount struct {
    31  	Balance        string                  `json:"balance"`
    32  	Deposit        string                  `json:"deposit_balance"`
    33  	Delegate       string                  `json:"delegate_balance"`
    34  	Proxied        string                  `json:"proxied_balance"`
    35  	DepositProxied string                  `json:"deposit_proxied_balance"`
    36  	PendingRefund  string                  `json:"pending_refund_balance"`
    37  	ProxiedRoot    string                  `json:"proxied_root"`
    38  	ProxiedDetail  map[string]*DumpProxied `json:"proxied_detail"`
    39  
    40  	Reward          string            `json:"reward_balance"`
    41  	AvailableReward string            `json:"available_reward_balance"`
    42  	RewardRoot      string            `json:"reward_root"`
    43  	RewardDetail    map[string]string `json:"reward_detail"`
    44  
    45  	Tx1Root   string   `json:"tx1_root"`
    46  	Tx1Detail []string `json:"tx1_detail"`
    47  	Tx3Root   string   `json:"tx3_root"`
    48  	Tx3Detail []string `json:"tx3_detail"`
    49  
    50  	Nonce    uint64            `json:"nonce"`
    51  	Root     string            `json:"root"`
    52  	CodeHash string            `json:"codeHash"`
    53  	Code     string            `json:"code"`
    54  	Storage  map[string]string `json:"storage"`
    55  
    56  	Candidate  bool  `json:"candidate"`
    57  	Commission uint8 `json:"commission"`
    58  }
    59  
    60  type DumpProxied struct {
    61  	Proxied        string `json:"proxied_balance"`
    62  	DepositProxied string `json:"deposit_proxied_balance"`
    63  	PendingRefund  string `json:"pending_refund_balance"`
    64  }
    65  
    66  type Dump struct {
    67  	Root           string                 `json:"root"`
    68  	Accounts       map[string]DumpAccount `json:"accounts"`
    69  	RewardAccounts []string               `json:"reward_accounts"`
    70  	RefundAccounts []string               `json:"refund_accounts"`
    71  }
    72  
    73  func (self *StateDB) RawDump() Dump {
    74  	dump := Dump{
    75  		Root:     fmt.Sprintf("%x", self.trie.Hash()),
    76  		Accounts: make(map[string]DumpAccount),
    77  		//RewardAccounts: make([]string, 0),
    78  		//RefundAccounts: make([]string, 0),
    79  	}
    80  
    81  	it := trie.NewIterator(self.trie.NodeIterator(nil))
    82  	for it.Next() {
    83  		addr := self.trie.GetKey(it.Key)
    84  		if len(addr) == 20 {
    85  			var data Account
    86  			if err := rlp.DecodeBytes(it.Value, &data); err != nil {
    87  				panic(err)
    88  			}
    89  
    90  			obj := newObject(nil, common.BytesToAddress(addr), data, nil)
    91  			account := DumpAccount{
    92  				Balance:        data.Balance.String(),
    93  				Deposit:        data.DepositBalance.String(),
    94  				Delegate:       data.DelegateBalance.String(),
    95  				Proxied:        data.ProxiedBalance.String(),
    96  				DepositProxied: data.DepositProxiedBalance.String(),
    97  				PendingRefund:  data.PendingRefundBalance.String(),
    98  				ProxiedRoot:    data.ProxiedRoot.String(),
    99  				ProxiedDetail:  make(map[string]*DumpProxied),
   100  				Reward:         data.RewardBalance.String(),
   101  				//AvailableReward: data.AvailableRewardBalance.String(),
   102  				RewardRoot:   data.RewardRoot.String(),
   103  				RewardDetail: make(map[string]string),
   104  
   105  				Tx1Root: data.TX1Root.String(),
   106  				Tx3Root: data.TX3Root.String(),
   107  
   108  				Nonce:    data.Nonce,
   109  				Root:     common.Bytes2Hex(data.Root[:]),
   110  				CodeHash: common.Bytes2Hex(data.CodeHash),
   111  				Code:     common.Bytes2Hex(obj.Code(self.db)),
   112  				Storage:  make(map[string]string),
   113  
   114  				Candidate:  data.Candidate,
   115  				Commission: data.Commission,
   116  			}
   117  			storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
   118  			for storageIt.Next() {
   119  				account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
   120  			}
   121  
   122  			tx1It := trie.NewIterator(obj.getTX1Trie(self.db).NodeIterator(nil))
   123  			for tx1It.Next() {
   124  				tx1hash := common.BytesToHash(self.trie.GetKey(tx1It.Key))
   125  				account.Tx1Detail = append(account.Tx1Detail, tx1hash.Hex())
   126  			}
   127  
   128  			tx3It := trie.NewIterator(obj.getTX3Trie(self.db).NodeIterator(nil))
   129  			for tx3It.Next() {
   130  				tx3hash := common.BytesToHash(self.trie.GetKey(tx3It.Key))
   131  				account.Tx3Detail = append(account.Tx3Detail, tx3hash.Hex())
   132  			}
   133  
   134  			if data.ProxiedRoot.String() != "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" {
   135  				proxiedIt := trie.NewIterator(obj.getProxiedTrie(self.db).NodeIterator(nil))
   136  				for proxiedIt.Next() {
   137  					var apb accountProxiedBalance
   138  					rlp.DecodeBytes(proxiedIt.Value, &apb)
   139  					account.ProxiedDetail[common.Bytes2Hex(self.trie.GetKey(proxiedIt.Key))] = &DumpProxied{
   140  						Proxied:        apb.ProxiedBalance.String(),
   141  						DepositProxied: apb.DepositProxiedBalance.String(),
   142  						PendingRefund:  apb.PendingRefundBalance.String(),
   143  					}
   144  				}
   145  			}
   146  
   147  			if data.RewardRoot.String() != "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" {
   148  				rewardIt := trie.NewIterator(obj.getRewardTrie(self.db).NodeIterator(nil))
   149  				for rewardIt.Next() {
   150  					var key uint64
   151  					rlp.DecodeBytes(self.trie.GetKey(rewardIt.Key), &key)
   152  					var value big.Int
   153  					rlp.DecodeBytes(rewardIt.Value, &value)
   154  					account.RewardDetail[fmt.Sprintf("epoch_%d", key)] = value.String()
   155  				}
   156  			}
   157  
   158  			dump.Accounts[common.Bytes2Hex(addr)] = account
   159  		} else {
   160  			if bytes.Equal(addr, rewardSetKey) {
   161  				var data []common.Address
   162  				if err := rlp.DecodeBytes(it.Value, &data); err != nil {
   163  					panic(err)
   164  				}
   165  				for _, rewardAddr := range data {
   166  					//dump.RewardAccounts = append(dump.RewardAccounts, rewardAddr.Hex())
   167  					dump.RewardAccounts = append(dump.RewardAccounts, rewardAddr.String())
   168  				}
   169  			} else if bytes.Equal(addr, refundSetKey) {
   170  				var data []common.Address
   171  				if err := rlp.DecodeBytes(it.Value, &data); err != nil {
   172  					panic(err)
   173  				}
   174  				for _, refundAddr := range data {
   175  					//dump.RefundAccounts = append(dump.RefundAccounts, refundAddr.Hex())
   176  					dump.RefundAccounts = append(dump.RefundAccounts, refundAddr.String())
   177  				}
   178  			}
   179  		}
   180  	}
   181  	return dump
   182  }
   183  
   184  func (self *StateDB) Dump() []byte {
   185  	json, err := json.MarshalIndent(self.RawDump(), "", "    ")
   186  	if err != nil {
   187  		fmt.Println("dump err", err)
   188  	}
   189  
   190  	return json
   191  }