Skip to content

Commit

Permalink
feat!: bootstrap comet cmd for local state sync (backport #16061) (#1…
Browse files Browse the repository at this point in the history
…6080)

Co-authored-by: Marko <marbar3778@yahoo.com>
  • Loading branch information
mergify[bot] and tac0turtle authored May 10, 2023
1 parent 5e199ca commit 738220a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (store) [#15683](https://github.com/cosmos/cosmos-sdk/pull/15683) `rootmulti.Store.CacheMultiStoreWithVersion` now can handle loading archival states that don't persist any of the module stores the current state has.
* (simapp) [#15903](https://github.com/cosmos/cosmos-sdk/pull/15903) Add `AppStateFnWithExtendedCbs` with moduleStateCb callback function to allow access moduleState. Note, this function is present in simtestutil from `v0.47.2+`.
* (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

Expand Down
11 changes: 11 additions & 0 deletions docs/run-node/run-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ The previous command allow you to run a single node. This is enough for the next

The naive way would be to run the same commands again in separate terminal windows. This is possible, however in the Cosmos SDK, we leverage the power of [Docker Compose](https://docs.docker.com/compose/) to run a localnet. If you need inspiration on how to set up your own localnet with Docker Compose, you can have a look at the Cosmos SDK's [`docker-compose.yml`](https://github.com/cosmos/cosmos-sdk/blob/v0.46.0-rc1/docker-compose.yml).

## 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 `<app> comet bootstrap-state`
<!-- 3. TODO after https://github.com/cosmos/cosmos-sdk/pull/16060 is merged -->
## Next {hide}

Read about the [Interacting with your Node](./interact-node.md) {hide}
97 changes: 97 additions & 0 deletions server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ package server
import (
"fmt"

"github.com/tendermint/tendermint/light"
"github.com/tendermint/tendermint/node"
cmtstore "github.com/tendermint/tendermint/proto/tendermint/store"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/statesync"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/p2p"
pvm "github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/store"
tversion "github.com/tendermint/tendermint/version"
"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"
)

Expand Down Expand Up @@ -117,3 +126,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
}
1 change: 1 addition & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
VersionCmd(),
tmcmd.ResetAllCmd,
tmcmd.ResetStateCmd,
BootstrapStateCmd(appCreator),
)

startCmd := StartCmd(appCreator, defaultNodeHome)
Expand Down

0 comments on commit 738220a

Please sign in to comment.