github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/core/state/dump.go (about)

     1  // Copyright 2021 The adkgo Authors
     2  // This file is part of the adkgo library (adapted for adkgo from go--ethereum v1.10.8).
     3  //
     4  // the adkgo 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 adkgo 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 adkgo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/aidoskuneen/adk-node/common"
    25  	"github.com/aidoskuneen/adk-node/common/hexutil"
    26  	"github.com/aidoskuneen/adk-node/log"
    27  	"github.com/aidoskuneen/adk-node/rlp"
    28  	"github.com/aidoskuneen/adk-node/trie"
    29  )
    30  
    31  // DumpConfig is a set of options to control what portions of the statewill be
    32  // iterated and collected.
    33  type DumpConfig struct {
    34  	SkipCode          bool
    35  	SkipStorage       bool
    36  	OnlyWithAddresses bool
    37  	Start             []byte
    38  	Max               uint64
    39  }
    40  
    41  // DumpCollector interface which the state trie calls during iteration
    42  type DumpCollector interface {
    43  	// OnRoot is called with the state root
    44  	OnRoot(common.Hash)
    45  	// OnAccount is called once for each account in the trie
    46  	OnAccount(common.Address, DumpAccount)
    47  }
    48  
    49  // DumpAccount represents an account in the state.
    50  type DumpAccount struct {
    51  	Balance   string                 `json:"balance"`
    52  	Nonce     uint64                 `json:"nonce"`
    53  	Root      hexutil.Bytes          `json:"root"`
    54  	CodeHash  hexutil.Bytes          `json:"codeHash"`
    55  	Code      hexutil.Bytes          `json:"code,omitempty"`
    56  	Storage   map[common.Hash]string `json:"storage,omitempty"`
    57  	Address   *common.Address        `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
    58  	SecureKey hexutil.Bytes          `json:"key,omitempty"`     // If we don't have address, we can output the key
    59  
    60  }
    61  
    62  // Dump represents the full dump in a collected format, as one large map.
    63  type Dump struct {
    64  	Root     string                         `json:"root"`
    65  	Accounts map[common.Address]DumpAccount `json:"accounts"`
    66  }
    67  
    68  // OnRoot implements DumpCollector interface
    69  func (d *Dump) OnRoot(root common.Hash) {
    70  	d.Root = fmt.Sprintf("%x", root)
    71  }
    72  
    73  // OnAccount implements DumpCollector interface
    74  func (d *Dump) OnAccount(addr common.Address, account DumpAccount) {
    75  	d.Accounts[addr] = account
    76  }
    77  
    78  // IteratorDump is an implementation for iterating over data.
    79  type IteratorDump struct {
    80  	Root     string                         `json:"root"`
    81  	Accounts map[common.Address]DumpAccount `json:"accounts"`
    82  	Next     []byte                         `json:"next,omitempty"` // nil if no more accounts
    83  }
    84  
    85  // OnRoot implements DumpCollector interface
    86  func (d *IteratorDump) OnRoot(root common.Hash) {
    87  	d.Root = fmt.Sprintf("%x", root)
    88  }
    89  
    90  // OnAccount implements DumpCollector interface
    91  func (d *IteratorDump) OnAccount(addr common.Address, account DumpAccount) {
    92  	d.Accounts[addr] = account
    93  }
    94  
    95  // iterativeDump is a DumpCollector-implementation which dumps output line-by-line iteratively.
    96  type iterativeDump struct {
    97  	*json.Encoder
    98  }
    99  
   100  // OnAccount implements DumpCollector interface
   101  func (d iterativeDump) OnAccount(addr common.Address, account DumpAccount) {
   102  	dumpAccount := &DumpAccount{
   103  		Balance:   account.Balance,
   104  		Nonce:     account.Nonce,
   105  		Root:      account.Root,
   106  		CodeHash:  account.CodeHash,
   107  		Code:      account.Code,
   108  		Storage:   account.Storage,
   109  		SecureKey: account.SecureKey,
   110  		Address:   nil,
   111  	}
   112  	if addr != (common.Address{}) {
   113  		dumpAccount.Address = &addr
   114  	}
   115  	d.Encode(dumpAccount)
   116  }
   117  
   118  // OnRoot implements DumpCollector interface
   119  func (d iterativeDump) OnRoot(root common.Hash) {
   120  	d.Encode(struct {
   121  		Root common.Hash `json:"root"`
   122  	}{root})
   123  }
   124  
   125  // DumpToCollector iterates the state according to the given options and inserts
   126  // the items into a collector for aggregation or serialization.
   127  func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
   128  	// Sanitize the input to allow nil configs
   129  	if conf == nil {
   130  		conf = new(DumpConfig)
   131  	}
   132  	var (
   133  		missingPreimages int
   134  		accounts         uint64
   135  		start            = time.Now()
   136  		logged           = time.Now()
   137  	)
   138  	log.Info("Trie dumping started", "root", s.trie.Hash())
   139  	c.OnRoot(s.trie.Hash())
   140  
   141  	it := trie.NewIterator(s.trie.NodeIterator(conf.Start))
   142  	for it.Next() {
   143  		var data Account
   144  		if err := rlp.DecodeBytes(it.Value, &data); err != nil {
   145  			panic(err)
   146  		}
   147  		account := DumpAccount{
   148  			Balance:   data.Balance.String(),
   149  			Nonce:     data.Nonce,
   150  			Root:      data.Root[:],
   151  			CodeHash:  data.CodeHash,
   152  			SecureKey: it.Key,
   153  		}
   154  		addrBytes := s.trie.GetKey(it.Key)
   155  		if addrBytes == nil {
   156  			// Preimage missing
   157  			missingPreimages++
   158  			if conf.OnlyWithAddresses {
   159  				continue
   160  			}
   161  			account.SecureKey = it.Key
   162  		}
   163  		addr := common.BytesToAddress(addrBytes)
   164  		obj := newObject(s, addr, data)
   165  		if !conf.SkipCode {
   166  			account.Code = obj.Code(s.db)
   167  		}
   168  		if !conf.SkipStorage {
   169  			account.Storage = make(map[common.Hash]string)
   170  			storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
   171  			for storageIt.Next() {
   172  				_, content, _, err := rlp.Split(storageIt.Value)
   173  				if err != nil {
   174  					log.Error("Failed to decode the value returned by iterator", "error", err)
   175  					continue
   176  				}
   177  				account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
   178  			}
   179  		}
   180  		c.OnAccount(addr, account)
   181  		accounts++
   182  		if time.Since(logged) > 8*time.Second {
   183  			log.Info("Trie dumping in progress", "at", it.Key, "accounts", accounts,
   184  				"elapsed", common.PrettyDuration(time.Since(start)))
   185  			logged = time.Now()
   186  		}
   187  		if conf.Max > 0 && accounts >= conf.Max {
   188  			if it.Next() {
   189  				nextKey = it.Key
   190  			}
   191  			break
   192  		}
   193  	}
   194  	if missingPreimages > 0 {
   195  		log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
   196  	}
   197  	log.Info("Trie dumping complete", "accounts", accounts,
   198  		"elapsed", common.PrettyDuration(time.Since(start)))
   199  
   200  	return nextKey
   201  }
   202  
   203  // RawDump returns the entire state an a single large object
   204  func (s *StateDB) RawDump(opts *DumpConfig) Dump {
   205  	dump := &Dump{
   206  		Accounts: make(map[common.Address]DumpAccount),
   207  	}
   208  	s.DumpToCollector(dump, opts)
   209  	return *dump
   210  }
   211  
   212  // Dump returns a JSON string representing the entire state as a single json-object
   213  func (s *StateDB) Dump(opts *DumpConfig) []byte {
   214  	dump := s.RawDump(opts)
   215  	json, err := json.MarshalIndent(dump, "", "    ")
   216  	if err != nil {
   217  		fmt.Println("Dump err", err)
   218  	}
   219  	return json
   220  }
   221  
   222  // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
   223  func (s *StateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) {
   224  	s.DumpToCollector(iterativeDump{output}, opts)
   225  }
   226  
   227  // IteratorDump dumps out a batch of accounts starts with the given start key
   228  func (s *StateDB) IteratorDump(opts *DumpConfig) IteratorDump {
   229  	iterator := &IteratorDump{
   230  		Accounts: make(map[common.Address]DumpAccount),
   231  	}
   232  	iterator.Next = s.DumpToCollector(iterator, opts)
   233  	return *iterator
   234  }