2023-05-09 15:11:04 +08:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
2018-02-05 18:40:32 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2024-02-13 21:49:53 +08:00
|
|
|
package triedb
|
2018-02-05 18:40:32 +02:00
|
|
|
|
|
|
|
import (
|
2019-04-05 13:09:28 +03:00
|
|
|
"errors"
|
2023-09-24 12:21:00 +08:00
|
|
|
"strings"
|
2018-02-05 18:40:32 +02:00
|
|
|
|
2024-08-09 17:29:47 +08:00
|
|
|
versa "github.com/bnb-chain/versioned-state-database"
|
2018-02-05 18:40:32 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-09-24 12:21:00 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2018-02-05 18:40:32 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
all: activate pbss as experimental feature from eth (#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
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-05-09 15:11:04 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2023-07-11 21:43:23 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie/triestate"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb/database"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/hashdb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
2024-08-09 17:29:47 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb/versiondb"
|
2018-06-04 10:47:43 +03:00
|
|
|
)
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Config defines all necessary options for database.
|
|
|
|
type Config struct {
|
2023-09-24 12:21:00 +08:00
|
|
|
Preimages bool // Flag whether the preimage of node key is recorded
|
|
|
|
Cache int
|
2024-02-02 15:43:33 +08:00
|
|
|
NoTries bool
|
2024-08-09 17:29:47 +08:00
|
|
|
IsVerkle bool // Flag whether the db is holding a verkle tree
|
|
|
|
IsVersion bool
|
|
|
|
VersionDB *versiondb.Config
|
all: activate pbss as experimental feature from eth (#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
|
|
|
HashDB *hashdb.Config // Configs for hash-based scheme
|
|
|
|
PathDB *pathdb.Config // Configs for experimental path-based scheme
|
2018-06-21 12:28:05 +03:00
|
|
|
}
|
|
|
|
|
all: activate pbss as experimental feature from eth (#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
|
|
|
// HashDefaults represents a config for using hash-based scheme with
|
|
|
|
// default settings.
|
|
|
|
var HashDefaults = &Config{
|
|
|
|
Preimages: false,
|
|
|
|
HashDB: hashdb.Defaults,
|
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// backend defines the methods needed to access/update trie nodes in different
|
|
|
|
// state scheme.
|
|
|
|
type backend interface {
|
|
|
|
// Scheme returns the identifier of used storage scheme.
|
|
|
|
Scheme() string
|
2018-06-21 12:28:05 +03:00
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Initialized returns an indicator if the state data is already initialized
|
|
|
|
// according to the state scheme.
|
|
|
|
Initialized(genesisRoot common.Hash) bool
|
2018-06-04 10:47:43 +03:00
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Size returns the current storage size of the memory cache in front of the
|
|
|
|
// persistent database layer.
|
2023-11-06 14:11:17 +08:00
|
|
|
Size() (common.StorageSize, common.StorageSize, common.StorageSize)
|
2018-02-05 18:40:32 +02:00
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Update performs a state transition by committing dirty nodes contained
|
|
|
|
// in the given set in order to update state from the specified parent to
|
|
|
|
// the specified root.
|
2023-08-01 20:17:32 +08:00
|
|
|
//
|
|
|
|
// The passed in maps(nodes, states) will be retained to avoid copying
|
|
|
|
// everything. Therefore, these maps must not be changed afterwards.
|
|
|
|
Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error
|
2018-11-22 14:09:04 +02:00
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Commit writes all relevant trie nodes belonging to the specified state
|
|
|
|
// to disk. Report specifies whether logs will be displayed in info level.
|
|
|
|
Commit(root common.Hash, report bool) error
|
2018-06-21 12:28:05 +03:00
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Close closes the trie database backend and releases all held resources.
|
|
|
|
Close() error
|
2018-06-21 12:28:05 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Database is the wrapper of the underlying backend which is shared by different
|
|
|
|
// types of node backend as an entrypoint. It's responsible for all interactions
|
|
|
|
// relevant with trie nodes and node preimages.
|
|
|
|
type Database struct {
|
2023-08-01 20:17:32 +08:00
|
|
|
config *Config // Configuration for trie database
|
|
|
|
diskdb ethdb.Database // Persistent database to store the snapshot
|
|
|
|
preimages *preimageStore // The store for caching preimages
|
|
|
|
backend backend // The backend for managing trie nodes
|
2018-06-21 12:28:05 +03:00
|
|
|
}
|
|
|
|
|
all: activate pbss as experimental feature from eth (#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
|
|
|
// NewDatabase initializes the trie database with default settings, note
|
2023-05-09 15:11:04 +08:00
|
|
|
// the legacy hash-based scheme is used by default.
|
all: activate pbss as experimental feature from eth (#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
|
|
|
func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
|
2024-08-09 17:29:47 +08:00
|
|
|
if config != nil && config.IsVersion {
|
|
|
|
// TODO:: Wait for debugging to stabilize, and then consider initialization compatibility with other databases
|
|
|
|
db := &Database{
|
|
|
|
config: config,
|
|
|
|
diskdb: diskdb,
|
|
|
|
backend: versiondb.New(config.VersionDB),
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
all: activate pbss as experimental feature from eth (#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
|
|
|
// Sanitize the config and use the default one if it's not specified.
|
2024-03-08 16:07:29 +08:00
|
|
|
var triediskdb ethdb.Database
|
|
|
|
if diskdb != nil && diskdb.StateStore() != nil {
|
|
|
|
triediskdb = diskdb.StateStore()
|
|
|
|
} else {
|
|
|
|
triediskdb = diskdb
|
|
|
|
}
|
2023-09-24 12:21:00 +08:00
|
|
|
dbScheme := rawdb.ReadStateScheme(diskdb)
|
all: activate pbss as experimental feature from eth (#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
|
|
|
if config == nil {
|
2023-09-24 12:21:00 +08:00
|
|
|
if dbScheme == rawdb.PathScheme {
|
|
|
|
config = &Config{
|
|
|
|
PathDB: pathdb.Defaults,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config = HashDefaults
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.PathDB == nil && config.HashDB == nil {
|
|
|
|
if dbScheme == rawdb.PathScheme {
|
|
|
|
config.PathDB = pathdb.Defaults
|
|
|
|
} else {
|
|
|
|
config.HashDB = hashdb.Defaults
|
|
|
|
}
|
all: activate pbss as experimental feature from eth (#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
|
|
|
}
|
2024-04-02 19:39:43 +08:00
|
|
|
if config.PathDB != nil && config.NoTries {
|
|
|
|
config.PathDB.NoTries = true
|
|
|
|
}
|
all: activate pbss as experimental feature from eth (#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
|
|
|
var preimages *preimageStore
|
|
|
|
if config.Preimages {
|
2024-03-08 16:07:29 +08:00
|
|
|
preimages = newPreimageStore(triediskdb)
|
all: activate pbss as experimental feature from eth (#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
|
|
|
}
|
|
|
|
db := &Database{
|
|
|
|
config: config,
|
2024-03-08 16:07:29 +08:00
|
|
|
diskdb: triediskdb,
|
all: activate pbss as experimental feature from eth (#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
|
|
|
preimages: preimages,
|
|
|
|
}
|
2023-09-24 12:21:00 +08:00
|
|
|
/*
|
|
|
|
* 1. First, initialize db according to the user config
|
|
|
|
* 2. Second, initialize the db according to the scheme already used by db
|
|
|
|
* 3. Last, use the default scheme, namely hash scheme
|
|
|
|
*/
|
|
|
|
if config.HashDB != nil {
|
2024-03-08 16:07:29 +08:00
|
|
|
if rawdb.ReadStateScheme(triediskdb) == rawdb.PathScheme {
|
2024-03-19 15:18:58 +08:00
|
|
|
log.Warn("Incompatible state scheme", "old", rawdb.PathScheme, "new", rawdb.HashScheme)
|
2023-09-24 12:21:00 +08:00
|
|
|
}
|
2024-03-11 15:16:24 +08:00
|
|
|
db.backend = hashdb.New(triediskdb, config.HashDB, trie.MerkleResolver{})
|
2023-09-24 12:21:00 +08:00
|
|
|
} else if config.PathDB != nil {
|
2024-03-08 16:07:29 +08:00
|
|
|
if rawdb.ReadStateScheme(triediskdb) == rawdb.HashScheme {
|
2024-03-19 15:18:58 +08:00
|
|
|
log.Warn("Incompatible state scheme", "old", rawdb.HashScheme, "new", rawdb.PathScheme)
|
2023-09-24 12:21:00 +08:00
|
|
|
}
|
2024-03-08 16:07:29 +08:00
|
|
|
db.backend = pathdb.New(triediskdb, config.PathDB)
|
2023-09-24 12:21:00 +08:00
|
|
|
} else if strings.Compare(dbScheme, rawdb.PathScheme) == 0 {
|
|
|
|
if config.PathDB == nil {
|
|
|
|
config.PathDB = pathdb.Defaults
|
|
|
|
}
|
2024-03-08 16:07:29 +08:00
|
|
|
db.backend = pathdb.New(triediskdb, config.PathDB)
|
all: activate pbss as experimental feature from eth (#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
|
|
|
} else {
|
2024-02-13 21:49:53 +08:00
|
|
|
var resolver hashdb.ChildResolver
|
|
|
|
if config.IsVerkle {
|
|
|
|
// TODO define verkle resolver
|
|
|
|
log.Crit("Verkle node resolver is not defined")
|
|
|
|
} else {
|
|
|
|
resolver = trie.MerkleResolver{}
|
|
|
|
}
|
2023-09-24 12:21:00 +08:00
|
|
|
if config.HashDB == nil {
|
|
|
|
config.HashDB = hashdb.Defaults
|
|
|
|
}
|
2024-03-11 15:16:24 +08:00
|
|
|
db.backend = hashdb.New(triediskdb, config.HashDB, resolver)
|
2020-11-18 17:51:33 +08:00
|
|
|
}
|
|
|
|
return db
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 16:39:29 +08:00
|
|
|
func (db *Database) Config() *Config {
|
|
|
|
return db.config
|
|
|
|
}
|
|
|
|
|
2024-03-08 15:36:25 +08:00
|
|
|
func (db *Database) DiskDB() ethdb.Database {
|
|
|
|
return db.diskdb
|
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Reader returns a reader for accessing all trie nodes with provided state root.
|
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-21 03:31:45 +08:00
|
|
|
// An error will be returned if the requested state is not available.
|
2024-02-13 21:49:53 +08:00
|
|
|
func (db *Database) Reader(blockRoot common.Hash) (database.Reader, error) {
|
2023-08-01 20:17:32 +08:00
|
|
|
switch b := db.backend.(type) {
|
|
|
|
case *hashdb.Database:
|
|
|
|
return b.Reader(blockRoot)
|
|
|
|
case *pathdb.Database:
|
|
|
|
return b.Reader(blockRoot)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2023-08-01 20:17:32 +08:00
|
|
|
return nil, errors.New("unknown backend")
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Update performs a state transition by committing dirty nodes contained in the
|
|
|
|
// given set in order to update state from the specified parent to the specified
|
|
|
|
// root. The held pre-images accumulated up to this point will be flushed in case
|
|
|
|
// the size exceeds the threshold.
|
2018-02-05 18:40:32 +02:00
|
|
|
//
|
2023-08-01 20:17:32 +08:00
|
|
|
// The passed in maps(nodes, states) will be retained to avoid copying everything.
|
|
|
|
// Therefore, these maps must not be changed afterwards.
|
2023-07-24 18:22:09 +08:00
|
|
|
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
|
2023-05-09 15:11:04 +08:00
|
|
|
if db.preimages != nil {
|
|
|
|
db.preimages.commit(false)
|
2018-11-12 18:47:34 +02:00
|
|
|
}
|
2023-08-01 20:17:32 +08:00
|
|
|
return db.backend.Update(root, parent, block, nodes, states)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Commit iterates over all the children of a particular node, writes them out
|
|
|
|
// to disk. As a side effect, all pre-images accumulated up to this point are
|
|
|
|
// also written.
|
|
|
|
func (db *Database) Commit(root common.Hash, report bool) error {
|
|
|
|
if db.preimages != nil {
|
|
|
|
db.preimages.commit(true)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2023-05-09 15:11:04 +08:00
|
|
|
return db.backend.Commit(root, report)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:08:39 +03:00
|
|
|
// Size returns the storage size of diff layer nodes above the persistent disk
|
|
|
|
// layer, the dirty nodes buffered within the disk layer, and the size of cached
|
|
|
|
// preimages.
|
2023-11-06 14:11:17 +08:00
|
|
|
func (db *Database) Size() (common.StorageSize, common.StorageSize, common.StorageSize, common.StorageSize) {
|
2023-05-09 15:11:04 +08:00
|
|
|
var (
|
2023-11-06 14:11:17 +08:00
|
|
|
diffs, nodes, immutablenodes common.StorageSize
|
|
|
|
preimages common.StorageSize
|
2023-05-09 15:11:04 +08:00
|
|
|
)
|
2023-11-06 14:11:17 +08:00
|
|
|
diffs, nodes, immutablenodes = db.backend.Size()
|
2023-05-09 15:11:04 +08:00
|
|
|
if db.preimages != nil {
|
|
|
|
preimages = db.preimages.size()
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2023-11-06 14:11:17 +08:00
|
|
|
return diffs, nodes, immutablenodes, preimages
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Initialized returns an indicator if the state data is already initialized
|
|
|
|
// according to the state scheme.
|
|
|
|
func (db *Database) Initialized(genesisRoot common.Hash) bool {
|
|
|
|
return db.backend.Initialized(genesisRoot)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Scheme returns the node scheme used in the database.
|
|
|
|
func (db *Database) Scheme() string {
|
|
|
|
return db.backend.Scheme()
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Close flushes the dangling preimages to disk and closes the trie database.
|
|
|
|
// It is meant to be called when closing the blockchain object, so that all
|
|
|
|
// resources held can be released correctly.
|
|
|
|
func (db *Database) Close() error {
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
db.WritePreimages()
|
|
|
|
return db.backend.Close()
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
// WritePreimages flushes all accumulated preimages to disk forcibly.
|
|
|
|
func (db *Database) WritePreimages() {
|
2023-05-09 15:11:04 +08:00
|
|
|
if db.preimages != nil {
|
|
|
|
db.preimages.commit(true)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:49:53 +08:00
|
|
|
// Preimage retrieves a cached trie node pre-image from preimage store.
|
2023-09-19 14:47:24 +02:00
|
|
|
func (db *Database) Preimage(hash common.Hash) []byte {
|
|
|
|
if db.preimages == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return db.preimages.preimage(hash)
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:49:53 +08:00
|
|
|
// InsertPreimage writes pre-images of trie node to the preimage store.
|
|
|
|
func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
|
|
|
|
if db.preimages == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
db.preimages.insertPreimage(preimages)
|
|
|
|
}
|
|
|
|
|
2018-06-04 10:47:43 +03:00
|
|
|
// Cap iteratively flushes old but still referenced trie nodes until the total
|
2023-05-09 15:11:04 +08:00
|
|
|
// memory usage goes below the given threshold. The held pre-images accumulated
|
|
|
|
// up to this point will be flushed in case the size exceeds the threshold.
|
2019-03-26 15:48:31 +01:00
|
|
|
//
|
2023-05-09 15:11:04 +08:00
|
|
|
// It's only supported by hash-based database and will return an error for others.
|
2018-06-04 10:47:43 +03:00
|
|
|
func (db *Database) Cap(limit common.StorageSize) error {
|
2023-05-09 15:11:04 +08:00
|
|
|
hdb, ok := db.backend.(*hashdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2020-11-18 17:51:33 +08:00
|
|
|
if db.preimages != nil {
|
2023-05-09 15:11:04 +08:00
|
|
|
db.preimages.commit(false)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2023-05-09 15:11:04 +08:00
|
|
|
return hdb.Cap(limit)
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Reference adds a new reference from a parent node to a child node. This function
|
|
|
|
// is used to add reference between internal trie node and external node(e.g. storage
|
|
|
|
// trie root), all internal trie nodes are referenced together by database itself.
|
|
|
|
//
|
|
|
|
// It's only supported by hash-based database and will return an error for others.
|
|
|
|
func (db *Database) Reference(root common.Hash, parent common.Hash) error {
|
|
|
|
hdb, ok := db.backend.(*hashdb.Database)
|
2018-02-05 18:40:32 +02:00
|
|
|
if !ok {
|
2023-05-09 15:11:04 +08:00
|
|
|
return errors.New("not supported")
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2023-05-09 15:11:04 +08:00
|
|
|
hdb.Reference(root, parent)
|
2018-02-05 18:40:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 15:11:04 +08:00
|
|
|
// Dereference removes an existing reference from a root node. It's only
|
|
|
|
// supported by hash-based database and will return an error for others.
|
|
|
|
func (db *Database) Dereference(root common.Hash) error {
|
|
|
|
hdb, ok := db.backend.(*hashdb.Database)
|
2018-02-05 18:40:32 +02:00
|
|
|
if !ok {
|
2023-05-09 15:11:04 +08:00
|
|
|
return errors.New("not supported")
|
2020-07-28 21:30:31 +08:00
|
|
|
}
|
2023-05-09 15:11:04 +08:00
|
|
|
hdb.Dereference(root)
|
2020-07-28 21:30:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
all: activate pbss as experimental feature from eth (#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
|
|
|
// Recover rollbacks the database to a specified historical point. The state is
|
|
|
|
// supported as the rollback destination only if it's canonical state and the
|
|
|
|
// corresponding trie histories are existent. It's only supported by path-based
|
|
|
|
// database and will return an error for others.
|
|
|
|
func (db *Database) Recover(target common.Hash) error {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
2024-02-13 21:49:53 +08:00
|
|
|
var loader triestate.TrieLoader
|
|
|
|
if db.config.IsVerkle {
|
|
|
|
// TODO define verkle loader
|
|
|
|
log.Crit("Verkle loader is not defined")
|
|
|
|
} else {
|
|
|
|
loader = trie.NewMerkleLoader(db)
|
|
|
|
}
|
|
|
|
return pdb.Recover(target, loader)
|
all: activate pbss as experimental feature from eth (#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
|
|
|
}
|
|
|
|
|
|
|
|
// Recoverable returns the indicator if the specified state is enabled to be
|
|
|
|
// recovered. It's only supported by path-based database and will return an
|
|
|
|
// error for others.
|
|
|
|
func (db *Database) Recoverable(root common.Hash) (bool, error) {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return false, errors.New("not supported")
|
|
|
|
}
|
|
|
|
return pdb.Recoverable(root), nil
|
|
|
|
}
|
|
|
|
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 15:00:53 +08:00
|
|
|
// Disable deactivates the database and invalidates all available state layers
|
|
|
|
// as stale to prevent access to the persistent state, which is in the syncing
|
|
|
|
// stage.
|
|
|
|
//
|
all: activate pbss as experimental feature from eth (#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
|
|
|
// It's only supported by path-based database and will return an error for others.
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 15:00:53 +08:00
|
|
|
func (db *Database) Disable() error {
|
all: activate pbss as experimental feature from eth (#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
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 15:00:53 +08:00
|
|
|
return pdb.Disable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable activates database and resets the state tree with the provided persistent
|
|
|
|
// state root once the state sync is finished.
|
|
|
|
func (db *Database) Enable(root common.Hash) error {
|
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
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 15:00:53 +08:00
|
|
|
return pdb.Enable(root)
|
all: activate pbss as experimental feature from eth (#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
|
|
|
}
|
|
|
|
|
|
|
|
// Journal commits an entire diff hierarchy to disk into a single journal entry.
|
|
|
|
// This is meant to be used during shutdown to persist the snapshot without
|
|
|
|
// flattening everything down (bad for reorgs). It's only supported by path-based
|
|
|
|
// database and will return an error for others.
|
|
|
|
func (db *Database) Journal(root common.Hash) error {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
|
|
|
return pdb.Journal(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBufferSize sets the node buffer size to the provided value(in bytes).
|
|
|
|
// It's only supported by path-based database and will return an error for
|
|
|
|
// others.
|
|
|
|
func (db *Database) SetBufferSize(size int) error {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not supported")
|
|
|
|
}
|
|
|
|
return pdb.SetBufferSize(size)
|
|
|
|
}
|
2023-11-06 14:11:17 +08:00
|
|
|
|
|
|
|
// Head return the top non-fork difflayer/disklayer root hash for rewinding.
|
2024-01-25 22:07:44 +08:00
|
|
|
// It's only supported by path-based database and will return empty hash for
|
2023-11-06 14:11:17 +08:00
|
|
|
// others.
|
|
|
|
func (db *Database) Head() common.Hash {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
return pdb.Head()
|
|
|
|
}
|
2024-01-25 22:07:44 +08:00
|
|
|
|
2024-04-23 10:06:50 +08:00
|
|
|
// GetAllRooHash returns all MPT root hash in diffLayer and diskLayer.
|
2024-01-25 22:07:44 +08:00
|
|
|
// It's only supported by path-based database and will return nil for
|
|
|
|
// others.
|
|
|
|
func (db *Database) GetAllRooHash() [][]string {
|
|
|
|
pdb, ok := db.backend.(*pathdb.Database)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Not supported")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pdb.GetAllRooHash()
|
|
|
|
}
|
2024-02-02 15:43:33 +08:00
|
|
|
|
2023-11-14 13:09:40 +01:00
|
|
|
// IsVerkle returns the indicator if the database is holding a verkle tree.
|
|
|
|
func (db *Database) IsVerkle() bool {
|
|
|
|
return db.config.IsVerkle
|
|
|
|
}
|
2024-08-09 17:29:47 +08:00
|
|
|
|
|
|
|
// VersaDB returns versioned database instance, it is useless for hashdb and pathdb
|
|
|
|
func (db *Database) VersaDB() versa.Database {
|
|
|
|
vdb, ok := db.backend.(*versiondb.VersionDB)
|
|
|
|
if !ok {
|
|
|
|
log.Crit("only version db support")
|
|
|
|
}
|
|
|
|
return vdb.VersaDB()
|
|
|
|
}
|