Skip to content

Commit

Permalink
Cache updates to state.store in readCache
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jan 12, 2017
1 parent c1c79d1 commit 88fd1b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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
Expand Down
37 changes: 25 additions & 12 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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()
}

//----------------------------------------
Expand Down

0 comments on commit 88fd1b7

Please sign in to comment.