Skip to content
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

fix: fix race conditions from #14332 #14402

Merged
merged 11 commits into from
Dec 26, 2022
11 changes: 4 additions & 7 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ test-integration:
test-integration-cov:
go test ./integration/... -timeout 30m -coverpkg=../... -coverprofile=integration-profile.out -covermode=atomic

test-e2e: test-e2e-server
go test ./e2e/... -mod=readonly -timeout 30m -tags='e2e'
test-e2e:
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e'

test-e2e-cov: test-e2e-server
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e' -coverpkg=../... -coverprofile=e2e-profile.out -covermode=atomic

test-e2e-server:
go test ./e2e/server -mod=readonly -timeout 30m -tags='e2e' -coverpkg=../... -coverprofile=e2e-server-profile.out -covermode=atomic
test-e2e-cov:
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e' -coverpkg=../... -coverprofile=e2e-profile.out -covermode=atomic
4 changes: 2 additions & 2 deletions tests/e2e/server/export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !race
// +build !race
//go:build e2e
// +build e2e

package server_test

Expand Down
20 changes: 8 additions & 12 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,6 @@ func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) map[string

// ExportGenesisForModules performs export genesis functionality for modules
func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec, modulesToExport []string) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)

if len(modulesToExport) == 0 {
modulesToExport = m.OrderExportGenesis
}
Expand All @@ -395,22 +393,20 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,
panic(err)
}

channels := make([]chan json.RawMessage, len(modulesToExport))
modulesWithGenesis := make([]string, 0, len(modulesToExport))

for i, moduleName := range modulesToExport {
channels := make(map[string]chan json.RawMessage)
for _, moduleName := range modulesToExport {
if module, ok := m.Modules[moduleName].(HasGenesis); ok {
channels[i] = make(chan json.RawMessage)
modulesWithGenesis = append(modulesWithGenesis, moduleName)

channels[moduleName] = make(chan json.RawMessage)
go func(module HasGenesis, ch chan json.RawMessage) {
ctx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) // avoid race conditions
ch <- module.ExportGenesis(ctx, cdc)
}(module, channels[i])
}(module, channels[moduleName])
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
}
}

for i, moduleName := range modulesWithGenesis {
genesisData[moduleName] = <-channels[i]
genesisData := make(map[string]json.RawMessage)
for moduleName := range channels {
genesisData[moduleName] = <-channels[moduleName]
}

return genesisData
Expand Down
4 changes: 3 additions & 1 deletion types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestManager_ExportGenesis(t *testing.T) {
require.NotNil(t, mm)
require.Equal(t, 2, len(mm.Modules))

ctx := sdk.Context{}
ctx := sdk.NewContext(nil, tmproto.Header{}, false, log.NewNopLogger())
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
mockAppModule1.EXPECT().ExportGenesis(gomock.Eq(ctx), gomock.Eq(cdc)).AnyTimes().Return(json.RawMessage(`{"key1": "value1"}`))
Expand All @@ -200,6 +200,8 @@ func TestManager_ExportGenesis(t *testing.T) {
}
require.Equal(t, want, mm.ExportGenesis(ctx, cdc))
require.Equal(t, want, mm.ExportGenesisForModules(ctx, cdc, []string{}))
require.Equal(t, map[string]json.RawMessage{"module1": json.RawMessage(`{"key1": "value1"}`)}, mm.ExportGenesisForModules(ctx, cdc, []string{"module1"}))
require.NotEqual(t, map[string]json.RawMessage{"module1": json.RawMessage(`{"key1": "value1"}`)}, mm.ExportGenesisForModules(ctx, cdc, []string{"module2"}))

require.Panics(t, func() {
mm.ExportGenesisForModules(ctx, cdc, []string{"module1", "modulefoo"})
Expand Down