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

Fix missing module for migration and add command to export IAVL #1955

Merged
merged 11 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/seid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func newApp(
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
stateStore := app.GetStateStore()
migrationHeight := cast.ToInt64(appOpts.Get("migrate-height"))
migrator := ss.NewMigrator(homeDir, db, stateStore)
migrator := ss.NewMigrator(db, stateStore)
if err := migrator.Migrate(migrationHeight, homeDir); err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/cmd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tools

import (
"github.com/spf13/cobra"

migration "github.com/sei-protocol/sei-chain/tools/migration/cmd"
scanner "github.com/sei-protocol/sei-chain/tools/tx-scanner/cmd"
"github.com/spf13/cobra"
)

func ToolCmd() *cobra.Command {
Expand All @@ -14,5 +15,6 @@ func ToolCmd() *cobra.Command {
toolsCmd.AddCommand(scanner.ScanCmd())
toolsCmd.AddCommand(migration.MigrateCmd())
toolsCmd.AddCommand(migration.VerifyMigrationCmd())
toolsCmd.AddCommand(migration.GenerateStats())
return toolsCmd
}
76 changes: 75 additions & 1 deletion tools/migration/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package cmd

import (
"bytes"
"fmt"
"path/filepath"
"time"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/iavl"
"github.com/sei-protocol/sei-chain/tools/migration/sc"
"github.com/sei-protocol/sei-chain/tools/migration/ss"
"github.com/sei-protocol/sei-chain/tools/migration/utils"
"github.com/sei-protocol/sei-db/config"
sstypes "github.com/sei-protocol/sei-db/ss"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -91,6 +95,76 @@
return err
}

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

func GenerateStats() *cobra.Command {
cmd := &cobra.Command{
Use: "iavl-stats",
Short: "A tool to generate archive node iavl stats like number of keys and size per module.",
Run: generateIavlStats,
}
cmd.PersistentFlags().String("home-dir", "/root/.sei", "Sei home directory")
return cmd
}

func generateIavlStats(cmd *cobra.Command, _ []string) {
homeDir, _ := cmd.Flags().GetString("home-dir")
dataDir := filepath.Join(homeDir, "data")
db, err := dbm.NewGoLevelDB("application", dataDir)
if err != nil {
panic(err)
}

fmt.Println("Aggregating iavl module stats...")
for _, module := range utils.Modules {

startTimeModule := time.Now()

Check warning

Code scanning / CodeQL

Calling the system time Warning

Calling the system time may be a possible source of non-determinism
fmt.Printf("Aggregating stats for module %s...\n", module)

// Prepare the prefixed DB for the module
prefixDB := dbm.NewPrefixDB(db, []byte(utils.BuildRawPrefix(module)))

// Get an iterator for the prefixed DB
itr, err := prefixDB.Iterator(nil, nil)
if err != nil {
panic(fmt.Errorf("failed to create iterator: %w", err))
}
defer itr.Close()

// Aggregated stats for the current module
var totalKeys int
var totalKeySize int
var totalValueSize int

for ; itr.Valid(); itr.Next() {
value := bytes.Clone(itr.Value())
node, err := iavl.MakeNode(value)
if err != nil {
panic(fmt.Errorf("failed to make node: %w", err))
}

// Only export leaf nodes
if node.GetHeight() == 0 {
totalKeys++
totalKeySize += len(itr.Key())
totalValueSize += len(itr.Value())
if totalKeys%1000000 == 0 {
fmt.Printf("Module %s num of keys %d, key size %d, value size %d\n",
module, totalKeys, totalKeySize, totalValueSize)
}
}
}

if err := itr.Error(); err != nil {
panic(fmt.Errorf("iterator error for module %s: %w", module, err))
}

moduleDuration := time.Since(startTimeModule)
fmt.Printf("Module %s stats finished, total keys %d, total key size %d, total value size %d, time taken %s\n",
module, totalKeys, totalKeySize, totalValueSize, moduleDuration)
}

fmt.Println("SeiDB Archive Migration: Aggregation completed.")
}
34 changes: 4 additions & 30 deletions tools/migration/sc/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,12 @@
rootmulti2 "github.com/cosmos/cosmos-sdk/storev2/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
acltypes "github.com/cosmos/cosmos-sdk/x/accesscontrol/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/sei-protocol/sei-chain/app/params"
epochmoduletypes "github.com/sei-protocol/sei-chain/x/epoch/types"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
tokenfactorytypes "github.com/sei-protocol/sei-chain/x/tokenfactory/types"
"github.com/sei-protocol/sei-chain/tools/migration/utils"
"github.com/sei-protocol/sei-db/config"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
Expand All @@ -51,22 +33,14 @@
storeV2 store.CommitMultiStore
}

var Keys = sdk.NewKVStoreKeys(
acltypes.StoreKey, authtypes.StoreKey, authzkeeper.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey, oracletypes.StoreKey,
evmtypes.StoreKey, wasm.StoreKey, epochmoduletypes.StoreKey, tokenfactorytypes.StoreKey,
)

func NewMigrator(homeDir string, db dbm.DB) *Migrator {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))

// Creating CMS for store V1
cmsV1 := rootmulti.NewStore(db, logger)
for _, key := range Keys {
for _, key := range utils.ModuleKeys {
cmsV1.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
err := cmsV1.LoadLatestVersion()
if err != nil {
panic(err)
Expand All @@ -79,9 +53,9 @@
ssConfig.Enable = true
ssConfig.KeepRecent = 0
cmsV2 := rootmulti2.NewStore(homeDir, logger, scConfig, ssConfig, true)
for _, key := range Keys {
for _, key := range utils.ModuleKeys {
cmsV2.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
err = cmsV2.LoadLatestVersion()
if err != nil {
panic(err)
Expand Down Expand Up @@ -174,7 +148,7 @@
pk := paramskeeper.NewKeeper(encodingConfig.Marshaler, encodingConfig.Amino, keyParams, tkeyParams)
wasmKeeper := keeper.NewKeeper(
encodingConfig.Marshaler,
Keys[wasm.StoreKey],
utils.ModuleKeys[wasm.StoreKey],
paramskeeper.Keeper{},
pk.Subspace("wasm"),
authkeeper.AccountKeeper{},
Expand Down
25 changes: 7 additions & 18 deletions tools/migration/ss/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"bytes"
"fmt"
"github.com/sei-protocol/sei-chain/tools/migration/utils"

Check failure on line 6 in tools/migration/ss/migrator.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"time"

"github.com/armon/go-metrics"
Expand All @@ -21,11 +22,7 @@
DefaultCacheSize int = 10000
)

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, stateStore types.StateStore) *Migrator {
func NewMigrator(db dbm.DB, stateStore types.StateStore) *Migrator {
return &Migrator{
iavlDB: db,
stateStore: stateStore,
Expand Down Expand Up @@ -78,8 +75,8 @@

func (m *Migrator) Verify(version int64) error {
var verifyErr error
for _, module := range modules {
tree, err := ReadTree(m.iavlDB, version, []byte(buildTreePrefix(module)))
for _, module := range utils.Modules {
tree, err := ReadTree(m.iavlDB, version, []byte(utils.BuildTreePrefix(module)))
if err != nil {
fmt.Printf("Error reading tree %s: %s\n", module, err.Error())
return err
Expand Down Expand Up @@ -125,7 +122,7 @@
var batchLeafNodeCount int
startModuleFound := startModule == "" // true if no start module specified

for _, module := range modules {
for _, module := range utils.Modules {
if !startModuleFound {
if module == startModule {
startModuleFound = true
Expand All @@ -136,12 +133,12 @@
startTimeModule := time.Now() // Measure time for each module
fmt.Printf("SeiDB Archive Migration: Iterating through %s module...\n", module)

prefixDB := dbm.NewPrefixDB(db, []byte(buildRawPrefix(module)))
prefixDB := dbm.NewPrefixDB(db, []byte(utils.BuildRawPrefix(module)))
var itr dbm.Iterator
var err error

// If there is a starting key, seek to it, otherwise start from the beginning
if startKey != nil && bytes.HasPrefix(startKey, []byte(buildRawPrefix(module))) {
if startKey != nil && bytes.HasPrefix(startKey, []byte(utils.BuildRawPrefix(module))) {
itr, err = prefixDB.Iterator(startKey, nil) // Start from the latest key
} else {
itr, err = prefixDB.Iterator(nil, nil) // Start from the beginning
Expand Down Expand Up @@ -204,14 +201,6 @@
return nil
}

func buildRawPrefix(moduleName string) string {
return fmt.Sprintf("s/k:%s/n", moduleName)
}

func buildTreePrefix(moduleName string) string {
return fmt.Sprintf("s/k:%s/", moduleName)
}

func ReadTree(db dbm.DB, version int64, prefix []byte) (*iavl.MutableTree, error) {
// TODO: Verify if we need a prefix here (or can just iterate through all modules)
if len(prefix) != 0 {
Expand Down
66 changes: 66 additions & 0 deletions tools/migration/utils/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package utils

import (
"fmt"

Check failure on line 4 in tools/migration/utils/helper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"github.com/CosmWasm/wasmd/x/wasm"
sdk "github.com/cosmos/cosmos-sdk/types"
acltypes "github.com/cosmos/cosmos-sdk/x/accesscontrol/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
epochmoduletypes "github.com/sei-protocol/sei-chain/x/epoch/types"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
tokenfactorytypes "github.com/sei-protocol/sei-chain/x/tokenfactory/types"
)

var ModuleKeys = sdk.NewKVStoreKeys(
acltypes.StoreKey, authtypes.StoreKey, authzkeeper.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey, oracletypes.StoreKey,
evmtypes.StoreKey, wasm.StoreKey, epochmoduletypes.StoreKey, tokenfactorytypes.StoreKey,
)

var Modules = []string{
"acc",
"aclaccesscontrol",
"authz",
"bank",
"capability",
"distribution",
"epoch",
"evidence",
"evm",
"feegrant",
"gov",
"ibc",
"mint",
"oracle",
"params",
"slashing",
"staking",
"tokenfactory",
"transfer",
"upgrade",
"wasm"}

func BuildRawPrefix(moduleName string) string {
return fmt.Sprintf("s/k:%s/n", moduleName)
}

func BuildTreePrefix(moduleName string) string {
return fmt.Sprintf("s/k:%s/", moduleName)
}
1 change: 0 additions & 1 deletion tools/migration/wasm/keeper.go

This file was deleted.

Loading