-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use memstore instead of block wise transient store
- Loading branch information
Showing
17 changed files
with
250 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package state | ||
|
||
import ( | ||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type Context struct { | ||
sdk.Context | ||
|
||
memStore storetypes.MultiStore | ||
memStoreKey storetypes.StoreKey | ||
} | ||
|
||
func NewContext(ctx sdk.Context) Context { | ||
memStore, memStoreKey := newMemStore() | ||
return Context{ | ||
Context: ctx, | ||
memStore: memStore, | ||
memStoreKey: memStoreKey, | ||
} | ||
} | ||
|
||
func (c Context) WithSDKContext(sdkCtx sdk.Context) Context { | ||
c.Context = sdkCtx | ||
return c | ||
} | ||
|
||
func (c Context) WithMemStore(memStore storetypes.MultiStore) Context { | ||
c.memStore = memStore | ||
return c | ||
} | ||
|
||
func (c Context) CacheContext() (cc Context, writeCache func()) { | ||
cacheCtx, commit := c.Context.CacheContext() | ||
cacheMemStore := c.memStore.CacheMultiStore() | ||
|
||
cc = c.WithSDKContext(cacheCtx).WithMemStore(cacheMemStore) | ||
writeCache = func() { | ||
commit() | ||
cacheMemStore.Write() | ||
} | ||
|
||
return cc, writeCache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
corestoretypes "cosmossdk.io/core/store" | ||
"cosmossdk.io/log" | ||
"cosmossdk.io/store" | ||
storemetrics "cosmossdk.io/store/metrics" | ||
storetypes "cosmossdk.io/store/types" | ||
dbm "github.com/cosmos/cosmos-db" | ||
) | ||
|
||
const ( | ||
storeKey = "stateDB" | ||
) | ||
|
||
func newMemStore() (storetypes.MultiStore, storetypes.StoreKey) { | ||
memStoreKey := storetypes.NewMemoryStoreKey(storeKey) | ||
memStore := store.NewCommitMultiStore(dbm.NewMemDB(), log.NewNopLogger(), storemetrics.NewNoOpMetrics()) | ||
memStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) | ||
if err := memStore.LoadLatestVersion(); err != nil { | ||
panic(fmt.Sprintf("failed to initialize memory store: %v", err)) | ||
} | ||
|
||
return memStore, memStoreKey | ||
} | ||
|
||
// CoreKVStore is a wrapper of Core/Store kvstore interface | ||
// Remove after https://github.com/cosmos/cosmos-sdk/issues/14714 is closed | ||
type coreKVStore struct { | ||
kvStore storetypes.KVStore | ||
} | ||
|
||
// NewKVStore returns a wrapper of Core/Store kvstore interface | ||
// Remove once store migrates to core/store kvstore interface | ||
func newKVStore(store storetypes.KVStore) corestoretypes.KVStore { | ||
return coreKVStore{kvStore: store} | ||
} | ||
|
||
// Get returns nil iff key doesn't exist. Errors on nil key. | ||
func (store coreKVStore) Get(key []byte) ([]byte, error) { | ||
return store.kvStore.Get(key), nil | ||
} | ||
|
||
// Has checks if a key exists. Errors on nil key. | ||
func (store coreKVStore) Has(key []byte) (bool, error) { | ||
return store.kvStore.Has(key), nil | ||
} | ||
|
||
// Set sets the key. Errors on nil key or value. | ||
func (store coreKVStore) Set(key, value []byte) error { | ||
store.kvStore.Set(key, value) | ||
return nil | ||
} | ||
|
||
// Delete deletes the key. Errors on nil key. | ||
func (store coreKVStore) Delete(key []byte) error { | ||
store.kvStore.Delete(key) | ||
return nil | ||
} | ||
|
||
// Iterator iterates over a domain of keys in ascending order. End is exclusive. | ||
// Start must be less than end, or the Iterator is invalid. | ||
// Iterator must be closed by caller. | ||
// To iterate over entire domain, use store.Iterator(nil, nil) | ||
// CONTRACT: No writes may happen within a domain while an iterator exists over it. | ||
// Exceptionally allowed for cachekv.Store, safe to write in the modules. | ||
func (store coreKVStore) Iterator(start, end []byte) (store.Iterator, error) { | ||
return store.kvStore.Iterator(start, end), nil | ||
} | ||
|
||
// ReverseIterator iterates over a domain of keys in descending order. End is exclusive. | ||
// Start must be less than end, or the Iterator is invalid. | ||
// Iterator must be closed by caller. | ||
// CONTRACT: No writes may happen within a domain while an iterator exists over it. | ||
// Exceptionally allowed for cachekv.Store, safe to write in the modules. | ||
func (store coreKVStore) ReverseIterator(start, end []byte) (store.Iterator, error) { | ||
return store.kvStore.ReverseIterator(start, end), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.