Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: format refund logs and update cosmos-sdk #991

Merged
merged 10 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ require (
replace (
github.com/Shopify/sarama v1.26.1 => github.com/Shopify/sarama v1.21.0
// TODO: bump to official release
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20231214014755-d940f55f667c
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20240102095228-21ae4531c279
github.com/grpc-ecosystem/grpc-gateway/v2 => github.com/prysmaticlabs/grpc-gateway/v2 v2.3.1-0.20210702154020-550e1cd83ec1
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/go-amino => github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20231214014755-d940f55f667c h1:8R2s5MCR8ZYn5ChktnXmuwC1z5l0Ldrfpt8WdMqmOQE=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20231214014755-d940f55f667c/go.mod h1:OjdXpDHofs6gcPLM9oGD+mm8DPc6Lsevz0Kia53zt3Q=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20240102095228-21ae4531c279 h1:DFDFV0dL2mpFP1WdymlM2QWa6DepaMyAl9odA6ioRfY=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.5-0.20240102095228-21ae4531c279/go.mod h1:OjdXpDHofs6gcPLM9oGD+mm8DPc6Lsevz0Kia53zt3Q=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2 h1:iAlp9gqG0f2LGAauf3ZiijWlT6NI+W2r9y70HH9LI3k=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2/go.mod h1:LiCO7jev+3HwLGAiN9gpD0z+jTz95RqgSavbse55XOY=
github.com/bnb-chain/bnc-tendermint v0.32.3-bc.10 h1:E4iSwEbJCLYchHiHE1gnOM3jjmJXLBxARhy/RCl8CpI=
Expand Down
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we record the key and update the lasted proceed key outside the loop?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if outside the loop, you will never know the key. will this be heavy in Endblock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked the codes, and found that swap would be gc in the breathe block, so I think this change is unnecessary

https://github.com/bnb-chain/node/blob/13bb81c70a49e55e296ed2b59c680ff9a85db8f5/plugins/tokens/plugin.go#L86C9-L86C9

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