diff --git a/cmd/util/cmd/read-badger/cmd/commits.go b/cmd/util/cmd/read-badger/cmd/commits.go index 022038c9734..873bf4746a5 100644 --- a/cmd/util/cmd/read-badger/cmd/commits.go +++ b/cmd/util/cmd/read-badger/cmd/commits.go @@ -4,7 +4,7 @@ import ( "github.com/rs/zerolog/log" "github.com/spf13/cobra" - findBlockByCommit "github.com/onflow/flow-go/cmd/util/cmd/read-badger/cmd/find-block-by-commit" + findBlockByCommits "github.com/onflow/flow-go/cmd/util/cmd/read-badger/cmd/find-block-by-commits" "github.com/onflow/flow-go/model/flow" ) @@ -14,7 +14,7 @@ func init() { commitsCmd.Flags().StringVarP(&flagBlockID, "block-id", "b", "", "the block id of which to query the state commitment") _ = commitsCmd.MarkFlagRequired("block-id") - rootCmd.AddCommand(findBlockByCommit.Init(InitStorages)) + rootCmd.AddCommand(findBlockByCommits.Init(InitStorages)) } var commitsCmd = &cobra.Command{ diff --git a/cmd/util/cmd/read-badger/cmd/find-block-by-commit/main.go b/cmd/util/cmd/read-badger/cmd/find-block-by-commits/main.go similarity index 55% rename from cmd/util/cmd/read-badger/cmd/find-block-by-commit/main.go rename to cmd/util/cmd/read-badger/cmd/find-block-by-commits/main.go index fda5469d667..ab36f72374b 100644 --- a/cmd/util/cmd/read-badger/cmd/find-block-by-commit/main.go +++ b/cmd/util/cmd/read-badger/cmd/find-block-by-commits/main.go @@ -3,6 +3,7 @@ package find import ( "encoding/hex" "fmt" + "strings" "github.com/dgraph-io/badger/v2" "github.com/rs/zerolog" @@ -21,7 +22,7 @@ var cmd = &cobra.Command{ var flagStartHeight uint64 var flagEndHeight uint64 -var flagStateCommitment string +var flagStateCommitments string var loader func() (*storage.All, *badger.DB) = nil @@ -34,21 +35,26 @@ func Init(f func() (*storage.All, *badger.DB)) *cobra.Command { cmd.Flags().Uint64Var(&flagEndHeight, "end-height", 0, "end height to block for commit") _ = cmd.MarkFlagRequired("end-height") - cmd.Flags().StringVar(&flagStateCommitment, "state-commitment", "", - "State commitment (64 chars, hex-encoded)") - _ = cmd.MarkFlagRequired("state-commitment") + cmd.Flags().StringVar(&flagStateCommitments, "state-commitments", "", + "State commitments (each must be 64 chars, hex-encoded)") + _ = cmd.MarkFlagRequired("state-commitments") return cmd } -func FindBlockIDByCommit( +func FindBlockIDByCommits( log zerolog.Logger, headers storage.Headers, commits storage.Commits, - commit flow.StateCommitment, + stateCommitments []flow.StateCommitment, startHeight uint64, endHeight uint64, ) (flow.Identifier, error) { + commitMap := make(map[flow.StateCommitment]struct{}, len(stateCommitments)) + for _, commit := range stateCommitments { + commitMap[commit] = struct{}{} + } + for height := startHeight; height <= endHeight; height++ { log.Info().Msgf("finding for height %v for height range: [%v, %v]", height, startHeight, endHeight) blockID, err := headers.BlockIDByHeight(height) @@ -56,12 +62,13 @@ func FindBlockIDByCommit( return flow.ZeroID, fmt.Errorf("could not find block by height %v: %w", height, err) } - executedCommit, err := commits.ByBlockID(blockID) + commit, err := commits.ByBlockID(blockID) if err != nil { return flow.ZeroID, fmt.Errorf("could not find commitment at height %v: %w", height, err) } - if commit == executedCommit { + _, ok := commitMap[commit] + if ok { log.Info().Msgf("successfully found block %v at height %v for commit %v", blockID, height, commit) return blockID, nil @@ -71,25 +78,51 @@ func FindBlockIDByCommit( return flow.ZeroID, fmt.Errorf("could not find commit within height range [%v,%v]", startHeight, endHeight) } -func run(*cobra.Command, []string) { - stateCommitmentBytes, err := hex.DecodeString(flagStateCommitment) +func toStateCommitments(commitsStr string) ([]flow.StateCommitment, error) { + commitSlice := strings.Split(commitsStr, ",") + commits := make([]flow.StateCommitment, len(commitSlice)) + for _, c := range commitSlice { + commit, err := toStateCommitment(c) + if err != nil { + return nil, err + } + + commits = append(commits, commit) + } + return commits, nil + +} + +func toStateCommitment(commit string) (flow.StateCommitment, error) { + stateCommitmentBytes, err := hex.DecodeString(commit) if err != nil { - log.Fatal().Err(err).Msg("invalid flag, cannot decode") + return flow.DummyStateCommitment, fmt.Errorf("invalid commit string %v, cannot decode", commit) } stateCommitment, err := flow.ToStateCommitment(stateCommitmentBytes) if err != nil { - log.Fatal().Err(err).Msgf("invalid number of bytes, got %d expected %d", len(stateCommitmentBytes), len(stateCommitment)) + return flow.DummyStateCommitment, fmt.Errorf("invalid number of bytes, got %d expected %d, %v", len(stateCommitmentBytes), len(stateCommitment), commit) + } + return stateCommitment, nil +} + +func run(*cobra.Command, []string) { + log.Info().Msgf("looking up block in height range [%v, %v] for commits %v", + flagStartHeight, flagEndHeight, flagStateCommitments) + + stateCommitments, err := toStateCommitments(flagStateCommitments) + if err != nil { + log.Fatal().Err(err).Msgf("fail to convert commitment") } storage, db := loader() defer db.Close() - _, err = FindBlockIDByCommit( + _, err = FindBlockIDByCommits( log.Logger, storage.Headers, storage.Commits, - stateCommitment, + stateCommitments, flagStartHeight, flagEndHeight, ) @@ -97,5 +130,4 @@ func run(*cobra.Command, []string) { if err != nil { log.Fatal().Err(err).Msgf("fail to find block id by commit") } - }