Skip to content

Commit

Permalink
fix: incorrect behavior for refund swap
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Nov 29, 2023
1 parent 610de96 commit 247b706
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
13 changes: 6 additions & 7 deletions plugins/tokens/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,19 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap
i++
}

iterator = swapKeeper.GetSwapIterator(ctx)
defer iterator.Close()
swapIterator := swapKeeper.GetSwapIterator(ctx)
defer swapIterator.Close()
i = 0
for ; iterator.Valid(); iterator.Next() {
for ; swapIterator.Valid(); swapIterator.Next() {
if i >= MaxUnlockItems {
break
}
var automaticSwap swap.AtomicSwap
swapKeeper.CDC().MustUnmarshalBinaryBare(iterator.Value(), &automaticSwap)
swapID := iterator.Key()[len(swap.HashKey):]
result := swap.HandleClaimHashTimerLockedTransferAfterBCFusion(ctx, swapKeeper, swap.ClaimHTLTMsg{
From: automaticSwap.From,
SwapID: swapID,
RandomNumber: automaticSwap.RandomNumber,
result := swap.HandleRefundHashTimerLockedTransferAfterBCFusion(ctx, swapKeeper, swap.RefundHTLTMsg{
From: automaticSwap.From,
SwapID: swapID,
})
if !result.IsOK() {
logger.Error("Refound error", "swapId", swapID)
Expand Down
26 changes: 14 additions & 12 deletions plugins/tokens/swap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func NewHandler(kp Keeper) sdk.Handler {
}
return handleDepositHashTimerLockedTransfer(ctx, kp, msg)
case ClaimHTLTMsg:
return handleClaimHashTimerLockedTransfer(ctx, kp, msg, false)
return handleClaimHashTimerLockedTransfer(ctx, kp, msg)
case RefundHTLTMsg:
return handleRefundHashTimerLockedTransfer(ctx, kp, msg)
return handleRefundHashTimerLockedTransfer(ctx, kp, msg, false)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
Expand Down Expand Up @@ -107,23 +107,19 @@ func handleDepositHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg Deposi

}

func HandleClaimHashTimerLockedTransferAfterBCFusion(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg) sdk.Result {
return handleClaimHashTimerLockedTransfer(ctx, kp, msg, true)
}

func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg, isBCFusionRefund bool) sdk.Result {
func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTLTMsg) sdk.Result {
swap := kp.GetSwap(ctx, msg.SwapID)
if swap == nil {
return ErrNonExistSwapID(fmt.Sprintf("No matched swap with swapID %v", msg.SwapID)).Result()
}
if swap.Status != Open {
return ErrUnexpectedSwapStatus(fmt.Sprintf("Expected swap status is Open, actually it is %s", swap.Status.String())).Result()
}
if !isBCFusionRefund && swap.ExpireHeight <= ctx.BlockHeight() {
if swap.ExpireHeight <= ctx.BlockHeight() {
return ErrClaimExpiredSwap(fmt.Sprintf("Current block height is %d, the swap expire height(%d) is passed", ctx.BlockHeight(), swap.ExpireHeight)).Result()
}

if !isBCFusionRefund && !bytes.Equal(CalculateRandomHash(msg.RandomNumber, swap.Timestamp), swap.RandomNumberHash) {
if !bytes.Equal(CalculateRandomHash(msg.RandomNumber, swap.Timestamp), swap.RandomNumberHash) {
return ErrMismatchedRandomNumber("Mismatched random number").Result()
}

Expand Down Expand Up @@ -168,16 +164,22 @@ func handleClaimHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg ClaimHTL
return sdk.Result{Tags: tags}
}

func handleRefundHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg RefundHTLTMsg) sdk.Result {
func HandleRefundHashTimerLockedTransferAfterBCFusion(ctx sdk.Context, kp Keeper, msg RefundHTLTMsg) sdk.Result {
return handleRefundHashTimerLockedTransfer(ctx, kp, msg, true)
}

func handleRefundHashTimerLockedTransfer(ctx sdk.Context, kp Keeper, msg RefundHTLTMsg, isBCFusionRefund bool) sdk.Result {
swap := kp.GetSwap(ctx, msg.SwapID)
if swap == nil {
return ErrNonExistSwapID(fmt.Sprintf("No matched swap with swapID %v", msg.SwapID)).Result()
}
if swap.Status != Open {
return ErrUnexpectedSwapStatus(fmt.Sprintf("Expected swap status is Open, actually it is %s", swap.Status.String())).Result()
}
if ctx.BlockHeight() < swap.ExpireHeight {
return ErrRefundUnexpiredSwap(fmt.Sprintf("Current block height is %d, the expire height (%d) is still not reached", ctx.BlockHeight(), swap.ExpireHeight)).Result()
if !isBCFusionRefund {
if ctx.BlockHeight() < swap.ExpireHeight {
return ErrRefundUnexpiredSwap(fmt.Sprintf("Current block height is %d, the expire height (%d) is still not reached", ctx.BlockHeight(), swap.ExpireHeight)).Result()
}
}

tags := sdk.EmptyTags()
Expand Down

0 comments on commit 247b706

Please sign in to comment.