diff --git a/core/blockchain.go b/core/blockchain.go index eea945f30c..83c09336d7 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -536,7 +536,6 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine, stateSizer, err := state.NewSizeTracker(bc.db, bc.triedb) if err == nil { bc.stateSizer = stateSizer - triedb.EnableForceFlush() } else { log.Info("Failed to setup size tracker", "err", err) } diff --git a/core/state/state_sizer.go b/core/state/state_sizer.go index 3692be85f8..8a0f8c6009 100644 --- a/core/state/state_sizer.go +++ b/core/state/state_sizer.go @@ -360,6 +360,8 @@ wait: done chan buildResult ) + t.triedb.SetForceFlush(true) + for { select { case u := <-t.updateCh: @@ -384,6 +386,8 @@ wait: log.Info("Measuring persistent state size", "root", root.Hex()) case result := <-done: + t.triedb.SetForceFlush(false) + if result.err != nil { return nil, result.err } diff --git a/triedb/database.go b/triedb/database.go index 61f612d7cc..37a57ac022 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -385,11 +385,11 @@ func (db *Database) SnapshotCompleted() bool { return pdb.SnapshotCompleted() } -// EnableForceFlush enables the pathdb to flush any pending changes to disk immediately, -// regardless of the buffer size threshold. This can be used to accelerate +// SetForceFlush enables or disables the pathdb to flush any pending changes to disk +// immediately, regardless of the buffer size threshold. This can be used to accelerate // state sizer initialization by making buffered state changes visible on disk. -func (db *Database) EnableForceFlush() { +func (db *Database) SetForceFlush(enabled bool) { if pdb, ok := db.backend.(*pathdb.Database); ok { - pdb.EnableForceFlush() + pdb.SetForceFlush(enabled) } } diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index f708e8ef0a..0ca5d8a73a 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -361,17 +361,13 @@ func (db *Database) Commit(root common.Hash, report bool) error { return db.tree.cap(root, 0) } -// EnableForceFlush enables force flushing for the next state update. -// This will cause the next Update() call to flush the disk buffer immediately, -// regardless of the buffer threshold, while preserving the 128 diff layers in memory. -func (db *Database) EnableForceFlush() { +// SetForceFlush enables or disables force flushing for the next state update. +func (db *Database) SetForceFlush(enabled bool) { db.lock.Lock() defer db.lock.Unlock() - if !db.forceFlush { - log.Info("Enabling force flush for next pathdb update") - db.forceFlush = true - } + db.forceFlush = enabled + log.Info("Set triedb force flush for next pathdb update", "enabled", enabled) } // Disable deactivates the database and invalidates all available state layers