-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(x/erc20): migrate legacy key store
- Loading branch information
1 parent
0a123f8
commit 24db824
Showing
8 changed files
with
135 additions
and
17 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,73 @@ | ||
package v8 | ||
|
||
import ( | ||
sdkmath "cosmossdk.io/math" | ||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" | ||
"github.com/functionx/fx-core/v8/x/erc20/types" | ||
) | ||
|
||
func (m Migrator) migrateKeys(ctx sdk.Context) error { | ||
store := ctx.KVStore(m.storeKey) | ||
if err := m.migrateParams(ctx, store); err != nil { | ||
return err | ||
} | ||
|
||
if err := m.migrateRelationToCache(ctx, store); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (m Migrator) migrateRelationToCache(ctx sdk.Context, store storetypes.KVStore) error { | ||
// 1. migrate ibc transfer relation | ||
iterator := storetypes.KVStorePrefixIterator(store, KeyPrefixIBCTransfer) | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
key := iterator.Key() | ||
newKey := key[len(KeyPrefixIBCTransfer):] | ||
if err := m.keeper.Cache.Set(ctx, string(newKey), sdkmath.ZeroInt()); err != nil { | ||
return err | ||
} | ||
store.Delete(key) | ||
} | ||
|
||
// 2. migrate outgoing transfer relation | ||
iterator = storetypes.KVStorePrefixIterator(store, KeyPrefixOutgoingTransfer) | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
key := iterator.Key() | ||
originTokenKey := OutgoingTransferKeyToOriginTokenKey(key) | ||
if err := m.keeper.Cache.Set(ctx, originTokenKey, sdkmath.ZeroInt()); err != nil { | ||
return err | ||
} | ||
store.Delete(key) | ||
} | ||
return nil | ||
} | ||
|
||
func (m Migrator) migrateParams(ctx sdk.Context, store storetypes.KVStore) error { | ||
bz := store.Get(ParamsKey) | ||
if len(bz) == 0 { | ||
return nil | ||
} | ||
var legacyParams types.LegacyParams | ||
m.cdc.MustUnmarshal(bz, &legacyParams) | ||
|
||
store.Delete(ParamsKey) | ||
return m.keeper.Params.Set(ctx, types.Params{EnableErc20: legacyParams.EnableErc20}) | ||
} | ||
|
||
func OutgoingTransferKeyToOriginTokenKey(key []byte) string { | ||
// key = prefix + moduleName(string-) + sdk.Uint64ToBigEndian(id)(len = 8) | ||
// 1. remove prefix | ||
key = key[len(KeyPrefixOutgoingTransfer):] | ||
// 2. get moduleName | ||
moduleName := string(key[:len(key)-8]) | ||
// 3. get id | ||
id := sdk.BigEndianToUint64(key[len(key)-8:]) | ||
// 4. new originTokenKey | ||
return crosschaintypes.NewOriginTokenKey(moduleName, id) | ||
} |
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,21 @@ | ||
package v8_test | ||
|
||
import ( | ||
"testing" | ||
|
||
tmrand "github.com/cometbft/cometbft/libs/rand" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/functionx/fx-core/v8/x/crosschain/types" | ||
v8 "github.com/functionx/fx-core/v8/x/erc20/migrations/v8" | ||
) | ||
|
||
func TestOutgoingTransferKeyToOriginTokenKey(t *testing.T) { | ||
moduleName := tmrand.Str(5) | ||
txID := uint64(tmrand.Int63n(100000)) | ||
oldKey := append(append(v8.KeyPrefixOutgoingTransfer, []byte(moduleName)...), sdk.Uint64ToBigEndian(txID)...) | ||
expectKey := types.NewOriginTokenKey(moduleName, txID) | ||
actual := v8.OutgoingTransferKeyToOriginTokenKey(oldKey) | ||
require.EqualValues(t, expectKey, actual) | ||
} |
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