Skip to content

Commit

Permalink
Problem: no api to create cachemulti store from external cache store (#…
Browse files Browse the repository at this point in the history
…258)

Solution:
- add API NewFromParent to cache multistore.

Update store/CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

fix test

fix lint
  • Loading branch information
yihuang authored Apr 5, 2024
1 parent 29d3142 commit 0bb12ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#242](https://github.com/crypto-org-chain/cosmos-sdk/pull/242) Init cache on cache lazily, save memory allocations.
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API to use new CoW cache store.
* [#244](https://github.com/crypto-org-chain/cosmos-sdk/pull/244) Add `Discard` method to CacheWrap to discard the write buffer.
* [#258](https://github.com/crypto-org-chain/cosmos-sdk/pull/258) Add `NewFromParent` API to cachemulti store to create a new store from block-stm multiversion data structure.

## v1.1.0 (March 20, 2024)

Expand Down
25 changes: 15 additions & 10 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Store struct {

traceWriter io.Writer
traceContext types.TraceContext
parentStore func(types.StoreKey) types.CacheWrap
parentStore func(types.StoreKey) types.CacheWrapper

branched bool
}
Expand Down Expand Up @@ -60,12 +60,17 @@ func NewStore(
return NewFromKVStore(stores, traceWriter, traceContext)
}

func newCacheMultiStoreFromCMS(cms Store) Store {
// NewFromParent constructs a cache multistore with a parent store lazily,
// the parent is usually another cache multistore or the block-stm multiversion store.
func NewFromParent(
parentStore func(types.StoreKey) types.CacheWrapper,
traceWriter io.Writer, traceContext types.TraceContext,
) Store {
return Store{
stores: make(map[types.StoreKey]types.CacheWrap),
traceWriter: cms.traceWriter,
traceContext: cms.traceContext,
parentStore: cms.getCacheWrap,
traceWriter: traceWriter,
traceContext: traceContext,
parentStore: parentStore,
}
}

Expand Down Expand Up @@ -141,10 +146,10 @@ func (cms Store) CacheWrap() types.CacheWrap {

// Implements MultiStore.
func (cms Store) CacheMultiStore() types.CacheMultiStore {
return newCacheMultiStoreFromCMS(cms)
return NewFromParent(cms.getCacheWrapper, cms.traceWriter, cms.traceContext)
}

func (cms Store) getCacheWrap(key types.StoreKey) types.CacheWrap {
func (cms Store) getCacheWrapper(key types.StoreKey) types.CacheWrapper {
store, ok := cms.stores[key]
if !ok && cms.parentStore != nil {
// load on demand
Expand All @@ -158,7 +163,7 @@ func (cms Store) getCacheWrap(key types.StoreKey) types.CacheWrap {

// GetStore returns an underlying Store by key.
func (cms Store) GetStore(key types.StoreKey) types.Store {
store, ok := cms.getCacheWrap(key).(types.Store)
store, ok := cms.getCacheWrapper(key).(types.Store)
if !ok {
panic(fmt.Sprintf("store with key %v is not Store", key))
}
Expand All @@ -167,7 +172,7 @@ func (cms Store) GetStore(key types.StoreKey) types.Store {

// GetKVStore returns an underlying KVStore by key.
func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
store, ok := cms.getCacheWrap(key).(types.KVStore)
store, ok := cms.getCacheWrapper(key).(types.KVStore)
if !ok {
panic(fmt.Sprintf("store with key %v is not KVStore", key))
}
Expand All @@ -176,7 +181,7 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {

// GetObjKVStore returns an underlying KVStore by key.
func (cms Store) GetObjKVStore(key types.StoreKey) types.ObjKVStore {
store, ok := cms.getCacheWrap(key).(types.ObjKVStore)
store, ok := cms.getCacheWrapper(key).(types.ObjKVStore)
if !ok {
panic(fmt.Sprintf("store with key %v is not ObjKVStore", key))
}
Expand Down
4 changes: 2 additions & 2 deletions store/cachemulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestRunAtomic(t *testing.T) {
keys["obj"]: objStore.CacheWrap(),
}}

s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrap}
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrapper}
s.RunAtomic(func(ms types.CacheMultiStore) error {
ms.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value"))
ms.GetObjKVStore(keys["obj"]).Set([]byte("key"), "value")
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestBranchStore(t *testing.T) {
keys["obj"]: objStore.CacheWrap(),
}}

s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrap}
s := Store{stores: map[types.StoreKey]types.CacheWrap{}, parentStore: parent.getCacheWrapper}
s.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value"))
snapshot := s.Clone()
s.GetKVStore(keys["abc"]).Set([]byte("key"), []byte("value2"))
Expand Down

0 comments on commit 0bb12ec

Please sign in to comment.