From ed5f01e49fdafa91f63ee489dad15a66979d742c Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 10:17:01 +0530 Subject: [PATCH 01/11] fix: bring back concurrent export genesis in v2 --- runtime/v2/manager.go | 34 ++++++++++++++++++++------ server/v2/stf/branch/writer_map.go | 37 +++++++++++++++++++++++------ server/v2/stf/core_store_service.go | 11 +++++++-- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index ddec8015fe2..bd061462de1 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -9,6 +9,7 @@ import ( "reflect" "slices" "sort" + "sync" gogoproto "github.com/cosmos/gogoproto/proto" "google.golang.org/grpc" @@ -204,13 +205,19 @@ func (m *MM[T]) ExportGenesisForModules( return nil, err } + type genesisResult struct { + moduleName string + bz json.RawMessage + err error + } + type ModuleI interface { ExportGenesis(ctx context.Context) (json.RawMessage, error) } - genesisData := make(map[string]json.RawMessage) + var wg sync.WaitGroup + results := make(chan genesisResult, len(modulesToExport)) - // TODO: make async export genesis https://github.com/cosmos/cosmos-sdk/issues/21303 for _, moduleName := range modulesToExport { mod := m.modules[moduleName] var moduleI ModuleI @@ -223,12 +230,25 @@ func (m *MM[T]) ExportGenesisForModules( continue } - res, err := moduleI.ExportGenesis(ctx) - if err != nil { - return nil, err - } + wg.Add(1) + go func(moduleName string, moduleI ModuleI) { + defer wg.Done() + jm, err := moduleI.ExportGenesis(ctx) + results <- genesisResult{moduleName, jm, err} + }(moduleName, moduleI) + } + + go func() { + wg.Wait() + close(results) + }() - genesisData[moduleName] = res + genesisData := make(map[string]json.RawMessage) + for res := range results { + if res.err != nil { + return nil, fmt.Errorf("genesis export error in %s: %w", res.moduleName, res.err) + } + genesisData[res.moduleName] = res.bz } return genesisData, nil diff --git a/server/v2/stf/branch/writer_map.go b/server/v2/stf/branch/writer_map.go index 244b51b3e78..e5e656bafcd 100644 --- a/server/v2/stf/branch/writer_map.go +++ b/server/v2/stf/branch/writer_map.go @@ -2,6 +2,7 @@ package branch import ( "fmt" + "sync" "unsafe" "cosmossdk.io/core/store" @@ -25,6 +26,7 @@ type WriterMap struct { state store.ReaderMap branchedWriterState map[string]store.Writer branch func(state store.Reader) store.Writer + mu sync.RWMutex // mutex to protect branchedWriterState } func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { @@ -32,16 +34,37 @@ func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { } func (b WriterMap) GetWriter(actor []byte) (store.Writer, error) { - // Simplify and optimize state retrieval - if actorState, ok := b.branchedWriterState[unsafeString(actor)]; ok { + actorKey := unsafeString(actor) + + // Step 1: Attempt to read the map with a read lock + b.mu.RLock() + actorState, ok := b.branchedWriterState[actorKey] + b.mu.RUnlock() + + if ok { + // If the actorState is found, return it return actorState, nil - } else if writerState, err := b.state.GetReader(actor); err != nil { - return nil, err - } else { - actorState = b.branch(writerState) - b.branchedWriterState[string(actor)] = actorState + } + + // Step 2: If not found, proceed with acquiring a write lock to update the map + b.mu.Lock() + defer b.mu.Unlock() + + // Double-check: Ensure that the actorState wasn't created by another goroutine while waiting for the write lock + if actorState, ok = b.branchedWriterState[actorKey]; ok { return actorState, nil } + + // Step 3: If still not found, create the actorState and update the map + writerState, err := b.state.GetReader(actor) + if err != nil { + return nil, err + } + + actorState = b.branch(writerState) + b.branchedWriterState[actorKey] = actorState // This line is now protected by the mutex + + return actorState, nil } func (b WriterMap) ApplyStateChanges(stateChanges []store.StateChanges) error { diff --git a/server/v2/stf/core_store_service.go b/server/v2/stf/core_store_service.go index 9ee67ca367a..ba55301e302 100644 --- a/server/v2/stf/core_store_service.go +++ b/server/v2/stf/core_store_service.go @@ -2,6 +2,7 @@ package stf import ( "context" + "sync" "cosmossdk.io/core/store" ) @@ -9,15 +10,16 @@ import ( var _ store.KVStoreService = (*storeService)(nil) func NewKVStoreService(address []byte) store.KVStoreService { - return storeService{actor: address} + return storeService{actor: address, mu: &sync.RWMutex{}} } func NewMemoryStoreService(address []byte) store.MemoryStoreService { - return storeService{actor: address} + return storeService{actor: address, mu: &sync.RWMutex{}} } type storeService struct { actor []byte + mu *sync.RWMutex } func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { @@ -26,10 +28,15 @@ func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { panic(err) } + s.mu.Lock() + state, err := exCtx.state.GetWriter(s.actor) if err != nil { panic(err) } + + s.mu.Unlock() + return state } From 958a386bf0e03d7466e028d9009d05b04937d241 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 10:32:05 +0530 Subject: [PATCH 02/11] update godoc --- server/v2/stf/branch/writer_map.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/v2/stf/branch/writer_map.go b/server/v2/stf/branch/writer_map.go index e5e656bafcd..622d813fcaf 100644 --- a/server/v2/stf/branch/writer_map.go +++ b/server/v2/stf/branch/writer_map.go @@ -36,33 +36,33 @@ func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { func (b WriterMap) GetWriter(actor []byte) (store.Writer, error) { actorKey := unsafeString(actor) - // Step 1: Attempt to read the map with a read lock + // attempt to read the map with a read lock b.mu.RLock() actorState, ok := b.branchedWriterState[actorKey] b.mu.RUnlock() if ok { - // If the actorState is found, return it + // if the actorState is found, return it return actorState, nil } - // Step 2: If not found, proceed with acquiring a write lock to update the map + // if not found, proceed with acquiring a write lock to update the map b.mu.Lock() defer b.mu.Unlock() - // Double-check: Ensure that the actorState wasn't created by another goroutine while waiting for the write lock + // ensure that the actorState wasn't created by another goroutine while waiting for the write lock if actorState, ok = b.branchedWriterState[actorKey]; ok { return actorState, nil } - // Step 3: If still not found, create the actorState and update the map + // if still not found, create the actorState and update the map writerState, err := b.state.GetReader(actor) if err != nil { return nil, err } actorState = b.branch(writerState) - b.branchedWriterState[actorKey] = actorState // This line is now protected by the mutex + b.branchedWriterState[actorKey] = actorState return actorState, nil } From f0027dc547bd767e4f53e06c1e4765d054c9c243 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 10:45:23 +0530 Subject: [PATCH 03/11] improve code --- server/v2/stf/branch/writer_map.go | 18 +++++------------- server/v2/stf/core_store_service.go | 3 +-- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/server/v2/stf/branch/writer_map.go b/server/v2/stf/branch/writer_map.go index 622d813fcaf..0d436049cda 100644 --- a/server/v2/stf/branch/writer_map.go +++ b/server/v2/stf/branch/writer_map.go @@ -34,24 +34,16 @@ func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { } func (b WriterMap) GetWriter(actor []byte) (store.Writer, error) { + // Simplify and optimize state retrieval actorKey := unsafeString(actor) - // attempt to read the map with a read lock - b.mu.RLock() - actorState, ok := b.branchedWriterState[actorKey] - b.mu.RUnlock() - - if ok { - // if the actorState is found, return it - return actorState, nil - } - - // if not found, proceed with acquiring a write lock to update the map + // acquire a lock to ensure thread-safe access to the branchedWriterState map b.mu.Lock() defer b.mu.Unlock() - // ensure that the actorState wasn't created by another goroutine while waiting for the write lock - if actorState, ok = b.branchedWriterState[actorKey]; ok { + // check if the writer for the given actor already exists in the map + actorState, ok := b.branchedWriterState[actorKey] + if ok { return actorState, nil } diff --git a/server/v2/stf/core_store_service.go b/server/v2/stf/core_store_service.go index ba55301e302..d118b070ad2 100644 --- a/server/v2/stf/core_store_service.go +++ b/server/v2/stf/core_store_service.go @@ -29,14 +29,13 @@ func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { } s.mu.Lock() + defer s.mu.Unlock() state, err := exCtx.state.GetWriter(s.actor) if err != nil { panic(err) } - s.mu.Unlock() - return state } From 265d869780b54a8fadc77a8ff76819526d87ee46 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 16:52:06 +0530 Subject: [PATCH 04/11] try other approach --- runtime/v2/builder.go | 2 +- runtime/v2/manager.go | 56 +++++++++++++++++------------ server/v2/stf/branch/writer_map.go | 27 ++++---------- server/v2/stf/core_store_service.go | 9 ++--- 4 files changed, 42 insertions(+), 52 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index 33e4f46bb77..2d6829a8f1c 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -174,7 +174,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { return nil }, ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) { - genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx) + genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx, a.app.db, a.app.stf) if err != nil { return nil, fmt.Errorf("failed to export genesis: %w", err) } diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index bd061462de1..7005ec359b4 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -9,7 +9,6 @@ import ( "reflect" "slices" "sort" - "sync" gogoproto "github.com/cosmos/gogoproto/proto" "google.golang.org/grpc" @@ -25,6 +24,7 @@ import ( "cosmossdk.io/core/registry" "cosmossdk.io/core/transaction" "cosmossdk.io/log" + "cosmossdk.io/server/v2/appmanager" "cosmossdk.io/server/v2/stf" ) @@ -195,6 +195,8 @@ func (m *MM[T]) InitGenesisJSON( // ExportGenesisForModules performs export genesis functionality for modules func (m *MM[T]) ExportGenesisForModules( ctx context.Context, + db appmanager.Store, + appStf *stf.STF[T], modulesToExport ...string, ) (map[string]json.RawMessage, error) { if len(modulesToExport) == 0 { @@ -205,50 +207,58 @@ func (m *MM[T]) ExportGenesisForModules( return nil, err } + _, state, err := db.StateLatest() + if err != nil { + return nil, err + } + type genesisResult struct { - moduleName string - bz json.RawMessage - err error + bz json.RawMessage + err error } type ModuleI interface { ExportGenesis(ctx context.Context) (json.RawMessage, error) } - var wg sync.WaitGroup - results := make(chan genesisResult, len(modulesToExport)) - + 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 { + } else if module, hasABCIGenesis := mod.(appmodulev2.HasGenesis); hasABCIGenesis { moduleI = module.(ModuleI) } else { continue } - wg.Add(1) - go func(moduleName string, moduleI ModuleI) { - defer wg.Done() - jm, err := moduleI.ExportGenesis(ctx) - results <- genesisResult{moduleName, jm, err} - }(moduleName, moduleI) + channels[moduleName] = make(chan genesisResult) + go func(moduleI ModuleI, ch chan genesisResult) { + _, err = appStf.RunWithCtx(ctx, state, 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 + }) + if err != nil { + panic(err) + } + return + }(moduleI, channels[moduleName]) } - go func() { - wg.Wait() - close(results) - }() - genesisData := make(map[string]json.RawMessage) - for res := range results { + for moduleName := range channels { + res := <-channels[moduleName] if res.err != nil { - return nil, fmt.Errorf("genesis export error in %s: %w", res.moduleName, res.err) + return nil, fmt.Errorf("genesis export error in %s: %w", moduleName, res.err) } - genesisData[res.moduleName] = res.bz + + genesisData[moduleName] = res.bz } return genesisData, nil diff --git a/server/v2/stf/branch/writer_map.go b/server/v2/stf/branch/writer_map.go index 0d436049cda..244b51b3e78 100644 --- a/server/v2/stf/branch/writer_map.go +++ b/server/v2/stf/branch/writer_map.go @@ -2,7 +2,6 @@ package branch import ( "fmt" - "sync" "unsafe" "cosmossdk.io/core/store" @@ -26,7 +25,6 @@ type WriterMap struct { state store.ReaderMap branchedWriterState map[string]store.Writer branch func(state store.Reader) store.Writer - mu sync.RWMutex // mutex to protect branchedWriterState } func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { @@ -35,28 +33,15 @@ func (b WriterMap) GetReader(actor []byte) (store.Reader, error) { func (b WriterMap) GetWriter(actor []byte) (store.Writer, error) { // Simplify and optimize state retrieval - actorKey := unsafeString(actor) - - // acquire a lock to ensure thread-safe access to the branchedWriterState map - b.mu.Lock() - defer b.mu.Unlock() - - // check if the writer for the given actor already exists in the map - actorState, ok := b.branchedWriterState[actorKey] - if ok { + if actorState, ok := b.branchedWriterState[unsafeString(actor)]; ok { return actorState, nil - } - - // if still not found, create the actorState and update the map - writerState, err := b.state.GetReader(actor) - if err != nil { + } else if writerState, err := b.state.GetReader(actor); err != nil { return nil, err + } else { + actorState = b.branch(writerState) + b.branchedWriterState[string(actor)] = actorState + return actorState, nil } - - actorState = b.branch(writerState) - b.branchedWriterState[actorKey] = actorState - - return actorState, nil } func (b WriterMap) ApplyStateChanges(stateChanges []store.StateChanges) error { diff --git a/server/v2/stf/core_store_service.go b/server/v2/stf/core_store_service.go index d118b070ad2..bfa8ba5053e 100644 --- a/server/v2/stf/core_store_service.go +++ b/server/v2/stf/core_store_service.go @@ -2,7 +2,6 @@ package stf import ( "context" - "sync" "cosmossdk.io/core/store" ) @@ -10,16 +9,15 @@ import ( var _ store.KVStoreService = (*storeService)(nil) func NewKVStoreService(address []byte) store.KVStoreService { - return storeService{actor: address, mu: &sync.RWMutex{}} + return storeService{actor: address} } func NewMemoryStoreService(address []byte) store.MemoryStoreService { - return storeService{actor: address, mu: &sync.RWMutex{}} + return storeService{actor: address} } type storeService struct { actor []byte - mu *sync.RWMutex } func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { @@ -28,9 +26,6 @@ func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { panic(err) } - s.mu.Lock() - defer s.mu.Unlock() - state, err := exCtx.state.GetWriter(s.actor) if err != nil { panic(err) From 75a57516dc3fc057a3b6a29ab60013034aa00f10 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 17:35:52 +0530 Subject: [PATCH 05/11] get state at version --- runtime/v2/builder.go | 7 ++++++- runtime/v2/manager.go | 14 +++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index 2d6829a8f1c..55c75ac4b0c 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -174,7 +174,12 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { return nil }, ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) { - genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx, a.app.db, a.app.stf) + state, err := a.app.db.StateAt(version) + if err != nil { + return nil, fmt.Errorf("failed to export genesis: %w", err) + } + + genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx, a.app.stf, state) if err != nil { return nil, fmt.Errorf("failed to export genesis: %w", err) } diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index 7005ec359b4..f990ab58fa4 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -22,9 +22,9 @@ import ( appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/legacy" "cosmossdk.io/core/registry" + "cosmossdk.io/core/store" "cosmossdk.io/core/transaction" "cosmossdk.io/log" - "cosmossdk.io/server/v2/appmanager" "cosmossdk.io/server/v2/stf" ) @@ -195,8 +195,8 @@ func (m *MM[T]) InitGenesisJSON( // ExportGenesisForModules performs export genesis functionality for modules func (m *MM[T]) ExportGenesisForModules( ctx context.Context, - db appmanager.Store, appStf *stf.STF[T], + state store.ReaderMap, modulesToExport ...string, ) (map[string]json.RawMessage, error) { if len(modulesToExport) == 0 { @@ -207,11 +207,6 @@ func (m *MM[T]) ExportGenesisForModules( return nil, err } - _, state, err := db.StateLatest() - if err != nil { - return nil, err - } - type genesisResult struct { bz json.RawMessage err error @@ -235,7 +230,7 @@ func (m *MM[T]) ExportGenesisForModules( channels[moduleName] = make(chan genesisResult) go func(moduleI ModuleI, ch chan genesisResult) { - _, err = appStf.RunWithCtx(ctx, state, func(ctx context.Context) error { + _, _ = appStf.RunWithCtx(ctx, state, func(ctx context.Context) error { jm, err := moduleI.ExportGenesis(ctx) if err != nil { ch <- genesisResult{nil, err} @@ -244,9 +239,6 @@ func (m *MM[T]) ExportGenesisForModules( ch <- genesisResult{jm, nil} return nil }) - if err != nil { - panic(err) - } return }(moduleI, channels[moduleName]) } From 6606640b0a4dd7ffbbd2fb0e2f2552db82db8d1d Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 17:36:47 +0530 Subject: [PATCH 06/11] revert change --- server/v2/stf/core_store_service.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/v2/stf/core_store_service.go b/server/v2/stf/core_store_service.go index bfa8ba5053e..9ee67ca367a 100644 --- a/server/v2/stf/core_store_service.go +++ b/server/v2/stf/core_store_service.go @@ -30,7 +30,6 @@ func (s storeService) OpenKVStore(ctx context.Context) store.KVStore { if err != nil { panic(err) } - return state } From d4ca906551f8794d2c836a6f62a8e2f95dc6f100 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Thu, 5 Sep 2024 17:38:33 +0530 Subject: [PATCH 07/11] typo --- runtime/v2/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index f990ab58fa4..587498e582b 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -222,7 +222,7 @@ func (m *MM[T]) ExportGenesisForModules( var moduleI ModuleI if module, hasGenesis := mod.(appmodulev2.HasGenesis); hasGenesis { moduleI = module.(ModuleI) - } else if module, hasABCIGenesis := mod.(appmodulev2.HasGenesis); hasABCIGenesis { + } else if module, hasABCIGenesis := mod.(appmodulev2.HasABCIGenesis); hasABCIGenesis { moduleI = module.(ModuleI) } else { continue From a91e7de2edb8639ef5adb1642efd25af0589e8ff Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Tue, 10 Sep 2024 11:36:50 +0530 Subject: [PATCH 08/11] address comments --- runtime/v2/builder.go | 2 +- runtime/v2/manager.go | 3 ++- server/v2/appmanager/appmanager.go | 19 +++---------------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index 55c75ac4b0c..0fc2c596cdd 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -176,7 +176,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) { state, err := a.app.db.StateAt(version) if err != nil { - return nil, fmt.Errorf("failed to export genesis: %w", err) + return nil, fmt.Errorf("unable to get latest state: %w", err) } genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx, a.app.stf, state) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index 587498e582b..ed031a8587c 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -25,6 +25,7 @@ import ( "cosmossdk.io/core/store" "cosmossdk.io/core/transaction" "cosmossdk.io/log" + "cosmossdk.io/server/v2/appmanager" "cosmossdk.io/server/v2/stf" ) @@ -195,7 +196,7 @@ func (m *MM[T]) InitGenesisJSON( // ExportGenesisForModules performs export genesis functionality for modules func (m *MM[T]) ExportGenesisForModules( ctx context.Context, - appStf *stf.STF[T], + appStf appmanager.StateTransitionFunction[T], state store.ReaderMap, modulesToExport ...string, ) (map[string]json.RawMessage, error) { diff --git a/server/v2/appmanager/appmanager.go b/server/v2/appmanager/appmanager.go index 023f76bd206..e2b7af328b3 100644 --- a/server/v2/appmanager/appmanager.go +++ b/server/v2/appmanager/appmanager.go @@ -89,24 +89,11 @@ func (a AppManager[T]) InitGenesis( // ExportGenesis exports the genesis state of the application. func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) { - zeroState, err := a.db.StateAt(version) - if err != nil { - return nil, fmt.Errorf("unable to get latest state: %w", err) + if a.exportGenesis == nil { + return nil, errors.New("export genesis function not set") } - bz := make([]byte, 0) - _, err = a.stf.RunWithCtx(ctx, zeroState, func(ctx context.Context) error { - if a.exportGenesis == nil { - return errors.New("export genesis function not set") - } - - bz, err = a.exportGenesis(ctx, version) - if err != nil { - return fmt.Errorf("failed to export genesis state: %w", err) - } - - return nil - }) + bz, err := a.exportGenesis(ctx, version) if err != nil { return nil, fmt.Errorf("failed to export genesis state: %w", err) } From 2964763ec2445413a4bdfae3ba349583b6ec808e Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Tue, 10 Sep 2024 19:25:44 +0530 Subject: [PATCH 09/11] fix lint --- runtime/v2/manager.go | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index ed031a8587c..bb2883d4bb1 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -240,7 +240,6 @@ func (m *MM[T]) ExportGenesisForModules( ch <- genesisResult{jm, nil} return nil }) - return }(moduleI, channels[moduleName]) } From 4d1a612fc2149d60ea2d39346190e96e969e809d Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 20 Sep 2024 10:40:09 -0500 Subject: [PATCH 10/11] use stateFactory in genesis expoert --- runtime/v2/builder.go | 7 ++++++- runtime/v2/manager.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index d3e2f5af7ed..e4a42f13ee9 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -201,7 +201,12 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { var genesisJson map[string]json.RawMessage _, err = genesisCtx.Run(ctx, func(ctx context.Context) error { - genesisJson, err = a.app.moduleManager.ExportGenesisForModules(ctx, a.branch(state)) + genesisJson, err = a.app.moduleManager.ExportGenesisForModules( + ctx, + func() store.WriterMap { + return a.branch(state) + }, + ) return err }) if err != nil { diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index 5c45e11f664..4da233a1917 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -195,7 +195,7 @@ func (m *MM[T]) InitGenesisJSON( // ExportGenesisForModules performs export genesis functionality for modules func (m *MM[T]) ExportGenesisForModules( ctx context.Context, - state store.WriterMap, + stateFactory func() store.WriterMap, modulesToExport ...string, ) (map[string]json.RawMessage, error) { if len(modulesToExport) == 0 { @@ -229,7 +229,7 @@ func (m *MM[T]) ExportGenesisForModules( channels[moduleName] = make(chan genesisResult) go func(moduleI ModuleI, ch chan genesisResult) { - genesisCtx := services.NewGenesisContext(state) + genesisCtx := services.NewGenesisContext(stateFactory()) _, _ = genesisCtx.Run(ctx, func(ctx context.Context) error { jm, err := moduleI.ExportGenesis(ctx) if err != nil { From 5c99189a59162eedd4d5058f89a2eed68295bbc3 Mon Sep 17 00:00:00 2001 From: akhilkumarpilli Date: Mon, 23 Sep 2024 14:57:48 +0530 Subject: [PATCH 11/11] remove genesisCtx call outside --- runtime/v2/builder.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index e4a42f13ee9..c6fad39f41a 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -197,18 +197,13 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { if err != nil { return nil, fmt.Errorf("unable to get state at given version: %w", err) } - genesisCtx := services.NewGenesisContext(a.branch(state)) - - var genesisJson map[string]json.RawMessage - _, err = genesisCtx.Run(ctx, func(ctx context.Context) error { - genesisJson, err = a.app.moduleManager.ExportGenesisForModules( - ctx, - func() store.WriterMap { - return a.branch(state) - }, - ) - return err - }) + + genesisJson, err := a.app.moduleManager.ExportGenesisForModules( + ctx, + func() store.WriterMap { + return a.branch(state) + }, + ) if err != nil { return nil, fmt.Errorf("failed to export genesis: %w", err) }