|
|
@ -48,6 +48,10 @@ type LeafCallback func(leaf []byte, parent common.Hash) error |
|
|
|
type Trie struct { |
|
|
|
type Trie struct { |
|
|
|
db *Database |
|
|
|
db *Database |
|
|
|
root node |
|
|
|
root node |
|
|
|
|
|
|
|
// Keep track of the number leafs which have been inserted since the last
|
|
|
|
|
|
|
|
// hashing operation. This number will not directly map to the number of
|
|
|
|
|
|
|
|
// actually unhashed nodes
|
|
|
|
|
|
|
|
unhashed int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// newFlag returns the cache flag value for a newly created node.
|
|
|
|
// newFlag returns the cache flag value for a newly created node.
|
|
|
@ -163,6 +167,7 @@ func (t *Trie) Update(key, value []byte) { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) TryUpdate(key, value []byte) error { |
|
|
|
func (t *Trie) TryUpdate(key, value []byte) error { |
|
|
|
|
|
|
|
t.unhashed++ |
|
|
|
k := keybytesToHex(key) |
|
|
|
k := keybytesToHex(key) |
|
|
|
if len(value) != 0 { |
|
|
|
if len(value) != 0 { |
|
|
|
_, n, err := t.insert(t.root, nil, k, valueNode(value)) |
|
|
|
_, n, err := t.insert(t.root, nil, k, valueNode(value)) |
|
|
@ -259,6 +264,7 @@ func (t *Trie) Delete(key []byte) { |
|
|
|
// TryDelete removes any existing value for key from the trie.
|
|
|
|
// TryDelete removes any existing value for key from the trie.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) TryDelete(key []byte) error { |
|
|
|
func (t *Trie) TryDelete(key []byte) error { |
|
|
|
|
|
|
|
t.unhashed++ |
|
|
|
k := keybytesToHex(key) |
|
|
|
k := keybytesToHex(key) |
|
|
|
_, n, err := t.delete(t.root, nil, k) |
|
|
|
_, n, err := t.delete(t.root, nil, k) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -405,7 +411,7 @@ func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { |
|
|
|
// Hash returns the root hash of the trie. It does not write to the
|
|
|
|
// Hash returns the root hash of the trie. It does not write to the
|
|
|
|
// database and can be used even if the trie doesn't have one.
|
|
|
|
// database and can be used even if the trie doesn't have one.
|
|
|
|
func (t *Trie) Hash() common.Hash { |
|
|
|
func (t *Trie) Hash() common.Hash { |
|
|
|
hash, cached, _ := t.hashRoot(nil, nil) |
|
|
|
hash, cached, _ := t.hashRoot(nil) |
|
|
|
t.root = cached |
|
|
|
t.root = cached |
|
|
|
return common.BytesToHash(hash.(hashNode)) |
|
|
|
return common.BytesToHash(hash.(hashNode)) |
|
|
|
} |
|
|
|
} |
|
|
@ -456,12 +462,14 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// hashRoot calculates the root hash of the given trie
|
|
|
|
// hashRoot calculates the root hash of the given trie
|
|
|
|
func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) { |
|
|
|
func (t *Trie) hashRoot(db *Database) (node, node, error) { |
|
|
|
if t.root == nil { |
|
|
|
if t.root == nil { |
|
|
|
return hashNode(emptyRoot.Bytes()), nil, nil |
|
|
|
return hashNode(emptyRoot.Bytes()), nil, nil |
|
|
|
} |
|
|
|
} |
|
|
|
h := newHasher() |
|
|
|
// If the number of changes is below 100, we let one thread handle it
|
|
|
|
|
|
|
|
h := newHasher(t.unhashed >= 100) |
|
|
|
defer returnHasherToPool(h) |
|
|
|
defer returnHasherToPool(h) |
|
|
|
hashed, cached := h.hash(t.root, true) |
|
|
|
hashed, cached := h.hash(t.root, true) |
|
|
|
|
|
|
|
t.unhashed = 0 |
|
|
|
return hashed, cached, nil |
|
|
|
return hashed, cached, nil |
|
|
|
} |
|
|
|
} |
|
|
|