Skip to content

Commit 684d9bf

Browse files
mergify[bot]charleenfeidamiannolan
authored
chore: rm event emission after context caching (cosmos#2662) (cosmos#2728)
(cherry picked from commit c912fd9) Co-authored-by: Charly <charly@interchain.berlin> Co-authored-by: Damian Nolan <damiannolan@gmail.com>
1 parent 471e466 commit 684d9bf

File tree

3 files changed

+8
-29
lines changed

3 files changed

+8
-29
lines changed

modules/apps/27-interchain-accounts/host/keeper/relay.go

-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ func (k Keeper) executeTx(ctx sdk.Context, sourcePort, destPort, destChannel str
7373
txMsgData.MsgResponses[i] = any
7474
}
7575

76-
// NOTE: The context returned by CacheContext() creates a new EventManager, so events must be correctly propagated back to the current context
77-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
7876
writeCache()
7977

8078
txResponse, err := proto.Marshal(txMsgData)

modules/apps/29-fee/keeper/escrow.go

-12
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ func (k Keeper) DistributePacketFeesOnAcknowledgement(ctx sdk.Context, forwardRe
7474
k.distributePacketFeeOnAcknowledgement(cacheCtx, refundAddr, forwardAddr, reverseRelayer, packetFee)
7575
}
7676

77-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
78-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
79-
8077
// write the cache
8178
writeFn()
8279

@@ -130,9 +127,6 @@ func (k Keeper) DistributePacketFeesOnTimeout(ctx sdk.Context, timeoutRelayer sd
130127
k.distributePacketFeeOnTimeout(cacheCtx, refundAddr, timeoutRelayer, packetFee)
131128
}
132129

133-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
134-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
135-
136130
// write the cache
137131
writeFn()
138132

@@ -177,9 +171,6 @@ func (k Keeper) distributeFee(ctx sdk.Context, receiver, refundAccAddress sdk.Ac
177171

178172
// write the cache
179173
writeFn()
180-
181-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
182-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
183174
}
184175

185176
// RefundFeesOnChannelClosure will refund all fees associated with the given port and channel identifiers.
@@ -228,9 +219,6 @@ func (k Keeper) RefundFeesOnChannelClosure(ctx sdk.Context, portID, channelID st
228219
}
229220
}
230221

231-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
232-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
233-
234222
// write the cache
235223
writeFn()
236224

modules/core/keeper/msg_server.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,11 @@ func (k Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacke
393393
cacheCtx, writeFn := ctx.CacheContext()
394394
err = k.ChannelKeeper.RecvPacket(cacheCtx, cap, msg.Packet, msg.ProofCommitment, msg.ProofHeight)
395395

396-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
397-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
398-
399396
switch err {
400397
case nil:
401398
writeFn()
402399
case channeltypes.ErrNoOpMsg:
400+
// no-ops do not need event emission as they will be ignored
403401
return &channeltypes.MsgRecvPacketResponse{Result: channeltypes.NOOP}, nil
404402
default:
405403
return nil, sdkerrors.Wrap(err, "receive packet verification failed")
@@ -410,12 +408,13 @@ func (k Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacke
410408
// Cache context so that we may discard state changes from callback if the acknowledgement is unsuccessful.
411409
cacheCtx, writeFn = ctx.CacheContext()
412410
ack := cbs.OnRecvPacket(cacheCtx, msg.Packet, relayer)
413-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
414-
// Events from callback are emitted regardless of acknowledgement success
415-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
416411
if ack == nil || ack.Success() {
417412
// write application state changes for asynchronous and successful acknowledgements
418413
writeFn()
414+
} else {
415+
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
416+
// Events should still be emitted from failed acks and asynchronous acks
417+
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
419418
}
420419

421420
// Set packet acknowledgement only if the acknowledgement is not nil.
@@ -471,13 +470,11 @@ func (k Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (*c
471470
cacheCtx, writeFn := ctx.CacheContext()
472471
err = k.ChannelKeeper.TimeoutPacket(cacheCtx, msg.Packet, msg.ProofUnreceived, msg.ProofHeight, msg.NextSequenceRecv)
473472

474-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
475-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
476-
477473
switch err {
478474
case nil:
479475
writeFn()
480476
case channeltypes.ErrNoOpMsg:
477+
// no-ops do not need event emission as they will be ignored
481478
return &channeltypes.MsgTimeoutResponse{Result: channeltypes.NOOP}, nil
482479
default:
483480
return nil, sdkerrors.Wrap(err, "timeout packet verification failed")
@@ -539,13 +536,11 @@ func (k Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTimeo
539536
cacheCtx, writeFn := ctx.CacheContext()
540537
err = k.ChannelKeeper.TimeoutOnClose(cacheCtx, cap, msg.Packet, msg.ProofUnreceived, msg.ProofClose, msg.ProofHeight, msg.NextSequenceRecv)
541538

542-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
543-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
544-
545539
switch err {
546540
case nil:
547541
writeFn()
548542
case channeltypes.ErrNoOpMsg:
543+
// no-ops do not need event emission as they will be ignored
549544
return &channeltypes.MsgTimeoutOnCloseResponse{Result: channeltypes.NOOP}, nil
550545
default:
551546
return nil, sdkerrors.Wrap(err, "timeout on close packet verification failed")
@@ -610,13 +605,11 @@ func (k Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAckn
610605
cacheCtx, writeFn := ctx.CacheContext()
611606
err = k.ChannelKeeper.AcknowledgePacket(cacheCtx, cap, msg.Packet, msg.Acknowledgement, msg.ProofAcked, msg.ProofHeight)
612607

613-
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
614-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
615-
616608
switch err {
617609
case nil:
618610
writeFn()
619611
case channeltypes.ErrNoOpMsg:
612+
// no-ops do not need event emission as they will be ignored
620613
return &channeltypes.MsgAcknowledgementResponse{Result: channeltypes.NOOP}, nil
621614
default:
622615
return nil, sdkerrors.Wrap(err, "acknowledge packet verification failed")

0 commit comments

Comments
 (0)