From 9d7028d070f2971c4ba940512500bfe17ce855d4 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 31 Oct 2022 23:18:26 +0100 Subject: [PATCH] feat: emit cached context events (backport #13063) (#13702) --- CHANGELOG.md | 4 ++++ types/context.go | 11 +++++++++-- types/context_test.go | 5 +++++ x/gov/abci.go | 6 ------ x/group/keeper/msg_server.go | 8 +------- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2b0ba368f8..5d34c0e703d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (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). * (store) [#13530](https://github.com/cosmos/cosmos-sdk/pull/13530) Fix app-hash mismatch if upgrade migration commit is interrupted. +## API Breaking Changes + +* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Update `Context#CacheContext` to automatically emit all events on the parent context's `EventManager`. + ## [v0.46.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.3) - 2022-10-20 ATTENTION: diff --git a/types/context.go b/types/context.go index d4bca9cec645..8a050a1b0803 100644 --- a/types/context.go +++ b/types/context.go @@ -268,11 +268,18 @@ func (c Context) TransientStore(key storetypes.StoreKey) KVStore { // CacheContext returns a new Context with the multi-store cached and a new // EventManager. The cached context is written to the context when writeCache -// is called. +// is called. Note, events are automatically emitted on the parent context's +// EventManager when the caller executes the write. func (c Context) CacheContext() (cc Context, writeCache func()) { cms := c.MultiStore().CacheMultiStore() cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) - return cc, cms.Write + + writeCache = func() { + c.EventManager().EmitEvents(cc.EventManager().Events()) + cms.Write() + } + + return cc, writeCache } var _ context.Context = Context{} diff --git a/types/context_test.go b/types/context_test.go index 95406f55c8a9..f5c7cadeb541 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -42,6 +42,10 @@ func (s *contextTestSuite) TestCacheContext() { s.Require().Equal(v1, cstore.Get(k1)) s.Require().Nil(cstore.Get(k2)) + // emit some events + cctx.EventManager().EmitEvent(types.NewEvent("foo", types.NewAttribute("key", "value"))) + cctx.EventManager().EmitEvent(types.NewEvent("bar", types.NewAttribute("key", "value"))) + cstore.Set(k2, v2) s.Require().Equal(v2, cstore.Get(k2)) s.Require().Nil(store.Get(k2)) @@ -49,6 +53,7 @@ func (s *contextTestSuite) TestCacheContext() { write() s.Require().Equal(v2, store.Get(k2)) + s.Require().Len(ctx.EventManager().Events(), 2) } func (s *contextTestSuite) TestLogContext() { diff --git a/x/gov/abci.go b/x/gov/abci.go index 9f97cba47b4d..cde12c34a80c 100644 --- a/x/gov/abci.go +++ b/x/gov/abci.go @@ -84,12 +84,6 @@ func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) { tagValue = types.AttributeValueProposalPassed logMsg = "passed" - // The cached context is created with a new EventManager. However, since - // the proposal handler execution was successful, we want to track/keep - // any events emitted, so we re-emit to "merge" the events into the - // original Context's EventManager. - ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events()) - // write state to the underlying multi-store writeCache() } else { diff --git a/x/group/keeper/msg_server.go b/x/group/keeper/msg_server.go index fb1ab258901c..6f6cee539f93 100644 --- a/x/group/keeper/msg_server.go +++ b/x/group/keeper/msg_server.go @@ -754,19 +754,13 @@ func (k Keeper) Exec(goCtx context.Context, req *group.MsgExec) (*group.MsgExecR return nil, err } - results, err := k.doExecuteMsgs(cacheCtx, k.router, proposal, addr) - if err != nil { + if _, err := k.doExecuteMsgs(cacheCtx, k.router, proposal, addr); err != nil { proposal.ExecutorResult = group.PROPOSAL_EXECUTOR_RESULT_FAILURE logs = fmt.Sprintf("proposal execution failed on proposal %d, because of error %s", id, err.Error()) k.Logger(ctx).Info("proposal execution failed", "cause", err, "proposalID", id) } else { proposal.ExecutorResult = group.PROPOSAL_EXECUTOR_RESULT_SUCCESS flush() - - for _, res := range results { - // NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context - ctx.EventManager().EmitEvents(res.GetEvents()) - } } }