From 10fcb68b437eb45d752b9b739964ae701cd9ce60 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 9 Oct 2024 08:53:22 -0500 Subject: [PATCH 1/3] genesis service event bindings --- runtime/v2/module.go | 2 +- runtime/v2/services/genesis.go | 54 +++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/runtime/v2/module.go b/runtime/v2/module.go index 11cd7037fff3..91b3812b79ea 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -243,7 +243,7 @@ func DefaultServiceBindings() depinject.Config { } headerService header.Service = services.NewGenesisHeaderService(stf.HeaderService{}) cometService comet.Service = &services.ContextAwareCometInfoService{} - eventService = stf.NewEventService() + eventService event.Service = services.NewGenesisEventService(stf.NewEventService()) ) return depinject.Supply( kvServiceFactory, diff --git a/runtime/v2/services/genesis.go b/runtime/v2/services/genesis.go index 5d09e5c78f74..81572a7b0cb0 100644 --- a/runtime/v2/services/genesis.go +++ b/runtime/v2/services/genesis.go @@ -4,8 +4,10 @@ import ( "context" "fmt" + "cosmossdk.io/core/event" "cosmossdk.io/core/header" "cosmossdk.io/core/store" + "cosmossdk.io/core/transaction" ) var ( @@ -105,6 +107,18 @@ func (g *GenesisKVStoreService) OpenKVStore(ctx context.Context) store.KVStore { return readonlyKVStore{state} } +type readonlyKVStore struct { + store.Reader +} + +func (r readonlyKVStore) Set(key, value []byte) error { + panic("tried to call Set on a readonly store") +} + +func (r readonlyKVStore) Delete(key []byte) error { + panic("tried to call Delete on a readonly store") +} + // GenesisHeaderService is a header.Service implementation that is used during // genesis initialization. It wraps an inner execution context header.Service. type GenesisHeaderService struct { @@ -123,20 +137,46 @@ func (g *GenesisHeaderService) HeaderInfo(ctx context.Context) header.Info { // NewGenesisHeaderService creates a new GenesisHeaderService. // - executionService is the header.Service to use when the genesis context is not active. -func NewGenesisHeaderService(executionService header.Service) *GenesisHeaderService { +func NewGenesisHeaderService(executionService header.Service) header.Service { return &GenesisHeaderService{ executionService: executionService, } } -type readonlyKVStore struct { - store.Reader +// GenesisEventService is an event.Service implementation that is used during +// genesis initialization. It wraps an inner execution context event.Service. +// During genesis initialization, it returns a blackHoleEventManager into which +// events enter and disappear completely. +type GenesisEventService struct { + executionService event.Service } -func (r readonlyKVStore) Set(key, value []byte) error { - panic("tried to call Set on a readonly store") +// NewGenesisEventService creates a new GenesisEventService. +// - executionService is the event.Service to use when the genesis context is not active. +func NewGenesisEventService(executionService event.Service) event.Service { + return &GenesisEventService{ + executionService: executionService, + } } -func (r readonlyKVStore) Delete(key []byte) error { - panic("tried to call Delete on a readonly store") +func (g *GenesisEventService) EventManager(ctx context.Context) event.Manager { + v := ctx.Value(genesisContextKey) + if v == nil { + return g.executionService.EventManager(ctx) + } + return &blackHoleEventManager{} +} + +var _ event.Manager = (*blackHoleEventManager)(nil) + +// blackHoleEventManager is an event.Manager that does nothing. +// It is used during genesis initialization, genesis events are not emitted. +type blackHoleEventManager struct{} + +func (b *blackHoleEventManager) Emit(_ transaction.Msg) error { + return nil +} + +func (b *blackHoleEventManager) EmitKV(_ string, _ ...event.Attribute) error { + return nil } From 40f53d9ed3255f28b21c66a7c315ae5329aa18e8 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 9 Oct 2024 10:13:54 -0500 Subject: [PATCH 2/3] clean up --- runtime/v2/services/genesis.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/v2/services/genesis.go b/runtime/v2/services/genesis.go index 81572a7b0cb0..7e748f562b5c 100644 --- a/runtime/v2/services/genesis.go +++ b/runtime/v2/services/genesis.go @@ -2,6 +2,7 @@ package services import ( "context" + "errors" "fmt" "cosmossdk.io/core/event" @@ -112,11 +113,11 @@ type readonlyKVStore struct { } func (r readonlyKVStore) Set(key, value []byte) error { - panic("tried to call Set on a readonly store") + return errors.New("tried to call Set on a readonly store") } func (r readonlyKVStore) Delete(key []byte) error { - panic("tried to call Delete on a readonly store") + return errors.New("tried to call Delete on a readonly store") } // GenesisHeaderService is a header.Service implementation that is used during From 149b0a7aac352b054cb5ee2c57cfb801aa19fdb5 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 9 Oct 2024 10:14:35 -0500 Subject: [PATCH 3/3] lint fix --- runtime/v2/module.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/v2/module.go b/runtime/v2/module.go index 91b3812b79ea..9637871c6aba 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -241,9 +241,9 @@ func DefaultServiceBindings() depinject.Config { stf.NewKVStoreService(actor), ) } - headerService header.Service = services.NewGenesisHeaderService(stf.HeaderService{}) - cometService comet.Service = &services.ContextAwareCometInfoService{} - eventService event.Service = services.NewGenesisEventService(stf.NewEventService()) + cometService comet.Service = &services.ContextAwareCometInfoService{} + headerService = services.NewGenesisHeaderService(stf.HeaderService{}) + eventService = services.NewGenesisEventService(stf.NewEventService()) ) return depinject.Supply( kvServiceFactory,