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

chore: migrate OutgoingTxBatchBlockKey with batch nonce #773

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
nextversion "github.com/functionx/fx-core/v8/app/upgrades/v8"
"github.com/functionx/fx-core/v8/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v8/types"
"github.com/functionx/fx-core/v8/x/crosschain/types"
fxgovv8 "github.com/functionx/fx-core/v8/x/gov/migrations/v8"
fxgovtypes "github.com/functionx/fx-core/v8/x/gov/types"
fxstakingv8 "github.com/functionx/fx-core/v8/x/staking/migrations/v8"
Expand Down Expand Up @@ -105,6 +106,8 @@ func checkAppUpgrade(t *testing.T, ctx sdk.Context, myApp *app.App) {
checkGovCustomParams(t, ctx, myApp)

checkErc20Keys(t, ctx, myApp)

checkOutgoingBatch(t, ctx, myApp)
}

func checkErc20Keys(t *testing.T, ctx sdk.Context, myApp *app.App) {
Expand Down Expand Up @@ -144,3 +147,14 @@ func checkKeysIsDelete(t *testing.T, kvStore storetypes.KVStore, keys [][]byte)
checkFn(removeKey)
}
}

func checkOutgoingBatch(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()
for _, keeper := range myApp.CrosschainKeepers.ToSlice() {
kvStore := ctx.KVStore(myApp.GetKey(keeper.ModuleName()))
keeper.IterateOutgoingTxBatches(ctx, func(batch *types.OutgoingTxBatch) bool {
assert.True(t, kvStore.Has(types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce)))
return false
})
}
}
2 changes: 1 addition & 1 deletion x/crosschain/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewMigrator(k Keeper) Migrator {
}

func (m Migrator) Migrate7to8(ctx sdk.Context) error {
return v8.Migrate(ctx, m.keeper.storeKey)
return v8.Migrate(ctx, m.keeper.storeKey, m.keeper.cdc)
}

func (m Migrator) Migrate7to8WithArbExternalBlockTime(ctx sdk.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/keeper/outgoing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (k Keeper) StoreBatch(ctx sdk.Context, batch *types.OutgoingTxBatch) error
key := types.GetOutgoingTxBatchKey(batch.TokenContract, batch.BatchNonce)
store.Set(key, k.cdc.MustMarshal(batch))

blockKey := types.GetOutgoingTxBatchBlockKey(batch.Block)
blockKey := types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce)
// Note: Only one OutgoingTxBatch can be submitted in a block
if store.Has(blockKey) {
return types.ErrInvalid.Wrapf("block:[%v] has batch request", batch.Block)
Expand All @@ -128,7 +128,7 @@ func (k Keeper) StoreBatch(ctx sdk.Context, batch *types.OutgoingTxBatch) error
func (k Keeper) DeleteBatch(ctx sdk.Context, batch *types.OutgoingTxBatch) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetOutgoingTxBatchKey(batch.TokenContract, batch.BatchNonce))
store.Delete(types.GetOutgoingTxBatchBlockKey(batch.Block))
store.Delete(types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce))
}

// GetOutgoingTxBatch loads a batch object. Returns nil when not exists.
Expand Down
24 changes: 23 additions & 1 deletion x/crosschain/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package v8

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/functionx/fx-core/v8/app/upgrades/store"
"github.com/functionx/fx-core/v8/x/crosschain/types"
)

var (
Expand All @@ -31,7 +33,27 @@ func GetRemovedStoreKeys() [][]byte {
}
}

func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey) error {
func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store.RemoveStoreKeys(ctx, storeKey, GetRemovedStoreKeys())
migrateOutgoingTxBatchBlockKey(ctx, storeKey, cdc)
return nil
}

func migrateOutgoingTxBatchBlockKey(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
kvStore := ctx.KVStore(storeKey)
iter := storetypes.KVStorePrefixIterator(kvStore, types.OutgoingTxBatchBlockKey)
defer iter.Close()

batches := make([]*types.OutgoingTxBatch, 0, 100)
for ; iter.Valid(); iter.Next() {
batch := new(types.OutgoingTxBatch)
cdc.MustUnmarshal(iter.Value(), batch)
batches = append(batches, batch)
kvStore.Delete(iter.Key())
}

for _, batch := range batches {
newBlockKey := types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce)
kvStore.Set(newBlockKey, cdc.MustMarshal(batch))
}
}
4 changes: 2 additions & 2 deletions x/crosschain/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func GetOutgoingTxBatchKey(tokenContract string, batchNonce uint64) []byte {
}

// GetOutgoingTxBatchBlockKey returns the following key format
func GetOutgoingTxBatchBlockKey(blockHeight uint64) []byte {
return append(OutgoingTxBatchBlockKey, sdk.Uint64ToBigEndian(blockHeight)...)
func GetOutgoingTxBatchBlockKey(blockHeight, batchNonce uint64) []byte {
return append(append(OutgoingTxBatchBlockKey, sdk.Uint64ToBigEndian(blockHeight)...), sdk.Uint64ToBigEndian(batchNonce)...)
}

// GetBatchConfirmKey returns the following key format
Expand Down