Skip to content

Commit

Permalink
all: activate pbss
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Jan 4, 2023
1 parent 1ee650b commit 3688352
Show file tree
Hide file tree
Showing 68 changed files with 1,812 additions and 862 deletions.
25 changes: 18 additions & 7 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 +94,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 +110,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 +159,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 +195,23 @@ func initGenesis(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}
triedb := trie.NewDatabaseWithConfig(chaindb, &trie.Config{
defer chaindb.Close()

scheme := utils.ParseStateScheme(ctx)
if name == "lightchaindata" {
scheme = rawdb.HashScheme
}
config := &trie.Config{
Preimages: ctx.Bool(utils.CachePreimagesFlag.Name),
})
Scheme: scheme,
}
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 @@ -466,6 +475,8 @@ func dump(ctx *cli.Context) error {
return err
}
config := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
Preimages: true, // always enable preimage lookup
}
state, err := state.New(root, state.NewDatabaseWithConfig(db, config), nil)
Expand Down
11 changes: 9 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,13 @@ func dbDumpTrie(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()

config := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
}
triedb := trie.NewDatabase(db, config)
defer triedb.Close()

var (
state []byte
storage []byte
Expand Down Expand Up @@ -515,7 +522,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 @@ -150,7 +150,7 @@ var (
utils.GpoIgnoreGasPriceFlag,
utils.MinerNotifyFullFlag,
configFileFlag,
}, utils.NetworkFlags, utils.DatabasePathFlags)
}, utils.NetworkFlags, utils.DatabasePathFlags, utils.StateSchemeFlags)

rpcFlags = []cli.Flag{
utils.HTTPEnabledFlag,
Expand Down
53 changes: 42 additions & 11 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 @@ -116,7 +116,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 @@ -131,7 +131,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 @@ -152,7 +152,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 @@ -215,13 +215,20 @@ func verifyState(ctx *cli.Context) error {
log.Error("Failed to load head block")
return errors.New("no head block")
}
snapconfig := snapshot.Config{
dbConfig := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
}
triedb := trie.NewDatabase(chaindb, dbConfig)
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 @@ -263,6 +270,13 @@ func traverseState(ctx *cli.Context) error {
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
config := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
}
triedb := trie.NewDatabase(chaindb, config)
defer triedb.Close()

headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
Expand All @@ -287,7 +301,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 +366,13 @@ func traverseRawState(ctx *cli.Context) error {
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
config := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
}
triedb := trie.NewDatabase(chaindb, config)
defer triedb.Close()

headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
Expand All @@ -377,7 +397,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 @@ -393,6 +412,11 @@ func traverseRawState(ctx *cli.Context) error {
hasher = crypto.NewKeccakState()
got = make([]byte, 32)
)
reader := triedb.GetReader(root)
if reader == nil {
log.Error("state is not existent", "root", root)
return nil
}
accIter := t.NodeIterator(nil)
for accIter.Next(true) {
nodes += 1
Expand All @@ -401,7 +425,7 @@ func traverseRawState(ctx *cli.Context) error {
// 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.NodeBlob(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 @@ -438,7 +462,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.NodeBlob(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 @@ -498,13 +522,20 @@ func dumpState(ctx *cli.Context) error {
if err != nil {
return err
}
dbConfig := &trie.Config{
ReadOnly: true,
Scheme: utils.ParseStateScheme(ctx),
}
triedb := trie.NewDatabase(db, dbConfig)
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
32 changes: 32 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ var (
Usage: "Manually specify TerminalTotalDifficultyPassed, 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 @@ -1019,6 +1030,11 @@ var (
RemoteDBFlag,
HttpHeaderFlag,
}
// StateSchemeFlags is the flag group of all trie node scheme flags
StateSchemeFlags = []cli.Flag{
StateHistoryFlag,
PathBasedSchemeFlag,
}
)

// MakeDataDir retrieves the currently requested data directory, terminating
Expand Down Expand Up @@ -1818,6 +1834,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 @@ -2269,6 +2291,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 @@ -2313,3 +2337,11 @@ 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
}
7 changes: 5 additions & 2 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import (
)

// Tests that simple header verification works, for both good and bad blocks.
func TestHeaderVerification(t *testing.T) {
func TestHeaderVerificationHashBased(t *testing.T) { testHeaderVerification(t, rawdb.HashScheme) }
func TestHeaderVerificationPathBased(t *testing.T) { 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 +49,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 3688352

Please sign in to comment.