-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters