Skip to content

Commit

Permalink
find block by commitments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jan 12, 2024
1 parent ece92d2 commit 3b5e1e7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/util/cmd/read-badger/cmd/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package find
import (
"encoding/hex"
"fmt"
"strings"

"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
Expand All @@ -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

Expand All @@ -34,34 +35,40 @@ 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)
if err != nil {
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
Expand All @@ -71,31 +78,56 @@ 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,
)

if err != nil {
log.Fatal().Err(err).Msgf("fail to find block id by commit")
}

}

0 comments on commit 3b5e1e7

Please sign in to comment.