Skip to content

Commit 5faeeaa

Browse files
authored
feat: add metrics to block validation (#677)
* feat: add metrics to block validation * more metrics * bump version
1 parent d1e4b59 commit 5faeeaa

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

core/block_validator.go

+24
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@ import (
2020
"errors"
2121
"fmt"
2222
"sync"
23+
"time"
2324

2425
"github.com/scroll-tech/go-ethereum/consensus"
2526
"github.com/scroll-tech/go-ethereum/core/rawdb"
2627
"github.com/scroll-tech/go-ethereum/core/state"
2728
"github.com/scroll-tech/go-ethereum/core/types"
2829
"github.com/scroll-tech/go-ethereum/ethdb"
2930
"github.com/scroll-tech/go-ethereum/log"
31+
"github.com/scroll-tech/go-ethereum/metrics"
3032
"github.com/scroll-tech/go-ethereum/params"
3133
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
3234
"github.com/scroll-tech/go-ethereum/trie"
3335
)
3436

37+
var (
38+
validateL1MessagesTimer = metrics.NewRegisteredTimer("validator/l1msg", nil)
39+
validateRowConsumptionTimer = metrics.NewRegisteredTimer("validator/rowconsumption", nil)
40+
validateTraceTimer = metrics.NewRegisteredTimer("validator/trace", nil)
41+
validateLockTimer = metrics.NewRegisteredTimer("validator/lock", nil)
42+
validateCccTimer = metrics.NewRegisteredTimer("validator/ccc", nil)
43+
)
44+
3545
// BlockValidator is responsible for validating block headers, uncles and
3646
// processed state.
3747
//
@@ -134,6 +144,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
134144
// - L1 messages follow the QueueIndex order.
135145
// - The L1 messages included in the block match the node's view of the L1 ledger.
136146
func (v *BlockValidator) ValidateL1Messages(block *types.Block) error {
147+
defer func(t0 time.Time) {
148+
validateL1MessagesTimer.Update(time.Since(t0))
149+
}(time.Now())
150+
137151
// skip DB read if the block contains no L1 messages
138152
if !block.ContainsL1Messages() {
139153
return nil
@@ -288,6 +302,10 @@ func (v *BlockValidator) createTraceEnvAndGetBlockTrace(block *types.Block) (*ty
288302
}
289303

290304
func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) {
305+
defer func(t0 time.Time) {
306+
validateRowConsumptionTimer.Update(time.Since(t0))
307+
}(time.Now())
308+
291309
log.Trace(
292310
"Validator apply ccc for block",
293311
"id", v.circuitCapacityChecker.ID,
@@ -296,17 +314,23 @@ func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*typ
296314
"len(txs)", block.Transactions().Len(),
297315
)
298316

317+
traceStartTime := time.Now()
299318
traces, err := v.createTraceEnvAndGetBlockTrace(block)
300319
if err != nil {
301320
return nil, err
302321
}
322+
validateTraceTimer.Update(time.Since(traceStartTime))
303323

324+
lockStartTime := time.Now()
304325
v.cMu.Lock()
305326
defer v.cMu.Unlock()
327+
validateLockTimer.Update(time.Since(lockStartTime))
306328

329+
cccStartTime := time.Now()
307330
v.circuitCapacityChecker.Reset()
308331
log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID)
309332
rc, err := v.circuitCapacityChecker.ApplyBlock(traces)
333+
validateCccTimer.Update(time.Since(cccStartTime))
310334

311335
log.Trace(
312336
"Validator apply ccc for block result",

core/state_processor.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package core
1919
import (
2020
"fmt"
2121
"math/big"
22+
"time"
2223

2324
"github.com/scroll-tech/go-ethereum/common"
2425
"github.com/scroll-tech/go-ethereum/consensus"
@@ -32,7 +33,14 @@ import (
3233
"github.com/scroll-tech/go-ethereum/rollup/fees"
3334
)
3435

35-
var processorBlockTransactionGauge = metrics.NewRegisteredGauge("processor/block/transactions", nil)
36+
var (
37+
processorBlockTransactionGauge = metrics.NewRegisteredGauge("processor/block/transactions", nil)
38+
processBlockTimer = metrics.NewRegisteredTimer("processor/block/process", nil)
39+
finalizeBlockTimer = metrics.NewRegisteredTimer("processor/block/finalize", nil)
40+
applyTransactionTimer = metrics.NewRegisteredTimer("processor/tx/apply", nil)
41+
applyMessageTimer = metrics.NewRegisteredTimer("processor/tx/msg/apply", nil)
42+
updateStatedbTimer = metrics.NewRegisteredTimer("processor/tx/statedb/update", nil)
43+
)
3644

3745
// StateProcessor is a basic Processor, which takes care of transitioning
3846
// state from one point to another.
@@ -61,6 +69,10 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
6169
// returns the amount of gas that was used in the process. If any of the
6270
// transactions failed to execute due to insufficient gas it will return an error.
6371
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
72+
defer func(t0 time.Time) {
73+
processBlockTimer.Update(time.Since(t0))
74+
}(time.Now())
75+
6476
var (
6577
receipts types.Receipts
6678
usedGas = new(uint64)
@@ -92,12 +104,18 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
92104
allLogs = append(allLogs, receipt.Logs...)
93105
}
94106
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
107+
finalizeBlockStartTime := time.Now()
95108
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
109+
finalizeBlockTimer.Update(time.Since(finalizeBlockStartTime))
96110

97111
return receipts, allLogs, *usedGas, nil
98112
}
99113

100114
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
115+
defer func(t0 time.Time) {
116+
applyTransactionTimer.Update(time.Since(t0))
117+
}(time.Now())
118+
101119
// Create a new context to be used in the EVM environment.
102120
txContext := NewEVMTxContext(msg)
103121
evm.Reset(txContext, statedb)
@@ -108,18 +126,22 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
108126
}
109127

110128
// Apply the transaction to the current state (included in the env).
129+
applyMessageStartTime := time.Now()
111130
result, err := ApplyMessage(evm, msg, gp, l1DataFee)
131+
applyMessageTimer.Update(time.Since(applyMessageStartTime))
112132
if err != nil {
113133
return nil, err
114134
}
115135

116136
// Update the state with pending changes.
117137
var root []byte
138+
updateStatedbStartTime := time.Now()
118139
if config.IsByzantium(blockNumber) {
119140
statedb.Finalise(true)
120141
} else {
121142
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
122143
}
144+
updateStatedbTimer.Update(time.Since(updateStatedbStartTime))
123145
*usedGas += result.UsedGas
124146

125147
// If the result contains a revert reason, return it.

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 1 // Minor version component of the current release
27-
VersionPatch = 28 // Patch version component of the current release
27+
VersionPatch = 29 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

0 commit comments

Comments
 (0)