From 5051618a2936b8a36491040340da79aae095ad4c Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 29 Mar 2024 15:59:45 +0800 Subject: [PATCH] Problem: MultiStore interface is bloated (#240) * Problem: MultiStore interface is bloated Solution: - Split out specialied methods from it, keeping the MultiStore generic * Update store/CHANGELOG.md Signed-off-by: yihuang --------- Signed-off-by: yihuang --- baseapp/abci.go | 2 +- baseapp/baseapp.go | 2 +- baseapp/options.go | 2 +- server/mock/store.go | 2 +- store/CHANGELOG.md | 1 + store/cachemulti/store.go | 14 -------------- store/types/store.go | 14 +++++++++----- 7 files changed, 14 insertions(+), 23 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 04ef73f002f4..ff391460f536 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -1213,7 +1213,7 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e // use custom query multi-store if provided qms := app.qms if qms == nil { - qms = app.cms.(storetypes.MultiStore) + qms = storetypes.RootMultiStore(app.cms) } lastBlockHeight := qms.LatestVersion() diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 1b39ffba0bf3..aee79df5f658 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -65,7 +65,7 @@ type BaseApp struct { name string // application name from abci.BlockInfo db dbm.DB // common DB backend cms storetypes.CommitMultiStore // Main (uncached) state - qms storetypes.MultiStore // Optional alternative multistore for querying only. + qms storetypes.RootMultiStore // 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 13f5c9afb580..0c28e3755c12 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -314,7 +314,7 @@ func (app *BaseApp) SetTxEncoder(txEncoder sdk.TxEncoder) { // 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 storetypes.MultiStore) { +func (app *BaseApp) SetQueryMultiStore(ms storetypes.RootMultiStore) { app.qms = ms } diff --git a/server/mock/store.go b/server/mock/store.go index c6829bb18b6b..9c756eed4964 100644 --- a/server/mock/store.go +++ b/server/mock/store.go @@ -12,7 +12,7 @@ import ( storetypes "cosmossdk.io/store/types" ) -var _ storetypes.MultiStore = multiStore{} +var _ storetypes.CommitMultiStore = multiStore{} type multiStore struct { kv map[storetypes.StoreKey]kvStore diff --git a/store/CHANGELOG.md b/store/CHANGELOG.md index 4066039f3f7e..13c55db25952 100644 --- a/store/CHANGELOG.md +++ b/store/CHANGELOG.md @@ -29,6 +29,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#207](https://github.com/crypto-org-chain/cosmos-sdk/pull/207) Remove api CacheWrapWithTrace. * [#205](https://github.com/crypto-org-chain/cosmos-sdk/pull/205) Support object store. +* [#240](https://github.com/crypto-org-chain/cosmos-sdk/pull/240) Split methods from `MultiStore` into specialized `RootMultiStore`, keep `MultiStore` generic. ## v1.1.0 (March 20, 2024) diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index 5f6b4856052b..57734bbb770b 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -111,11 +111,6 @@ func (cms Store) TracingEnabled() bool { return cms.traceWriter != nil } -// 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 @@ -139,15 +134,6 @@ func (cms Store) CacheMultiStore() types.CacheMultiStore { return newCacheMultiStoreFromCMS(cms) } -// CacheMultiStoreWithVersion implements the MultiStore interface. It will panic -// as an already cached multi-store cannot load previous versions. -// -// TODO: The store implementation can possibly be modified to support this as it -// seems safe to load previous versions (heights). -func (cms Store) CacheMultiStoreWithVersion(_ int64) (types.CacheMultiStore, error) { - panic("cannot branch cached multi-store with a version") -} - // GetStore returns an underlying Store by key. func (cms Store) GetStore(key types.StoreKey) types.Store { s := cms.stores[key] diff --git a/store/types/store.go b/store/types/store.go index 08f8044b30dc..239e4ae94b3f 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -128,10 +128,6 @@ type MultiStore interface { // call CacheMultiStore.Write(). CacheMultiStore() CacheMultiStore - // CacheMultiStoreWithVersion branches the underlying MultiStore where - // each stored is loaded at a specific version (height). - CacheMultiStoreWithVersion(version int64) (CacheMultiStore, error) - // Convenience for fetching substores. // If the store does not exist, panics. GetStore(StoreKey) Store @@ -150,6 +146,14 @@ type MultiStore interface { // implied that the caller should update the context when necessary between // tracing operations. The modified MultiStore is returned. SetTracingContext(TraceContext) MultiStore +} + +type RootMultiStore interface { + MultiStore + + // CacheMultiStoreWithVersion branches the underlying MultiStore where + // each stored is loaded at a specific version (height). + CacheMultiStoreWithVersion(version int64) (CacheMultiStore, error) // LatestVersion returns the latest version in the store LatestVersion() int64 @@ -164,7 +168,7 @@ type CacheMultiStore interface { // CommitMultiStore is an interface for a MultiStore without cache capabilities. type CommitMultiStore interface { Committer - MultiStore + RootMultiStore snapshottypes.Snapshotter // Mount a store of type using the given db.