feat: add event core api to runtime (#15547)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
This commit is contained in:
Marko
2023-03-30 14:16:31 +00:00
committed by GitHub
co-authored by Facundo Medica
parent 51f3e70a12
commit 9a09f8656b
7 changed files with 83 additions and 4 deletions
+59
View File
@@ -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)
}
+6
View File
@@ -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{}
}