Skip to content

Commit

Permalink
add migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Dec 11, 2024
1 parent 70c8600 commit a620991
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/crypto/evm_address.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package crypto

import (
"github.com/ethereum/go-ethereum/common"
"strings"

"github.com/ethereum/go-ethereum/common"

"github.com/zeta-chain/node/pkg/constant"
)

Expand Down
1 change: 1 addition & 0 deletions testutil/sample/fungible.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func ForeignCoins(t *testing.T, address string) types.ForeignCoins {
Symbol: StringRandom(r, 32),
CoinType: coin.CoinType_ERC20,
GasLimit: r.Uint64(),
LiquidityCap: UintInRange(0, 10000000000),
}
}

Expand Down
1 change: 1 addition & 0 deletions x/crosschain/types/message_whitelist_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/pkg/errors"

"github.com/zeta-chain/node/pkg/crypto"
"github.com/zeta-chain/node/x/fungible/types"
)
Expand Down
24 changes: 24 additions & 0 deletions x/fungible/keeper/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v3 "github.com/zeta-chain/node/x/fungible/migrations/v3"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
fungibleKeeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{
fungibleKeeper: keeper,
}
}

// Migrate2to3 migrates the store from consensus version 2 to 3
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.fungibleKeeper)
}
27 changes: 27 additions & 0 deletions x/fungible/migrations/v3/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v3

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/zeta-chain/node/pkg/crypto"
"github.com/zeta-chain/node/x/fungible/types"
)

type fungibleKeeper interface {
GetAllForeignCoins(ctx sdk.Context) (list []types.ForeignCoins)
SetForeignCoins(ctx sdk.Context, foreignCoins types.ForeignCoins)
}

// MigrateStore migrates the x/fungible module state from the consensus version 2 to 3
// It updates all existing address in ForeignCoin to use checksum format if the address is EVM type
func MigrateStore(ctx sdk.Context, fungibleKeeper fungibleKeeper) error {
fcs := fungibleKeeper.GetAllForeignCoins(ctx)
for _, fc := range fcs {
if fc.Asset != "" && crypto.IsEVMAddress(fc.Asset) && !crypto.IsChecksumAddress(fc.Asset) {
fc.Asset = crypto.ToChecksumAddress(fc.Asset)
fungibleKeeper.SetForeignCoins(ctx, fc)
}
}

return nil
}
76 changes: 76 additions & 0 deletions x/fungible/migrations/v3/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package v3_test

import (
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/node/testutil/keeper"
"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/fungible/migrations/v3"
"github.com/zeta-chain/node/x/fungible/types"
"testing"
)

func TestMigrateStore(t *testing.T) {
tests := []struct {
name string
assetList []string
expectedList []string
}{
{
name: "no asset to update",
assetList: []string{},
expectedList: []string{},
},
{
name: "assets to update",
assetList: []string{
"",
"0x5a4f260a7d716c859a2736151cb38b9c58c32c64", // lowercase
"",
"0xc0ffee254729296a45a3885639AC7E10F9d54979", // checksum
"",
"",
"Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr",
"BrS9iNMC3y8J4QTmCz8VrGrYepdoxXYvKxcDMiixwLn5",
"0x999999CF1046E68E36E1AA2E0E07105EDDD1F08E", // uppcase
},
expectedList: []string{
"",
"0x5a4f260A7D716c859A2736151cB38b9c58C32c64",
"",
"0xc0ffee254729296a45a3885639AC7E10F9d54979",
"",
"",
"Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr",
"BrS9iNMC3y8J4QTmCz8VrGrYepdoxXYvKxcDMiixwLn5",
"0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E",
},
},
}

for _, tt := range tests {
k, ctx, _, _ := keepertest.FungibleKeeper(t)
// Arrange

// set sample foreign coins
expectedForeignCoins := make([]types.ForeignCoins, len(tt.assetList))
for i, asset := range tt.assetList {
expectedForeignCoins[i] = sample.ForeignCoins(t, sample.EthAddress().Hex())
expectedForeignCoins[i].Asset = asset
k.SetForeignCoins(ctx, expectedForeignCoins[i])
}

// update for expected list
for i := range tt.assetList {
expectedForeignCoins[i].Asset = tt.expectedList[i]
}

// Act
err := v3.MigrateStore(ctx, k)
require.NoError(t, err)

// Assert
actualForeignCoins := k.GetAllForeignCoins(ctx)
require.ElementsMatch(t, expectedForeignCoins, actualForeignCoins)
}

}
6 changes: 5 additions & 1 deletion x/fungible/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (am AppModule) Name() string {
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
panic(err)
}
}

// RegisterInvariants registers the fungible module's invariants.
Expand Down Expand Up @@ -153,7 +157,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock executes all ABCI BeginBlock logic respective to the fungible module.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit a620991

Please sign in to comment.