-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat!: bootstrap comet cmd for local state sync #16061
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
74d49c2
bootstrap comet for local state sync
tac0turtle 95c1959
change to server.ctx
tac0turtle ff3d06b
add shrot docs
tac0turtle 4dfdb81
Merge branch 'main' into marko/bootstrap_cmt
tac0turtle 6734f27
ammend comments
tac0turtle 9f2e35b
Update server/cmt_cmds.go
tac0turtle 825c84e
Update server/cmt_cmds.go
tac0turtle 261bf90
bootstrap block store state
yihuang 4f058f5
bootstrap to state height by default
yihuang 143b1a5
Merge branch 'main' into marko/bootstrap_cmt
yihuang cf45554
minor cleanup
tac0turtle fe98575
Apply suggestions from code review
yihuang d4b6d71
Update server/cmt_cmds.go
yihuang 5b4b4c6
Update server/cmt_cmds.go
yihuang a4e2be9
fix linting
tac0turtle 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
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
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 |
---|---|---|
|
@@ -5,8 +5,15 @@ import ( | |
"strconv" | ||
"strings" | ||
|
||
"cosmossdk.io/log" | ||
"github.com/cometbft/cometbft/light" | ||
"github.com/cometbft/cometbft/node" | ||
"github.com/cometbft/cometbft/p2p" | ||
pvm "github.com/cometbft/cometbft/privval" | ||
cmtstore "github.com/cometbft/cometbft/proto/tendermint/store" | ||
sm "github.com/cometbft/cometbft/state" | ||
"github.com/cometbft/cometbft/statesync" | ||
"github.com/cometbft/cometbft/store" | ||
cmtversion "github.com/cometbft/cometbft/version" | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/yaml" | ||
|
@@ -15,6 +22,8 @@ import ( | |
"github.com/cosmos/cosmos-sdk/client/flags" | ||
rpc "github.com/cosmos/cosmos-sdk/client/rpc" | ||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" | ||
servercmtlog "github.com/cosmos/cosmos-sdk/server/log" | ||
"github.com/cosmos/cosmos-sdk/server/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
"github.com/cosmos/cosmos-sdk/version" | ||
|
@@ -249,3 +258,96 @@ $ %s query block --%s=%s <hash> | |
|
||
return cmd | ||
} | ||
|
||
func BootstrapStateCmd(appCreator types.AppCreator) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "bootstrap-state", | ||
Short: "Bootstrap CometBFT state at an arbitrary block height using a light client", | ||
Long: ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is going to say the same thing, why add a long description? I think we should remove it. |
||
Bootstrap CometBFT state at an arbitrary block height using a light client | ||
`, | ||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
serverCtx := GetServerContextFromCmd(cmd) | ||
logger := log.NewLogger(cmd.OutOrStdout()) | ||
cfg := serverCtx.Config | ||
|
||
height, err := cmd.Flags().GetInt64("height") | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return err | ||
} | ||
if height == 0 { | ||
home := serverCtx.Viper.GetString(flags.FlagHome) | ||
db, err := openDB(home, GetAppDBBackend(serverCtx.Viper)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
app := appCreator(logger, db, nil, serverCtx.Viper) | ||
height = app.CommitMultiStore().LastCommitID().Version | ||
} | ||
|
||
blockStoreDB, err := node.DefaultDBProvider(&node.DBContext{ID: "blockstore", Config: cfg}) | ||
if err != nil { | ||
return err | ||
} | ||
blockStore := store.NewBlockStore(blockStoreDB) | ||
|
||
tac0turtle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stateDB, err := node.DefaultDBProvider(&node.DBContext{ID: "state", Config: cfg}) | ||
if err != nil { | ||
return err | ||
} | ||
stateStore := sm.NewStore(stateDB, sm.StoreOptions{ | ||
DiscardABCIResponses: cfg.Storage.DiscardABCIResponses, | ||
}) | ||
|
||
genState, _, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, node.DefaultGenesisDocProviderFunc(cfg)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stateProvider, err := statesync.NewLightClientStateProvider( | ||
cmd.Context(), | ||
genState.ChainID, genState.Version, genState.InitialHeight, | ||
cfg.StateSync.RPCServers, light.TrustOptions{ | ||
Period: cfg.StateSync.TrustPeriod, | ||
Height: cfg.StateSync.TrustHeight, | ||
Hash: cfg.StateSync.TrustHashBytes(), | ||
}, servercmtlog.CometLoggerWrapper{Logger: logger.With("module", "light")}) | ||
if err != nil { | ||
return fmt.Errorf("failed to set up light client state provider: %w", err) | ||
} | ||
|
||
state, err := stateProvider.State(cmd.Context(), uint64(height)) | ||
if err != nil { | ||
return fmt.Errorf("failed to get state: %w", err) | ||
} | ||
|
||
commit, err := stateProvider.Commit(cmd.Context(), uint64(height)) | ||
if err != nil { | ||
return fmt.Errorf("failed to get commit: %w", err) | ||
} | ||
|
||
if err := stateStore.Bootstrap(state); err != nil { | ||
return fmt.Errorf("failed to bootstrap state: %w", err) | ||
} | ||
|
||
if err := blockStore.SaveSeenCommit(state.LastBlockHeight, commit); err != nil { | ||
return fmt.Errorf("failed to save seen commit: %w", err) | ||
} | ||
|
||
store.SaveBlockStoreState(&cmtstore.BlockStoreState{ | ||
// maintain the invariant that blocks in range [Base, Height] exists. | ||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Base: state.LastBlockHeight + 1, | ||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Height: state.LastBlockHeight, | ||
}, blockStoreDB) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().Int64("height", 0, "Block height to bootstrap state at, if not provided will use the latest block height in app state") | ||
|
||
return cmd | ||
} |
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
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.
Is this blocked on #16060 then?
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.
the cmd behavior itself is standalone and well-defined, but for end user, it only make sense to work together with local snapshot import feature, I think can be merged separately.