diff --git a/types/context.go b/types/context.go index e3604df954ba..da69c129cff1 100644 --- a/types/context.go +++ b/types/context.go @@ -262,11 +262,18 @@ func (c Context) TransientStore(key 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 } // ContextKey defines a type alias for a stdlib Context key. diff --git a/types/context_test.go b/types/context_test.go index 9533a52ee2e3..2fb1025ee200 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 8749fc7f685e..4d5ec074d46b 100644 --- a/x/gov/abci.go +++ b/x/gov/abci.go @@ -77,12 +77,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 {