Skip to content

Commit

Permalink
fix: format refound logs and skip skip the processed swap
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Jan 2, 2024
1 parent ca8aa34 commit 65bcf2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
18 changes: 15 additions & 3 deletions plugins/tokens/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tokens

import (
"bytes"
"encoding/binary"
"fmt"

Expand Down Expand Up @@ -71,21 +72,29 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap
}
addr, id, err := timelock.ParseKeyRecord(iterator.Key())
if err != nil {
logger.Error("ParseKeyRecord error", "error", err)
logger.Error("failed to parse timelock record", "error", err)
continue
}
err = timelockKeeper.TimeUnlock(ctx, addr, id, true)
if err != nil {
logger.Error("TimeUnlock error", "error", err)
logger.Error("failed to unlock the time locks", "error", err)
continue
}
logger.Info("succeed to unlock the time locks", "addr", addr, "id", id)
i++
}

swapIterator := swapKeeper.GetSwapIterator(ctx)
defer swapIterator.Close()
i = 0
lastProcessedRefundSwapKey := swapKeeper.GetLatestProcessedRefundSwapKey(ctx)
for ; swapIterator.Valid(); swapIterator.Next() {
if len(lastProcessedRefundSwapKey) > 0 &&
bytes.Compare(swapIterator.Key(), lastProcessedRefundSwapKey) <= 0 {
// skip the processed swap
continue
}

if i >= MaxUnlockItems {
break
}
Expand All @@ -104,9 +113,12 @@ func EndBlocker(ctx sdk.Context, timelockKeeper timelock.Keeper, swapKeeper swap
SwapID: swapID,
})
if !result.IsOK() {
logger.Error("Refund error", "swapId", swapID, "result", fmt.Sprintf("%+v", result))
logger.Error("failed to refund swap", "swapId", swapID, "result", fmt.Sprintf("%+v", result))
continue
}

logger.Info("succeed to refund swap", "swapId", swapID, "swap", fmt.Sprintf("%+v", swapItem))
swapKeeper.SetLatestProcessedRefundSwapKey(ctx, swapIterator.Key())
i++
}
}
Expand Down
10 changes: 10 additions & 0 deletions plugins/tokens/swap/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ func (kp *Keeper) GetSwap(ctx sdk.Context, swapID SwapBytes) *AtomicSwap {
return &swap
}

func (kp *Keeper) GetLatestProcessedRefundSwapKey(ctx sdk.Context) []byte {
kvStore := ctx.KVStore(kp.storeKey)
return kvStore.Get(LatestProcessedRefundSwapKey)
}

func (kp *Keeper) SetLatestProcessedRefundSwapKey(ctx sdk.Context, key []byte) {
kvStore := ctx.KVStore(kp.storeKey)
kvStore.Set(LatestProcessedRefundSwapKey, key)
}

func (kp *Keeper) GetSwapIterator(ctx sdk.Context) (iterator store.Iterator) {
kvStore := ctx.KVStore(kp.storeKey)
return sdk.KVStorePrefixIterator(kvStore, HashKey)
Expand Down
11 changes: 6 additions & 5 deletions plugins/tokens/swap/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const (
)

var (
HashKey = []byte{0x01}
SwapCreatorQueueKey = []byte{0x02}
SwapRecipientQueueKey = []byte{0x03}
SwapCloseTimeKey = []byte{0x04}
SwapIndexKey = []byte{0x05}
HashKey = []byte{0x01}
SwapCreatorQueueKey = []byte{0x02}
SwapRecipientQueueKey = []byte{0x03}
SwapCloseTimeKey = []byte{0x04}
SwapIndexKey = []byte{0x05}
LatestProcessedRefundSwapKey = []byte{0x06}
)

func BuildHashKey(randomNumberHash []byte) []byte {
Expand Down

0 comments on commit 65bcf2c

Please sign in to comment.