Skip to content

Commit

Permalink
genesis service event bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Oct 9, 2024
1 parent 3220aab commit 4bb71dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func DefaultServiceBindings() depinject.Config {
}
headerService header.Service = services.NewGenesisHeaderService(stf.HeaderService{})
cometService comet.Service = &services.ContextAwareCometInfoService{}
eventService = stf.NewEventService()
eventService = services.NewGenesisEventService(stf.NewEventService())
)
return depinject.Supply(
kvServiceFactory,
Expand Down
46 changes: 40 additions & 6 deletions runtime/v2/services/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"

"cosmossdk.io/core/event"
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
)

var (
Expand Down Expand Up @@ -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 {
Expand All @@ -129,14 +143,34 @@ func NewGenesisHeaderService(executionService header.Service) *GenesisHeaderServ
}
}

type readonlyKVStore struct {
store.Reader
type GenesisEventService struct {
executionService event.Service
}

func (r readonlyKVStore) Set(key, value []byte) error {
panic("tried to call Set on a readonly store")
func NewGenesisEventService(executionService event.Service) *GenesisEventService {
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(eventType string, attrs ...event.Attribute) error {
return nil
}

0 comments on commit 4bb71dc

Please sign in to comment.