Skip to content

Commit

Permalink
Add migrate-iavl flag for online migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbhat1 committed Sep 25, 2024
1 parent e58f02a commit 27a1676
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ type App struct {

genesisImportConfig genesistypes.GenesisImportConfig

stateStore seidb.StateStore
receiptStore seidb.StateStore
}

Expand Down Expand Up @@ -396,7 +397,7 @@ func New(
cdc := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry

bAppOptions := SetupSeiDB(logger, homePath, appOpts, baseAppOptions)
bAppOptions, stateStore := SetupSeiDB(logger, homePath, appOpts, baseAppOptions)

bApp := baseapp.NewBaseApp(AppName, logger, db, encodingConfig.TxConfig.TxDecoder(), tmConfig, appOpts, bAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
Expand Down Expand Up @@ -429,6 +430,7 @@ func New(
versionInfo: version.NewInfo(),
metricCounter: &map[string]float32{},
encodingConfig: encodingConfig,
stateStore: stateStore,
}

for _, option := range appOptions {
Expand Down Expand Up @@ -1064,6 +1066,9 @@ func (app *App) Name() string { return app.BaseApp.Name() }
// GetBaseApp returns the base app of the application
func (app App) GetBaseApp() *baseapp.BaseApp { return app.BaseApp }

// GetStateStore returns the state store of the application
func (app App) GetStateStore() seidb.StateStore { return app.stateStore }

// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
metrics.GaugeSeidVersionAndCommit(app.versionInfo.Version, app.versionInfo.GitCommit)
Expand Down
7 changes: 4 additions & 3 deletions app/seidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/storev2/rootmulti"
"github.com/sei-protocol/sei-db/config"
seidb "github.com/sei-protocol/sei-db/ss/types"
"github.com/spf13/cast"
"github.com/tendermint/tendermint/libs/log"
)
Expand Down Expand Up @@ -40,11 +41,11 @@ func SetupSeiDB(
homePath string,
appOpts servertypes.AppOptions,
baseAppOptions []func(*baseapp.BaseApp),
) []func(*baseapp.BaseApp) {
) ([]func(*baseapp.BaseApp), seidb.StateStore) {
scEnabled := cast.ToBool(appOpts.Get(FlagSCEnable))
if !scEnabled {
logger.Info("SeiDB is disabled, falling back to IAVL")
return baseAppOptions
return baseAppOptions, nil
}
logger.Info("SeiDB SC is enabled, running node with StoreV2 commit store")
scConfig := parseSCConfigs(appOpts)
Expand All @@ -63,7 +64,7 @@ func SetupSeiDB(
},
}, baseAppOptions...)

return baseAppOptions
return baseAppOptions, cms.GetStateStore()

Check failure on line 67 in app/seidb.go

View workflow job for this annotation

GitHub Actions / forward-compatibility

cms.GetStateStore undefined (type *"github.com/cosmos/cosmos-sdk/storev2/rootmulti".Store has no field or method GetStateStore)
}

func parseSCConfigs(appOpts servertypes.AppOptions) config.StateCommitConfig {
Expand Down
20 changes: 19 additions & 1 deletion cmd/seid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/utils/tracing"
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/sei-protocol/sei-chain/app/params"
"github.com/sei-protocol/sei-chain/evmrpc"
"github.com/sei-protocol/sei-chain/tools"
"github.com/sei-protocol/sei-chain/tools/migration/ss"
"github.com/sei-protocol/sei-chain/x/evm/blocktest"
"github.com/sei-protocol/sei-chain/x/evm/querier"
"github.com/sei-protocol/sei-chain/x/evm/replay"
Expand Down Expand Up @@ -220,6 +222,7 @@ func txCommand() *cobra.Command {

func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
startCmd.Flags().Bool("migrate-iavl", false, "Run migration of IAVL data store to SeiDB State Store")
}

// newApp creates a new Cosmos SDK app
Expand Down Expand Up @@ -266,7 +269,7 @@ func newApp(
// This makes it such that the wasm VM gas converts to sdk gas at a 6.66x rate vs that of the previous multiplier
wasmGasRegisterConfig.GasMultiplier = 21_000_000

return app.New(
app := app.New(
logger,
db,
traceStore,
Expand Down Expand Up @@ -302,6 +305,21 @@ func newApp(
baseapp.SetSnapshotDirectory(cast.ToString(appOpts.Get(server.FlagStateSyncSnapshotDir))),
baseapp.SetOccEnabled(cast.ToBool(appOpts.Get(baseapp.FlagOccEnabled))),
)

// Start migration if --migrate flag is set
if cast.ToBool(appOpts.Get("migrate-iavl")) {
go func() {
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
stateStore := app.GetStateStore()
latestVersion := rootmulti.GetLatestVersion(db)
migrator := ss.NewMigrator(homeDir, db, stateStore)
if err := migrator.Migrate(latestVersion, homeDir); err != nil {
panic(err)
}
}()

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
}

return app
}

// appExport creates a new simapp (optionally at a given height)
Expand Down
31 changes: 25 additions & 6 deletions tools/migration/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/sei-protocol/sei-chain/tools/migration/sc"
"github.com/sei-protocol/sei-chain/tools/migration/ss"
"github.com/sei-protocol/sei-db/config"
sstypes "github.com/sei-protocol/sei-db/ss"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
)

Expand Down Expand Up @@ -51,13 +54,16 @@ func migrateSC(version int64, homeDir string, db dbm.DB) error {
}

func migrateSS(version int64, homeDir string, db dbm.DB) error {
migrator := ss.NewMigrator(homeDir, db)
return migrator.Migrate(version, homeDir)
}
ssConfig := config.DefaultStateStoreConfig()
ssConfig.Enable = true

func verifySS(version int64, homeDir string, db dbm.DB) error {
migrator := ss.NewMigrator(homeDir, db)
return migrator.Verify(version)
stateStore, err := sstypes.NewStateStore(log.NewNopLogger(), homeDir, ssConfig)
if err != nil {
return err
}

migrator := ss.NewMigrator(homeDir, db, stateStore)
return migrator.Migrate(version, homeDir)
}

func VerifyMigrationCmd() *cobra.Command {
Expand Down Expand Up @@ -97,3 +103,16 @@ func verify(cmd *cobra.Command, _ []string) {

fmt.Println("Verification Succeeded")
}

func verifySS(version int64, homeDir string, db dbm.DB) error {
ssConfig := config.DefaultStateStoreConfig()
ssConfig.Enable = true

stateStore, err := sstypes.NewStateStore(log.NewNopLogger(), homeDir, ssConfig)
if err != nil {
return err
}

migrator := ss.NewMigrator(homeDir, db, stateStore)
return migrator.Verify(version)
}
14 changes: 1 addition & 13 deletions tools/migration/ss/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"time"

"github.com/cosmos/iavl"
"github.com/sei-protocol/sei-db/config"
"github.com/sei-protocol/sei-db/ss"
"github.com/sei-protocol/sei-db/ss/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
)

Expand All @@ -27,16 +24,7 @@ var modules = []string{
"wasm", "aclaccesscontrol", "oracle", "epoch", "mint", "acc", "bank", "feegrant", "staking", "distribution", "slashing", "gov", "params", "ibc", "upgrade", "evidence", "transfer", "tokenfactory",
}

func NewMigrator(homeDir string, db dbm.DB) *Migrator {
// TODO: Pass in more configs outside default, in particular ImportNumWorkers
ssConfig := config.DefaultStateStoreConfig()
ssConfig.Enable = true

stateStore, err := ss.NewStateStore(log.NewNopLogger(), homeDir, ssConfig)
if err != nil {
panic(err)
}

func NewMigrator(homeDir string, db dbm.DB, stateStore types.StateStore) *Migrator {
return &Migrator{
iavlDB: db,
stateStore: stateStore,
Expand Down

0 comments on commit 27a1676

Please sign in to comment.