Skip to content

Commit 15accd7

Browse files
yihuangtac0turtlealexanderbez
authored
feat: support alternative query multistore (cosmos#13529)
* support customize query multistore * Update CHANGELOG.md * fix test * Update baseapp/abci.go Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com> * Update baseapp/baseapp.go Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com> * Update baseapp/options.go Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Marko <marbar3778@yahoo.com> Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
1 parent ed9cd41 commit 15accd7

File tree

8 files changed

+34
-2
lines changed

8 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
152152
* (x/gov) [#13160](https://github.com/cosmos/cosmos-sdk/pull/13160) Remove custom marshaling of proposl and voteoption.
153153
* (types) [#13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx`
154154
* (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.
155+
* (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.
155156

156157
### CLI Breaking Changes
157158

baseapp/abci.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,13 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e
643643
return sdk.Context{}, err
644644
}
645645

646-
lastBlockHeight := app.LastBlockHeight()
646+
// use custom query multistore if provided
647+
qms := app.qms
648+
if qms == nil {
649+
qms = app.cms.(sdk.MultiStore)
650+
}
651+
652+
lastBlockHeight := qms.LatestVersion()
647653
if height > lastBlockHeight {
648654
return sdk.Context{},
649655
sdkerrors.Wrap(
@@ -665,7 +671,7 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e
665671
)
666672
}
667673

668-
cacheMS, err := app.cms.CacheMultiStoreWithVersion(height)
674+
cacheMS, err := qms.CacheMultiStoreWithVersion(height)
669675
if err != nil {
670676
return sdk.Context{},
671677
sdkerrors.Wrapf(

baseapp/baseapp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type BaseApp struct { //nolint: maligned
4949
name string // application name from abci.Info
5050
db dbm.DB // common DB backend
5151
cms sdk.CommitMultiStore // Main (uncached) state
52+
qms sdk.MultiStore // Optional alternative multistore for querying only.
5253
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
5354
grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls
5455
msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages

baseapp/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ func (app *BaseApp) SetStreamingService(s StreamingService) {
240240
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
241241
app.txDecoder = txDecoder
242242
}
243+
244+
// SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service.
245+
//
246+
// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317
247+
func (app *BaseApp) SetQueryMultiStore(ms sdk.MultiStore) {
248+
app.qms = ms
249+
}

server/mock/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func (ms multiStore) RollbackToVersion(version int64) error {
152152
panic("not implemented")
153153
}
154154

155+
func (ms multiStore) LatestVersion() int64 {
156+
panic("not implemented")
157+
}
158+
155159
var _ sdk.KVStore = kvStore{}
156160

157161
type kvStore struct {

store/cachemulti/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ func (cms Store) ListeningEnabled(key types.StoreKey) bool {
138138
return false
139139
}
140140

141+
// LatestVersion returns the branch version of the store
142+
func (cms Store) LatestVersion() int64 {
143+
panic("cannot get latest version from branch cached multi-store")
144+
}
145+
141146
// GetStoreType returns the type of the store.
142147
func (cms Store) GetStoreType() types.StoreType {
143148
return types.StoreTypeMulti

store/rootmulti/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ func (rs *Store) ListeningEnabled(key types.StoreKey) bool {
407407
return false
408408
}
409409

410+
// LatestVersion returns the latest version in the store
411+
func (rs *Store) LatestVersion() int64 {
412+
return rs.LastCommitID().Version
413+
}
414+
410415
// LastCommitID implements Committer/CommitStore.
411416
func (rs *Store) LastCommitID() types.CommitID {
412417
if rs.lastCommitInfo == nil {

store/types/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ type MultiStore interface {
134134
// AddListeners adds WriteListeners for the KVStore belonging to the provided StoreKey
135135
// It appends the listeners to a current set, if one already exists
136136
AddListeners(key StoreKey, listeners []WriteListener)
137+
138+
// LatestVersion returns the latest version in the store
139+
LatestVersion() int64
137140
}
138141

139142
// From MultiStore.CacheMultiStore()....

0 commit comments

Comments
 (0)