Skip to content
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 15 commits into from
May 10, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/bank) [#15764](https://github.com/cosmos/cosmos-sdk/pull/15764) Speedup x/bank InitGenesis
* (x/auth) [#15867](https://github.com/cosmos/cosmos-sdk/pull/15867) Support better logging for signature verification failure.
* (types/query) [#16041](https://github.com/cosmos/cosmos-sdk/pull/16041) change pagination max limit to a variable in order to be modifed by application devs
* (server) [#16061](https://github.com/cosmos/cosmos-sdk/pull/16061) add comet bootstrap command

### State Machine Breaking

Expand Down
12 changes: 12 additions & 0 deletions docs/docs/run-node/01-run-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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), but you must leave state sync set to false, this is crucial for when you start your node.
yihuang marked this conversation as resolved.
Show resolved Hide resolved
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 -->
Copy link
Member

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?

Copy link
Collaborator

@yihuang yihuang May 10, 2023

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.

79 changes: 79 additions & 0 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import (
"strconv"
"strings"

logger "cosmossdk.io/log"
cfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/light"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
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"
Expand All @@ -15,6 +22,7 @@ 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"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -249,3 +257,74 @@ $ %s query block --%s=%s <hash>

return cmd
}

func BootstrapStateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bootstrap-state",
Short: "Bootstrap cometbft state in an arbitrary block height using light client",
Long: `
Copy link
Member

Choose a reason for hiding this comment

The 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 in an arbitrary block height using light client
`,
tac0turtle 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)

return bootstrapStateCmd(cmd, serverCtx.Config)

},
}
return cmd
}

func bootstrapStateCmd(cmd *cobra.Command, cfg *cfg.Config) error {

tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
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
}

log := logger.NewLogger(cmd.OutOrStderr()).With("module", "light")

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: log})
if err != nil {
return fmt.Errorf("failed to set up light client state provider: %w", err)
}

state, err := stateProvider.State(cmd.Context(), uint64(cfg.StateSync.TrustHeight))
yihuang marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

commit, err := stateProvider.Commit(cmd.Context(), uint64(cfg.StateSync.TrustHeight))
if err != nil {
return err
}

if err := stateStore.Bootstrap(state); err != nil {
return err
}

return blockStore.SaveSeenCommit(state.LastBlockHeight, commit)
}
1 change: 1 addition & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
VersionCmd(),
cmtcmd.ResetAllCmd,
cmtcmd.ResetStateCmd,
BootstrapStateCmd(),
)

startCmd := StartCmd(appCreator, defaultNodeHome)
Expand Down