-
Notifications
You must be signed in to change notification settings - Fork 4.1k
refactor(runtime/v2): genesis event service bindings #22192
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,13 @@ package services | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "cosmossdk.io/core/event" | ||
| "cosmossdk.io/core/header" | ||
| "cosmossdk.io/core/store" | ||
| "cosmossdk.io/core/transaction" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -105,6 +108,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 { | ||
| return errors.New("tried to call Set on a readonly store") | ||
| } | ||
|
|
||
| func (r readonlyKVStore) Delete(key []byte) error { | ||
| return errors.New("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 +138,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 | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using an explicit field instead of embedding the interface
Embedding the
store.Readerinterface directly in thereadonlyKVStorestruct can lead to unintended method implementations. It's generally better to use an explicit field for better control and clarity.Consider modifying the struct as follows:
This change would require updating the methods to use
r.readerinstead of directly calling the methods.