Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAkhunov committed Aug 10, 2018
1 parent 674595d commit 80cfa77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 15 additions & 10 deletions state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Database struct {
// EXTCODESIZE calls.
codeSizeCache *lru.Cache

dataCache *lru.Cache
storeCache *lru.Cache

Tracing bool
}
Expand All @@ -68,7 +68,7 @@ type Database struct {
// implements Ethereum's state.Database interface. An error is returned if the
// latest state failed to load. The underlying storage structure is defined by
// the Cosmos SDK IAVL tree.
func NewDatabase(stateDB, codeDB dbm.DB, dataCacheSize int) (*Database, error) {
func NewDatabase(stateDB, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
// Initialize an implementation of Ethereum state.Database and create a
// Cosmos SDK multi-store.
db := &Database{
Expand All @@ -95,8 +95,13 @@ func NewDatabase(stateDB, codeDB dbm.DB, dataCacheSize int) (*Database, error) {
db.codeDB = codeDB
db.ethTrieDB = ethtrie.NewDatabase(&core.EthereumDB{CodeDB: codeDB})

db.codeSizeCache, _ = lru.New(codeSizeCacheSize)
db.dataCache, _ = lru.New(dataCacheSize)
var err error
if db.codeSizeCache, err = lru.New(codeSizeCacheSize); err != nil {
return nil, err
}
if db.storeCache, err = lru.New(storeCacheSize); err != nil {
return nil, err
}

return db, nil
}
Expand Down Expand Up @@ -136,7 +141,7 @@ func (db *Database) OpenTrie(root ethcmn.Hash) (ethstate.Trie, error) {
store: db.accountsCache,
accountsCache: db.accountsCache,
storageCache: db.storageCache,
dataCache: db.dataCache,
storeCache: db.storeCache,
ethTrieDB: db.ethTrieDB,
empty: isRootEmpty(root),
root: rootHashFromVersion(db.stateStore.LastCommitID().Version),
Expand All @@ -158,11 +163,11 @@ func (db *Database) OpenStorageTrie(addrHash, root ethcmn.Hash) (ethstate.Trie,
// a contract storage trie does not need an accountCache, storageCache or
// an Ethereum trie because it will not be used upon commitment.
return &Trie{
store: db.storageCache,
dataCache: db.dataCache,
prefix: addrHash.Bytes(),
empty: isRootEmpty(root),
root: rootHashFromVersion(db.stateStore.LastCommitID().Version),
store: db.storageCache,
storeCache: db.storeCache,
prefix: addrHash.Bytes(),
empty: isRootEmpty(root),
root: rootHashFromVersion(db.stateStore.LastCommitID().Version),
}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions state/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Trie struct {
// ordering.
storageCache store.CacheKVStore

dataCache *lru.Cache
storeCache *lru.Cache

// Store is an IAVL KV store that is part of a larger store except it used
// for a specific prefix. It will either be an accountsCache or a
Expand Down Expand Up @@ -69,11 +69,11 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) {
key = t.prefixKey(key)
}
keyStr := string(key)
if cached, ok := t.dataCache.Get(keyStr); ok {
if cached, ok := t.storeCache.Get(keyStr); ok {
return cached.([]byte), nil
}
value := t.store.Get(key)
t.dataCache.Add(keyStr, value)
t.storeCache.Add(keyStr, value)
return value, nil
}

Expand All @@ -92,7 +92,7 @@ func (t *Trie) TryUpdate(key, value []byte) error {
}

t.store.Set(key, value)
t.dataCache.Add(string(key), value)
t.storeCache.Add(string(key), value)
return nil
}

Expand All @@ -108,7 +108,7 @@ func (t *Trie) TryDelete(key []byte) error {
}

t.store.Delete(key)
t.dataCache.Remove(string(key))
t.storeCache.Remove(string(key))
return nil
}

Expand Down

0 comments on commit 80cfa77

Please sign in to comment.