From 793293f0413674ab45ac372e9c040bc4b47ca011 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Fri, 22 Aug 2025 16:38:30 +0800 Subject: [PATCH] trie: move locking into trieDB insert method #25030 (#1091) Move locking into trieDB insert function Co-authored-by: aaronbuchwald --- trie/committer.go | 6 +----- trie/database.go | 5 ++++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/trie/committer.go b/trie/committer.go index de3f67902a..170615e5e3 100644 --- a/trie/committer.go +++ b/trie/committer.go @@ -193,9 +193,7 @@ func (c *committer) store(n node, db *Database) node { } else if db != nil { // No leaf-callback used, but there's still a database. Do serial // insertion - db.Lock.Lock() db.insert(common.BytesToHash(hash), size, n) - db.Lock.Unlock() } return hash } @@ -208,10 +206,8 @@ func (c *committer) commitLoop(db *Database) { size = item.size n = item.node ) - // We are pooling the trie nodes into an intermediate memory Cache - db.Lock.Lock() + // We are pooling the trie nodes into an intermediate memory cache db.insert(hash, size, n) - db.Lock.Unlock() if c.onleaf != nil { switch n := n.(type) { diff --git a/trie/database.go b/trie/database.go index 86d1b578ed..2f07aa890c 100644 --- a/trie/database.go +++ b/trie/database.go @@ -304,7 +304,10 @@ func (db *Database) DiskDB() ethdb.KeyValueStore { // All nodes inserted by this function will be reference tracked // and in theory should only used for **trie nodes** insertion. func (db *Database) insert(hash common.Hash, size int, node node) { - // If the Node's already cached, skip + db.Lock.Lock() + defer db.Lock.Unlock() + + // If the node's already cached, skip if _, ok := db.dirties[hash]; ok { return }