Skip to content

Commit 9d7028d

Browse files
authored
feat: emit cached context events (backport #13063) (#13702)
1 parent 4eed46e commit 9d7028d

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
5050
* (x/auth/tx) [#12474](https://github.com/cosmos/cosmos-sdk/pull/12474) Remove condition in GetTxsEvent that disallowed multiple equal signs, which would break event queries with base64 strings (i.e. query by signature).
5151
* (store) [#13530](https://github.com/cosmos/cosmos-sdk/pull/13530) Fix app-hash mismatch if upgrade migration commit is interrupted.
5252

53+
## API Breaking Changes
54+
55+
* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Update `Context#CacheContext` to automatically emit all events on the parent context's `EventManager`.
56+
5357
## [v0.46.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.3) - 2022-10-20
5458

5559
ATTENTION:

types/context.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,18 @@ func (c Context) TransientStore(key storetypes.StoreKey) KVStore {
268268

269269
// CacheContext returns a new Context with the multi-store cached and a new
270270
// EventManager. The cached context is written to the context when writeCache
271-
// is called.
271+
// is called. Note, events are automatically emitted on the parent context's
272+
// EventManager when the caller executes the write.
272273
func (c Context) CacheContext() (cc Context, writeCache func()) {
273274
cms := c.MultiStore().CacheMultiStore()
274275
cc = c.WithMultiStore(cms).WithEventManager(NewEventManager())
275-
return cc, cms.Write
276+
277+
writeCache = func() {
278+
c.EventManager().EmitEvents(cc.EventManager().Events())
279+
cms.Write()
280+
}
281+
282+
return cc, writeCache
276283
}
277284

278285
var _ context.Context = Context{}

types/context_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ func (s *contextTestSuite) TestCacheContext() {
4242
s.Require().Equal(v1, cstore.Get(k1))
4343
s.Require().Nil(cstore.Get(k2))
4444

45+
// emit some events
46+
cctx.EventManager().EmitEvent(types.NewEvent("foo", types.NewAttribute("key", "value")))
47+
cctx.EventManager().EmitEvent(types.NewEvent("bar", types.NewAttribute("key", "value")))
48+
4549
cstore.Set(k2, v2)
4650
s.Require().Equal(v2, cstore.Get(k2))
4751
s.Require().Nil(store.Get(k2))
4852

4953
write()
5054

5155
s.Require().Equal(v2, store.Get(k2))
56+
s.Require().Len(ctx.EventManager().Events(), 2)
5257
}
5358

5459
func (s *contextTestSuite) TestLogContext() {

x/gov/abci.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
8484
tagValue = types.AttributeValueProposalPassed
8585
logMsg = "passed"
8686

87-
// The cached context is created with a new EventManager. However, since
88-
// the proposal handler execution was successful, we want to track/keep
89-
// any events emitted, so we re-emit to "merge" the events into the
90-
// original Context's EventManager.
91-
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
92-
9387
// write state to the underlying multi-store
9488
writeCache()
9589
} else {

x/group/keeper/msg_server.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -754,19 +754,13 @@ func (k Keeper) Exec(goCtx context.Context, req *group.MsgExec) (*group.MsgExecR
754754
return nil, err
755755
}
756756

757-
results, err := k.doExecuteMsgs(cacheCtx, k.router, proposal, addr)
758-
if err != nil {
757+
if _, err := k.doExecuteMsgs(cacheCtx, k.router, proposal, addr); err != nil {
759758
proposal.ExecutorResult = group.PROPOSAL_EXECUTOR_RESULT_FAILURE
760759
logs = fmt.Sprintf("proposal execution failed on proposal %d, because of error %s", id, err.Error())
761760
k.Logger(ctx).Info("proposal execution failed", "cause", err, "proposalID", id)
762761
} else {
763762
proposal.ExecutorResult = group.PROPOSAL_EXECUTOR_RESULT_SUCCESS
764763
flush()
765-
766-
for _, res := range results {
767-
// NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context
768-
ctx.EventManager().EmitEvents(res.GetEvents())
769-
}
770764
}
771765
}
772766

0 commit comments

Comments
 (0)