diff --git a/CHANGELOG.md b/CHANGELOG.md index 988728938ed1..809e8038448d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (mempool) [#14484](https://github.com/cosmos/cosmos-sdk/pull/14484) Add priority nonce mempool option for transaction replacement. * (x/bank) [#14894](https://github.com/cosmos/cosmos-sdk/pull/14894) Return a human readable denomination for IBC vouchers when querying bank balances. Added a `ResolveDenom` parameter to `types.QueryAllBalancesRequest` and `--resolve-denom` flag to `GetBalancesCmd()`. * (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) Allow runtime to pass event core api service to modules ### Improvements diff --git a/runtime/events.go b/runtime/events.go new file mode 100644 index 000000000000..4139ede7b7d6 --- /dev/null +++ b/runtime/events.go @@ -0,0 +1,59 @@ +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 { + return e.EventManagerI.EmitTypedEvent(event) +} + +// 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 { + return e.EventManagerI.EmitTypedEvent(event) +} diff --git a/runtime/module.go b/runtime/module.go index 30556ba050a6..19751195fe7a 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -13,6 +13,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 +64,7 @@ func init() { ProvideKVStoreService, ProvideMemoryStoreService, ProvideTransientStoreService, + ProvideEventService, ), appmodule.Invoke(SetupAppBuilder), ) @@ -202,3 +204,7 @@ func ProvideTransientStoreService(key depinject.ModuleKey, app *AppBuilder) stor storeKey := ProvideTransientStoreKey(key, app) return transientStoreService{key: storeKey} } + +func ProvideEventService() event.Service { + return EventService{} +} diff --git a/simapp/app.go b/simapp/app.go index cbbebb652e23..526f2f26f400 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.Params) // add keepers diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index 4e5c8e30b670..f1a8c6203531 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "context" "cosmossdk.io/collections" + "cosmossdk.io/core/event" storetypes "cosmossdk.io/core/store" "cosmossdk.io/errors" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" @@ -20,16 +21,18 @@ var StoreKey = "Consensus" type Keeper struct { storeService storetypes.KVStoreService + event event.Service authority string Params collections.Item[cmtproto.ConsensusParams] } -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 { sb := collections.NewSchemaBuilder(storeService) return Keeper{ storeService: storeService, authority: authority, + event: em, Params: collections.NewItem(sb, collections.NewPrefix("Consensus"), "params", codec.CollValue[cmtproto.ConsensusParams](cdc)), } } @@ -70,5 +73,13 @@ func (k Keeper) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (* return nil, err } + if err := k.event.EventManager(ctx).EmitKV( + ctx, + "update_consensus_params", + event.Attribute{Key: "authority", Value: req.Authority}, + event.Attribute{Key: "parameters", Value: consensusParams.String()}); err != nil { + return nil, err + } + return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/consensus/keeper/keeper_test.go b/x/consensus/keeper/keeper_test.go index 7e8e2c4c4143..c893f6c0836f 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/module.go b/x/consensus/module.go index 1e2f5dec4211..2ecef0e90279 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.Params)