Skip to content

Commit

Permalink
Merge pull request #989 from bnb-chain/bc-fusion-fix2
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 authored Dec 15, 2023
2 parents 5ee5e0f + 0c727fe commit ca8aa34
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
11 changes: 9 additions & 2 deletions plugins/tokens/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,12 +92,19 @@ 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,
})
if !result.IsOK() {
logger.Error("Refund error", "swapId", swapID)
logger.Error("Refund error", "swapId", swapID, "result", fmt.Sprintf("%+v", result))
continue
}
i++
Expand Down
2 changes: 1 addition & 1 deletion plugins/tokens/timelock/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/tokens/timelock/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/tokens/timelock/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit ca8aa34

Please sign in to comment.