Skip to content

Commit

Permalink
Problem: versiondb multistore can't query mem store state
Browse files Browse the repository at this point in the history
Solution:
- share the mem store instance with the main store
  • Loading branch information
yihuang committed Oct 26, 2023
1 parent f9d69a7 commit 0b03964
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [#1215](https://github.com/crypto-org-chain/cronos/pull/1215) Update ethermint to fix of concurrent write in fee history.
- [#1217](https://github.com/crypto-org-chain/cronos/pull/1217) Use the default chain-id behavour in sdk.
- [#]() Fix mem store in versiondb multistore.

*October 17, 2023*

Expand Down
34 changes: 17 additions & 17 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,6 @@ func New(

keys, memKeys, tkeys := StoreKeys(skipGravity)

// load state streaming if enabled
if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, logger, 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,
Expand Down Expand Up @@ -868,6 +851,23 @@ func New(
app.MountTransientStores(tkeys)
app.MountMemoryStores(memKeys)

// load state streaming if enabled
if _, _, err := streaming.LoadStreamingServices(bApp, appOpts, appCodec, logger, 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.SetPreBlocker(app.PreBlocker)
Expand Down
11 changes: 7 additions & 4 deletions app/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,7 +39,12 @@ func setupVersionDB(

verDB := versiondb.NewMultiStore(versionDB, exposeStoreKeys)
verDB.MountTransientStores(tkeys)
verDB.MountMemoryStores(memKeys)

memStores := make(map[storetypes.StoreKey]storetypes.KVStore)
for _, memKey := range memKeys {
memStores[memKey] = app.CommitMultiStore().GetKVStore(memKey)
}
verDB.MountMemoryStores(memStores)

app.SetQueryMultiStore(verDB)
return verDB, nil
Expand Down
4 changes: 1 addition & 3 deletions app/versiondb_placeholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions versiondb/multistore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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"
Expand Down Expand Up @@ -89,10 +88,11 @@ func (s *MultiStore) MountTransientStores(keys map[string]*types.TransientStoreK
}
}

// MountMemoryStores simlates the same behavior as sdk to support grpc query service.
func (s *MultiStore) MountMemoryStores(keys map[string]*types.MemoryStoreKey) {
for _, key := range keys {
s.transientStores[key] = mem.NewStore()
// 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[types.StoreKey]types.KVStore) {
for key, store := range keys {
s.transientStores[key] = store
}
}

Expand Down

0 comments on commit 0b03964

Please sign in to comment.