diff --git a/core/state/database.go b/core/state/database.go index da26719816..b520fb3b80 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -238,6 +238,13 @@ func (db *cachingDB) purgeLoop() { // OpenTrie opens the main account trie at a specific root hash. func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { + //try cache first + if db.accountTrieCache != nil { + if tr, ok := db.accountTrieCache.Get(root); ok { + return tr.(*trie.SecureTrie), nil + } + } + tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb) if err != nil { return nil, err @@ -247,6 +254,17 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // OpenStorageTrie opens the storage trie of an account. func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error) { + // try cache first + if db.storageTrieCache != nil { + if tries, exist := db.storageTrieCache.Get(addrHash); exist { + for _, triePair := range tries { + if triePair != nil && triePair.root == root { + return triePair.trie.(*trie.SecureTrie).Copy(), nil + } + } + } + } + tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, addrHash, root), db.triedb) if err != nil { return nil, err