diff --git a/trie/database.go b/trie/database.go index 1fac2abb1..f55a132f0 100644 --- a/trie/database.go +++ b/trie/database.go @@ -121,6 +121,14 @@ func (c *HashCache) PutIfAbsent(key []byte, value node) { } } +// Contains checks whether the key was cached +func (c *HashCache) Contains(key []byte) bool { + c.lock.RLock() + defer c.lock.RUnlock() + _, exist := c.inner[common.BytesToHash(key)] + return exist +} + // Get reads value of given key from cache with lock protection func (c *HashCache) Get(key common.Hash) (node, bool) { c.lock.RLock() diff --git a/trie/hasher.go b/trie/hasher.go index 0414f6e29..159073f3c 100644 --- a/trie/hasher.go +++ b/trie/hasher.go @@ -76,6 +76,9 @@ func returnHasherToPool(h *hasher) { func (h *hasher) hash(n node, force bool) (hashed node, cached node) { // Return the cached hash if it's available if hash, _ := n.cache(); hash != nil { + if h.dirties != nil && !h.dirties.Contains(hash) { + h.dirties.Put(hash, n) + } return hash, n } // Trie not processed yet, walk the children