-
Notifications
You must be signed in to change notification settings - Fork 224
feat(cmd): DB verification with cmd tool #3335
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
Open
MaksymMalicki
wants to merge
14
commits into
main
Choose a base branch
from
maksym/doctor-cmd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7720a17
first commit
MaksymMalicki f3e65cc
add trie verifier
MaksymMalicki aca1c92
add unit tests
MaksymMalicki 0cbd88a
trie verification reworked
MaksymMalicki 4b2f796
lint
MaksymMalicki 2ba401b
restructure
MaksymMalicki 0978c28
split pr into two
MaksymMalicki d49e376
feat(cmd): trie verifier in the cmd (#3338)
MaksymMalicki cd9f31e
linter
MaksymMalicki 561527e
refactor the trie verify cmd
MaksymMalicki 73cb507
linter
MaksymMalicki f1ad37f
Cleanup the logs
MaksymMalicki df3a1eb
comments, refactor
MaksymMalicki 1bb79e5
linter
MaksymMalicki 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package verify | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "slices" | ||
|
|
||
| "github.com/NethermindEth/juno/core/felt" | ||
| "github.com/NethermindEth/juno/utils" | ||
| verifytrie "github.com/NethermindEth/juno/verify/trie" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| verifyTrieType = "type" | ||
| verifyContractAddr = "address" | ||
| ) | ||
|
|
||
| func verifyTrieCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "trie", | ||
| Short: "Verify trie integrity", | ||
| Long: `Verify trie integrity by rebuilding tries and comparing root hashes.`, | ||
| RunE: runTrieVerify, | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| } | ||
|
|
||
| cmd.Flags().StringSlice( | ||
| verifyTrieType, | ||
| nil, | ||
| "Trie types to verify (contract, class, contract-storage)."+ | ||
| "If not specified, all trie types are verified.", | ||
| ) | ||
|
|
||
| cmd.Flags().String( | ||
| verifyContractAddr, | ||
| "", | ||
| "Contract address to verify (only used with --type contract-storage). "+ | ||
| "If not specified, all contract storage tries are verified.", | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runTrieVerify(cmd *cobra.Command, args []string) error { | ||
| dbPath, err := cmd.Flags().GetString(verifyDBPathF) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| database, err := openDB(dbPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer database.Close() | ||
|
|
||
| trieTypes, err := cmd.Flags().GetStringSlice(verifyTrieType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| contractAddrStr, err := cmd.Flags().GetString(verifyContractAddr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var tries []verifytrie.TrieType | ||
| if len(trieTypes) > 0 { | ||
| tries = make([]verifytrie.TrieType, 0, len(trieTypes)) | ||
| for _, t := range trieTypes { | ||
| tt := verifytrie.TrieType(t) | ||
| if !tt.IsValid() { | ||
| return fmt.Errorf("invalid trie type %q (allowed: contract, class, contract-storage)", t) | ||
| } | ||
| tries = append(tries, tt) | ||
| } | ||
| } | ||
|
|
||
| var contractAddr *felt.Felt | ||
| if contractAddrStr != "" { | ||
| hasContractStorage := slices.Contains(tries, verifytrie.ContractStorageTrie) | ||
| if len(tries) == 0 { | ||
| hasContractStorage = true | ||
| } | ||
|
|
||
| if !hasContractStorage { | ||
| return fmt.Errorf("--address flag can only be used with --type contract-storage") | ||
| } | ||
|
|
||
| var addr felt.Felt | ||
| _, err = addr.SetString(contractAddrStr) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid contract address %s: %w", contractAddrStr, err) | ||
| } | ||
| contractAddr = &addr | ||
| } | ||
|
|
||
| logLevel := utils.NewLogLevel(utils.INFO) | ||
| logger, err := utils.NewZapLogger(logLevel, true) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create logger: %w", err) | ||
| } | ||
|
|
||
| verifier := verifytrie.NewTrieVerifier(database, logger, tries, contractAddr) | ||
| ctx := cmd.Context() | ||
| return verifier.Run(ctx) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package verify | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/NethermindEth/juno/db/pebblev2" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRunTrieVerify_AddressFlagValidation(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| trieTypes []string | ||
| address string | ||
| expectError bool | ||
| expectedErrMsg string | ||
| }{ | ||
| { | ||
| name: "address with contract-storage type should succeed", | ||
| trieTypes: []string{"contract-storage"}, | ||
| address: "0x123", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "address with contract and class types should fail", | ||
| trieTypes: []string{"contract", "class"}, | ||
| address: "0x123", | ||
| expectError: true, | ||
| expectedErrMsg: "--address flag can only be used with --type contract-storage", | ||
| }, | ||
| { | ||
| name: "address with no type specified should succeed (default includes contract-storage)", | ||
| trieTypes: []string{}, | ||
| address: "0x123", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "invalid type should fail", | ||
| trieTypes: []string{"invalid-type"}, | ||
| address: "", | ||
| expectError: true, | ||
| expectedErrMsg: "invalid trie type", | ||
| }, | ||
| { | ||
| name: "invalid address format should fail", | ||
| trieTypes: []string{"contract-storage"}, | ||
| address: "not-a-hex", | ||
| expectError: true, | ||
| expectedErrMsg: "invalid contract address", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| dbPath := filepath.Join(tempDir, "test.db") | ||
|
|
||
| testDB, err := pebblev2.New(dbPath) | ||
| require.NoError(t, err) | ||
| testDB.Close() | ||
|
|
||
| parentCmd := VerifyCmd("") | ||
| args := []string{"--db-path", dbPath, "trie"} | ||
|
|
||
| for _, trieType := range tt.trieTypes { | ||
| args = append(args, "--type", trieType) | ||
| } | ||
|
|
||
| if tt.address != "" { | ||
| args = append(args, "--address", tt.address) | ||
| } | ||
|
|
||
| parentCmd.SetArgs(args) | ||
| parentCmd.SetOut(os.Stderr) | ||
| parentCmd.SetErr(os.Stderr) | ||
|
|
||
| err = parentCmd.ExecuteContext(context.Background()) | ||
|
|
||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| if tt.expectedErrMsg != "" { | ||
| assert.Contains(t, err.Error(), tt.expectedErrMsg) | ||
| } | ||
| } else if err != nil { | ||
| // For "success" cases, we're testing flag validation, not full execution. | ||
| // The command may fail downstream (empty DB, no data) - that's expected. | ||
| // We only verify that the specific flag validation error we're testing didn't occur. | ||
| addrFlagErr := "--address flag can only be used with --type contract-storage" | ||
| assert.NotContains(t, err.Error(), addrFlagErr, | ||
| "flag validation should pass; downstream errors are acceptable") | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package verify | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/NethermindEth/juno/db" | ||
| "github.com/NethermindEth/juno/db/pebblev2" | ||
| "github.com/NethermindEth/juno/utils" | ||
| "github.com/NethermindEth/juno/verify/trie" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type Verifier interface { | ||
| Name() string | ||
| Run(ctx context.Context) error | ||
| } | ||
|
|
||
| const verifyDBPathF = "db-path" | ||
|
|
||
| func VerifyCmd(defaultDBPath string) *cobra.Command { | ||
| verifyCmd := &cobra.Command{ | ||
| Use: "verify", | ||
| Short: "Verify database integrity", | ||
| Long: `Verify database integrity using various verification methods.`, | ||
| } | ||
|
|
||
| verifyCmd.PersistentFlags().String(verifyDBPathF, defaultDBPath, "Path to the database") | ||
| verifyCmd.AddCommand(verifyTrieCmd()) | ||
| verifyCmd.RunE = verifyAll | ||
|
|
||
| return verifyCmd | ||
| } | ||
|
|
||
| func verifyAll(cmd *cobra.Command, args []string) error { | ||
| dbPath, err := cmd.Flags().GetString(verifyDBPathF) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| database, err := openDB(dbPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer database.Close() | ||
|
|
||
| logLevel := utils.NewLogLevel(utils.INFO) | ||
| logger, err := utils.NewZapLogger(logLevel, true) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create logger: %w", err) | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
|
|
||
| verifiers := []Verifier{ | ||
| trie.NewTrieVerifier(database, logger, nil, nil), | ||
| } | ||
|
|
||
| for _, v := range verifiers { | ||
| if err := v.Run(ctx); err != nil { | ||
| return fmt.Errorf("%s verification stopped: %w", v.Name(), err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func openDB(path string) (db.KeyValueStore, error) { | ||
| _, err := os.Stat(path) | ||
| if os.IsNotExist(err) { | ||
| return nil, errors.New("database path does not exist") | ||
| } | ||
|
|
||
| database, err := pebblev2.New(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open db: %w", err) | ||
| } | ||
|
|
||
| return database, nil | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package trie | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| ) | ||
|
|
||
| func TraverseBinary[T any]( | ||
| ctx context.Context, | ||
| depth uint8, | ||
| maxConcurrentDepth uint8, | ||
| leftFn func(ctx context.Context) (T, error), | ||
| rightFn func(ctx context.Context) (T, error), | ||
| ) (left, right T, err error) { | ||
| if depth <= maxConcurrentDepth { | ||
| return traverseConcurrently(ctx, leftFn, rightFn) | ||
| } | ||
| return traverseSequentially(ctx, leftFn, rightFn) | ||
| } | ||
|
|
||
| func traverseConcurrently[T any]( | ||
| ctx context.Context, | ||
| leftFn func(ctx context.Context) (T, error), | ||
| rightFn func(ctx context.Context) (T, error), | ||
| ) (left, right T, err error) { | ||
| var leftErr, rightErr error | ||
| var wg sync.WaitGroup | ||
|
|
||
| wg.Go(func() { | ||
| left, leftErr = leftFn(ctx) | ||
| }) | ||
|
|
||
| right, rightErr = rightFn(ctx) | ||
| wg.Wait() | ||
|
|
||
| if leftErr != nil { | ||
| return left, right, leftErr | ||
| } | ||
| if rightErr != nil { | ||
| return left, right, rightErr | ||
| } | ||
| return left, right, nil | ||
| } | ||
|
|
||
| func traverseSequentially[T any]( | ||
| ctx context.Context, | ||
| leftFn func(ctx context.Context) (T, error), | ||
| rightFn func(ctx context.Context) (T, error), | ||
| ) (left, right T, err error) { | ||
| left, err = leftFn(ctx) | ||
| if err != nil { | ||
| return left, right, err | ||
| } | ||
| right, err = rightFn(ctx) | ||
| return left, right, err | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.