made changes for tracking depth during block processing

This commit is contained in:
shantichanal 2025-08-11 03:00:20 +03:00 committed by Gary Rong
parent f6f3931685
commit e869586e13
5 changed files with 71 additions and 32 deletions

View file

@ -2134,38 +2134,6 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
mgasps := float64(res.GasUsed) * 1000 / float64(elapsed) mgasps := float64(res.GasUsed) * 1000 / float64(elapsed)
chainMgaspsMeter.Update(time.Duration(mgasps)) 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{ return &blockProcessingResult{
usedGas: res.GasUsed, usedGas: res.GasUsed,
procTime: proctime, procTime: proctime,

View file

@ -1464,3 +1464,11 @@ func (s *StateDB) Witness() *stateless.Witness {
func (s *StateDB) AccessEvents() *AccessEvents { func (s *StateDB) AccessEvents() *AccessEvents {
return s.accessEvents return s.accessEvents
} }
func (s *StateDB) BeginTrieDepthWindow() {
trie.StateDepthMetricsStartBlock()
}
func (s *StateDB) EndTrieDepthWindow() {
trie.StateDepthMetricsEndBlock()
}

View file

@ -64,6 +64,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
allLogs []*types.Log allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit()) 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 // 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 { if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {

View file

@ -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) { func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
if len(key) == 0 { if len(key) == 0 {
if t.owner == (common.Hash{}) {
stateDepthAggregator.record(int64(len(prefix)))
}
if v, ok := n.(valueNode); ok { if v, ok := n.(valueNode); ok {
return !bytes.Equal(v, value.(valueNode)), value, nil return !bytes.Equal(v, value.(valueNode)), value, nil
} }

56
trie/trie_metrics.go Normal file
View file

@ -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() }