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
|
|
|
)
|
|
|
|
|
2022-10-11 07:37:00 +00:00
|
|
|
// DumpConfig is a set of options to control what portions of the state will be
|
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
|
|
|
// 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
|
2023-05-23 10:10:26 +00:00
|
|
|
OnAccount(*common.Address, DumpAccount)
|
2020-06-30 08:12:51 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-11-28 12:54:17 +00:00
|
|
|
Balance string `json:"balance"`
|
|
|
|
Nonce uint64 `json:"nonce"`
|
|
|
|
Root hexutil.Bytes `json:"root"`
|
|
|
|
CodeHash hexutil.Bytes `json:"codeHash"`
|
|
|
|
Code hexutil.Bytes `json:"code,omitempty"`
|
|
|
|
Storage map[common.Hash]string `json:"storage,omitempty"`
|
|
|
|
Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
|
|
|
|
AddressHash hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
|
2019-06-24 14:16:44 +00:00
|
|
|
|
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 {
|
2023-11-28 12:54:17 +00:00
|
|
|
Root string `json:"root"`
|
|
|
|
Accounts map[string]DumpAccount `json:"accounts"`
|
|
|
|
// Next can be set to represent that this dump is only partial, and Next
|
|
|
|
// is where an iterator should be positioned in order to continue the dump.
|
|
|
|
Next []byte `json:"next,omitempty"` // nil if no more accounts
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-05-23 10:10:26 +00:00
|
|
|
func (d *Dump) OnAccount(addr *common.Address, account DumpAccount) {
|
2023-11-28 12:54:17 +00:00
|
|
|
if addr == nil {
|
|
|
|
d.Accounts[fmt.Sprintf("pre(%s)", account.AddressHash)] = account
|
2023-05-23 10:10:26 +00:00
|
|
|
}
|
|
|
|
if addr != nil {
|
2023-11-28 12:54:17 +00:00
|
|
|
d.Accounts[(*addr).String()] = account
|
2023-05-23 10:10:26 +00:00
|
|
|
}
|
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
|
2023-05-23 10:10:26 +00:00
|
|
|
func (d iterativeDump) OnAccount(addr *common.Address, account DumpAccount) {
|
2019-06-24 14:16:44 +00:00
|
|
|
dumpAccount := &DumpAccount{
|
2023-11-28 12:54:17 +00:00
|
|
|
Balance: account.Balance,
|
|
|
|
Nonce: account.Nonce,
|
|
|
|
Root: account.Root,
|
|
|
|
CodeHash: account.CodeHash,
|
|
|
|
Code: account.Code,
|
|
|
|
Storage: account.Storage,
|
|
|
|
AddressHash: account.AddressHash,
|
|
|
|
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, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
trieIt, err := s.trie.NodeIterator(conf.Start)
|
|
|
|
if err != nil {
|
2023-12-08 10:06:01 +00:00
|
|
|
log.Error("Trie dumping error", "err", err)
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
it := trie.NewIterator(trieIt)
|
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)
|
|
|
|
}
|
2023-05-23 10:10:26 +00:00
|
|
|
var (
|
2023-11-28 12:54:17 +00:00
|
|
|
account = DumpAccount{
|
|
|
|
Balance: data.Balance.String(),
|
|
|
|
Nonce: data.Nonce,
|
|
|
|
Root: data.Root[:],
|
|
|
|
CodeHash: data.CodeHash,
|
|
|
|
AddressHash: it.Key,
|
|
|
|
}
|
2023-05-23 10:10:26 +00:00
|
|
|
address *common.Address
|
2023-11-28 12:54:17 +00:00
|
|
|
addr common.Address
|
|
|
|
addrBytes = s.trie.GetKey(it.Key)
|
2023-05-23 10:10:26 +00:00
|
|
|
)
|
2020-05-07 13:06:31 +00:00
|
|
|
if addrBytes == nil {
|
2019-06-24 14:16:44 +00:00
|
|
|
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
|
|
|
|
}
|
2023-05-23 10:10:26 +00:00
|
|
|
} else {
|
2023-11-28 12:54:17 +00:00
|
|
|
addr = common.BytesToAddress(addrBytes)
|
2023-05-23 10:10:26 +00:00
|
|
|
address = &addr
|
2023-11-28 12:54:17 +00:00
|
|
|
account.Address = address
|
2019-06-24 14:16:44 +00:00
|
|
|
}
|
2023-07-11 13:43:23 +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 {
|
2023-07-24 10:22:09 +00:00
|
|
|
account.Code = obj.Code()
|
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)
|
2023-07-24 10:22:09 +00:00
|
|
|
tr, err := obj.getTrie()
|
2022-12-21 09:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to load storage trie", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-20 19:31:45 +00:00
|
|
|
trieIt, err := tr.NodeIterator(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create trie iterator", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
storageIt := trie.NewIterator(trieIt)
|
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
|
|
|
}
|
2023-05-23 10:10:26 +00:00
|
|
|
c.OnAccount(address, 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
|
|
|
}
|
|
|
|
|
2023-11-28 12:54:17 +00:00
|
|
|
// RawDump returns the state. If the processing is aborted e.g. due to options
|
|
|
|
// reaching Max, the `Next` key is set on the returned Dump.
|
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{
|
2023-11-28 12:54:17 +00:00
|
|
|
Accounts: make(map[string]DumpAccount),
|
2014-12-23 17:35:36 +00:00
|
|
|
}
|
2023-11-28 12:54:17 +00:00
|
|
|
dump.Next = 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 {
|
2023-11-28 12:54:17 +00:00
|
|
|
log.Error("Error dumping state", "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
|
|
|
}
|