-
Notifications
You must be signed in to change notification settings - Fork 180
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
[Util] Find block ID by state commitment #5240
Merged
+143
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
665ed3d
add util command to find block id by commitment and height range
zhangchiqing f0e7058
add checkpoint-trie-stats to util
zhangchiqing 1435e7e
find block by commitments
zhangchiqing bd5e08a
Apply suggestions from code review
zhangchiqing f9bf295
fix lint
zhangchiqing 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
138 changes: 138 additions & 0 deletions
138
cmd/util/cmd/read-badger/cmd/find-block-by-commits/main.go
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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package find | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
var cmd = &cobra.Command{ | ||
Use: "find-block-id-commit", | ||
Short: "find block ID by commit", | ||
Run: run, | ||
} | ||
|
||
var flagStartHeight uint64 | ||
var flagEndHeight uint64 | ||
var flagStateCommitments string | ||
|
||
var loader func() (*storage.All, *badger.DB) = nil | ||
|
||
func Init(f func() (*storage.All, *badger.DB)) *cobra.Command { | ||
loader = f | ||
|
||
cmd.Flags().Uint64Var(&flagStartHeight, "start-height", 0, "start height to block for commit") | ||
_ = cmd.MarkFlagRequired("start-height") | ||
|
||
cmd.Flags().Uint64Var(&flagEndHeight, "end-height", 0, "end height to block for commit") | ||
_ = cmd.MarkFlagRequired("end-height") | ||
|
||
cmd.Flags().StringVar(&flagStateCommitments, "state-commitments", "", | ||
"Comma separated list of state commitments (each must be 64 chars, hex-encoded)") | ||
_ = cmd.MarkFlagRequired("state-commitments") | ||
|
||
return cmd | ||
} | ||
|
||
func FindBlockIDByCommits( | ||
log zerolog.Logger, | ||
headers storage.Headers, | ||
commits storage.Commits, | ||
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) | ||
if err != nil { | ||
return flow.ZeroID, fmt.Errorf("could not find block by height %v: %w", height, err) | ||
} | ||
|
||
commit, err := commits.ByBlockID(blockID) | ||
if err != nil { | ||
return flow.ZeroID, fmt.Errorf("could not find commitment at height %v: %w", height, err) | ||
} | ||
|
||
_, ok := commitMap[commit] | ||
if ok { | ||
log.Info().Msgf("successfully found block %v at height %v for commit %v", | ||
blockID, height, commit) | ||
return blockID, nil | ||
} | ||
} | ||
|
||
return flow.ZeroID, fmt.Errorf("could not find commit within height range [%v,%v]", startHeight, endHeight) | ||
} | ||
|
||
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 { | ||
return flow.DummyStateCommitment, fmt.Errorf("invalid commit string %v, cannot decode", commit) | ||
} | ||
|
||
stateCommitment, err := flow.ToStateCommitment(stateCommitmentBytes) | ||
if err != nil { | ||
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 func() { | ||
err := db.Close() | ||
if err != nil { | ||
log.Warn().Err(err).Msg("error closing db") | ||
} | ||
}() | ||
|
||
_, err = FindBlockIDByCommits( | ||
log.Logger, | ||
storage.Headers, | ||
storage.Commits, | ||
stateCommitments, | ||
flagStartHeight, | ||
flagEndHeight, | ||
) | ||
|
||
if err != nil { | ||
log.Fatal().Err(err).Msgf("fail to find block id by commit") | ||
} | ||
} |
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
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.
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.
This adds the checkpoint trie stats to the util, which is useful to read the trie root hashes of a checkpoint file without reading the entire checkpoint file.