From f7332e9d6e356e5b3146bc6301e938476cf75758 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Fri, 5 Sep 2025 12:10:20 +0800 Subject: [PATCH] all: polish --- cmd/utils/flags.go | 2 +- core/blockchain.go | 6 +++--- core/state/state_sizer.go | 23 +++++++++++------------ eth/api_debug.go | 2 -- eth/backend.go | 4 ++-- eth/ethconfig/gen_config.go | 6 ++++++ triedb/database.go | 9 --------- triedb/pathdb/database.go | 13 +------------ triedb/pathdb/disklayer.go | 2 +- 9 files changed, 25 insertions(+), 42 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 2111893110..aca3ce9196 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2221,7 +2221,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh TrieJournalDirectory: stack.ResolvePath("triedb"), // Enable state size tracking if enabled - EnableStateSizeTracking: ctx.Bool(StateSizeTrackingFlag.Name), + StateSizeTracking: ctx.Bool(StateSizeTrackingFlag.Name), } if options.ArchiveMode && !options.Preimages { options.Preimages = true diff --git a/core/blockchain.go b/core/blockchain.go index cb7fb0c4e3..2d5527a114 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -197,8 +197,8 @@ type BlockChainConfig struct { // If the value is -1, indexing is disabled. TxLookupLimit int64 - // EnableStateSizeTracking indicates whether the state size tracking is enabled. - EnableStateSizeTracking bool + // StateSizeTracking indicates whether the state size tracking is enabled. + StateSizeTracking bool } // DefaultConfig returns the default config. @@ -532,7 +532,7 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine, } // Start state size tracker - if bc.cfg.EnableStateSizeTracking { + if bc.cfg.StateSizeTracking { stateSizer, err := state.NewSizeTracker(bc.db, bc.triedb) if err == nil { bc.stateSizer = stateSizer diff --git a/core/state/state_sizer.go b/core/state/state_sizer.go index 784d8f8e11..ede4b5b384 100644 --- a/core/state/state_sizer.go +++ b/core/state/state_sizer.go @@ -292,6 +292,7 @@ func (t *SizeTracker) run() { case u := <-t.updateCh: base, found := stats[u.originRoot] if !found { + log.Debug("Ignored the state size without parent", "parent", u.originRoot, "root", u.root, "number", u.blockNumber) continue } diff, err := calSizeStats(u) @@ -352,8 +353,6 @@ wait: done chan buildResult ) - t.triedb.SetForceFlush(true) - for { select { case u := <-t.updateCh: @@ -378,8 +377,6 @@ wait: log.Info("Measuring persistent state size", "root", root.Hex(), "number", entry.blockNumber) case result := <-done: - t.triedb.SetForceFlush(false) - if result.err != nil { return nil, result.err } @@ -410,6 +407,7 @@ wait: // Set initial latest stats stats[result.root] = result.stat + t.mu.Lock() t.latestStats = &result.stat t.mu.Unlock() @@ -561,16 +559,18 @@ func (t *SizeTracker) iterateTable(closed chan struct{}, prefix []byte, name str return count, bytes, nil } -// iterateTableParallel performs parallel iteration over a table by splitting into hex ranges -// For storage tables, it splits on the first byte of the account hash (after the prefix) +// iterateTableParallel performs parallel iteration over a table by splitting into +// hex ranges. For storage tables, it splits on the first byte of the account hash +// (after the prefix). func (t *SizeTracker) iterateTableParallel(closed chan struct{}, prefix []byte, name string) (int64, int64, error) { var ( - start = time.Now() - workers = runtime.NumCPU() totalCount int64 totalBytes int64 - group errgroup.Group - mu sync.Mutex + + start = time.Now() + workers = runtime.NumCPU() + group errgroup.Group + mu sync.Mutex ) group.SetLimit(workers) @@ -598,11 +598,9 @@ func (t *SizeTracker) iterateTableParallel(closed chan struct{}, prefix []byte, return nil }) } - if err := group.Wait(); err != nil { return 0, 0, err } - log.Info("Finished parallel state iteration", "category", name, "count", totalCount, "size", common.StorageSize(totalBytes), "elapsed", common.PrettyDuration(time.Since(start))) return totalCount, totalBytes, nil } @@ -611,5 +609,6 @@ func (t *SizeTracker) iterateTableParallel(closed chan struct{}, prefix []byte, func (t *SizeTracker) GetLatestStats() *SizeStats { t.mu.RLock() defer t.mu.RUnlock() + return t.latestStats } diff --git a/eth/api_debug.go b/eth/api_debug.go index 3ea6aaeb9e..703c584fbc 100644 --- a/eth/api_debug.go +++ b/eth/api_debug.go @@ -451,12 +451,10 @@ func (api *DebugAPI) StateSize() (interface{}, error) { if sizer == nil { return nil, errors.New("state size tracker is not enabled") } - stats := sizer.GetLatestStats() if stats == nil { return nil, errors.New("state size statistics are not ready yet") } - return map[string]interface{}{ "stateRoot": stats.StateRoot, "blockNumber": hexutil.Uint64(stats.BlockNumber), diff --git a/eth/backend.go b/eth/backend.go index ef28878a2d..4356733189 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -240,8 +240,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { // within the data directory. The corresponding paths will be either: // - DATADIR/triedb/merkle.journal // - DATADIR/triedb/verkle.journal - TrieJournalDirectory: stack.ResolvePath("triedb"), - EnableStateSizeTracking: config.EnableStateSizeTracking, + TrieJournalDirectory: stack.ResolvePath("triedb"), + StateSizeTracking: config.EnableStateSizeTracking, } ) if config.VMTrace != "" { diff --git a/eth/ethconfig/gen_config.go b/eth/ethconfig/gen_config.go index 0a188ba23c..2fdd219dee 100644 --- a/eth/ethconfig/gen_config.go +++ b/eth/ethconfig/gen_config.go @@ -49,6 +49,7 @@ func (c Config) MarshalTOML() (interface{}, error) { BlobPool blobpool.Config GPO gasprice.Config EnablePreimageRecording bool + EnableStateSizeTracking bool VMTrace string VMTraceJsonConfig string RPCGasCap uint64 @@ -90,6 +91,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.BlobPool = c.BlobPool enc.GPO = c.GPO enc.EnablePreimageRecording = c.EnablePreimageRecording + enc.EnableStateSizeTracking = c.EnableStateSizeTracking enc.VMTrace = c.VMTrace enc.VMTraceJsonConfig = c.VMTraceJsonConfig enc.RPCGasCap = c.RPCGasCap @@ -135,6 +137,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { BlobPool *blobpool.Config GPO *gasprice.Config EnablePreimageRecording *bool + EnableStateSizeTracking *bool VMTrace *string VMTraceJsonConfig *string RPCGasCap *uint64 @@ -243,6 +246,9 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.EnablePreimageRecording != nil { c.EnablePreimageRecording = *dec.EnablePreimageRecording } + if dec.EnableStateSizeTracking != nil { + c.EnableStateSizeTracking = *dec.EnableStateSizeTracking + } if dec.VMTrace != nil { c.VMTrace = *dec.VMTrace } diff --git a/triedb/database.go b/triedb/database.go index 37a57ac022..d2637bd909 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -384,12 +384,3 @@ func (db *Database) SnapshotCompleted() bool { } return pdb.SnapshotCompleted() } - -// 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) SetForceFlush(enabled bool) { - if pdb, ok := db.backend.(*pathdb.Database); ok { - pdb.SetForceFlush(enabled) - } -} diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 0ca5d8a73a..0be875af8f 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -136,9 +136,7 @@ type Database struct { stateFreezer ethdb.ResettableAncientStore // Freezer for storing state histories, nil possible in tests stateIndexer *historyIndexer // History indexer historical state data, nil possible - - lock sync.RWMutex // Lock to prevent mutations from happening at the same time - forceFlush bool // Flag to force buffer flush regardless of size + lock sync.RWMutex // Lock to prevent mutations from happening at the same time } // New attempts to load an already existing layer from a persistent key-value @@ -361,15 +359,6 @@ func (db *Database) Commit(root common.Hash, report bool) error { return db.tree.cap(root, 0) } -// SetForceFlush enables or disables force flushing for the next state update. -func (db *Database) SetForceFlush(enabled bool) { - db.lock.Lock() - defer db.lock.Unlock() - - 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 // as stale to prevent access to the persistent state, which is in the syncing // stage. diff --git a/triedb/pathdb/disklayer.go b/triedb/pathdb/disklayer.go index 5a53167203..2042e91611 100644 --- a/triedb/pathdb/disklayer.go +++ b/triedb/pathdb/disklayer.go @@ -417,7 +417,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) { // Terminate the background state snapshot generation before mutating the // persistent state. - if combined.full() || force || flush || dl.db.forceFlush { + if combined.full() || force || flush { // Wait until the previous frozen buffer is fully flushed if dl.frozen != nil { if err := dl.frozen.waitFlush(); err != nil {