From c099ac1cc608fc489d4761278451ccdb5a241657 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 26 Aug 2022 08:19:56 -1000 Subject: [PATCH 01/11] updates --- types/context.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/types/context.go b/types/context.go index d4bca9cec645..9109c468f96e 100644 --- a/types/context.go +++ b/types/context.go @@ -275,6 +275,21 @@ func (c Context) CacheContext() (cc Context, writeCache func()) { return cc, cms.Write } +// CacheContextWriteEvents behaves the same as CacheContext except that it +// automatically emits the events from the cached context when the caller +// executes the writeCache function. +func (c Context) CacheContextWriteEvents() (cc Context, writeCache func()) { + cms := c.MultiStore().CacheMultiStore() + cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) + + writeCache = func() { + c.EventManager().EmitEvents(cc.EventManager().Events()) + cms.Write() + } + + return cc, writeCache +} + var _ context.Context = Context{} // ContextKey defines a type alias for a stdlib Context key. From 7a0a78756d0ad791027a60872c9c3cab8b4ee757 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 26 Aug 2022 08:27:39 -1000 Subject: [PATCH 02/11] updates --- types/context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index 9109c468f96e..0a39e427a987 100644 --- a/types/context.go +++ b/types/context.go @@ -275,10 +275,10 @@ func (c Context) CacheContext() (cc Context, writeCache func()) { return cc, cms.Write } -// CacheContextWriteEvents behaves the same as CacheContext except that it +// CacheContextEmitEvents behaves the same as CacheContext except that it // automatically emits the events from the cached context when the caller // executes the writeCache function. -func (c Context) CacheContextWriteEvents() (cc Context, writeCache func()) { +func (c Context) CacheContextEmitEvents() (cc Context, writeCache func()) { cms := c.MultiStore().CacheMultiStore() cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) From ee5bb033f42a3e5beccd11e14299390eacbaeeb1 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Fri, 26 Aug 2022 08:28:53 -1000 Subject: [PATCH 03/11] updates --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 069a91f883ce..8d5a13f825c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features -* (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assests via authz MsgSend grant. +* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Implement `Context#CacheContextEmitEvents` which automatically emits all events emitted by the cached context when writing state. +* (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant. * (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins. * (testutil) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Add generic `testutil.RandSliceElem` function which selects a random element from the list. * (x/authz) [#13047](https://github.com/cosmos/cosmos-sdk/pull/13047) Add a GetAuthorization function to the keeper. From ca9cd1d62cfc0c96ca20c8c9a6625ea351dfc137 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 31 Aug 2022 11:25:52 -1000 Subject: [PATCH 04/11] updates --- types/context.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/types/context.go b/types/context.go index 0a39e427a987..6eea3fb3d72f 100644 --- a/types/context.go +++ b/types/context.go @@ -268,19 +268,11 @@ 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 -} - -// CacheContextEmitEvents behaves the same as CacheContext except that it -// automatically emits the events from the cached context when the caller -// executes the writeCache function. -func (c Context) CacheContextEmitEvents() (cc Context, writeCache func()) { - cms := c.MultiStore().CacheMultiStore() - cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) writeCache = func() { c.EventManager().EmitEvents(cc.EventManager().Events()) @@ -290,6 +282,15 @@ func (c Context) CacheContextEmitEvents() (cc Context, writeCache func()) { return cc, writeCache } +// CacheContextWithoutEmitEvents behaves the same as CacheContext except that it +// does not automatically emit events from the cached context when the caller +// executes the writeCache function. +func (c Context) CacheContextWithoutEmitEvents() (cc Context, writeCache func()) { + cms := c.MultiStore().CacheMultiStore() + cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) + return cc, cms.Write +} + var _ context.Context = Context{} // ContextKey defines a type alias for a stdlib Context key. From 2ce390c923932c3fc5d1976542bea0d785fa74b0 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 31 Aug 2022 11:27:08 -1000 Subject: [PATCH 05/11] updates --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de42988d1a7c..6845d5414306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features -* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Implement `Context#CacheContextEmitEvents` which automatically emits all events emitted by the cached context when writing state. +* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Update `Context#CacheContext` to automatically emit all events on the parent context's `EventManager`. Callers that desire the old behavior should call `CacheContextWithoutEmitEvents` instead. * (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant. * (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins. * (testutil) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Add generic `testutil.RandSliceElem` function which selects a random element from the list. From 20e8cd2ddb2ed225fd24e1bfbc06326d3878e4c8 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 31 Aug 2022 11:27:36 -1000 Subject: [PATCH 06/11] updates --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6845d5414306..924fb8f23ba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features -* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Update `Context#CacheContext` to automatically emit all events on the parent context's `EventManager`. Callers that desire the old behavior should call `CacheContextWithoutEmitEvents` instead. * (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant. * (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins. * (testutil) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Add generic `testutil.RandSliceElem` function which selects a random element from the list. @@ -93,6 +92,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### 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`. Callers that desire the old behavior should call `CacheContextWithoutEmitEvents` instead. * (x/bank) [#12706](https://github.com/cosmos/cosmos-sdk/pull/12706) Removed the `testutil` package from the `x/bank/client` package. * (simapp) [#12747](https://github.com/cosmos/cosmos-sdk/pull/12747) Remove `simapp.MakeTestEncodingConfig`. Please use `moduletestutil.MakeTestEncodingConfig` (`types/module/testutil`) in tests instead. * (x/bank) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) `NewSendAuthorization` takes a new argument of an optional list of addresses allowed to receive bank assests via authz MsgSend grant. You can pass `nil` for the same behavior as before, i.e. any recipient is allowed. From 8a2e08775c72fd48bbf2509decc3397a8e39edfa Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 31 Aug 2022 20:18:54 -1000 Subject: [PATCH 07/11] updates --- x/gov/abci.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/x/gov/abci.go b/x/gov/abci.go index a4edadc6888e..f45cc68c3ff0 100644 --- a/x/gov/abci.go +++ b/x/gov/abci.go @@ -85,12 +85,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 { From a0c1faf318d562cc8983719a2bc6a88f7fe13504 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 1 Sep 2022 07:51:14 -1000 Subject: [PATCH 08/11] updates --- x/group/keeper/msg_server.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) 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()) - } } } From be6820c04b9ef684a9ecb525599d86d22c314bee Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 1 Sep 2022 07:51:58 -1000 Subject: [PATCH 09/11] updates --- CHANGELOG.md | 2 +- types/context.go | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21188f431fc6..7bee3495755c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,7 +93,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### 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`. Callers that desire the old behavior should call `CacheContextWithoutEmitEvents` instead. +* (context) [#13063](https://github.com/cosmos/cosmos-sdk/pull/13063) Update `Context#CacheContext` to automatically emit all events on the parent context's `EventManager`. * (x/bank) [#12706](https://github.com/cosmos/cosmos-sdk/pull/12706) Removed the `testutil` package from the `x/bank/client` package. * (simapp) [#12747](https://github.com/cosmos/cosmos-sdk/pull/12747) Remove `simapp.MakeTestEncodingConfig`. Please use `moduletestutil.MakeTestEncodingConfig` (`types/module/testutil`) in tests instead. * (x/bank) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) `NewSendAuthorization` takes a new argument of an optional list of addresses allowed to receive bank assests via authz MsgSend grant. You can pass `nil` for the same behavior as before, i.e. any recipient is allowed. diff --git a/types/context.go b/types/context.go index 6eea3fb3d72f..8a050a1b0803 100644 --- a/types/context.go +++ b/types/context.go @@ -282,15 +282,6 @@ func (c Context) CacheContext() (cc Context, writeCache func()) { return cc, writeCache } -// CacheContextWithoutEmitEvents behaves the same as CacheContext except that it -// does not automatically emit events from the cached context when the caller -// executes the writeCache function. -func (c Context) CacheContextWithoutEmitEvents() (cc Context, writeCache func()) { - cms := c.MultiStore().CacheMultiStore() - cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) - return cc, cms.Write -} - var _ context.Context = Context{} // ContextKey defines a type alias for a stdlib Context key. From 6744e03b13dd3c98b045e99267e940ffced66dc6 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 1 Sep 2022 10:28:38 -1000 Subject: [PATCH 10/11] updates --- types/context_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/context_test.go b/types/context_test.go index 95406f55c8a9..7423d52bf6cc 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 evens + 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() { From 2c6fd05c9a9a60a38461b9af6e21912eec920dbc Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 1 Sep 2022 10:42:45 -1000 Subject: [PATCH 11/11] updates --- types/context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/context_test.go b/types/context_test.go index 7423d52bf6cc..f5c7cadeb541 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -42,7 +42,7 @@ func (s *contextTestSuite) TestCacheContext() { s.Require().Equal(v1, cstore.Get(k1)) s.Require().Nil(cstore.Get(k2)) - // emit some evens + // emit some events cctx.EventManager().EmitEvent(types.NewEvent("foo", types.NewAttribute("key", "value"))) cctx.EventManager().EmitEvent(types.NewEvent("bar", types.NewAttribute("key", "value")))