diff --git a/CHANGELOG.md b/CHANGELOG.md index 483d097f12..8a6a353c2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#1241](https://github.com/crypto-org-chain/cronos/pull/1241) Improve parallelization of memiavl restoration. - [#1302](https://github.com/crypto-org-chain/cronos/pull/1302) Fix concurrent map access in rootmulti store. - [#1304](https://github.com/crypto-org-chain/cronos/pull/1304) Write versiondb with fsync, and relax the version requirement on startup. +- [#1230](https://github.com/crypto-org-chain/cronos/pull/1230) Fix mem store in versiondb multistore. ### State Machine Breaking diff --git a/app/app.go b/app/app.go index 170edc02a6..3157ab4f60 100644 --- a/app/app.go +++ b/app/app.go @@ -373,23 +373,6 @@ func New( keys, memKeys, tkeys := StoreKeys(skipGravity) - // load state streaming if enabled - if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, keys); err != nil { - fmt.Printf("failed to load state streaming: %s", err) - os.Exit(1) - } - - // wire up the versiondb's `StreamingService` and `MultiStore`. - streamers := cast.ToStringSlice(appOpts.Get("store.streamers")) - var qms sdk.MultiStore - if slices.Contains(streamers, "versiondb") { - var err error - qms, err = setupVersionDB(homePath, bApp, keys, tkeys, memKeys) - if err != nil { - panic(err) - } - } - app := &App{ BaseApp: bApp, cdc: cdc, @@ -781,6 +764,23 @@ func New( app.MountTransientStores(tkeys) app.MountMemoryStores(memKeys) + // load state streaming if enabled + if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, keys); err != nil { + fmt.Printf("failed to load state streaming: %s", err) + os.Exit(1) + } + + // wire up the versiondb's `StreamingService` and `MultiStore`. + streamers := cast.ToStringSlice(appOpts.Get("store.streamers")) + var qms sdk.MultiStore + if slices.Contains(streamers, "versiondb") { + var err error + qms, err = app.setupVersionDB(homePath, keys, tkeys, memKeys) + if err != nil { + panic(err) + } + } + // initialize BaseApp app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) diff --git a/app/versiondb.go b/app/versiondb.go index 752d17942c..10b0ad76ad 100644 --- a/app/versiondb.go +++ b/app/versiondb.go @@ -7,16 +7,14 @@ import ( "os" "path/filepath" - "github.com/cosmos/cosmos-sdk/baseapp" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/crypto-org-chain/cronos/versiondb" "github.com/crypto-org-chain/cronos/versiondb/tsrocksdb" ) -func setupVersionDB( +func (app *App) setupVersionDB( homePath string, - app *baseapp.BaseApp, keys map[string]*storetypes.KVStoreKey, tkeys map[string]*storetypes.TransientStoreKey, memKeys map[string]*storetypes.MemoryStoreKey, @@ -39,7 +37,7 @@ func setupVersionDB( service := versiondb.NewStreamingService(versionDB, exposeStoreKeys) app.SetStreamingService(service) - verDB := versiondb.NewMultiStore(versionDB, exposeStoreKeys) + verDB := versiondb.NewMultiStore(app.CommitMultiStore(), versionDB, exposeStoreKeys) verDB.MountTransientStores(tkeys) verDB.MountMemoryStores(memKeys) diff --git a/app/versiondb_placeholder.go b/app/versiondb_placeholder.go index 1c46fec500..1225c8e864 100644 --- a/app/versiondb_placeholder.go +++ b/app/versiondb_placeholder.go @@ -6,14 +6,12 @@ package app import ( "errors" - "github.com/cosmos/cosmos-sdk/baseapp" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" ) -func setupVersionDB( +func (app *App) setupVersionDB( homePath string, - app *baseapp.BaseApp, keys map[string]*storetypes.KVStoreKey, tkeys map[string]*storetypes.TransientStoreKey, memKeys map[string]*storetypes.MemoryStoreKey, diff --git a/go.mod b/go.mod index d8e98e4dbc..37d12867ce 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/cosmos/cosmos-sdk v0.46.15-0.20230807104542-537257060180 github.com/cosmos/gogoproto v1.4.8 github.com/cosmos/ibc-go/v6 v6.2.0 - github.com/crypto-org-chain/cronos/store v0.0.4 + github.com/crypto-org-chain/cronos/store v0.0.5 github.com/crypto-org-chain/cronos/versiondb v0.0.0-00010101000000-000000000000 github.com/ethereum/go-ethereum v1.10.26 github.com/evmos/ethermint v0.0.0-00010101000000-000000000000 @@ -77,7 +77,7 @@ require ( github.com/cosmos/ibc-go/v5 v5.2.1 // indirect github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect github.com/creachadair/taskgroup v0.3.2 // indirect - github.com/crypto-org-chain/cronos/memiavl v0.0.4 // indirect + github.com/crypto-org-chain/cronos/memiavl v0.0.5 // indirect github.com/danieljoos/wincred v1.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect diff --git a/store/go.mod b/store/go.mod index 53c4660de9..5e03816ee2 100644 --- a/store/go.mod +++ b/store/go.mod @@ -7,7 +7,7 @@ require ( github.com/confio/ics23/go v0.9.0 github.com/cosmos/cosmos-sdk v0.46.14 github.com/cosmos/iavl v0.19.6 - github.com/crypto-org-chain/cronos/memiavl v0.0.4 + github.com/crypto-org-chain/cronos/memiavl v0.0.5 github.com/gogo/protobuf v1.3.2 github.com/spf13/cast v1.5.0 github.com/stretchr/testify v1.8.3 diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 41406f2303..07bfe41e42 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -224,12 +224,12 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor // Implements interface MultiStore func (rs *Store) GetStore(key types.StoreKey) types.Store { - panic("uncached KVStore is not supposed to be accessed directly") + return rs.stores[key] } // Implements interface MultiStore func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { - panic("uncached KVStore is not supposed to be accessed directly") + return rs.stores[key] } // Implements interface MultiStore diff --git a/versiondb/go.mod b/versiondb/go.mod index 598151f4eb..be1fb47c11 100644 --- a/versiondb/go.mod +++ b/versiondb/go.mod @@ -7,7 +7,7 @@ require ( github.com/alitto/pond v1.8.2 github.com/cosmos/cosmos-sdk v0.46.14 github.com/cosmos/iavl v0.19.6 - github.com/crypto-org-chain/cronos/memiavl v0.0.3 + github.com/crypto-org-chain/cronos/memiavl v0.0.5 github.com/gogo/protobuf v1.3.3 github.com/golang/snappy v0.0.4 github.com/linxGnu/grocksdb v1.8.0 diff --git a/versiondb/multistore.go b/versiondb/multistore.go index 423aec8496..878f6dd08e 100644 --- a/versiondb/multistore.go +++ b/versiondb/multistore.go @@ -5,8 +5,6 @@ import ( "sync" "github.com/cosmos/cosmos-sdk/store/cachemulti" - "github.com/cosmos/cosmos-sdk/store/mem" - "github.com/cosmos/cosmos-sdk/store/transient" "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -19,7 +17,10 @@ type MultiStore struct { storeKeys []types.StoreKey // transient or memory stores - transientStores map[types.StoreKey]types.KVStore + transientStores map[types.StoreKey]struct{} + + // proxy the calls for transient or mem stores to the parent + parent types.MultiStore traceWriter io.Writer traceContext types.TraceContext @@ -27,8 +28,8 @@ type MultiStore struct { } // NewMultiStore returns a new versiondb `MultiStore`. -func NewMultiStore(versionDB VersionStore, storeKeys []types.StoreKey) *MultiStore { - return &MultiStore{versionDB: versionDB, storeKeys: storeKeys, transientStores: make(map[types.StoreKey]types.KVStore)} +func NewMultiStore(parent types.MultiStore, versionDB VersionStore, storeKeys []types.StoreKey) *MultiStore { + return &MultiStore{versionDB: versionDB, storeKeys: storeKeys, parent: parent, transientStores: make(map[types.StoreKey]struct{})} } // GetStoreType implements `MultiStore` interface. @@ -39,8 +40,8 @@ func (s *MultiStore) GetStoreType() types.StoreType { // cacheMultiStore branch out the multistore. func (s *MultiStore) cacheMultiStore(version *int64) sdk.CacheMultiStore { stores := make(map[types.StoreKey]types.CacheWrapper, len(s.transientStores)+len(s.storeKeys)) - for k, v := range s.transientStores { - stores[k] = v + for k := range s.transientStores { + stores[k] = s.parent.GetKVStore(k).(types.CacheWrapper) } for _, k := range s.storeKeys { stores[k] = NewKVStore(s.versionDB, k, version) @@ -75,9 +76,8 @@ func (s *MultiStore) GetStore(storeKey types.StoreKey) sdk.Store { // GetKVStore implements `MultiStore` interface func (s *MultiStore) GetKVStore(storeKey types.StoreKey) sdk.KVStore { - store, ok := s.transientStores[storeKey] - if ok { - return store + if _, ok := s.transientStores[storeKey]; ok { + return s.parent.GetKVStore(storeKey) } return NewKVStore(s.versionDB, storeKey, nil) } @@ -85,14 +85,15 @@ func (s *MultiStore) GetKVStore(storeKey types.StoreKey) sdk.KVStore { // MountTransientStores simlates the same behavior as sdk to support grpc query service. func (s *MultiStore) MountTransientStores(keys map[string]*types.TransientStoreKey) { for _, key := range keys { - s.transientStores[key] = transient.NewStore() + s.transientStores[key] = struct{}{} } } -// MountMemoryStores simlates the same behavior as sdk to support grpc query service. +// MountMemoryStores simlates the same behavior as sdk to support grpc query service, +// it shares the existing mem store instance. func (s *MultiStore) MountMemoryStores(keys map[string]*types.MemoryStoreKey) { for _, key := range keys { - s.transientStores[key] = mem.NewStore() + s.transientStores[key] = struct{}{} } }