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

feat: add event core api to runtime #15547

Merged
merged 14 commits into from
Mar 30, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) Allow runtime to pass event core api service to modules

### Improvements

Expand Down
3 changes: 2 additions & 1 deletion client/v2/autocli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
1 change: 0 additions & 1 deletion client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions runtime/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package runtime
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

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
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error {
return e.EventManagerI.EmitTypedEvent(event)
}
6 changes: 6 additions & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,6 +64,7 @@ func init() {
ProvideKVStoreService,
ProvideMemoryStoreService,
ProvideTransientStoreService,
ProvideEventService,
),
appmodule.Invoke(SetupAppBuilder),
)
Expand Down Expand Up @@ -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{}
}
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/consensus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion x/consensus/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()})
Fixed Show fixed Hide fixed

return &types.MsgUpdateParamsResponse{}, nil
}
4 changes: 3 additions & 1 deletion x/consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -147,6 +148,7 @@ type ConsensusInputs struct {
Config *modulev1.Module
Cdc codec.Codec
StoreService storetypes.KVStoreService
EventManager event.Service
}

//nolint:revive
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion x/feegrant/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down