Skip to content

Commit

Permalink
use tmp context for ballot logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Feb 21, 2024
1 parent 78c3c0c commit 76e2198
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions x/crosschain/keeper/msg_server_vote_inbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
index := msg.Digest()

// vote on inbound ballot
// use a temporary context to not commit any ballot state change in case of error
tmpCtx, commit := ctx.CacheContext()
finalized, isNew, err := k.zetaObserverKeeper.VoteOnInboundBallot(
ctx,
tmpCtx,
msg.SenderChainId,
msg.ReceiverChain,
msg.CoinType,
Expand All @@ -77,9 +79,13 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
// This check prevents double spending
if isNew {
if k.IsFinalizedInbound(ctx, msg.InTxHash, msg.SenderChainId, msg.EventIndex) {
return nil, errorsmod.Wrap(types.ErrObservedTxAlreadyFinalized, fmt.Sprintf("InTxHash:%s, SenderChainID:%d, EventIndex:%d", msg.InTxHash, msg.SenderChainId, msg.EventIndex))
return nil, errorsmod.Wrap(
types.ErrObservedTxAlreadyFinalized,
fmt.Sprintf("InTxHash:%s, SenderChainID:%d, EventIndex:%d", msg.InTxHash, msg.SenderChainId, msg.EventIndex),
)
}
}
commit()

// If the ballot is not finalized return nil here to add vote to commit state
if !finalized {
Expand Down Expand Up @@ -181,7 +187,7 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
}

// Receiver is not ZetaChain: Cross Chain SWAP
tmpCtx, commit := ctx.CacheContext()
tmpCtx, commit = ctx.CacheContext()
err = func() error {
err := k.PayGasAndUpdateCctx(
tmpCtx,
Expand Down
2 changes: 2 additions & 0 deletions x/crosschain/keeper/msg_server_vote_inbound_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func TestNoDoubleEventProtections(t *testing.T) {
ballot, found := zk.ObserverKeeper.GetBallot(ctx, msg.Digest())
require.True(t, found)
require.Equal(t, ballot.BallotStatus, observerTypes.BallotStatus_BallotFinalized_SuccessObservation)

//Perform the SAME event. Except, this time, we resubmit the event.
msg2 := &types.MsgVoteOnObservedInboundTx{
Creator: validatorAddr,
Expand All @@ -145,6 +146,7 @@ func TestNoDoubleEventProtections(t *testing.T) {
ctx,
msg2,
)
require.Error(t, err)
require.ErrorIs(t, err, types.ErrObservedTxAlreadyFinalized)
_, found = zk.ObserverKeeper.GetBallot(ctx, msg2.Digest())
require.False(t, found)
Expand Down
4 changes: 2 additions & 2 deletions x/observer/keeper/vote_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func (k Keeper) VoteOnInboundBallot(
// adds a vote and sets the ballot
ballot, err = k.AddVoteToBallot(ctx, ballot, voter, types.VoteType_SuccessObservation)
if err != nil {
return false, false, err
return false, isNew, err
}

// checks if the ballot is finalized
_, isFinalized := k.CheckIfFinalizingVote(ctx, ballot)
return isFinalized, false, nil
return isFinalized, isNew, nil
}

0 comments on commit 76e2198

Please sign in to comment.