From 45efe8d86b9535c8790c8b68269f95c35d6047e3 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Thu, 6 Oct 2022 15:43:17 +0800 Subject: [PATCH 1/7] support customize query multistore --- CHANGELOG.md | 1 + baseapp/abci.go | 10 ++++++++-- baseapp/baseapp.go | 1 + baseapp/options.go | 6 ++++++ store/cachemulti/store.go | 5 +++++ store/rootmulti/store.go | 5 +++++ store/types/store.go | 3 +++ 7 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b2cba94af8a..458764f64d7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,6 +150,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) [#]() 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..5bc5e8177203 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 + ms := app.qms + if ms == nil { + ms = app.cms.(sdk.MultiStore) + } + + lastBlockHeight := ms.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 := ms.CacheMultiStoreWithVersion(height) if err != nil { return sdk.Context{}, sdkerrors.Wrapf( diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b9618ca989b2..fa9966f944dc 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 query purpose. 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..c3a33d8864cc 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -240,3 +240,9 @@ 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. +// see: https://github.com/cosmos/cosmos-sdk/issues/13317 +func (app *BaseApp) SetQueryMultiStore(ms sdk.MultiStore) { + app.qms = ms +} 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().... From 46c1cc99db38182f8bee82916da4c97b78e3e177 Mon Sep 17 00:00:00 2001 From: yihuang Date: Thu, 13 Oct 2022 10:30:17 +0800 Subject: [PATCH 2/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 458764f64d7d..bf3711d8c051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,7 +150,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) [#]() Add method `LatestVersion` to `MultiStore` interface, add method `SetQueryMultiStore` to baesapp to support alternative `MultiStore` implementation for query service. +* (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 From f577f1a89966a22ca2eb62bc55ca7c8fcf187130 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Thu, 13 Oct 2022 14:09:45 +0800 Subject: [PATCH 3/7] fix test --- server/mock/store.go | 4 ++++ 1 file changed, 4 insertions(+) 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 { From 87fa94ff6fa509afcd4edb5f490422238d07da90 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 17 Oct 2022 22:56:07 +0800 Subject: [PATCH 4/7] Update baseapp/abci.go Co-authored-by: Aleksandr Bezobchuk --- baseapp/abci.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 5bc5e8177203..2fa7b10a59f5 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -644,9 +644,9 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e } // use custom query multistore if provided - ms := app.qms - if ms == nil { - ms = app.cms.(sdk.MultiStore) + qms := app.qms + if qms == nil { + qms = app.cms.(sdk.MultiStore) } lastBlockHeight := ms.LatestVersion() From 4ad9c7b1278494d75c169a7c475461ae4c4a9aa0 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 17 Oct 2022 22:56:16 +0800 Subject: [PATCH 5/7] Update baseapp/baseapp.go Co-authored-by: Aleksandr Bezobchuk --- baseapp/baseapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index e3214040200b..49a5c398acce 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -49,7 +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 query purpose. + 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 From 6463e8ba4b025d4aef55c71c67467f314b37bd6e Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 17 Oct 2022 22:56:53 +0800 Subject: [PATCH 6/7] Update baseapp/options.go Co-authored-by: Aleksandr Bezobchuk --- baseapp/options.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/baseapp/options.go b/baseapp/options.go index c3a33d8864cc..1a304e4eab1b 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -242,7 +242,8 @@ func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) { } // SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service. -// see: https://github.com/cosmos/cosmos-sdk/issues/13317 +// +// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317 func (app *BaseApp) SetQueryMultiStore(ms sdk.MultiStore) { app.qms = ms } From cddf15ff07252b9a53c7349582ed337fee6b44f1 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 17 Oct 2022 22:58:52 +0800 Subject: [PATCH 7/7] Apply suggestions from code review --- baseapp/abci.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 2fa7b10a59f5..4a2998a50db2 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -649,7 +649,7 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e qms = app.cms.(sdk.MultiStore) } - lastBlockHeight := ms.LatestVersion() + lastBlockHeight := qms.LatestVersion() if height > lastBlockHeight { return sdk.Context{}, sdkerrors.Wrap( @@ -671,7 +671,7 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e ) } - cacheMS, err := ms.CacheMultiStoreWithVersion(height) + cacheMS, err := qms.CacheMultiStoreWithVersion(height) if err != nil { return sdk.Context{}, sdkerrors.Wrapf(