Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: backport #13063 from cosmos/cosmos-sdk #323

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ 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))

write()

s.Require().Equal(v2, store.Get(k2))
s.Require().Len(ctx.EventManager().Events(), 2)
}

func (s *contextTestSuite) TestLogContext() {
Expand Down
6 changes: 0 additions & 6 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down