-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/geth: add check func to validate state scheme #2067
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,6 @@ import ( | |
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/crypto/kzg4844" | ||
"github.com/ethereum/go-ethereum/eth" | ||
|
||
"github.com/ethereum/go-ethereum/eth/downloader" | ||
"github.com/ethereum/go-ethereum/eth/ethconfig" | ||
"github.com/ethereum/go-ethereum/eth/filters" | ||
|
@@ -1941,7 +1940,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { | |
} | ||
// Parse state scheme, abort the process if it's not compatible. | ||
chaindb := tryMakeReadOnlyDatabase(ctx, stack) | ||
scheme, err := ParseStateScheme(ctx, chaindb) | ||
scheme, err := ResolveStateScheme(ctx, cfg.StateScheme, chaindb) | ||
chaindb.Close() | ||
if err != nil { | ||
Fatalf("%v", err) | ||
|
@@ -2487,6 +2486,52 @@ func MakeConsolePreloads(ctx *cli.Context) []string { | |
return preloads | ||
} | ||
|
||
// ResolveStateScheme resolve state scheme from CLI flag, config file and persistent state. | ||
// The differences between ResolveStateScheme and ParseStateScheme are: | ||
// - ResolveStateScheme adds config to compare with CLI and persistent state to ensure correctness. | ||
// - ResolveStateScheme is only used in SetEthConfig function. | ||
// | ||
// 1. If config isn't provided, write hash mode to config by default, so in current function, config is nonempty. | ||
// 2. If persistent state and cli is empty, use config param. | ||
// 3. If persistent state is empty, provide CLI flag and config, choose CLI to return. | ||
// 4. If persistent state is nonempty and CLI isn't provided, persistent state should be equal to config. | ||
// 5. If all three items are provided: if any two of the three are not equal, return error. | ||
func ResolveStateScheme(ctx *cli.Context, stateSchemeCfg string, disk ethdb.Database) (string, error) { | ||
stored := rawdb.ReadStateScheme(disk) | ||
if stored == "" { | ||
// there is no persistent state data in disk db(e.g. geth init) | ||
if !ctx.IsSet(StateSchemeFlag.Name) { | ||
log.Info("State scheme set by config", "scheme", stateSchemeCfg) | ||
return stateSchemeCfg, nil | ||
} | ||
// if both CLI flag and config are set, choose CLI | ||
scheme := ctx.String(StateSchemeFlag.Name) | ||
if !ValidateStateScheme(scheme) { | ||
return "", fmt.Errorf("invalid state scheme param in CLI: %s", scheme) | ||
} | ||
log.Info("State scheme set by CLI", "scheme", scheme) | ||
return scheme, nil | ||
} | ||
if !ctx.IsSet(StateSchemeFlag.Name) { | ||
if stored != stateSchemeCfg { | ||
return "", fmt.Errorf("incompatible state scheme, stored: %s, config: %s", stored, stateSchemeCfg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not return error if stateSchemeCfg is empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
} | ||
log.Info("State scheme set to already existing", "scheme", stored) | ||
return stored, nil | ||
} | ||
scheme := ctx.String(StateSchemeFlag.Name) | ||
if !ValidateStateScheme(scheme) { | ||
return "", fmt.Errorf("invalid state scheme param in CLI: %s", scheme) | ||
} | ||
// if there is persistent state data in disk db, and CLI flag, config are set, | ||
// when they all are different, return error | ||
if scheme != stored || scheme != stateSchemeCfg || stored != stateSchemeCfg { | ||
return "", fmt.Errorf("incompatible state scheme, stored: %s, config: %s, CLI: %s", stored, stateSchemeCfg, scheme) | ||
} | ||
log.Info("All are provided, state scheme set to already existing", "scheme", stored) | ||
return stored, nil | ||
} | ||
|
||
// ParseStateScheme resolves scheme identifier from CLI flag. If the provided | ||
// state scheme is not compatible with the one of persistent scheme, an error | ||
// will be returned. | ||
|
@@ -2506,7 +2551,7 @@ func ParseStateScheme(ctx *cli.Context, disk ethdb.Database) (string, error) { | |
if stored == "" { | ||
// use default scheme for empty database, flip it when | ||
// path mode is chosen as default | ||
log.Info("State schema set to default", "scheme", "hash") | ||
log.Info("State scheme set to default", "scheme", "hash") | ||
return rawdb.HashScheme, nil | ||
} | ||
log.Info("State scheme set to already existing", "scheme", stored) | ||
|
@@ -2515,6 +2560,9 @@ func ParseStateScheme(ctx *cli.Context, disk ethdb.Database) (string, error) { | |
// If state scheme is specified, ensure it's compatible with | ||
// persistent state. | ||
scheme := ctx.String(StateSchemeFlag.Name) | ||
if !ValidateStateScheme(scheme) { | ||
return "", fmt.Errorf("invalid state scheme param in CLI: %s", scheme) | ||
} | ||
if stored == "" || scheme == stored { | ||
log.Info("State scheme set by user", "scheme", scheme) | ||
return scheme, nil | ||
|
@@ -2545,3 +2593,12 @@ func MakeTrieDatabase(ctx *cli.Context, disk ethdb.Database, preimage bool, read | |
} | ||
return trie.NewDatabase(disk, config) | ||
} | ||
|
||
// ValidateStateScheme used to check state scheme whether is valid. | ||
// Valid state scheme: hash and path. | ||
func ValidateStateScheme(stateScheme string) bool { | ||
if stateScheme == rawdb.HashScheme || stateScheme == rawdb.PathScheme { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need judge if the stateSchemeCfg is not setted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stateSchemeCfg
is impossible to be empty inResolveStateScheme
function, because there is a default value in caller functionloadBaseConfig
.