Skip to content

Commit

Permalink
added locks to the tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsharma committed Nov 28, 2023
1 parent 955264d commit a7f72a9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (t *Trie) newFlag() nodeFlag {

// Copy returns a copy of Trie.
func (t *Trie) Copy() *Trie {
t.tracerMutex.Lock()
defer t.tracerMutex.Unlock()

return &Trie{
root: t.root,
owner: t.owner,
Expand Down Expand Up @@ -396,7 +399,9 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error
// New branch node is created as a child of the original short node.
// Track the newly inserted node in the tracer. The node identifier
// passed is the path from the root node.
t.tracerMutex.Lock()
t.tracer.onInsert(append(prefix, key[:matchlen]...))
t.tracerMutex.Unlock()

// Replace it with a short node leading up to the branch.
return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
Expand All @@ -417,7 +422,9 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error
// New short node is created and track it in the tracer. The node identifier
// passed is the path from the root node. Note the valueNode won't be tracked
// since it's always embedded in its parent.
t.tracerMutex.Lock()
t.tracer.onInsert(prefix)
t.tracerMutex.Unlock()

return true, &shortNode{key, value, t.newFlag()}, nil

Expand Down Expand Up @@ -487,7 +494,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
// The matched short node is deleted entirely and track
// it in the deletion set. The same the valueNode doesn't
// need to be tracked at all since it's always embedded.
t.tracerMutex.Lock()
t.tracer.onDelete(prefix)
t.tracerMutex.Unlock()

return true, nil, nil // remove n entirely for whole matches
}
Expand All @@ -504,7 +513,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
case *shortNode:
// The child shortNode is merged into its parent, track
// is deleted as well.
t.tracerMutex.Lock()
t.tracer.onDelete(append(prefix, n.Key...))
t.tracerMutex.Unlock()

// Deleting from the subtrie reduced it to another
// short node. Merge the nodes to avoid creating a
Expand Down Expand Up @@ -574,7 +585,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
// Replace the entire full node with the short node.
// Mark the original short node as deleted since the
// value is embedded into the parent now.
t.tracerMutex.Lock()
t.tracer.onDelete(append(prefix, byte(pos)))
t.tracerMutex.Unlock()

k := append([]byte{byte(pos)}, cnode.Key...)

Expand Down Expand Up @@ -664,6 +677,9 @@ func (t *Trie) Hash() common.Hash {
// Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
t.tracerMutex.Lock()
defer t.tracerMutex.Unlock()

defer t.tracer.reset()
defer func() {
t.committed = true
Expand Down Expand Up @@ -726,6 +742,10 @@ func (t *Trie) Reset() {
t.root = nil
t.owner = common.Hash{}
t.unhashed = 0

t.tracerMutex.Lock()
t.tracer.reset()
t.tracerMutex.Unlock()

t.committed = false
}

0 comments on commit a7f72a9

Please sign in to comment.