diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2016-10-19 19:35:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-19 19:35:49 +0800 |
commit | 25ac04a444d82f42138fc06e651c1ef9bac935dc (patch) | |
tree | 4b4925de834f243a05c73e661e77bc60287aeb9d /trie/trie.go | |
parent | 8e52c2e754cdb343d0eb880a33251e1ba593d327 (diff) | |
parent | 8d56bf5ceb74a7ed45c986450848a89e2df61189 (diff) | |
download | dexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.tar.gz dexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.tar.zst dexon-25ac04a444d82f42138fc06e651c1ef9bac935dc.zip |
Merge pull request #3153 from fjl/trie-unload-fix
trie: improve cache unloading mechanism
Diffstat (limited to 'trie/trie.go')
-rw-r--r-- | trie/trie.go | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/trie/trie.go b/trie/trie.go index 5a4b6185d..914bf20fa 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -105,13 +105,11 @@ func New(root common.Hash, db Database) (*Trie, error) { if db == nil { panic("trie.New: cannot use existing root without a database") } - if v, _ := trie.db.Get(root[:]); len(v) == 0 { - return nil, &MissingNodeError{ - RootHash: root, - NodeHash: root, - } + rootnode, err := trie.resolveHash(root[:], nil, nil) + if err != nil { + return nil, err } - trie.root = hashNode(root.Bytes()) + trie.root = rootnode } return trie, nil } @@ -158,14 +156,15 @@ func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode if err == nil && didResolve { n = n.copy() n.Val = newnode + n.flags.gen = t.cachegen } return value, n, didResolve, err case *fullNode: value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1) if err == nil && didResolve { n = n.copy() + n.flags.gen = t.cachegen n.Children[key[pos]] = newnode - } return value, n, didResolve, err case hashNode: @@ -261,7 +260,8 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error return false, n, err } n = n.copy() - n.Children[key[0]], n.flags.hash, n.flags.dirty = nn, nil, true + n.flags = t.newFlag() + n.Children[key[0]] = nn return true, n, nil case nil: @@ -345,7 +345,8 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { return false, n, err } n = n.copy() - n.Children[key[0]], n.flags.hash, n.flags.dirty = nn, nil, true + n.flags = t.newFlag() + n.Children[key[0]] = nn // Check how many non-nil entries are left after deleting and // reduce the full node to a short node if only one entry is @@ -443,7 +444,7 @@ func (t *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) { SuffixLen: len(suffix), } } - dec := mustDecodeNode(n, enc) + dec := mustDecodeNode(n, enc, t.cachegen) return dec, nil } |