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 (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
"time"
|
2014-08-06 07:53:00 +00:00
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-24 14:16:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-09-28 08:48:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-06-24 14:16:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-09-22 19:04:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2017-04-18 11:37:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2014-08-06 07:53:00 +00:00
|
|
|
)
|
|
|
|
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
// DumpConfig is a set of options to control what portions of the statewill be
|
|
|
|
// iterated and collected.
|
|
|
|
type DumpConfig struct {
|
|
|
|
SkipCode bool
|
|
|
|
SkipStorage bool
|
|
|
|
OnlyWithAddresses bool
|
|
|
|
Start []byte
|
|
|
|
Max uint64
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// DumpCollector interface which the state trie calls during iteration
|
|
|
|
type DumpCollector interface {
|
|
|
|
// OnRoot is called with the state root
|
|
|
|
OnRoot(common.Hash)
|
|
|
|
// OnAccount is called once for each account in the trie
|
|
|
|
OnAccount(common.Address, DumpAccount)
|
|
|
|
}
|
|
|
|
|
2020-03-31 10:08:44 +00:00
|
|
|
// DumpAccount represents an account in the state.
|
2016-09-22 19:04:58 +00:00
|
|
|
type DumpAccount struct {
|
2019-06-24 14:16:44 +00:00
|
|
|
Balance string `json:"balance"`
|
|
|
|
Nonce uint64 `json:"nonce"`
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
Root hexutil.Bytes `json:"root"`
|
|
|
|
CodeHash hexutil.Bytes `json:"codeHash"`
|
|
|
|
Code hexutil.Bytes `json:"code,omitempty"`
|
2019-06-24 14:16:44 +00:00
|
|
|
Storage map[common.Hash]string `json:"storage,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
|
|
|
}
|
|
|
|
|
2020-03-31 10:08: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"`
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// OnRoot implements DumpCollector interface
|
|
|
|
func (d *Dump) OnRoot(root common.Hash) {
|
|
|
|
d.Root = fmt.Sprintf("%x", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnAccount implements DumpCollector interface
|
|
|
|
func (d *Dump) OnAccount(addr common.Address, account DumpAccount) {
|
|
|
|
d.Accounts[addr] = account
|
2019-11-22 14:56:05 +00:00
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
|
2020-03-31 10:08:44 +00:00
|
|
|
// IteratorDump is an implementation for iterating over data.
|
|
|
|
type IteratorDump struct {
|
|
|
|
Root string `json:"root"`
|
|
|
|
Accounts map[common.Address]DumpAccount `json:"accounts"`
|
|
|
|
Next []byte `json:"next,omitempty"` // nil if no more accounts
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// OnRoot implements DumpCollector interface
|
|
|
|
func (d *IteratorDump) OnRoot(root common.Hash) {
|
2019-11-22 14:56:05 +00:00
|
|
|
d.Root = fmt.Sprintf("%x", root)
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// OnAccount implements DumpCollector interface
|
|
|
|
func (d *IteratorDump) OnAccount(addr common.Address, account DumpAccount) {
|
2019-11-22 14:56:05 +00:00
|
|
|
d.Accounts[addr] = account
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2020-03-31 10:08:44 +00:00
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// iterativeDump is a DumpCollector-implementation which dumps output line-by-line iteratively.
|
|
|
|
type iterativeDump struct {
|
|
|
|
*json.Encoder
|
2020-03-31 10:08:44 +00:00
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// OnAccount implements DumpCollector interface
|
|
|
|
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-11-22 14:56:05 +00:00
|
|
|
d.Encode(dumpAccount)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2019-11-22 14:56:05 +00:00
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// OnRoot implements DumpCollector interface
|
|
|
|
func (d iterativeDump) OnRoot(root common.Hash) {
|
2019-11-22 14:56:05 +00:00
|
|
|
d.Encode(struct {
|
2019-06-24 14:16:44 +00:00
|
|
|
Root common.Hash `json:"root"`
|
|
|
|
}{root})
|
|
|
|
}
|
2014-08-06 07:53:00 +00:00
|
|
|
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
// DumpToCollector iterates the state according to the given options and inserts
|
|
|
|
// the items into a collector for aggregation or serialization.
|
|
|
|
func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
|
|
|
|
// Sanitize the input to allow nil configs
|
|
|
|
if conf == nil {
|
|
|
|
conf = new(DumpConfig)
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
missingPreimages int
|
|
|
|
accounts uint64
|
|
|
|
start = time.Now()
|
|
|
|
logged = time.Now()
|
|
|
|
)
|
|
|
|
log.Info("Trie dumping started", "root", s.trie.Hash())
|
2020-06-30 08:12:51 +00:00
|
|
|
c.OnRoot(s.trie.Hash())
|
2020-03-31 10:08:44 +00:00
|
|
|
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
it := trie.NewIterator(s.trie.NodeIterator(conf.Start))
|
2014-12-23 17:35:36 +00:00
|
|
|
for it.Next() {
|
2021-09-28 08:48:07 +00:00
|
|
|
var data types.StateAccount
|
2016-09-22 19:04:58 +00:00
|
|
|
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
2016-04-15 09:16:56 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-09-22 19:04:58 +00:00
|
|
|
account := DumpAccount{
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
Balance: data.Balance.String(),
|
|
|
|
Nonce: data.Nonce,
|
|
|
|
Root: data.Root[:],
|
|
|
|
CodeHash: data.CodeHash,
|
|
|
|
SecureKey: it.Key,
|
2016-04-15 09:16:56 +00:00
|
|
|
}
|
2020-05-07 13:06:31 +00:00
|
|
|
addrBytes := s.trie.GetKey(it.Key)
|
|
|
|
if addrBytes == nil {
|
2019-06-24 14:16:44 +00:00
|
|
|
// Preimage missing
|
|
|
|
missingPreimages++
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
if conf.OnlyWithAddresses {
|
2019-06-24 14:16:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
account.SecureKey = it.Key
|
|
|
|
}
|
2020-05-07 13:06:31 +00:00
|
|
|
addr := common.BytesToAddress(addrBytes)
|
2021-01-26 11:15:31 +00:00
|
|
|
obj := newObject(s, addr, data)
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
if !conf.SkipCode {
|
|
|
|
account.Code = obj.Code(s.db)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
if !conf.SkipStorage {
|
2019-06-24 14:16:44 +00:00
|
|
|
account.Storage = make(map[common.Hash]string)
|
2019-11-22 14:56:05 +00:00
|
|
|
storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
|
2019-06-24 14:16:44 +00:00
|
|
|
for storageIt.Next() {
|
2019-08-12 14:14:40 +00:00
|
|
|
_, content, _, err := rlp.Split(storageIt.Value)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to decode the value returned by iterator", "error", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-11-22 14:56:05 +00:00
|
|
|
account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2014-12-23 17:35:36 +00:00
|
|
|
}
|
2020-06-30 08:12:51 +00:00
|
|
|
c.OnAccount(addr, account)
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
accounts++
|
|
|
|
if time.Since(logged) > 8*time.Second {
|
|
|
|
log.Info("Trie dumping in progress", "at", it.Key, "accounts", accounts,
|
|
|
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
if conf.Max > 0 && accounts >= conf.Max {
|
2020-03-31 10:08:44 +00:00
|
|
|
if it.Next() {
|
|
|
|
nextKey = it.Key
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
if missingPreimages > 0 {
|
|
|
|
log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
|
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
log.Info("Trie dumping complete", "accounts", accounts,
|
|
|
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
2020-03-31 10:08:44 +00:00
|
|
|
|
|
|
|
return nextKey
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RawDump returns the entire state an a single large object
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
func (s *StateDB) RawDump(opts *DumpConfig) 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
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
s.DumpToCollector(dump, opts)
|
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-06-24 14:16:44 +00:00
|
|
|
// Dump returns a JSON string representing the entire state as a single json-object
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
func (s *StateDB) Dump(opts *DumpConfig) []byte {
|
|
|
|
dump := s.RawDump(opts)
|
2019-06-24 14:16:44 +00:00
|
|
|
json, err := json.MarshalIndent(dump, "", " ")
|
2014-08-06 07:53:00 +00:00
|
|
|
if err != nil {
|
2020-06-30 08:12:51 +00:00
|
|
|
fmt.Println("Dump err", err)
|
2014-08-06 07:53:00 +00:00
|
|
|
}
|
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
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
func (s *StateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) {
|
|
|
|
s.DumpToCollector(iterativeDump{output}, opts)
|
2020-03-31 10:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IteratorDump dumps out a batch of accounts starts with the given start key
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
func (s *StateDB) IteratorDump(opts *DumpConfig) IteratorDump {
|
2020-03-31 10:08:44 +00:00
|
|
|
iterator := &IteratorDump{
|
|
|
|
Accounts: make(map[common.Address]DumpAccount),
|
|
|
|
}
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 08:05:39 +00:00
|
|
|
iterator.Next = s.DumpToCollector(iterator, opts)
|
2020-03-31 10:08:44 +00:00
|
|
|
return *iterator
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|