Skip to content

Commit

Permalink
trie: check for derefs while committing
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Sep 24, 2021
1 parent bb4f533 commit be80588
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions trie/committer.go
Expand Up @@ -19,6 +19,8 @@ package trie
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/log"
"runtime/debug"
"sync"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -77,7 +79,15 @@ func (c *committer) Commit(n node, db *Database) (hashNode, int, error) {
if db == nil {
return nil, 0, errors.New("no db provided")
}
derefcountA := db.DerefCount()
h, committed, err := c.commit(n, db)
derefcountB := db.DerefCount()
if derefcountA != derefcountB {
log.Warn("trie database dereference happened during commit.",
"before", derefcountA, "after", derefcountB)
log.Warn("Commit ended on error", "err", err)
debug.PrintStack()
}
if err != nil {
return nil, 0, err
}
Expand Down
7 changes: 7 additions & 0 deletions trie/database.go
Expand Up @@ -23,6 +23,7 @@ import (
"reflect"
"runtime"
"sync"
"sync/atomic"
"time"

"github.com/VictoriaMetrics/fastcache"
Expand Down Expand Up @@ -76,6 +77,7 @@ type Database struct {

preimages map[common.Hash][]byte // Preimages of nodes from the secure trie

derefs uint64 // counter on Dereference operations (accessed atomically)
gctime time.Duration // Time spent on garbage collection since last commit
gcnodes uint64 // Nodes garbage collected since last commit
gcsize common.StorageSize // Data storage garbage collected since last commit
Expand Down Expand Up @@ -509,6 +511,10 @@ func (db *Database) reference(child common.Hash, parent common.Hash) {
}
}

func (db *Database) DerefCount() uint64 {
return atomic.LoadUint64(&db.derefs)
}

// Dereference removes an existing reference from a root node.
func (db *Database) Dereference(root common.Hash) {
// Sanity check to ensure that the meta-root is not removed
Expand All @@ -518,6 +524,7 @@ func (db *Database) Dereference(root common.Hash) {
}
db.lock.Lock()
defer db.lock.Unlock()
atomic.AddUint64(&db.derefs, 1)

nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now()
db.dereference(root, common.Hash{})
Expand Down

0 comments on commit be80588

Please sign in to comment.