2023-02-06 10:28:40 -05:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HashScheme is the legacy hash-based state scheme with which trie nodes are
|
|
|
|
// stored in the disk with node hash as the database key. The advantage of this
|
|
|
|
// scheme is that different versions of trie nodes can be stored in disk, which
|
|
|
|
// is very beneficial for constructing archive nodes. The drawback is it will
|
|
|
|
// store different trie nodes on the same path to different locations on the disk
|
|
|
|
// with no data locality, and it's unfriendly for designing state pruning.
|
|
|
|
//
|
|
|
|
// Now this scheme is still kept for backward compatibility, and it will be used
|
|
|
|
// for archive node and some other tries(e.g. light trie).
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
const HashScheme = "hash"
|
2023-02-06 10:28:40 -05:00
|
|
|
|
|
|
|
// PathScheme is the new path-based state scheme with which trie nodes are stored
|
|
|
|
// in the disk with node path as the database key. This scheme will only store one
|
|
|
|
// version of state data in the disk, which means that the state pruning operation
|
|
|
|
// is native. At the same time, this scheme will put adjacent trie nodes in the same
|
|
|
|
// area of the disk with good data locality property. But this scheme needs to rely
|
|
|
|
// on extra state diffs to survive deep reorg.
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
const PathScheme = "path"
|
2023-02-06 10:28:40 -05:00
|
|
|
|
2023-08-01 20:17:32 +08:00
|
|
|
// hasher is used to compute the sha256 hash of the provided data.
|
|
|
|
type hasher struct{ sha crypto.KeccakState }
|
2023-02-06 10:28:40 -05:00
|
|
|
|
|
|
|
var hasherPool = sync.Pool{
|
2024-04-30 22:25:35 +08:00
|
|
|
New: func() interface{} { return &hasher{sha: crypto.NewKeccakState()} },
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:17:32 +08:00
|
|
|
func newHasher() *hasher {
|
|
|
|
return hasherPool.Get().(*hasher)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hasher) hash(data []byte) common.Hash {
|
|
|
|
return crypto.HashData(h.sha, data)
|
|
|
|
}
|
2023-02-06 10:28:40 -05:00
|
|
|
|
2023-08-01 20:17:32 +08:00
|
|
|
func (h *hasher) release() {
|
|
|
|
hasherPool.Put(h)
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// ReadAccountTrieNode retrieves the account trie node with the specified node path.
|
|
|
|
func ReadAccountTrieNode(db ethdb.KeyValueReader, path []byte) []byte {
|
|
|
|
data, _ := db.Get(accountTrieNodeKey(path))
|
|
|
|
return data
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// HasAccountTrieNode checks the presence of the account trie node with the
|
2023-09-22 14:31:10 +08:00
|
|
|
// specified node path, regardless of the node hash.
|
2024-04-30 22:25:35 +08:00
|
|
|
func HasAccountTrieNode(db ethdb.KeyValueReader, path []byte) bool {
|
2023-09-22 14:31:10 +08:00
|
|
|
has, err := db.Has(accountTrieNodeKey(path))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2023-02-06 10:28:40 -05:00
|
|
|
// WriteAccountTrieNode writes the provided account trie node into database.
|
|
|
|
func WriteAccountTrieNode(db ethdb.KeyValueWriter, path []byte, node []byte) {
|
|
|
|
if err := db.Put(accountTrieNodeKey(path), node); err != nil {
|
|
|
|
log.Crit("Failed to store account trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAccountTrieNode deletes the specified account trie node from the database.
|
|
|
|
func DeleteAccountTrieNode(db ethdb.KeyValueWriter, path []byte) {
|
|
|
|
if err := db.Delete(accountTrieNodeKey(path)); err != nil {
|
|
|
|
log.Crit("Failed to delete account trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// ReadStorageTrieNode retrieves the storage trie node with the specified node path.
|
|
|
|
func ReadStorageTrieNode(db ethdb.KeyValueReader, accountHash common.Hash, path []byte) []byte {
|
|
|
|
data, _ := db.Get(storageTrieNodeKey(accountHash, path))
|
|
|
|
return data
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// HasStorageTrieNode checks the presence of the storage trie node with the
|
2023-09-22 14:31:10 +08:00
|
|
|
// specified account hash and node path, regardless of the node hash.
|
2024-04-30 22:25:35 +08:00
|
|
|
func HasStorageTrieNode(db ethdb.KeyValueReader, accountHash common.Hash, path []byte) bool {
|
2023-09-22 14:31:10 +08:00
|
|
|
has, err := db.Has(storageTrieNodeKey(accountHash, path))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2023-02-06 10:28:40 -05:00
|
|
|
// WriteStorageTrieNode writes the provided storage trie node into database.
|
|
|
|
func WriteStorageTrieNode(db ethdb.KeyValueWriter, accountHash common.Hash, path []byte, node []byte) {
|
|
|
|
if err := db.Put(storageTrieNodeKey(accountHash, path), node); err != nil {
|
|
|
|
log.Crit("Failed to store storage trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStorageTrieNode deletes the specified storage trie node from the database.
|
|
|
|
func DeleteStorageTrieNode(db ethdb.KeyValueWriter, accountHash common.Hash, path []byte) {
|
|
|
|
if err := db.Delete(storageTrieNodeKey(accountHash, path)); err != nil {
|
|
|
|
log.Crit("Failed to delete storage trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLegacyTrieNode retrieves the legacy trie node with the given
|
|
|
|
// associated node hash.
|
|
|
|
func ReadLegacyTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
|
|
|
|
data, err := db.Get(hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasLegacyTrieNode checks if the trie node with the provided hash is present in db.
|
|
|
|
func HasLegacyTrieNode(db ethdb.KeyValueReader, hash common.Hash) bool {
|
|
|
|
ok, _ := db.Has(hash.Bytes())
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteLegacyTrieNode writes the provided legacy trie node to database.
|
|
|
|
func WriteLegacyTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
|
|
|
|
if err := db.Put(hash.Bytes(), node); err != nil {
|
|
|
|
log.Crit("Failed to store legacy trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLegacyTrieNode deletes the specified legacy trie node from database.
|
|
|
|
func DeleteLegacyTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
|
|
|
|
if err := db.Delete(hash.Bytes()); err != nil {
|
|
|
|
log.Crit("Failed to delete legacy trie node", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasTrieNode checks the trie node presence with the provided node info and
|
|
|
|
// the associated node hash.
|
|
|
|
func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash common.Hash, scheme string) bool {
|
|
|
|
switch scheme {
|
|
|
|
case HashScheme:
|
|
|
|
return HasLegacyTrieNode(db, hash)
|
|
|
|
case PathScheme:
|
2024-04-30 22:25:35 +08:00
|
|
|
var blob []byte
|
2023-02-06 10:28:40 -05:00
|
|
|
if owner == (common.Hash{}) {
|
2024-04-30 22:25:35 +08:00
|
|
|
blob = ReadAccountTrieNode(db, path)
|
|
|
|
} else {
|
|
|
|
blob = ReadStorageTrieNode(db, owner, path)
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
2024-04-30 22:25:35 +08:00
|
|
|
if len(blob) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h := newHasher()
|
|
|
|
defer h.release()
|
|
|
|
return h.hash(blob) == hash // exists but not match
|
2023-02-06 10:28:40 -05:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTrieNode retrieves the trie node from database with the provided node info
|
|
|
|
// and associated node hash.
|
|
|
|
func ReadTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash common.Hash, scheme string) []byte {
|
|
|
|
switch scheme {
|
|
|
|
case HashScheme:
|
|
|
|
return ReadLegacyTrieNode(db, hash)
|
|
|
|
case PathScheme:
|
2024-04-30 22:25:35 +08:00
|
|
|
var blob []byte
|
2023-02-06 10:28:40 -05:00
|
|
|
if owner == (common.Hash{}) {
|
2024-04-30 22:25:35 +08:00
|
|
|
blob = ReadAccountTrieNode(db, path)
|
2023-02-06 10:28:40 -05:00
|
|
|
} else {
|
2024-04-30 22:25:35 +08:00
|
|
|
blob = ReadStorageTrieNode(db, owner, path)
|
2023-02-06 10:28:40 -05:00
|
|
|
}
|
2024-04-30 22:25:35 +08:00
|
|
|
if len(blob) == 0 {
|
2023-02-06 10:28:40 -05:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-30 22:25:35 +08:00
|
|
|
h := newHasher()
|
|
|
|
defer h.release()
|
|
|
|
if h.hash(blob) != hash {
|
|
|
|
return nil // exists but not match
|
|
|
|
}
|
2023-02-06 10:28:40 -05:00
|
|
|
return blob
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// WriteTrieNode writes the trie node into database with the provided node info.
|
2023-02-06 10:28:40 -05:00
|
|
|
//
|
2024-04-30 22:25:35 +08:00
|
|
|
// hash-scheme requires the node hash as the identifier.
|
|
|
|
// path-scheme requires the node owner and path as the identifier.
|
2023-02-06 10:28:40 -05:00
|
|
|
func WriteTrieNode(db ethdb.KeyValueWriter, owner common.Hash, path []byte, hash common.Hash, node []byte, scheme string) {
|
|
|
|
switch scheme {
|
|
|
|
case HashScheme:
|
|
|
|
WriteLegacyTrieNode(db, hash, node)
|
|
|
|
case PathScheme:
|
|
|
|
if owner == (common.Hash{}) {
|
|
|
|
WriteAccountTrieNode(db, path, node)
|
|
|
|
} else {
|
|
|
|
WriteStorageTrieNode(db, owner, path, node)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:25:35 +08:00
|
|
|
// DeleteTrieNode deletes the trie node from database with the provided node info.
|
2023-02-06 10:28:40 -05:00
|
|
|
//
|
2024-04-30 22:25:35 +08:00
|
|
|
// hash-scheme requires the node hash as the identifier.
|
|
|
|
// path-scheme requires the node owner and path as the identifier.
|
2023-02-06 10:28:40 -05:00
|
|
|
func DeleteTrieNode(db ethdb.KeyValueWriter, owner common.Hash, path []byte, hash common.Hash, scheme string) {
|
|
|
|
switch scheme {
|
|
|
|
case HashScheme:
|
|
|
|
DeleteLegacyTrieNode(db, hash)
|
|
|
|
case PathScheme:
|
|
|
|
if owner == (common.Hash{}) {
|
|
|
|
DeleteAccountTrieNode(db, path)
|
|
|
|
} else {
|
|
|
|
DeleteStorageTrieNode(db, owner, path)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
|
|
|
}
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
|
|
|
|
// ReadStateScheme reads the state scheme of persistent state, or none
|
|
|
|
// if the state is not present in database.
|
|
|
|
func ReadStateScheme(db ethdb.Reader) string {
|
2024-04-30 22:25:35 +08:00
|
|
|
// Check if state in path-based scheme is present.
|
|
|
|
if HasAccountTrieNode(db, nil) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
return PathScheme
|
|
|
|
}
|
2023-12-22 14:50:41 +08:00
|
|
|
// The root node might be deleted during the initial snap sync, check
|
|
|
|
// the persistent state id then.
|
|
|
|
if id := ReadPersistentStateID(db); id != 0 {
|
|
|
|
return PathScheme
|
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
// In a hash-based scheme, the genesis state is consistently stored
|
|
|
|
// on the disk. To assess the scheme of the persistent state, it
|
|
|
|
// suffices to inspect the scheme of the genesis state.
|
|
|
|
header := ReadHeader(db, ReadCanonicalHash(db, 0), 0)
|
|
|
|
if header == nil {
|
|
|
|
return "" // empty datadir
|
|
|
|
}
|
2024-04-30 22:25:35 +08:00
|
|
|
if !HasLegacyTrieNode(db, header.Root) {
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
return "" // no state in disk
|
|
|
|
}
|
|
|
|
return HashScheme
|
|
|
|
}
|
2023-10-11 16:27:44 +08:00
|
|
|
|
|
|
|
// ParseStateScheme checks if the specified state scheme is compatible with
|
|
|
|
// the stored state.
|
|
|
|
//
|
|
|
|
// - If the provided scheme is none, use the scheme consistent with persistent
|
2024-02-29 12:40:59 +02:00
|
|
|
// state, or fallback to path-based scheme if state is empty.
|
2023-10-11 16:27:44 +08:00
|
|
|
//
|
|
|
|
// - If the provided scheme is hash, use hash-based scheme or error out if not
|
|
|
|
// compatible with persistent state scheme.
|
|
|
|
//
|
|
|
|
// - If the provided scheme is path: use path-based scheme or error out if not
|
|
|
|
// compatible with persistent state scheme.
|
|
|
|
func ParseStateScheme(provided string, disk ethdb.Database) (string, error) {
|
|
|
|
// If state scheme is not specified, use the scheme consistent
|
|
|
|
// with persistent state, or fallback to hash mode if database
|
|
|
|
// is empty.
|
|
|
|
stored := ReadStateScheme(disk)
|
|
|
|
if provided == "" {
|
|
|
|
if stored == "" {
|
2024-02-29 12:40:59 +02:00
|
|
|
log.Info("State schema set to default", "scheme", "path")
|
|
|
|
return PathScheme, nil // use default scheme for empty database
|
2023-10-11 16:27:44 +08:00
|
|
|
}
|
|
|
|
log.Info("State scheme set to already existing", "scheme", stored)
|
|
|
|
return stored, nil // reuse scheme of persistent scheme
|
|
|
|
}
|
|
|
|
// If state scheme is specified, ensure it's compatible with
|
|
|
|
// persistent state.
|
|
|
|
if stored == "" || provided == stored {
|
|
|
|
log.Info("State scheme set by user", "scheme", provided)
|
|
|
|
return provided, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("incompatible state scheme, stored: %s, provided: %s", stored, provided)
|
|
|
|
}
|