Skip to content

Commit 10fcb68

Browse files
committed
genesis service event bindings
1 parent 3220aab commit 10fcb68

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

runtime/v2/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func DefaultServiceBindings() depinject.Config {
243243
}
244244
headerService header.Service = services.NewGenesisHeaderService(stf.HeaderService{})
245245
cometService comet.Service = &services.ContextAwareCometInfoService{}
246-
eventService = stf.NewEventService()
246+
eventService event.Service = services.NewGenesisEventService(stf.NewEventService())
247247
)
248248
return depinject.Supply(
249249
kvServiceFactory,

runtime/v2/services/genesis.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66

7+
"cosmossdk.io/core/event"
78
"cosmossdk.io/core/header"
89
"cosmossdk.io/core/store"
10+
"cosmossdk.io/core/transaction"
911
)
1012

1113
var (
@@ -105,6 +107,18 @@ func (g *GenesisKVStoreService) OpenKVStore(ctx context.Context) store.KVStore {
105107
return readonlyKVStore{state}
106108
}
107109

110+
type readonlyKVStore struct {
111+
store.Reader
112+
}
113+
114+
func (r readonlyKVStore) Set(key, value []byte) error {
115+
panic("tried to call Set on a readonly store")
116+
}
117+
118+
func (r readonlyKVStore) Delete(key []byte) error {
119+
panic("tried to call Delete on a readonly store")
120+
}
121+
108122
// GenesisHeaderService is a header.Service implementation that is used during
109123
// genesis initialization. It wraps an inner execution context header.Service.
110124
type GenesisHeaderService struct {
@@ -123,20 +137,46 @@ func (g *GenesisHeaderService) HeaderInfo(ctx context.Context) header.Info {
123137

124138
// NewGenesisHeaderService creates a new GenesisHeaderService.
125139
// - executionService is the header.Service to use when the genesis context is not active.
126-
func NewGenesisHeaderService(executionService header.Service) *GenesisHeaderService {
140+
func NewGenesisHeaderService(executionService header.Service) header.Service {
127141
return &GenesisHeaderService{
128142
executionService: executionService,
129143
}
130144
}
131145

132-
type readonlyKVStore struct {
133-
store.Reader
146+
// GenesisEventService is an event.Service implementation that is used during
147+
// genesis initialization. It wraps an inner execution context event.Service.
148+
// During genesis initialization, it returns a blackHoleEventManager into which
149+
// events enter and disappear completely.
150+
type GenesisEventService struct {
151+
executionService event.Service
134152
}
135153

136-
func (r readonlyKVStore) Set(key, value []byte) error {
137-
panic("tried to call Set on a readonly store")
154+
// NewGenesisEventService creates a new GenesisEventService.
155+
// - executionService is the event.Service to use when the genesis context is not active.
156+
func NewGenesisEventService(executionService event.Service) event.Service {
157+
return &GenesisEventService{
158+
executionService: executionService,
159+
}
138160
}
139161

140-
func (r readonlyKVStore) Delete(key []byte) error {
141-
panic("tried to call Delete on a readonly store")
162+
func (g *GenesisEventService) EventManager(ctx context.Context) event.Manager {
163+
v := ctx.Value(genesisContextKey)
164+
if v == nil {
165+
return g.executionService.EventManager(ctx)
166+
}
167+
return &blackHoleEventManager{}
168+
}
169+
170+
var _ event.Manager = (*blackHoleEventManager)(nil)
171+
172+
// blackHoleEventManager is an event.Manager that does nothing.
173+
// It is used during genesis initialization, genesis events are not emitted.
174+
type blackHoleEventManager struct{}
175+
176+
func (b *blackHoleEventManager) Emit(_ transaction.Msg) error {
177+
return nil
178+
}
179+
180+
func (b *blackHoleEventManager) EmitKV(_ string, _ ...event.Attribute) error {
181+
return nil
142182
}

0 commit comments

Comments
 (0)