From 88fd1b752ead6e5f31ac1f80914f60484a716c81 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 12 Jan 2017 14:32:34 -0800 Subject: [PATCH] Cache updates to state.store in readCache --- app/app.go | 4 +++- state/state.go | 37 +++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/app.go b/app/app.go index 6e3b439ef..3c092f9d6 100644 --- a/app/app.go +++ b/app/app.go @@ -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 diff --git a/state/state.go b/state/state.go index 3d767d6d8..901ee6887 100644 --- a/state/state.go +++ b/state/state.go @@ -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() } //----------------------------------------