-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(runtime/v2): bring back concurrent export genesis in v2 #21554
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
Changes from all commits
ed5f01e
958a386
f0027dc
265d869
75a5751
6606640
d4ca906
a91e7de
2964763
1dce1ee
8e0c4c9
4d1a612
5c99189
943a6fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,8 +21,10 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/core/appmodule" | ||||||||||||||||||||||||||||||||||||||||||||||
| appmodulev2 "cosmossdk.io/core/appmodule/v2" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/core/registry" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/core/store" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/core/transaction" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/log" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/runtime/v2/services" | ||||||||||||||||||||||||||||||||||||||||||||||
| "cosmossdk.io/server/v2/stf" | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -193,6 +195,7 @@ func (m *MM[T]) InitGenesisJSON( | |||||||||||||||||||||||||||||||||||||||||||||
| // ExportGenesisForModules performs export genesis functionality for modules | ||||||||||||||||||||||||||||||||||||||||||||||
| func (m *MM[T]) ExportGenesisForModules( | ||||||||||||||||||||||||||||||||||||||||||||||
| ctx context.Context, | ||||||||||||||||||||||||||||||||||||||||||||||
| stateFactory func() store.WriterMap, | ||||||||||||||||||||||||||||||||||||||||||||||
| modulesToExport ...string, | ||||||||||||||||||||||||||||||||||||||||||||||
| ) (map[string]json.RawMessage, error) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if len(modulesToExport) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -203,17 +206,19 @@ func (m *MM[T]) ExportGenesisForModules( | |||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type genesisResult struct { | ||||||||||||||||||||||||||||||||||||||||||||||
| bz json.RawMessage | ||||||||||||||||||||||||||||||||||||||||||||||
| err error | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| type ModuleI interface { | ||||||||||||||||||||||||||||||||||||||||||||||
| ExportGenesis(ctx context.Context) (json.RawMessage, error) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| genesisData := make(map[string]json.RawMessage) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: make async export genesis https://github.com/cosmos/cosmos-sdk/issues/21303 | ||||||||||||||||||||||||||||||||||||||||||||||
| channels := make(map[string]chan genesisResult) | ||||||||||||||||||||||||||||||||||||||||||||||
| for _, moduleName := range modulesToExport { | ||||||||||||||||||||||||||||||||||||||||||||||
| mod := m.modules[moduleName] | ||||||||||||||||||||||||||||||||||||||||||||||
| var moduleI ModuleI | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if module, hasGenesis := mod.(appmodulev2.HasGenesis); hasGenesis { | ||||||||||||||||||||||||||||||||||||||||||||||
| moduleI = module.(ModuleI) | ||||||||||||||||||||||||||||||||||||||||||||||
| } else if module, hasABCIGenesis := mod.(appmodulev2.HasABCIGenesis); hasABCIGenesis { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -222,12 +227,29 @@ func (m *MM[T]) ExportGenesisForModules( | |||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| res, err := moduleI.ExportGenesis(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
| channels[moduleName] = make(chan genesisResult) | ||||||||||||||||||||||||||||||||||||||||||||||
| go func(moduleI ModuleI, ch chan genesisResult) { | ||||||||||||||||||||||||||||||||||||||||||||||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
| genesisCtx := services.NewGenesisContext(stateFactory()) | ||||||||||||||||||||||||||||||||||||||||||||||
| _, _ = genesisCtx.Run(ctx, func(ctx context.Context) error { | ||||||||||||||||||||||||||||||||||||||||||||||
| jm, err := moduleI.ExportGenesis(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| ch <- genesisResult{nil, err} | ||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| ch <- genesisResult{jm, nil} | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||
| }(moduleI, channels[moduleName]) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+231
to
+242
Check noticeCode scanning / CodeQL Spawning a Go routine
Spawning a Go routine may be a possible source of non-determinism
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| genesisData := make(map[string]json.RawMessage) | ||||||||||||||||||||||||||||||||||||||||||||||
| for moduleName := range channels { | ||||||||||||||||||||||||||||||||||||||||||||||
| res := <-channels[moduleName] | ||||||||||||||||||||||||||||||||||||||||||||||
| if res.err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("genesis export error in %s: %w", moduleName, res.err) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| genesisData[moduleName] = res | ||||||||||||||||||||||||||||||||||||||||||||||
| genesisData[moduleName] = res.bz | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+245
to
+252
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a deterministic order when collecting genesis data. Collecting the exported genesis data from the channels using a range loop over the To ensure deterministic ordering, consider the following:
Here's an example of how you can modify the code to achieve deterministic ordering: genesisData := make(map[string]json.RawMessage)
-for moduleName := range channels {
+moduleNames := make([]string, 0, len(channels))
+for moduleName := range channels {
+ moduleNames = append(moduleNames, moduleName)
+}
+sort.Strings(moduleNames)
+for _, moduleName := range moduleNames {
res := <-channels[moduleName]
if res.err != nil {
return nil, fmt.Errorf("genesis export error in %s: %w", moduleName, res.err)
}
genesisData[moduleName] = res.bz
}Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return genesisData, nil | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.