Skip to content

Commit

Permalink
chore: migrate OutgoingTxBatchBlockKey with batch nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
nulnut committed Oct 23, 2024
1 parent 12b35b8 commit 9368653
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
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
23 changes: 22 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,26 @@ 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)
}

for _, batch := range batches {
blockKey := types.GetOutgoingTxBatchBlockKey(batch.Block, batch.BatchNonce)
kvStore.Set(blockKey, 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

0 comments on commit 9368653

Please sign in to comment.