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) Add eventService

### Improvements

Expand Down
60 changes: 60 additions & 0 deletions runtime/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)
}