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

View File

@ -9,15 +9,18 @@ import (
// CONTRACT: State should be quick to copy. // CONTRACT: State should be quick to copy.
// See CacheWrap(). // See CacheWrap().
type State struct { type State struct {
chainID string chainID string
store types.KVStore store types.KVStore
cache *types.KVCache // optional 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 { func NewState(store types.KVStore) *State {
return &State{ return &State{
chainID: "", chainID: "",
store: store, 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) { 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) return s.store.Get(key)
} }
func (s *State) Set(key []byte, value []byte) { func (s *State) Set(key []byte, value []byte) {
if s.readCache != nil {
s.readCache[string(key)] = value
}
s.store.Set(key, value) s.store.Set(key, value)
} }
func (s *State) GetAccount(addr []byte) *types.Account { 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) { func (s *State) SetAccount(addr []byte, acc *types.Account) {
SetAccount(s.store, addr, acc) SetAccount(s, addr, acc)
} }
func (s *State) CacheWrap() *State { func (s *State) CacheWrap() *State {
cache := types.NewKVCache(s.store) cache := types.NewKVCache(s)
return &State{ return &State{
chainID: s.chainID, chainID: s.chainID,
store: cache, store: cache,
cache: cache, readCache: nil,
writeCache: cache,
} }
} }
// NOTE: errors if s is not from CacheWrap() // NOTE: errors if s is not from CacheWrap()
func (s *State) CacheSync() { func (s *State) CacheSync() {
s.cache.Sync() s.writeCache.Sync()
} }
//---------------------------------------- //----------------------------------------