diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c9cfd4a4eb4..879189b751bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -152,6 +152,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/gov) [#13160](https://github.com/cosmos/cosmos-sdk/pull/13160) Remove custom marshaling of proposl and voteoption. * (types) [#13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx` * (auth) [#13460](https://github.com/cosmos/cosmos-sdk/pull/13460) The `q auth address-by-id` CLI command has been renamed to `q auth address-by-acc-num` to be more explicit. However, the old `address-by-id` version is still kept as an alias, for backwards compatibility. +* (store) [#13529](https://github.com/cosmos/cosmos-sdk/pull/13529) Add method `LatestVersion` to `MultiStore` interface, add method `SetQueryMultiStore` to baesapp to support alternative `MultiStore` implementation for query service. ### CLI Breaking Changes diff --git a/baseapp/abci.go b/baseapp/abci.go index 4f52136fac10..4a2998a50db2 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -643,7 +643,13 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e return sdk.Context{}, err } - lastBlockHeight := app.LastBlockHeight() + // use custom query multistore if provided + qms := app.qms + if qms == nil { + qms = app.cms.(sdk.MultiStore) + } + + lastBlockHeight := qms.LatestVersion() if height > lastBlockHeight { return sdk.Context{}, sdkerrors.Wrap( @@ -665,7 +671,7 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e ) } - cacheMS, err := app.cms.CacheMultiStoreWithVersion(height) + cacheMS, err := qms.CacheMultiStoreWithVersion(height) if err != nil { return sdk.Context{}, sdkerrors.Wrapf( diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index dd3e8e92b456..49a5c398acce 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -49,6 +49,7 @@ type BaseApp struct { //nolint: maligned name string // application name from abci.Info db dbm.DB // common DB backend cms sdk.CommitMultiStore // Main (uncached) state + qms sdk.MultiStore // Optional alternative multistore for querying only. storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader() grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages diff --git a/baseapp/options.go b/baseapp/options.go index f9a67f186d7c..1a304e4eab1b 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -240,3 +240,10 @@ func (app *BaseApp) SetStreamingService(s StreamingService) { func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) { app.txDecoder = txDecoder } + +// SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service. +// +// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317 +func (app *BaseApp) SetQueryMultiStore(ms sdk.MultiStore) { + app.qms = ms +} diff --git a/server/mock/store.go b/server/mock/store.go index 51d44ed9d215..486b15e98192 100644 --- a/server/mock/store.go +++ b/server/mock/store.go @@ -152,6 +152,10 @@ func (ms multiStore) RollbackToVersion(version int64) error { panic("not implemented") } +func (ms multiStore) LatestVersion() int64 { + panic("not implemented") +} + var _ sdk.KVStore = kvStore{} type kvStore struct { diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index deb1d46272dd..42409ed160f8 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -138,6 +138,11 @@ func (cms Store) ListeningEnabled(key types.StoreKey) bool { return false } +// LatestVersion returns the branch version of the store +func (cms Store) LatestVersion() int64 { + panic("cannot get latest version from branch cached multi-store") +} + // GetStoreType returns the type of the store. func (cms Store) GetStoreType() types.StoreType { return types.StoreTypeMulti diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 0f652d24bab5..f4dfcedd4527 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -407,6 +407,11 @@ func (rs *Store) ListeningEnabled(key types.StoreKey) bool { return false } +// LatestVersion returns the latest version in the store +func (rs *Store) LatestVersion() int64 { + return rs.LastCommitID().Version +} + // LastCommitID implements Committer/CommitStore. func (rs *Store) LastCommitID() types.CommitID { if rs.lastCommitInfo == nil { diff --git a/store/types/store.go b/store/types/store.go index e3c0f0822ad2..403ede5f706f 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -134,6 +134,9 @@ type MultiStore interface { // AddListeners adds WriteListeners for the KVStore belonging to the provided StoreKey // It appends the listeners to a current set, if one already exists AddListeners(key StoreKey, listeners []WriteListener) + + // LatestVersion returns the latest version in the store + LatestVersion() int64 } // From MultiStore.CacheMultiStore()....