From e869586e134e481fad27e36f877ba78ec4cbf2f9 Mon Sep 17 00:00:00 2001 From: shantichanal <158101918+shantichanal@users.noreply.github.com> Date: Mon, 11 Aug 2025 03:00:20 +0300 Subject: [PATCH] made changes for tracking depth during block processing --- core/blockchain.go | 32 ----------------------- core/state/statedb.go | 8 ++++++ core/state_processor.go | 3 +++ trie/trie.go | 4 +++ trie/trie_metrics.go | 56 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 trie/trie_metrics.go diff --git a/core/blockchain.go b/core/blockchain.go index 94e74a7460..08d4c6f268 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2134,38 +2134,6 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s mgasps := float64(res.GasUsed) * 1000 / float64(elapsed) chainMgaspsMeter.Update(time.Duration(mgasps)) - //get state db trie node iterator - it, error := statedb.GetTrie().NodeIterator(nil) - if error != nil { - //log that iterator was not working - log.Error("Failed to get trie node iterator", "error", error) - } else { - var ( - maxDepth int - sumDepth int - count int - ) - - for it.Next(true) { - // it.Path() is a hex-nibble slice; the “terminator” nibble (0x10) - // exists only on leaf-nodes, so strip it if present. - d := len(it.Path()) - if d > 0 && it.Path()[d-1] == 0x10 { - d-- - } - if d > maxDepth { - maxDepth = d - } - sumDepth += d - count++ - } - - stateMaxDepthGauge.Update(int64(maxDepth)) - if count > 0 { - stateAvgDepthGauge.Update(int64(sumDepth / count)) - } - } - return &blockProcessingResult{ usedGas: res.GasUsed, procTime: proctime, diff --git a/core/state/statedb.go b/core/state/statedb.go index efb09a08a0..cc2154cfb9 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1464,3 +1464,11 @@ func (s *StateDB) Witness() *stateless.Witness { func (s *StateDB) AccessEvents() *AccessEvents { return s.accessEvents } + +func (s *StateDB) BeginTrieDepthWindow() { + trie.StateDepthMetricsStartBlock() +} + +func (s *StateDB) EndTrieDepthWindow() { + trie.StateDepthMetricsEndBlock() +} diff --git a/core/state_processor.go b/core/state_processor.go index ee98326467..ca0d27ee39 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -64,6 +64,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg allLogs []*types.Log gp = new(GasPool).AddGas(block.GasLimit()) ) + // Start the trie depth metrics window + statedb.BeginTrieDepthWindow() + defer statedb.EndTrieDepthWindow() // Mutate the block and state according to any hard-fork specs if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { diff --git a/trie/trie.go b/trie/trie.go index 6c998b3159..cc4ae30ba6 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -376,6 +376,10 @@ func (t *Trie) update(key, value []byte) error { func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { if len(key) == 0 { + if t.owner == (common.Hash{}) { + stateDepthAggregator.record(int64(len(prefix))) + } + if v, ok := n.(valueNode); ok { return !bytes.Equal(v, value.(valueNode)), value, nil } diff --git a/trie/trie_metrics.go b/trie/trie_metrics.go new file mode 100644 index 0000000000..a8b30f1c74 --- /dev/null +++ b/trie/trie_metrics.go @@ -0,0 +1,56 @@ +package trie + +import ( + "sync" + + "github.com/ethereum/go-ethereum/metrics" +) + +var ( + avgAccessDepthInBlock = metrics.NewRegisteredMeter("trie/access/depth/avg", nil) + minAccessDepthInBlock = metrics.NewRegisteredMeter("trie/access/depth/min", nil) + stateDepthAggregator = &depthAggregator{} +) + +// depthAggregator aggregates trie access depth metrics for a block. +type depthAggregator struct { + mu sync.Mutex + sum, cnt int64 + min int64 +} + +// start initializes the aggregator for a new block. +func (d *depthAggregator) start() { + d.mu.Lock() + d.sum, d.cnt, d.min = 0, 0, -1 + d.mu.Unlock() +} + +// record records the access depth for a trie operation. +func (d *depthAggregator) record(depth int64) { + d.mu.Lock() + v := depth + d.sum += v + d.cnt++ + if d.min < 0 || v < d.min { + d.min = v + } + d.mu.Unlock() +} + +// end finalizes the metrics for the current block and updates the registered metrics. +func (d *depthAggregator) end() { + d.mu.Lock() + sum, cnt, min := d.sum, d.cnt, d.min + d.mu.Unlock() + if cnt > 0 { + avgAccessDepthInBlock.Mark(sum / cnt) + minAccessDepthInBlock.Mark(min) + } +} + +// StateDepthMetricsStartBlock initializes the depth aggregator for a new block. +func StateDepthMetricsStartBlock() { stateDepthAggregator.start() } + +// StateDepthMetricsEndBlock finalizes the depth metrics for the current block. +func StateDepthMetricsEndBlock() { stateDepthAggregator.end() }