From 050f03af10b9d0a2de440b1baf3d5d6fda85e1f3 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 11 May 2023 12:02:37 +0000 Subject: [PATCH] feat!: bootstrap comet cmd for local state sync (backport #16061) (#16079) Co-authored-by: Marko --- CHANGELOG.md | 2 +- docs/docs/run-node/01-run-node.md | 12 ++++ server/tm_cmds.go | 96 +++++++++++++++++++++++++++++++ server/util.go | 1 + 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f43850cf359..bb25dc009f28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client) [#16075](https://github.com/cosmos/cosmos-sdk/pull/16075) Partly revert [#15953](https://github.com/cosmos/cosmos-sdk/issues/15953) and `factory.Prepare` now does nothing in offline mode. * (server) [#15984](https://github.com/cosmos/cosmos-sdk/pull/15984) Use `cosmossdk.io/log` package for logging instead of CometBFT logger. NOTE: v0.45 and v0.46 were not using CometBFT logger either. This keeps the same underlying logger (zerolog) as in v0.45.x+ and v0.46.x+ but now properly supporting filtered logging. * (gov) [#15979](https://github.com/cosmos/cosmos-sdk/pull/15979) Improve gov error message when failing to convert v1 proposal to v1beta1. - +* (server) [#16061](https://github.com/cosmos/cosmos-sdk/pull/16061) add comet bootstrap command ### Bug Fixes * (x/group) [#16017](https://github.com/cosmos/cosmos-sdk/pull/16017) Correctly apply account number in group v2 migration. diff --git a/docs/docs/run-node/01-run-node.md b/docs/docs/run-node/01-run-node.md index 871fd0d2b1ed..8369920707be 100644 --- a/docs/docs/run-node/01-run-node.md +++ b/docs/docs/run-node/01-run-node.md @@ -149,3 +149,15 @@ In config.toml: ```toml log_level: "state:info,p2p:info,consensus:info,x/staking:info,x/ibc:info,*error" ``` + +## State Sync + +State sync is the act in which a node syncs the latest or close to the latest state of a blockchain. This is useful for users who don't want to sync all the blocks in history. You can read more here: https://docs.cometbft.com/v0.37/core/state-sync + +### Local State Sync + +Local state sync work similar to normal state sync except that it works off a local snapshot of state instead of one provided via the p2p network. The steps to start local state sync are similar to normal state sync with a few different designs. + +1. As mentioned in https://docs.cometbft.com/v0.37/core/state-sync, one must set a height and hash in the config.toml along with a few rpc servers (the afromentioned link has instructions on how to do this). +2. Bootsrapping Comet state in order to start the node after the snapshot has been ingested. This can be done with the bootstrap command ` comet bootstrap-state` + diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 97ed7e6c2bc9..63b4e718d0a1 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -5,14 +5,22 @@ package server import ( "fmt" + "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" tversion "github.com/cometbft/cometbft/version" "github.com/spf13/cobra" "sigs.k8s.io/yaml" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -117,3 +125,91 @@ func VersionCmd() *cobra.Command { }, } } + +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", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + serverCtx := GetServerContextFromCmd(cmd) + cfg := serverCtx.Config + + height, err := cmd.Flags().GetInt64("height") + 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(serverCtx.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) + + 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(), + }, serverCtx.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{ + // it breaks the invariant that blocks in range [Base, Height] must exists, but it do works in practice. + Base: state.LastBlockHeight, + 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 +} diff --git a/server/util.go b/server/util.go index c601c078bba7..6d15d284af0b 100644 --- a/server/util.go +++ b/server/util.go @@ -306,6 +306,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type VersionCmd(), tmcmd.ResetAllCmd, tmcmd.ResetStateCmd, + BootstrapStateCmd(appCreator), ) startCmd := StartCmd(appCreator, defaultNodeHome)