Skip to content

Commit

Permalink
add update-commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jun 4, 2024
1 parent c8036fe commit b9d3d05
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
rollback_executed_height "github.com/onflow/flow-go/cmd/util/cmd/rollback-executed-height/cmd"
"github.com/onflow/flow-go/cmd/util/cmd/snapshot"
truncate_database "github.com/onflow/flow-go/cmd/util/cmd/truncate-database"
update_commitment "github.com/onflow/flow-go/cmd/util/cmd/update-commitment"
"github.com/onflow/flow-go/cmd/util/cmd/version"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func addCommands() {
rootCmd.AddCommand(export_json_transactions.Cmd)
rootCmd.AddCommand(read_hotstuff.RootCmd)
rootCmd.AddCommand(find_inconsistent_result.Cmd)
rootCmd.AddCommand(update_commitment.Cmd)
}

func initConfig() {
Expand Down
127 changes: 127 additions & 0 deletions cmd/util/cmd/update-commitment/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package update_commitment

import (
"encoding/hex"
"errors"
"fmt"

"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/onflow/flow-go/cmd/util/cmd/common"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
storagebadger "github.com/onflow/flow-go/storage/badger"
)

var (
flagDatadir string
flagBlockID string
flagCommitment string
flagForceAdd bool
)

var Cmd = &cobra.Command{
Use: "update-commitment",
Short: "update commitment",
Run: run,
}

func init() {
Cmd.Flags().StringVarP(&flagDatadir, "datadir", "d", "/var/flow/data/protocol", "directory to the badger dababase")
_ = Cmd.MarkPersistentFlagRequired("datadir")

Cmd.Flags().StringVar(&flagBlockID, "block-id", "", "block id")
_ = Cmd.MarkPersistentFlagRequired("block-id")

Cmd.Flags().StringVar(&flagCommitment, "commitment", "", "commitment")
_ = Cmd.MarkPersistentFlagRequired("commitment")

Cmd.Flags().BoolVar(&flagForceAdd, "force", false, "force adding even if it doesn't exist")
}

func run(*cobra.Command, []string) {
err := updateCommitment(flagDatadir, flagBlockID, flagCommitment, flagForceAdd)
if err != nil {
fmt.Printf("fatal: %v\n", err)
}
}

func updateCommitment(datadir, blockIDStr, commitStr string, force bool) error {
log.Info().Msgf("updating commitment for block %s, commitment %s, force %t", blockIDStr, commitStr, force)
// validate blockID
blockID, err := flow.HexStringToIdentifier(blockIDStr)
if err != nil {
return fmt.Errorf("invalid block id: %v", err)
}

stateCommitmentBytes, err := hex.DecodeString(commitStr)
if err != nil {
return fmt.Errorf("cannot get decode the state commitment: %v", err)
}

commit, err := flow.ToStateCommitment(stateCommitmentBytes)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("invalid state commitment length: %v", err)
}

log.Warn().Msgf("commitment not found for block %s", blockIDStr)

if !force {
return fmt.Errorf("commitment not found and force flag not set")
}
}

commits, db, err := createStorages(datadir)
defer db.Close()
if err != nil {
return fmt.Errorf("could not create storages: %v", err)
}

commitToRemove, err := commits.ByBlockID(blockID)
if err != nil {
return fmt.Errorf("could not get commit by block id: %v", err)
}

if commitToRemove == commit {
return fmt.Errorf("commitment to be updated is identical to the current one")
}

log.Info().Msgf("found commitment to be removed: %x", commitToRemove)

writeBatch := storagebadger.NewBatch(db)
err = commits.BatchRemoveByBlockID(blockID, writeBatch)
if err != nil {
return fmt.Errorf("could not batch remove commit by block id: %v", err)
}

err = writeBatch.Flush()
if err != nil {
return fmt.Errorf("could not flush write batch: %v", err)
}

log.Info().Msgf("commitment removed for block %s", blockIDStr)

log.Info().Msgf("storing new commitment: %x", commit)

err = commits.Store(blockID, commit)
if err != nil {
return fmt.Errorf("could not store commit: %v", err)
}

log.Info().Msgf("commitment successfully stored for block %s", blockIDStr)

return nil
}

func createStorages(dir string) (storage.Commits, *badger.DB, error) {
db := common.InitStorage(dir)
if db == nil {
return nil, nil, fmt.Errorf("could not initialize db")
}

storages := common.InitStorages(db)
return storages.Commits, db, nil
}

0 comments on commit b9d3d05

Please sign in to comment.