From dad1e235f7e016765ed4ac6de2b4236f9e150f4d Mon Sep 17 00:00:00 2001 From: j75689 Date: Fri, 15 Dec 2023 17:48:02 +0800 Subject: [PATCH 1/4] fix: only refund opened swap --- plugins/tokens/plugin.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/tokens/plugin.go b/plugins/tokens/plugin.go index d22bc4d1c..2cc96de93 100644 --- a/plugins/tokens/plugin.go +++ b/plugins/tokens/plugin.go @@ -92,6 +92,13 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap var automaticSwap swap.AtomicSwap swapKeeper.CDC().MustUnmarshalBinaryBare(swapIterator.Value(), &automaticSwap) swapID := swapIterator.Key()[len(swap.HashKey):] + swapItem := swapKeeper.GetSwap(ctx, swapID) + if swapItem == nil { + continue + } + if swapItem.Status != swap.Open { + continue + } result := swap.HandleRefundHashTimerLockedTransferAfterBCFusion(ctx, swapKeeper, swap.RefundHTLTMsg{ From: automaticSwap.From, SwapID: swapID, From 587060ad41f19e88f717a970ec1d49c59228ceec Mon Sep 17 00:00:00 2001 From: j75689 Date: Fri, 15 Dec 2023 17:30:01 +0800 Subject: [PATCH 2/4] fix: do not check timelock time if isBCFusionRefund --- plugins/tokens/plugin.go | 4 ++-- plugins/tokens/timelock/handler.go | 2 +- plugins/tokens/timelock/keeper.go | 4 ++-- plugins/tokens/timelock/keeper_test.go | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/tokens/plugin.go b/plugins/tokens/plugin.go index 2cc96de93..8b6987ad6 100644 --- a/plugins/tokens/plugin.go +++ b/plugins/tokens/plugin.go @@ -74,7 +74,7 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap logger.Error("ParseKeyRecord error", "error", err) continue } - err = timelockKeeper.TimeUnlock(ctx, addr, id) + err = timelockKeeper.TimeUnlock(ctx, addr, id, true) if err != nil { logger.Error("TimeUnlock error", "error", err) continue @@ -104,7 +104,7 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap SwapID: swapID, }) if !result.IsOK() { - logger.Error("Refund error", "swapId", swapID) + logger.Error("Refund error", "swapId", swapID, result.Tags) continue } i++ diff --git a/plugins/tokens/timelock/handler.go b/plugins/tokens/timelock/handler.go index f69741954..ee03c8f4c 100644 --- a/plugins/tokens/timelock/handler.go +++ b/plugins/tokens/timelock/handler.go @@ -60,7 +60,7 @@ func handleTimeRelock(ctx sdk.Context, keeper Keeper, msg TimeRelockMsg) sdk.Res } func handleTimeUnlock(ctx sdk.Context, keeper Keeper, msg TimeUnlockMsg) sdk.Result { - err := keeper.TimeUnlock(ctx, msg.From, msg.Id) + err := keeper.TimeUnlock(ctx, msg.From, msg.Id, false) if err != nil { return err.Result() } diff --git a/plugins/tokens/timelock/keeper.go b/plugins/tokens/timelock/keeper.go index 686f2025e..260ddc4a5 100644 --- a/plugins/tokens/timelock/keeper.go +++ b/plugins/tokens/timelock/keeper.go @@ -127,13 +127,13 @@ func (keeper Keeper) TimeLock(ctx sdk.Context, from sdk.AccAddress, description return record, nil } -func (keeper Keeper) TimeUnlock(ctx sdk.Context, from sdk.AccAddress, recordId int64) sdk.Error { +func (keeper Keeper) TimeUnlock(ctx sdk.Context, from sdk.AccAddress, recordId int64, isBCFusionRefund bool) sdk.Error { record, found := keeper.GetTimeLockRecord(ctx, from, recordId) if !found { return ErrTimeLockRecordDoesNotExist(DefaultCodespace, from, recordId) } - if ctx.BlockHeader().Time.Before(record.LockTime) { + if !isBCFusionRefund && ctx.BlockHeader().Time.Before(record.LockTime) { return ErrCanNotUnlock(DefaultCodespace, fmt.Sprintf("lock time(%s) is after now(%s)", record.LockTime.UTC().String(), ctx.BlockHeader().Time.UTC().String())) } diff --git a/plugins/tokens/timelock/keeper_test.go b/plugins/tokens/timelock/keeper_test.go index a973eed04..4f53c8fce 100644 --- a/plugins/tokens/timelock/keeper_test.go +++ b/plugins/tokens/timelock/keeper_test.go @@ -142,7 +142,7 @@ func TestKeeper_TimeUnlock_RecordNotExist(t *testing.T) { _, acc := testutils.NewAccount(ctx, accKeeper, 0) - err := keeper.TimeUnlock(ctx, acc.GetAddress(), 1) + err := keeper.TimeUnlock(ctx, acc.GetAddress(), 1, false) require.NotNil(t, err) require.Equal(t, err.Code(), CodeTimeLockRecordDoesNotExist) } @@ -169,7 +169,7 @@ func TestKeeper_TimeUnlock_ErrLockTime(t *testing.T) { record, err := keeper.TimeLock(ctx, acc.GetAddress(), "Test", lockCoins, time.Now().Add(1000*time.Second)) require.Nil(t, err) - err = keeper.TimeUnlock(ctx, acc.GetAddress(), record.Id) + err = keeper.TimeUnlock(ctx, acc.GetAddress(), record.Id, false) require.NotNil(t, err) require.Equal(t, err.Code(), CodeCanNotUnlock) } @@ -197,7 +197,7 @@ func TestKeeper_TimeUnlock_Success(t *testing.T) { require.Nil(t, err) ctx = ctx.WithBlockTime(time.Now().Add(2000 * time.Second)) - err = keeper.TimeUnlock(ctx, acc.GetAddress(), record.Id) + err = keeper.TimeUnlock(ctx, acc.GetAddress(), record.Id, false) require.Nil(t, err) } From 996e8aca56363442f88278e621a6813fd0dae1a5 Mon Sep 17 00:00:00 2001 From: j75689 Date: Fri, 15 Dec 2023 17:52:16 +0800 Subject: [PATCH 3/4] fixup! fix: only refund opened swap --- plugins/tokens/plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/tokens/plugin.go b/plugins/tokens/plugin.go index 8b6987ad6..89f9d9dcb 100644 --- a/plugins/tokens/plugin.go +++ b/plugins/tokens/plugin.go @@ -104,7 +104,7 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap SwapID: swapID, }) if !result.IsOK() { - logger.Error("Refund error", "swapId", swapID, result.Tags) + logger.Error("Refund error", "swapId", swapID, "result", fmt.Sprintf("%s", result)) continue } i++ From 0c727fec6ef7f0677fef28a0fe68d42f0636dbdb Mon Sep 17 00:00:00 2001 From: j75689 Date: Fri, 15 Dec 2023 17:53:09 +0800 Subject: [PATCH 4/4] fix: log format --- plugins/tokens/plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/tokens/plugin.go b/plugins/tokens/plugin.go index 89f9d9dcb..4b6fa35a1 100644 --- a/plugins/tokens/plugin.go +++ b/plugins/tokens/plugin.go @@ -104,7 +104,7 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap SwapID: swapID, }) if !result.IsOK() { - logger.Error("Refund error", "swapId", swapID, "result", fmt.Sprintf("%s", result)) + logger.Error("Refund error", "swapId", swapID, "result", fmt.Sprintf("%+v", result)) continue } i++