2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-10-31 13:43:14 +00:00
|
|
|
package state
|
2014-08-06 07:53:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-05-27 13:51:49 +00:00
|
|
|
"encoding/binary"
|
2014-08-06 07:53:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
2014-08-06 07:53:00 +00:00
|
|
|
)
|
|
|
|
|
2019-06-24 14:16:44 +00:00
|
|
|
// DumpAccount represents an account in the state
|
2016-09-22 19:04:58 +00:00
|
|
|
type DumpAccount struct {
|
2019-05-27 13:51:49 +00:00
|
|
|
Balance string `json:"balance"`
|
|
|
|
Nonce uint64 `json:"nonce"`
|
|
|
|
Root string `json:"root"`
|
|
|
|
CodeHash string `json:"codeHash"`
|
|
|
|
Code string `json:"code,omitempty"`
|
|
|
|
Storage map[string]string `json:"storage,omitempty"`
|
|
|
|
StorageSize *uint64 `json:",omitempty"`
|
|
|
|
Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
|
|
|
|
SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:16:44 +00:00
|
|
|
// Dump represents the full dump in a collected format, as one large map
|
2016-09-22 19:04:58 +00:00
|
|
|
type Dump struct {
|
2019-06-24 14:16:44 +00:00
|
|
|
Root string `json:"root"`
|
|
|
|
Accounts map[common.Address]DumpAccount `json:"accounts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively
|
2019-11-22 14:56:05 +00:00
|
|
|
type iterativeDump struct {
|
|
|
|
*json.Encoder
|
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
|
|
|
|
// Collector interface which the state trie calls during iteration
|
|
|
|
type collector interface {
|
|
|
|
onRoot(common.Hash)
|
|
|
|
onAccount(common.Address, DumpAccount)
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:56:05 +00:00
|
|
|
func (d *Dump) onRoot(root common.Hash) {
|
|
|
|
d.Root = fmt.Sprintf("%x", root)
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 14:56:05 +00:00
|
|
|
func (d *Dump) onAccount(addr common.Address, account DumpAccount) {
|
|
|
|
d.Accounts[addr] = account
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 14:56:05 +00:00
|
|
|
func (d iterativeDump) onAccount(addr common.Address, account DumpAccount) {
|
2019-06-24 14:16:44 +00:00
|
|
|
dumpAccount := &DumpAccount{
|
|
|
|
Balance: account.Balance,
|
|
|
|
Nonce: account.Nonce,
|
|
|
|
Root: account.Root,
|
|
|
|
CodeHash: account.CodeHash,
|
|
|
|
Code: account.Code,
|
|
|
|
Storage: account.Storage,
|
|
|
|
SecureKey: account.SecureKey,
|
|
|
|
Address: nil,
|
|
|
|
}
|
|
|
|
if addr != (common.Address{}) {
|
|
|
|
dumpAccount.Address = &addr
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
2019-12-03 13:02:09 +00:00
|
|
|
//nolint:errcheck
|
2019-11-22 14:56:05 +00:00
|
|
|
d.Encode(dumpAccount)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2019-12-03 13:02:09 +00:00
|
|
|
func (d iterativeDump) onRoot(root common.Hash) {
|
|
|
|
//nolint:errcheck
|
|
|
|
d.Encoder.Encode(struct {
|
2019-06-24 14:16:44 +00:00
|
|
|
Root common.Hash `json:"root"`
|
|
|
|
}{root})
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
|
2019-12-19 10:44:43 +00:00
|
|
|
emptyAddress := common.Address{}
|
2019-06-24 14:16:44 +00:00
|
|
|
missingPreimages := 0
|
2019-12-10 13:12:21 +00:00
|
|
|
|
|
|
|
tds.tMu.Lock()
|
|
|
|
h := tds.t.Hash()
|
|
|
|
tds.tMu.Unlock()
|
|
|
|
c.onRoot(h)
|
2019-05-27 13:51:49 +00:00
|
|
|
var acc accounts.Account
|
|
|
|
var prefix [32]byte
|
2019-11-15 22:48:49 +00:00
|
|
|
err := tds.db.Walk(dbutils.AccountsBucket, prefix[:], 0, func(k, v []byte) (bool, error) {
|
|
|
|
addr := common.BytesToAddress(tds.GetKey(k))
|
2019-05-27 13:51:49 +00:00
|
|
|
var err error
|
|
|
|
if err = acc.DecodeForStorage(v); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
root, err := tds.db.Get(dbutils.IntermediateTrieHashBucket, k)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
acc.Root = common.BytesToHash(root)
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
var code []byte
|
|
|
|
|
|
|
|
if !acc.IsEmptyCodeHash() {
|
2019-11-15 22:48:49 +00:00
|
|
|
if code, err = tds.db.Get(dbutils.CodeBucket, acc.CodeHash[:]); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2016-04-15 09:16:56 +00:00
|
|
|
}
|
2016-09-22 19:04:58 +00:00
|
|
|
account := DumpAccount{
|
2019-05-27 13:51:49 +00:00
|
|
|
Balance: acc.Balance.String(),
|
|
|
|
Nonce: acc.Nonce,
|
|
|
|
Root: common.Bytes2Hex(acc.Root[:]),
|
|
|
|
CodeHash: common.Bytes2Hex(acc.CodeHash[:]),
|
|
|
|
Storage: make(map[string]string),
|
2016-04-15 09:16:56 +00:00
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
if emptyAddress == addr {
|
|
|
|
// Preimage missing
|
|
|
|
missingPreimages++
|
|
|
|
if excludeMissingPreimages {
|
2019-05-27 13:51:49 +00:00
|
|
|
return true, nil
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
account.SecureKey = common.CopyBytes(k)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
if !excludeCode {
|
2019-05-27 13:51:49 +00:00
|
|
|
account.Code = common.Bytes2Hex(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.HasStorageSize {
|
|
|
|
var storageSize = acc.StorageSize
|
|
|
|
account.StorageSize = &storageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
binary.PutUvarint(buf, acc.GetIncarnation())
|
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
addrHash, err := tds.HashAddress(addr, false)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2020-01-15 14:55:43 +00:00
|
|
|
err = tds.db.Walk(dbutils.StorageBucket, dbutils.GenerateStoragePrefix(addrHash, acc.GetIncarnation()), uint(common.HashLength*8+common.IncarnationLength), func(ks, vs []byte) (bool, error) {
|
|
|
|
key := tds.GetKey(ks[common.HashLength+common.IncarnationLength:]) //remove account address and version from composite key
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
if !excludeStorage {
|
|
|
|
account.Storage[common.BytesToHash(key).String()] = common.Bytes2Hex(vs)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-12-23 17:35:36 +00:00
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
c.onAccount(addr, account)
|
2019-05-27 13:51:49 +00:00
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawDump returns the entire state an a single large object
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
|
2019-06-24 14:16:44 +00:00
|
|
|
dump := &Dump{
|
|
|
|
Accounts: make(map[common.Address]DumpAccount),
|
2014-12-23 17:35:36 +00:00
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
tds.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
|
2019-06-24 14:16:44 +00:00
|
|
|
return *dump
|
2015-02-28 18:15:57 +00:00
|
|
|
}
|
2014-08-06 07:53:00 +00:00
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) DefaultRawDump() Dump {
|
|
|
|
return tds.RawDump(false, false, false)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultDump returns a JSON string representing the state with the default params
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) DefaultDump() []byte {
|
|
|
|
return tds.Dump(false, false, false)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:16:44 +00:00
|
|
|
// Dump returns a JSON string representing the entire state as a single json-object
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
|
|
|
|
dump := tds.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
|
2019-06-24 14:16:44 +00:00
|
|
|
json, err := json.MarshalIndent(dump, "", " ")
|
2014-08-06 07:53:00 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("dump err", err)
|
|
|
|
}
|
2014-08-17 10:42:32 +00:00
|
|
|
return json
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
|
|
|
|
// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
|
2019-12-03 13:02:09 +00:00
|
|
|
tds.dump(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|