Skip to content

Commit

Permalink
Merge pull request #342 from thomas-nguy/thomas/fix-bug-cleaning-batch
Browse files Browse the repository at this point in the history
  • Loading branch information
zmanian authored Jan 28, 2022
2 parents 5924851 + 5dcb579 commit d52551b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion module/x/gravity/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func cleanupTimedOutBatchTxs(ctx sdk.Context, k keeper.Keeper) {
btx, _ := otx.(*types.BatchTx)

if btx.Timeout < ethereumHeight {
k.CancelBatchTx(ctx, common.HexToAddress(btx.TokenContract), btx.BatchNonce)
k.CancelBatchTx(ctx, btx)
}

return false
Expand Down
19 changes: 11 additions & 8 deletions module/x/gravity/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ func (k Keeper) getBatchTimeoutHeight(ctx sdk.Context) uint64 {
// It deletes all the transactions in the batch, then cancels all earlier batches
func (k Keeper) batchTxExecuted(ctx sdk.Context, tokenContract common.Address, nonce uint64) {
otx := k.GetOutgoingTx(ctx, types.MakeBatchTxKey(tokenContract, nonce))
if otx == nil {
k.Logger(ctx).Error("Failed to clean batches",
"token contract", tokenContract.Hex(),
"nonce", nonce)
return
}
batchTx, _ := otx.(*types.BatchTx)
k.IterateOutgoingTxsByType(ctx, types.BatchTxPrefixByte, func(key []byte, otx types.OutgoingTx) bool {
// If the iterated batches nonce is lower than the one that was just executed, cancel it
btx, _ := otx.(*types.BatchTx)
if (btx.BatchNonce < batchTx.BatchNonce) && (batchTx.TokenContract == tokenContract.Hex()) {
k.CancelBatchTx(ctx, tokenContract, btx.BatchNonce)
if (btx.BatchNonce < batchTx.BatchNonce) && (btx.TokenContract == batchTx.TokenContract) {
k.CancelBatchTx(ctx, btx)
}
return false
})
Expand Down Expand Up @@ -130,10 +136,7 @@ func (k Keeper) GetBatchFeesByTokenType(ctx sdk.Context, tokenContractAddr commo
}

// CancelBatchTx releases all TX in the batch and deletes the batch
func (k Keeper) CancelBatchTx(ctx sdk.Context, tokenContract common.Address, nonce uint64) {
otx := k.GetOutgoingTx(ctx, types.MakeBatchTxKey(tokenContract, nonce))
batch, _ := otx.(*types.BatchTx)

func (k Keeper) CancelBatchTx(ctx sdk.Context, batch *types.BatchTx) {
// free transactions from batch and reindex them
for _, tx := range batch.Transactions {
k.setUnbatchedSendToEthereum(ctx, tx)
Expand All @@ -148,8 +151,8 @@ func (k Keeper) CancelBatchTx(ctx sdk.Context, tokenContract common.Address, non
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.getBridgeContractAddress(ctx)),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.getBridgeChainID(ctx)))),
sdk.NewAttribute(types.AttributeKeyOutgoingBatchID, fmt.Sprint(nonce)),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(nonce)),
sdk.NewAttribute(types.AttributeKeyOutgoingBatchID, fmt.Sprint(batch.BatchNonce)),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(batch.BatchNonce)),
),
)
}
Expand Down

0 comments on commit d52551b

Please sign in to comment.