added locks to the tracer (#1022)

This commit is contained in:
Pratik Patil 2023-10-04 15:11:58 +05:30 committed by GitHub
parent d233e4f869
commit ceee53cb5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,6 +60,9 @@ func (t *Trie) newFlag() nodeFlag {
// Copy returns a copy of Trie. // Copy returns a copy of Trie.
func (t *Trie) Copy() *Trie { func (t *Trie) Copy() *Trie {
t.tracerMutex.Lock()
defer t.tracerMutex.Unlock()
return &Trie{ return &Trie{
root: t.root, root: t.root,
owner: t.owner, owner: t.owner,
@ -366,7 +369,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. // New branch node is created as a child of the original short node.
// Track the newly inserted node in the tracer. The node identifier // Track the newly inserted node in the tracer. The node identifier
// passed is the path from the root node. // passed is the path from the root node.
t.tracerMutex.Lock()
t.tracer.onInsert(append(prefix, key[:matchlen]...)) t.tracer.onInsert(append(prefix, key[:matchlen]...))
t.tracerMutex.Unlock()
// Replace it with a short node leading up to the branch. // Replace it with a short node leading up to the branch.
return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
@ -387,7 +392,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 // 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 // passed is the path from the root node. Note the valueNode won't be tracked
// since it's always embedded in its parent. // since it's always embedded in its parent.
t.tracerMutex.Lock()
t.tracer.onInsert(prefix) t.tracer.onInsert(prefix)
t.tracerMutex.Unlock()
return true, &shortNode{key, value, t.newFlag()}, nil return true, &shortNode{key, value, t.newFlag()}, nil
@ -453,7 +460,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
// The matched short node is deleted entirely and track // The matched short node is deleted entirely and track
// it in the deletion set. The same the valueNode doesn't // it in the deletion set. The same the valueNode doesn't
// need to be tracked at all since it's always embedded. // need to be tracked at all since it's always embedded.
t.tracerMutex.Lock()
t.tracer.onDelete(prefix) t.tracer.onDelete(prefix)
t.tracerMutex.Unlock()
return true, nil, nil // remove n entirely for whole matches return true, nil, nil // remove n entirely for whole matches
} }
@ -470,7 +479,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
case *shortNode: case *shortNode:
// The child shortNode is merged into its parent, track // The child shortNode is merged into its parent, track
// is deleted as well. // is deleted as well.
t.tracerMutex.Lock()
t.tracer.onDelete(append(prefix, n.Key...)) t.tracer.onDelete(append(prefix, n.Key...))
t.tracerMutex.Unlock()
// Deleting from the subtrie reduced it to another // Deleting from the subtrie reduced it to another
// short node. Merge the nodes to avoid creating a // short node. Merge the nodes to avoid creating a
@ -540,7 +551,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
// Replace the entire full node with the short node. // Replace the entire full node with the short node.
// Mark the original short node as deleted since the // Mark the original short node as deleted since the
// value is embedded into the parent now. // value is embedded into the parent now.
t.tracerMutex.Lock()
t.tracer.onDelete(append(prefix, byte(pos))) t.tracer.onDelete(append(prefix, byte(pos)))
t.tracerMutex.Unlock()
k := append([]byte{byte(pos)}, cnode.Key...) k := append([]byte{byte(pos)}, cnode.Key...)
@ -630,6 +643,9 @@ func (t *Trie) Hash() common.Hash {
// Once the trie is committed, it's not usable anymore. A new trie must // 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 // be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet) { func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet) {
t.tracerMutex.Lock()
defer t.tracerMutex.Unlock()
defer t.tracer.reset() defer t.tracer.reset()
nodes := NewNodeSet(t.owner, t.tracer.accessList) nodes := NewNodeSet(t.owner, t.tracer.accessList)
@ -682,5 +698,8 @@ func (t *Trie) Reset() {
t.root = nil t.root = nil
t.owner = common.Hash{} t.owner = common.Hash{}
t.unhashed = 0 t.unhashed = 0
t.tracerMutex.Lock()
t.tracer.reset() t.tracer.reset()
t.tracerMutex.Unlock()
} }