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

Problem: versiondb multistore can't query mem store state (backport #1230) #1339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 17 additions & 17 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 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 @@ -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)

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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion store/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion versiondb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions versiondb/multistore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"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 All @@ -19,16 +17,19 @@
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
traceContextMutex sync.Mutex
}

// 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.
Expand All @@ -39,9 +40,9 @@
// 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)
}

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m Error

expected either an append, delete, or copy to another map in a range with a map
for _, k := range s.storeKeys {
stores[k] = NewKVStore(s.versionDB, k, version)
}
Expand Down Expand Up @@ -75,24 +76,24 @@

// 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)
}

// 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{}{}
}
}

Expand Down
Loading