2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-10-31 08:43:14 -05:00
|
|
|
package state
|
2014-08-06 02:53:00 -05: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 03:05:39 -05:00
|
|
|
"time"
|
2014-08-06 02:53:00 -05:00
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-06-24 09:16:44 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-09-28 03:48:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-06-24 09:16:44 -05:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-09-22 14:04:58 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2017-04-18 06:37:10 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2014-08-06 02:53:00 -05:00
|
|
|
)
|
|
|
|
|
2022-10-11 02:37:00 -05: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 03:05:39 -05:00
|
|
|
// iterated and collected.
|
|
|
|
type DumpConfig struct {
|
|
|
|
SkipCode bool
|
|
|
|
SkipStorage bool
|
|
|
|
OnlyWithAddresses bool
|
|
|
|
Start []byte
|
|
|
|
Max uint64
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:12:51 -05: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 05:10:26 -05:00
|
|
|
OnAccount(*common.Address, DumpAccount)
|
2020-06-30 03:12:51 -05:00
|
|
|
}
|
|
|
|
|
2020-03-31 05:08:44 -05:00
|
|
|
// DumpAccount represents an account in the state.
|
2016-09-22 14:04:58 -05:00
|
|
|
type DumpAccount struct {
|
2023-11-28 06:54:17 -06: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 09:16:44 -05:00
|
|
|
|
2014-08-06 02:53:00 -05:00
|
|
|
}
|
|
|
|
|
2020-03-31 05:08:44 -05:00
|
|
|
// Dump represents the full dump in a collected format, as one large map.
|
2016-09-22 14:04:58 -05:00
|
|
|
type Dump struct {
|
2023-11-28 06:54:17 -06: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 09:16:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:12:51 -05: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 05:10:26 -05:00
|
|
|
func (d *Dump) OnAccount(addr *common.Address, account DumpAccount) {
|
2023-11-28 06:54:17 -06:00
|
|
|
if addr == nil {
|
|
|
|
d.Accounts[fmt.Sprintf("pre(%s)", account.AddressHash)] = account
|
2023-05-23 05:10:26 -05:00
|
|
|
}
|
|
|
|
if addr != nil {
|
2023-11-28 06:54:17 -06:00
|
|
|
d.Accounts[(*addr).String()] = account
|
2023-05-23 05:10:26 -05:00
|
|
|
}
|
2019-06-24 09:16:44 -05:00
|
|
|
}
|
2020-03-31 05:08:44 -05:00
|
|
|
|
2020-06-30 03:12:51 -05:00
|
|
|
// iterativeDump is a DumpCollector-implementation which dumps output line-by-line iteratively.
|
|
|
|
type iterativeDump struct {
|
|
|
|
*json.Encoder
|
2020-03-31 05:08:44 -05:00
|
|
|
}
|
2019-06-24 09:16:44 -05:00
|
|
|
|
2020-06-30 03:12:51 -05:00
|
|
|
// OnAccount implements DumpCollector interface
|
2023-05-23 05:10:26 -05:00
|
|
|
func (d iterativeDump) OnAccount(addr *common.Address, account DumpAccount) {
|
2019-06-24 09:16:44 -05:00
|
|
|
dumpAccount := &DumpAccount{
|
2023-11-28 06:54:17 -06: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 02:53:00 -05:00
|
|
|
}
|
2019-11-22 08:56:05 -06:00
|
|
|
d.Encode(dumpAccount)
|
2019-06-24 09:16:44 -05:00
|
|
|
}
|
2019-11-22 08:56:05 -06:00
|
|
|
|
2020-06-30 03:12:51 -05:00
|
|
|
// OnRoot implements DumpCollector interface
|
|
|
|
func (d iterativeDump) OnRoot(root common.Hash) {
|
2019-11-22 08:56:05 -06:00
|
|
|
d.Encode(struct {
|
2019-06-24 09:16:44 -05:00
|
|
|
Root common.Hash `json:"root"`
|
|
|
|
}{root})
|
|
|
|
}
|
2014-08-06 02:53:00 -05: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 03:05:39 -05: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 03:12:51 -05:00
|
|
|
c.OnRoot(s.trie.Hash())
|
2020-03-31 05:08:44 -05: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 14:31:45 -05:00
|
|
|
trieIt, err := s.trie.NodeIterator(conf.Start)
|
|
|
|
if err != nil {
|
2023-12-08 04:06:01 -06: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 14:31:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
it := trie.NewIterator(trieIt)
|
2014-12-23 11:35:36 -06:00
|
|
|
for it.Next() {
|
2021-09-28 03:48:07 -05:00
|
|
|
var data types.StateAccount
|
2016-09-22 14:04:58 -05:00
|
|
|
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
2016-04-15 04:16:56 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-05-23 05:10:26 -05:00
|
|
|
var (
|
2023-11-28 06:54:17 -06:00
|
|
|
account = DumpAccount{
|
|
|
|
Balance: data.Balance.String(),
|
|
|
|
Nonce: data.Nonce,
|
|
|
|
Root: data.Root[:],
|
|
|
|
CodeHash: data.CodeHash,
|
|
|
|
AddressHash: it.Key,
|
|
|
|
}
|
2023-05-23 05:10:26 -05:00
|
|
|
address *common.Address
|
2023-11-28 06:54:17 -06:00
|
|
|
addr common.Address
|
|
|
|
addrBytes = s.trie.GetKey(it.Key)
|
2023-05-23 05:10:26 -05:00
|
|
|
)
|
2020-05-07 08:06:31 -05:00
|
|
|
if addrBytes == nil {
|
2019-06-24 09:16:44 -05: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 03:05:39 -05:00
|
|
|
if conf.OnlyWithAddresses {
|
2019-06-24 09:16:44 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-05-23 05:10:26 -05:00
|
|
|
} else {
|
2023-11-28 06:54:17 -06:00
|
|
|
addr = common.BytesToAddress(addrBytes)
|
2023-05-23 05:10:26 -05:00
|
|
|
address = &addr
|
2023-11-28 06:54:17 -06:00
|
|
|
account.Address = address
|
2019-06-24 09:16:44 -05:00
|
|
|
}
|
2023-07-11 08:43:23 -05: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 03:05:39 -05:00
|
|
|
if !conf.SkipCode {
|
2023-07-24 05:22:09 -05:00
|
|
|
account.Code = obj.Code()
|
2019-06-24 09:16:44 -05: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 03:05:39 -05:00
|
|
|
if !conf.SkipStorage {
|
2019-06-24 09:16:44 -05:00
|
|
|
account.Storage = make(map[common.Hash]string)
|
2023-07-24 05:22:09 -05:00
|
|
|
tr, err := obj.getTrie()
|
2022-12-21 03:21:21 -06: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 14:31:45 -05: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 09:16:44 -05:00
|
|
|
for storageIt.Next() {
|
2019-08-12 09:14:40 -05: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 08:56:05 -06:00
|
|
|
account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
|
2019-06-24 09:16:44 -05:00
|
|
|
}
|
2014-12-23 11:35:36 -06:00
|
|
|
}
|
2023-05-23 05:10:26 -05: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 03:05:39 -05: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 05:08:44 -05:00
|
|
|
if it.Next() {
|
|
|
|
nextKey = it.Key
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2019-06-24 09:16:44 -05: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 03:05:39 -05:00
|
|
|
log.Info("Trie dumping complete", "accounts", accounts,
|
|
|
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
2020-03-31 05:08:44 -05:00
|
|
|
|
|
|
|
return nextKey
|
2019-06-24 09:16:44 -05:00
|
|
|
}
|
|
|
|
|
2023-11-28 06:54:17 -06: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 03:05:39 -05:00
|
|
|
func (s *StateDB) RawDump(opts *DumpConfig) Dump {
|
2019-06-24 09:16:44 -05:00
|
|
|
dump := &Dump{
|
2023-11-28 06:54:17 -06:00
|
|
|
Accounts: make(map[string]DumpAccount),
|
2014-12-23 11:35:36 -06:00
|
|
|
}
|
2023-11-28 06:54:17 -06:00
|
|
|
dump.Next = s.DumpToCollector(dump, opts)
|
2019-06-24 09:16:44 -05:00
|
|
|
return *dump
|
2015-02-28 12:15:57 -06:00
|
|
|
}
|
2014-08-06 02:53:00 -05:00
|
|
|
|
2019-06-24 09:16:44 -05: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 03:05:39 -05:00
|
|
|
func (s *StateDB) Dump(opts *DumpConfig) []byte {
|
|
|
|
dump := s.RawDump(opts)
|
2019-06-24 09:16:44 -05:00
|
|
|
json, err := json.MarshalIndent(dump, "", " ")
|
2014-08-06 02:53:00 -05:00
|
|
|
if err != nil {
|
2023-11-28 06:54:17 -06:00
|
|
|
log.Error("Error dumping state", "err", err)
|
2014-08-06 02:53:00 -05:00
|
|
|
}
|
2014-08-17 05:42:32 -05:00
|
|
|
return json
|
2014-08-06 02:53:00 -05:00
|
|
|
}
|
2019-06-24 09:16:44 -05: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 03:05:39 -05:00
|
|
|
func (s *StateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) {
|
|
|
|
s.DumpToCollector(iterativeDump{output}, opts)
|
2020-03-31 05:08:44 -05:00
|
|
|
}
|