From ce684e2473d57e5de686d07955e5c7178d98a385 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sat, 25 Mar 2023 12:32:28 +0100 Subject: [PATCH 1/8] add event core api to runtime --- runtime/events.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 runtime/events.go diff --git a/runtime/events.go b/runtime/events.go new file mode 100644 index 000000000000..abd8eb548428 --- /dev/null +++ b/runtime/events.go @@ -0,0 +1,62 @@ +package runtime + +import ( + "context" + + "cosmossdk.io/core/event" + "google.golang.org/protobuf/runtime/protoiface" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var _ event.Service = (*EventService)(nil) + +type EventService struct { + Events +} + +func (es EventService) EventManager(ctx context.Context) event.Manager { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return &Events{sdkCtx.EventManager()} +} + +var _ event.Manager = (*Events)(nil) + +type Events struct { + sdk.EventManagerI +} + +func NewEventManager(ctx context.Context) event.Manager { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return &Events{sdkCtx.EventManager()} +} + +// Emit emits an typed event that is defined in the protobuf file. +// In the future these events will be added to consensus +func (e Events) Emit(ctx context.Context, event protoiface.MessageV1) error { + e.EventManagerI.EmitTypedEvent(event) + return nil +} + +// EmitKV emits a key value pair event +func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Attribute) error { + attributes := make([]sdk.Attribute, 0, len(attrs)) + + for _, attr := range attrs { + attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value)) + } + + events := sdk.Events{ + sdk.NewEvent(eventType, attributes...), + } + e.EventManagerI.EmitEvents(events) + return nil +} + +// Emit emits an typed event that is defined in the protobuf file. +// In the future these events will be added to consensus +func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error { + e.EventManagerI.EmitTypedEvent(event) + + return nil +} From 4f7eb651059c47b8fc4000742d9571686e3731d3 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sun, 26 Mar 2023 22:13:35 +0200 Subject: [PATCH 2/8] fix lint and changelog --- CHANGELOG.md | 1 + runtime/events.go | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6fec52e63c8..f722f39836b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/groups) [#14879](https://github.com/cosmos/cosmos-sdk/pull/14879) Add `Query/Groups` query to get all the groups. * (x/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user defined initial block height * (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits +* (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Add eventService ### Improvements diff --git a/runtime/events.go b/runtime/events.go index abd8eb548428..c136e166ac51 100644 --- a/runtime/events.go +++ b/runtime/events.go @@ -34,8 +34,7 @@ func NewEventManager(ctx context.Context) event.Manager { // Emit emits an typed event that is defined in the protobuf file. // In the future these events will be added to consensus func (e Events) Emit(ctx context.Context, event protoiface.MessageV1) error { - e.EventManagerI.EmitTypedEvent(event) - return nil + return e.EventManagerI.EmitTypedEvent(event) } // EmitKV emits a key value pair event @@ -56,7 +55,6 @@ func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Att // Emit emits an typed event that is defined in the protobuf file. // In the future these events will be added to consensus func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error { - e.EventManagerI.EmitTypedEvent(event) - return nil + return e.EventManagerI.EmitTypedEvent(event) } From 1a9794b09bb8ea09ee2167496b2e5f7b1cd7d57d Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 28 Mar 2023 17:51:43 +0200 Subject: [PATCH 3/8] gofumpt --- client/v2/autocli/common_test.go | 3 ++- client/v2/autocli/query_test.go | 1 - runtime/events.go | 1 - x/feegrant/msgs.go | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/client/v2/autocli/common_test.go b/client/v2/autocli/common_test.go index a625d3068e57..98730c3f998a 100644 --- a/client/v2/autocli/common_test.go +++ b/client/v2/autocli/common_test.go @@ -49,7 +49,8 @@ func testExecCommon(t *testing.T, buildModuleCommand func(string, *Builder) (*co Builder: flag.Builder{ GetClientConn: func() (grpc.ClientConnInterface, error) { return conn, nil - }}, + }, + }, GetClientConn: func(*cobra.Command) (grpc.ClientConnInterface, error) { return conn, nil }, diff --git a/client/v2/autocli/query_test.go b/client/v2/autocli/query_test.go index d514a3899a03..d1dc06d90cea 100644 --- a/client/v2/autocli/query_test.go +++ b/client/v2/autocli/query_test.go @@ -159,7 +159,6 @@ func TestJSONParsing(t *testing.T) { "-u", "27", // shorthand ) assert.DeepEqual(t, conn.lastRequest, conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform()) - } func TestOptions(t *testing.T) { diff --git a/runtime/events.go b/runtime/events.go index c136e166ac51..4139ede7b7d6 100644 --- a/runtime/events.go +++ b/runtime/events.go @@ -55,6 +55,5 @@ func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Att // Emit emits an typed event that is defined in the protobuf file. // In the future these events will be added to consensus func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error { - return e.EventManagerI.EmitTypedEvent(event) } diff --git a/x/feegrant/msgs.go b/x/feegrant/msgs.go index c3120b587a99..596c633acabb 100644 --- a/x/feegrant/msgs.go +++ b/x/feegrant/msgs.go @@ -81,7 +81,6 @@ func NewMsgRevokeAllowance(granter sdk.AccAddress, grantee sdk.AccAddress) MsgRe // ValidateBasic implements the sdk.Msg interface. func (msg MsgRevokeAllowance) ValidateBasic() error { - return nil } From 4b7af606448a154bca2d2dc1cd183f726a182912 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 28 Mar 2023 18:03:14 +0200 Subject: [PATCH 4/8] i think --- runtime/module.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/runtime/module.go b/runtime/module.go index 30556ba050a6..b230e95ce4a3 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -1,6 +1,7 @@ package runtime import ( + "context" "fmt" "os" @@ -13,6 +14,7 @@ import ( runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1" appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" "cosmossdk.io/depinject" storetypes "cosmossdk.io/store/types" @@ -63,6 +65,7 @@ func init() { ProvideKVStoreService, ProvideMemoryStoreService, ProvideTransientStoreService, + ProvideEventService, ), appmodule.Invoke(SetupAppBuilder), ) @@ -202,3 +205,7 @@ func ProvideTransientStoreService(key depinject.ModuleKey, app *AppBuilder) stor storeKey := ProvideTransientStoreKey(key, app) return transientStoreService{key: storeKey} } + +func ProvideEventService(ctx context.Context, app *AppBuilder) event.Service { + return EventService{} +} From de0b9329e76223cb49ba007a7145c5652acc5318 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 28 Mar 2023 18:07:34 +0200 Subject: [PATCH 5/8] add changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04ee7c987db8..616646d3f94f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/groups) [#14879](https://github.com/cosmos/cosmos-sdk/pull/14879) Add `Query/Groups` query to get all the groups. * (x/genutil,cli) [#15147](https://github.com/cosmos/cosmos-sdk/pull/15147) Add `--initial-height` flag to cli init cmd to provide `genesis.json` with user defined initial block height * (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits -* (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Add eventService +* (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Allow runtime to pass event core api service to modules ### Improvements From efb5a097b150bdd556ee8252aefea5d087754713 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 28 Mar 2023 18:46:48 +0200 Subject: [PATCH 6/8] attempt --- runtime/module.go | 3 +-- simapp/app.go | 2 +- x/consensus/keeper/keeper.go | 5 ++++- x/consensus/keeper/keeper_test.go | 2 +- x/consensus/keeper/msg_server.go | 11 ++++++++++- x/consensus/module.go | 4 +++- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/runtime/module.go b/runtime/module.go index b230e95ce4a3..19751195fe7a 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -1,7 +1,6 @@ package runtime import ( - "context" "fmt" "os" @@ -206,6 +205,6 @@ func ProvideTransientStoreService(key depinject.ModuleKey, app *AppBuilder) stor return transientStoreService{key: storeKey} } -func ProvideEventService(ctx context.Context, app *AppBuilder) event.Service { +func ProvideEventService() event.Service { return EventService{} } diff --git a/simapp/app.go b/simapp/app.go index f4734dc4e957..83f6eb6fd89b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -285,7 +285,7 @@ func NewSimApp( app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]) // set the BaseApp's parameter store - app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String(), runtime.EventService{}) bApp.SetParamStore(&app.ConsensusParamsKeeper) // add keepers diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index d2e9efef6fd3..21f652309795 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -5,6 +5,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + "cosmossdk.io/core/event" storetypes "cosmossdk.io/core/store" "github.com/cosmos/cosmos-sdk/codec" @@ -17,15 +18,17 @@ var _ exported.ConsensusParamSetter = (*Keeper)(nil) type Keeper struct { storeService storetypes.KVStoreService cdc codec.BinaryCodec + event event.Service authority string } -func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string, em event.Service) Keeper { return Keeper{ storeService: storeService, cdc: cdc, authority: authority, + event: em, } } diff --git a/x/consensus/keeper/keeper_test.go b/x/consensus/keeper/keeper_test.go index 2ac0108c3581..973ad94bbf72 100644 --- a/x/consensus/keeper/keeper_test.go +++ b/x/consensus/keeper/keeper_test.go @@ -35,7 +35,7 @@ func (s *KeeperTestSuite) SetupTest() { encCfg := moduletestutil.MakeTestEncodingConfig() storeService := runtime.NewKVStoreService(key) - keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, storeService, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, storeService, authtypes.NewModuleAddress(govtypes.ModuleName).String(), runtime.EventService{}) s.ctx = ctx s.consensusParamsKeeper = &keeper diff --git a/x/consensus/keeper/msg_server.go b/x/consensus/keeper/msg_server.go index ed43cfdf29e5..73de9be57fa4 100644 --- a/x/consensus/keeper/msg_server.go +++ b/x/consensus/keeper/msg_server.go @@ -5,6 +5,7 @@ import ( cmttypes "github.com/cometbft/cometbft/types" + "cosmossdk.io/core/event" "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" @@ -36,7 +37,15 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam return nil, err } - k.Set(ctx, &consensusParams) + if err := k.Set(ctx, &consensusParams); err != nil { + return nil, err + } + + k.event.EventManager(goCtx).EmitKV( + goCtx, + "update_consensus_params", + event.Attribute{Key: "authority", Value: req.Authority}, + event.Attribute{Key: "parameters", Value: consensusParams.String()}) return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/consensus/module.go b/x/consensus/module.go index 8c6f87346daa..26720a3a7196 100644 --- a/x/consensus/module.go +++ b/x/consensus/module.go @@ -6,6 +6,7 @@ import ( modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" "cosmossdk.io/depinject" abci "github.com/cometbft/cometbft/abci/types" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -147,6 +148,7 @@ type ConsensusInputs struct { Config *modulev1.Module Cdc codec.Codec StoreService storetypes.KVStoreService + EventManager event.Service } //nolint:revive @@ -165,7 +167,7 @@ func ProvideModule(in ConsensusInputs) ConsensusOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } - k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String()) + k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String(), in.EventManager) m := NewAppModule(in.Cdc, k) baseappOpt := func(app *baseapp.BaseApp) { app.SetParamStore(&k) From 14dd58f66ae3694c25114c21eada27fb74858f6b Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 29 Mar 2023 11:42:43 +0200 Subject: [PATCH 7/8] fix error handling --- x/consensus/keeper/msg_server.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/consensus/keeper/msg_server.go b/x/consensus/keeper/msg_server.go index 73de9be57fa4..72063d0f1f24 100644 --- a/x/consensus/keeper/msg_server.go +++ b/x/consensus/keeper/msg_server.go @@ -41,11 +41,13 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam return nil, err } - k.event.EventManager(goCtx).EmitKV( + if err := k.event.EventManager(goCtx).EmitKV( goCtx, "update_consensus_params", event.Attribute{Key: "authority", Value: req.Authority}, - event.Attribute{Key: "parameters", Value: consensusParams.String()}) + event.Attribute{Key: "parameters", Value: consensusParams.String()}); err != nil { + return nil, err + } return &types.MsgUpdateParamsResponse{}, nil } From f61d32a1d9617c3e827728341b236734f3ff3859 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Wed, 29 Mar 2023 11:46:51 +0200 Subject: [PATCH 8/8] fix old build --- simapp/app.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 8caea53a591c..97cdadbf8c7a 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -285,16 +285,8 @@ func NewSimApp( app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey]) // set the BaseApp's parameter store -<<<<<<< HEAD app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String(), runtime.EventService{}) - bApp.SetParamStore(&app.ConsensusParamsKeeper) -||||||| d8a6dcc647 - app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String()) - bApp.SetParamStore(&app.ConsensusParamsKeeper) -======= - app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String()) bApp.SetParamStore(app.ConsensusParamsKeeper.Params) ->>>>>>> main // add keepers app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String())