Merge pull request #837 from cosmos/joon/824-cachecontext
Add CacheContext
This commit is contained in:
commit
9d90c6b8f7
|
@ -7,6 +7,10 @@ BREAKING CHANGES
|
|||
* Remove go-wire, use go-amino
|
||||
* [store] Add `SubspaceIterator` and `ReverseSubspaceIterator` to `KVStore` interface
|
||||
|
||||
FEATURES:
|
||||
|
||||
* Add CacheContext
|
||||
|
||||
## 0.14.1 (April 9, 2018)
|
||||
|
||||
BUG FIXES
|
||||
|
|
|
@ -171,6 +171,14 @@ func (c Context) WithTxBytes(txBytes []byte) Context {
|
|||
return c.withValue(contextKeyTxBytes, txBytes)
|
||||
}
|
||||
|
||||
// Cache the multistore and return a new cached context. The cached context is
|
||||
// written to the context when writeCache is called.
|
||||
func (c Context) CacheContext() (cc Context, writeCache func()) {
|
||||
cms := c.multiStore().CacheMultiStore()
|
||||
cc = c.WithMultiStore(cms)
|
||||
return cc, cms.Write
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
// thePast
|
||||
|
||||
|
|
|
@ -3,6 +3,11 @@ package types_test
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
)
|
||||
|
@ -18,3 +23,39 @@ func TestContextGetOpShouldNeverPanic(t *testing.T) {
|
|||
_, _ = ctx.GetOp(index)
|
||||
}
|
||||
}
|
||||
|
||||
func defaultContext(key types.StoreKey) types.Context {
|
||||
db := dbm.NewMemDB()
|
||||
cms := store.NewCommitMultiStore(db)
|
||||
cms.MountStoreWithDB(key, types.StoreTypeIAVL, db)
|
||||
cms.LoadLatestVersion()
|
||||
ctx := types.NewContext(cms, abci.Header{}, false, nil)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCacheContext(t *testing.T) {
|
||||
key := types.NewKVStoreKey(t.Name())
|
||||
k1 := []byte("hello")
|
||||
v1 := []byte("world")
|
||||
k2 := []byte("key")
|
||||
v2 := []byte("value")
|
||||
|
||||
ctx := defaultContext(key)
|
||||
store := ctx.KVStore(key)
|
||||
store.Set(k1, v1)
|
||||
assert.Equal(t, v1, store.Get(k1))
|
||||
assert.Nil(t, store.Get(k2))
|
||||
|
||||
cctx, write := ctx.CacheContext()
|
||||
cstore := cctx.KVStore(key)
|
||||
assert.Equal(t, v1, cstore.Get(k1))
|
||||
assert.Nil(t, cstore.Get(k2))
|
||||
|
||||
cstore.Set(k2, v2)
|
||||
assert.Equal(t, v2, cstore.Get(k2))
|
||||
assert.Nil(t, store.Get(k2))
|
||||
|
||||
write()
|
||||
|
||||
assert.Equal(t, v2, store.Get(k2))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue