Skip to content

Commit

Permalink
Merge branch 'main' into pointers/erc1155-cw1155
Browse files Browse the repository at this point in the history
  • Loading branch information
dvli2007 authored Nov 21, 2024
2 parents 26e5f38 + cea68db commit cf69463
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/seidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func SetupSeiDB(
migrationHeight := cast.ToInt64(appOpts.Get(FlagMigrateHeight))
baseAppOptions = append([]func(*baseapp.BaseApp){
func(baseApp *baseapp.BaseApp) {
if migrationEnabled {
if migrationEnabled || migrationHeight > 0 {
originalCMS := baseApp.CommitMultiStore()
baseApp.SetQueryMultiStore(originalCMS)
baseApp.SetMigrationHeight(migrationHeight)
Expand Down
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
2 changes: 1 addition & 1 deletion evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (b *Backend) StateAtBlock(ctx context.Context, block *ethtypes.Block, reexe
if !associatedNow {
err := types.NewAssociationMissingErr(msg.From.Hex())
metrics.IncrementAssociationError("state_at_block", err)
return nil, emptyRelease, err
continue // don't return error, just continue bc we want to process the rest of the txs and return the statedb
}
if err := helpers.NewAssociationHelper(b.keeper, b.keeper.BankKeeper(), b.keeper.AccountKeeper()).AssociateAddresses(statedb.Ctx(), seiAddr, msg.From, nil); err != nil {
return nil, emptyRelease, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ replace (
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.45
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.2.0
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.2
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-23
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20241120141252-bf6b5923d7d7
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.45
// Latest goleveldb is broken, we have to stick to this version
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/go-ethereum v1.13.5-sei-23 h1:rkgeOHC56QTco4mIyGd6cZHtlonulLsaPLZCaMY6TAw=
github.com/sei-protocol/go-ethereum v1.13.5-sei-23/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20241120141252-bf6b5923d7d7 h1:5G1wqz9+1lDKQIpnoaP/oXneLfuhJozPd1/BDNCtaxw=
github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20241120141252-bf6b5923d7d7/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
github.com/sei-protocol/sei-cosmos v0.3.45 h1:l6ss5Y4OC+vvu+74lWz8EUBJN68x0CRuaEYtqA+TvOs=
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 @@ func verifySS(version int64, homeDir string, db dbm.DB) error {
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()
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 @@ import (
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,20 +33,12 @@ type Migrator struct {
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)
}
err := cmsV1.LoadLatestVersion()
Expand All @@ -79,7 +53,7 @@ func NewMigrator(homeDir string, db dbm.DB) *Migrator {
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)
}
err = cmsV2.LoadLatestVersion()
Expand Down Expand Up @@ -174,7 +148,7 @@ func CreateWasmSnapshotter(cms sdk.MultiStore, homeDir string) *keeper.WasmSnaps
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 @@ -7,6 +7,7 @@ import (

"github.com/armon/go-metrics"
"github.com/cosmos/iavl"
"github.com/sei-protocol/sei-chain/tools/migration/utils"
"github.com/sei-protocol/sei-db/ss/types"
dbm "github.com/tendermint/tm-db"
)
Expand All @@ -21,11 +22,7 @@ const (
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) Migrate(version int64, homeDir string) error {

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 @@ func ExportLeafNodesFromKey(db dbm.DB, ch chan<- types.RawSnapshotNode, startKey
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 @@ func ExportLeafNodesFromKey(db dbm.DB, ch chan<- types.RawSnapshotNode, startKey
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 @@ func ExportLeafNodesFromKey(db dbm.DB, ch chan<- types.RawSnapshotNode, startKey
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
67 changes: 67 additions & 0 deletions tools/migration/utils/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package utils

import (
"fmt"

"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.

0 comments on commit cf69463

Please sign in to comment.