Cache updates to state.store in readCache

This commit is contained in:
Jae Kwon 2017-01-12 14:32:34 -08:00
parent c1c79d1e3d
commit 88fd1b752e
2 changed files with 28 additions and 13 deletions

View File

@ -131,6 +131,9 @@ func (app *Basecoin) Commit() (res tmsp.Result) {
// Commit eyes.
res = app.eyesCli.CommitSync()
// Wrap the committed state in cache for CheckTx
app.cacheState = app.state.CacheWrap()
if res.IsErr() {
PanicSanity("Error getting hash: " + res.Error())
}
@ -149,7 +152,6 @@ func (app *Basecoin) BeginBlock(height uint64) {
for _, plugin := range app.plugins.GetList() {
plugin.Plugin.BeginBlock(app.state, height)
}
app.cacheState = app.state.CacheWrap()
}
// TMSP::EndBlock

View File

@ -9,15 +9,18 @@ import (
// CONTRACT: State should be quick to copy.
// See CacheWrap().
type State struct {
chainID string
store types.KVStore
cache *types.KVCache // optional
chainID string
store types.KVStore
readCache map[string][]byte // optional, for caching writes to store
writeCache *types.KVCache // optional, for caching writes w/o writing to store
}
func NewState(store types.KVStore) *State {
return &State{
chainID: "",
store: store,
chainID: "",
store: store,
readCache: make(map[string][]byte),
writeCache: nil,
}
}
@ -33,33 +36,43 @@ func (s *State) GetChainID() string {
}
func (s *State) Get(key []byte) (value []byte) {
if s.readCache != nil {
value, ok := s.readCache[string(key)]
if ok {
return value
}
}
return s.store.Get(key)
}
func (s *State) Set(key []byte, value []byte) {
if s.readCache != nil {
s.readCache[string(key)] = value
}
s.store.Set(key, value)
}
func (s *State) GetAccount(addr []byte) *types.Account {
return GetAccount(s.store, addr)
return GetAccount(s, addr)
}
func (s *State) SetAccount(addr []byte, acc *types.Account) {
SetAccount(s.store, addr, acc)
SetAccount(s, addr, acc)
}
func (s *State) CacheWrap() *State {
cache := types.NewKVCache(s.store)
cache := types.NewKVCache(s)
return &State{
chainID: s.chainID,
store: cache,
cache: cache,
chainID: s.chainID,
store: cache,
readCache: nil,
writeCache: cache,
}
}
// NOTE: errors if s is not from CacheWrap()
func (s *State) CacheSync() {
s.cache.Sync()
s.writeCache.Sync()
}
//----------------------------------------