Skip to content

Commit

Permalink
all: activate pbss
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman authored and rjl493456442 committed Jun 25, 2023
1 parent 9c34cf2 commit 7ecaff2
Show file tree
Hide file tree
Showing 67 changed files with 1,846 additions and 758 deletions.
25 changes: 17 additions & 8 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
"github.com/urfave/cli/v2"
)

Expand All @@ -49,7 +50,7 @@ var (
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Flags: flags.Merge([]cli.Flag{utils.CachePreimagesFlag}, utils.DatabasePathFlags),
Flags: flags.Merge([]cli.Flag{utils.CachePreimagesFlag}, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
The init command initializes a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
Expand Down Expand Up @@ -94,7 +95,7 @@ if one is set. Otherwise it prints the genesis from the datadir.`,
utils.MetricsInfluxDBBucketFlag,
utils.MetricsInfluxDBOrganizationFlag,
utils.TxLookupLimitFlag,
}, utils.DatabasePathFlags),
}, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
The import command imports blocks from an RLP-encoded form. The form can be one file
with several RLP-encoded blocks, or several files can be used.
Expand All @@ -110,7 +111,7 @@ processing will proceed even if an individual RLP-file import failure occurs.`,
Flags: flags.Merge([]cli.Flag{
utils.CacheFlag,
utils.SyncModeFlag,
}, utils.DatabasePathFlags),
}, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
Requires a first argument of the file to write to.
Optional second and third arguments control the first and
Expand Down Expand Up @@ -159,7 +160,7 @@ It's deprecated, please use "geth db export" instead.
utils.IncludeIncompletesFlag,
utils.StartKeyFlag,
utils.DumpLimitFlag,
}, utils.DatabasePathFlags),
}, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
This command dumps out the state for a given block (or latest, if none provided).
`,
Expand Down Expand Up @@ -195,14 +196,19 @@ func initGenesis(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}
triedb := trie.NewDatabaseWithConfig(chaindb, &trie.Config{
Preimages: ctx.Bool(utils.CachePreimagesFlag.Name),
})
defer chaindb.Close()

config := &trie.Config{Preimages: ctx.Bool(utils.CachePreimagesFlag.Name)}
if ctx.Bool(utils.PathBasedSchemeFlag.Name) {
config.PathDB = pathdb.Defaults
}
triedb := trie.NewDatabase(chaindb, config)
defer triedb.Close()

_, hash, err := core.SetupGenesisBlock(chaindb, triedb, genesis)
if err != nil {
utils.Fatalf("Failed to write genesis block: %v", err)
}
chaindb.Close()
log.Info("Successfully wrote genesis state", "database", name, "hash", hash)
}
return nil
Expand Down Expand Up @@ -468,6 +474,9 @@ func dump(ctx *cli.Context) error {
config := &trie.Config{
Preimages: true, // always enable preimage lookup
}
if ctx.Bool(utils.PathBasedSchemeFlag.Name) {
config.PathDB = pathdb.ReadOnly
}
state, err := state.New(root, state.NewDatabaseWithConfig(db, config), nil)
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ WARNING: This is a low-level operation which may cause database corruption!`,
ArgsUsage: "<hex-encoded state root> <hex-encoded account hash> <hex-encoded storage trie root> <hex-encoded start (optional)> <int max elements (optional)>",
Flags: flags.Merge([]cli.Flag{
utils.SyncModeFlag,
}, utils.NetworkFlags, utils.DatabasePathFlags),
}, utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: "This command looks up the specified database key from the database.",
}
dbDumpFreezerIndex = &cli.Command{
Expand Down Expand Up @@ -482,6 +482,9 @@ func dbDumpTrie(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()

triedb := utils.MakeDatabase(ctx, db, true)
defer triedb.Close()

var (
state []byte
storage []byte
Expand Down Expand Up @@ -515,7 +518,7 @@ func dbDumpTrie(ctx *cli.Context) error {
}
}
id := trie.StorageTrieID(common.BytesToHash(state), common.BytesToHash(account), common.BytesToHash(storage))
theTrie, err := trie.New(id, trie.NewDatabase(db))
theTrie, err := trie.New(id, triedb)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var (
utils.GpoMaxGasPriceFlag,
utils.GpoIgnoreGasPriceFlag,
configFileFlag,
}, utils.NetworkFlags, utils.DatabasePathFlags)
}, utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags)

rpcFlags = []cli.Flag{
utils.HTTPEnabledFlag,
Expand Down
37 changes: 26 additions & 11 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ the trie clean cache with default directory will be deleted.
Usage: "Recalculate state hash based on the snapshot for verification",
ArgsUsage: "<root>",
Action: verifyState,
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags),
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
geth snapshot verify-state <state-root>
will traverse the whole accounts and storages set based on the specified
Expand Down Expand Up @@ -107,7 +107,7 @@ information about the specified address.
Usage: "Traverse the state with given root hash and perform quick verification",
ArgsUsage: "<root>",
Action: traverseState,
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags),
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
geth snapshot traverse-state <state-root>
will traverse the whole state from the given state root and will abort if any
Expand All @@ -122,7 +122,7 @@ It's also usable without snapshot enabled.
Usage: "Traverse the state with given root hash and perform detailed verification",
ArgsUsage: "<root>",
Action: traverseRawState,
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags),
Flags: flags.Merge(utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
geth snapshot traverse-rawstate <state-root>
will traverse the whole state from the given root and will abort if any referenced
Expand All @@ -143,7 +143,7 @@ It's also usable without snapshot enabled.
utils.ExcludeStorageFlag,
utils.StartKeyFlag,
utils.DumpLimitFlag,
}, utils.NetworkFlags, utils.DatabasePathFlags),
}, utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags),
Description: `
This command is semantically equivalent to 'geth dump', but uses the snapshots
as the backend data source, making this command a lot faster.
Expand Down Expand Up @@ -205,13 +205,16 @@ func verifyState(ctx *cli.Context) error {
log.Error("Failed to load head block")
return errors.New("no head block")
}
snapconfig := snapshot.Config{
triedb := utils.MakeDatabase(ctx, chaindb, true)
defer triedb.Close()

snapConfig := snapshot.Config{
CacheSize: 256,
Recovery: false,
NoBuild: true,
AsyncBuild: false,
}
snaptree, err := snapshot.New(snapconfig, chaindb, trie.NewDatabase(chaindb), headBlock.Root())
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, headBlock.Root())
if err != nil {
log.Error("Failed to open snapshot tree", "err", err)
return err
Expand Down Expand Up @@ -253,6 +256,9 @@ func traverseState(ctx *cli.Context) error {
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
triedb := utils.MakeDatabase(ctx, chaindb, true)
defer triedb.Close()

headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
Expand All @@ -277,7 +283,6 @@ func traverseState(ctx *cli.Context) error {
root = headBlock.Root()
log.Info("Start traversing the state", "root", root, "number", headBlock.NumberU64())
}
triedb := trie.NewDatabase(chaindb)
t, err := trie.NewStateTrie(trie.StateTrieID(root), triedb)
if err != nil {
log.Error("Failed to open trie", "root", root, "err", err)
Expand Down Expand Up @@ -353,6 +358,9 @@ func traverseRawState(ctx *cli.Context) error {
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
triedb := utils.MakeDatabase(ctx, chaindb, true)
defer triedb.Close()

headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
Expand All @@ -377,7 +385,6 @@ func traverseRawState(ctx *cli.Context) error {
root = headBlock.Root()
log.Info("Start traversing the state", "root", root, "number", headBlock.NumberU64())
}
triedb := trie.NewDatabase(chaindb)
t, err := trie.NewStateTrie(trie.StateTrieID(root), triedb)
if err != nil {
log.Error("Failed to open trie", "root", root, "err", err)
Expand All @@ -398,14 +405,19 @@ func traverseRawState(ctx *cli.Context) error {
log.Error("Failed to open iterator", "root", root, "err", err)
return err
}
reader, err := triedb.Reader(root)
if err != nil {
log.Error("state is not existent", "root", root)
return nil
}
for accIter.Next(true) {
nodes += 1
node := accIter.Hash()

// Check the present for non-empty hash node(embedded node doesn't
// have their own hash).
if node != (common.Hash{}) {
blob := rawdb.ReadLegacyTrieNode(chaindb, node)
blob, _ := reader.Node(common.Hash{}, accIter.Path(), node)
if len(blob) == 0 {
log.Error("Missing trie node(account)", "hash", node)
return errors.New("missing account")
Expand Down Expand Up @@ -446,7 +458,7 @@ func traverseRawState(ctx *cli.Context) error {
// Check the presence for non-empty hash node(embedded node doesn't
// have their own hash).
if node != (common.Hash{}) {
blob := rawdb.ReadLegacyTrieNode(chaindb, node)
blob, _ := reader.Node(common.BytesToHash(accIter.LeafKey()), storageIter.Path(), node)
if len(blob) == 0 {
log.Error("Missing trie node(storage)", "hash", node)
return errors.New("missing storage")
Expand Down Expand Up @@ -506,13 +518,16 @@ func dumpState(ctx *cli.Context) error {
if err != nil {
return err
}
triedb := utils.MakeDatabase(ctx, db, true)
defer triedb.Close()

snapConfig := snapshot.Config{
CacheSize: 256,
Recovery: false,
NoBuild: true,
AsyncBuild: false,
}
snaptree, err := snapshot.New(snapConfig, db, trie.NewDatabase(db), root)
snaptree, err := snapshot.New(snapConfig, db, triedb, root)
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
pcsclite "github.com/gballet/go-libpcsclite"
gopsutil "github.com/shirou/gopsutil/mem"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -268,6 +270,17 @@ var (
Usage: "Manually specify the Cancun fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
PathBasedSchemeFlag = &cli.BoolFlag{
Name: "trie.path-based",
Usage: "Enables experiment path-based state scheme (default = disabled)",
Category: flags.MiscCategory,
}
StateHistoryFlag = &cli.Uint64Flag{
Name: "trie.state-history",
Usage: "Number of recent blocks to maintain state history for (default = 90,000 blocks 0 = entire chain)",
Value: ethconfig.Defaults.StateHistory,
Category: flags.MiscCategory,
}
// Light server and client settings
LightServeFlag = &cli.IntFlag{
Name: "light.serve",
Expand Down Expand Up @@ -938,6 +951,11 @@ var (
RemoteDBFlag,
HttpHeaderFlag,
}
// StateSchemeFlags is the flag group of all trie node scheme flags
StateSchemeFlags = []cli.Flag{
StateHistoryFlag,
PathBasedSchemeFlag,
}
)

func init() {
Expand Down Expand Up @@ -1686,6 +1704,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.Preimages = true
log.Info("Enabling recording of key preimages since archive mode is used")
}
if ctx.IsSet(StateHistoryFlag.Name) {
cfg.StateHistory = ctx.Uint64(StateHistoryFlag.Name)
}
if ctx.IsSet(PathBasedSchemeFlag.Name) {
cfg.StateScheme = ParseStateScheme(ctx)
}
if ctx.IsSet(TxLookupLimitFlag.Name) {
cfg.TxLookupLimit = ctx.Uint64(TxLookupLimitFlag.Name)
}
Expand Down Expand Up @@ -2119,6 +2143,8 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
Preimages: ctx.Bool(CachePreimagesFlag.Name),
NodeScheme: ParseStateScheme(ctx),
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
}
if cache.TrieDirtyDisabled && !cache.Preimages {
cache.Preimages = true
Expand Down Expand Up @@ -2163,3 +2189,26 @@ func MakeConsolePreloads(ctx *cli.Context) []string {
}
return preloads
}

// ParseStateScheme resolves scheme identifier from CLI flag.
func ParseStateScheme(ctx *cli.Context) string {
if ctx.Bool(PathBasedSchemeFlag.Name) {
return rawdb.PathScheme
}
return rawdb.HashScheme
}

// MakeDatabase constructs a trie database based on the configured scheme.
func MakeDatabase(ctx *cli.Context, disk ethdb.Database, readOnly bool) *trie.Database {
scheme := ParseStateScheme(ctx)
if scheme == rawdb.HashScheme {
return trie.NewDatabase(disk, nil)
}
config := &trie.Config{}
if readOnly {
config.PathDB = pathdb.ReadOnly
} else {
config.PathDB = pathdb.Defaults
}
return trie.NewDatabase(disk, config)
}
7 changes: 6 additions & 1 deletion core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import (

// Tests that simple header verification works, for both good and bad blocks.
func TestHeaderVerification(t *testing.T) {
testHeaderVerification(t, rawdb.HashScheme)
testHeaderVerification(t, rawdb.PathScheme)
}

func testHeaderVerification(t *testing.T, scheme string) {
// Create a simple chain to verify
var (
gspec = &Genesis{Config: params.TestChainConfig}
Expand All @@ -46,7 +51,7 @@ func TestHeaderVerification(t *testing.T) {
headers[i] = block.Header()
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
defer chain.Stop()

for i := 0; i < len(blocks); i++ {
Expand Down
Loading

0 comments on commit 7ecaff2

Please sign in to comment.