From f6f3931685337746a0f4bca7328ffc2f6d43357c Mon Sep 17 00:00:00 2001 From: shantichanal <158101918+shantichanal@users.noreply.github.com> Date: Fri, 8 Aug 2025 06:46:17 +0300 Subject: [PATCH] Update blockchain.go --- core/blockchain.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 0b92a94b6c..94e74a7460 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -64,6 +64,8 @@ var ( headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) headFinalizedBlockGauge = metrics.NewRegisteredGauge("chain/head/finalized", nil) headSafeBlockGauge = metrics.NewRegisteredGauge("chain/head/safe", nil) + stateMaxDepthGauge = metrics.NewRegisteredGauge("chain/state/maxdepth", nil) + stateAvgDepthGauge = metrics.NewRegisteredGauge("chain/state/avgdepth", nil) chainInfoGauge = metrics.NewRegisteredGaugeInfo("chain/info", nil) chainMgaspsMeter = metrics.NewRegisteredResettingTimer("chain/mgasps", nil) @@ -2132,6 +2134,38 @@ 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,