feat: add metrics to block validation (#677)

* feat: add metrics to block validation

* more metrics

* bump version
This commit is contained in:
Péter Garamvölgyi 2024-03-27 16:36:08 +00:00 committed by GitHub
parent d1e4b59eec
commit 5faeeaaf62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
"time"
"github.com/scroll-tech/go-ethereum/consensus" "github.com/scroll-tech/go-ethereum/consensus"
"github.com/scroll-tech/go-ethereum/core/rawdb" "github.com/scroll-tech/go-ethereum/core/rawdb"
@ -27,11 +28,20 @@ import (
"github.com/scroll-tech/go-ethereum/core/types" "github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethdb" "github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log" "github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/params" "github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker" "github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
"github.com/scroll-tech/go-ethereum/trie" "github.com/scroll-tech/go-ethereum/trie"
) )
var (
validateL1MessagesTimer = metrics.NewRegisteredTimer("validator/l1msg", nil)
validateRowConsumptionTimer = metrics.NewRegisteredTimer("validator/rowconsumption", nil)
validateTraceTimer = metrics.NewRegisteredTimer("validator/trace", nil)
validateLockTimer = metrics.NewRegisteredTimer("validator/lock", nil)
validateCccTimer = metrics.NewRegisteredTimer("validator/ccc", nil)
)
// BlockValidator is responsible for validating block headers, uncles and // BlockValidator is responsible for validating block headers, uncles and
// processed state. // processed state.
// //
@ -134,6 +144,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
// - L1 messages follow the QueueIndex order. // - L1 messages follow the QueueIndex order.
// - The L1 messages included in the block match the node's view of the L1 ledger. // - The L1 messages included in the block match the node's view of the L1 ledger.
func (v *BlockValidator) ValidateL1Messages(block *types.Block) error { func (v *BlockValidator) ValidateL1Messages(block *types.Block) error {
defer func(t0 time.Time) {
validateL1MessagesTimer.Update(time.Since(t0))
}(time.Now())
// skip DB read if the block contains no L1 messages // skip DB read if the block contains no L1 messages
if !block.ContainsL1Messages() { if !block.ContainsL1Messages() {
return nil return nil
@ -288,6 +302,10 @@ func (v *BlockValidator) createTraceEnvAndGetBlockTrace(block *types.Block) (*ty
} }
func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) { func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) {
defer func(t0 time.Time) {
validateRowConsumptionTimer.Update(time.Since(t0))
}(time.Now())
log.Trace( log.Trace(
"Validator apply ccc for block", "Validator apply ccc for block",
"id", v.circuitCapacityChecker.ID, "id", v.circuitCapacityChecker.ID,
@ -296,17 +314,23 @@ func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*typ
"len(txs)", block.Transactions().Len(), "len(txs)", block.Transactions().Len(),
) )
traceStartTime := time.Now()
traces, err := v.createTraceEnvAndGetBlockTrace(block) traces, err := v.createTraceEnvAndGetBlockTrace(block)
if err != nil { if err != nil {
return nil, err return nil, err
} }
validateTraceTimer.Update(time.Since(traceStartTime))
lockStartTime := time.Now()
v.cMu.Lock() v.cMu.Lock()
defer v.cMu.Unlock() defer v.cMu.Unlock()
validateLockTimer.Update(time.Since(lockStartTime))
cccStartTime := time.Now()
v.circuitCapacityChecker.Reset() v.circuitCapacityChecker.Reset()
log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID) log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID)
rc, err := v.circuitCapacityChecker.ApplyBlock(traces) rc, err := v.circuitCapacityChecker.ApplyBlock(traces)
validateCccTimer.Update(time.Since(cccStartTime))
log.Trace( log.Trace(
"Validator apply ccc for block result", "Validator apply ccc for block result",

View file

@ -19,6 +19,7 @@ package core
import ( import (
"fmt" "fmt"
"math/big" "math/big"
"time"
"github.com/scroll-tech/go-ethereum/common" "github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/consensus" "github.com/scroll-tech/go-ethereum/consensus"
@ -32,7 +33,14 @@ import (
"github.com/scroll-tech/go-ethereum/rollup/fees" "github.com/scroll-tech/go-ethereum/rollup/fees"
) )
var processorBlockTransactionGauge = metrics.NewRegisteredGauge("processor/block/transactions", nil) var (
processorBlockTransactionGauge = metrics.NewRegisteredGauge("processor/block/transactions", nil)
processBlockTimer = metrics.NewRegisteredTimer("processor/block/process", nil)
finalizeBlockTimer = metrics.NewRegisteredTimer("processor/block/finalize", nil)
applyTransactionTimer = metrics.NewRegisteredTimer("processor/tx/apply", nil)
applyMessageTimer = metrics.NewRegisteredTimer("processor/tx/msg/apply", nil)
updateStatedbTimer = metrics.NewRegisteredTimer("processor/tx/statedb/update", nil)
)
// StateProcessor is a basic Processor, which takes care of transitioning // StateProcessor is a basic Processor, which takes care of transitioning
// state from one point to another. // state from one point to another.
@ -61,6 +69,10 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
// returns the amount of gas that was used in the process. If any of the // returns the amount of gas that was used in the process. If any of the
// transactions failed to execute due to insufficient gas it will return an error. // transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
defer func(t0 time.Time) {
processBlockTimer.Update(time.Since(t0))
}(time.Now())
var ( var (
receipts types.Receipts receipts types.Receipts
usedGas = new(uint64) usedGas = new(uint64)
@ -92,12 +104,18 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
allLogs = append(allLogs, receipt.Logs...) allLogs = append(allLogs, receipt.Logs...)
} }
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards) // Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
finalizeBlockStartTime := time.Now()
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles()) p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
finalizeBlockTimer.Update(time.Since(finalizeBlockStartTime))
return receipts, allLogs, *usedGas, nil return receipts, allLogs, *usedGas, nil
} }
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) { 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) {
defer func(t0 time.Time) {
applyTransactionTimer.Update(time.Since(t0))
}(time.Now())
// Create a new context to be used in the EVM environment. // Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg) txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb) evm.Reset(txContext, statedb)
@ -108,18 +126,22 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
} }
// Apply the transaction to the current state (included in the env). // Apply the transaction to the current state (included in the env).
applyMessageStartTime := time.Now()
result, err := ApplyMessage(evm, msg, gp, l1DataFee) result, err := ApplyMessage(evm, msg, gp, l1DataFee)
applyMessageTimer.Update(time.Since(applyMessageStartTime))
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Update the state with pending changes. // Update the state with pending changes.
var root []byte var root []byte
updateStatedbStartTime := time.Now()
if config.IsByzantium(blockNumber) { if config.IsByzantium(blockNumber) {
statedb.Finalise(true) statedb.Finalise(true)
} else { } else {
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes() root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
} }
updateStatedbTimer.Update(time.Since(updateStatedbStartTime))
*usedGas += result.UsedGas *usedGas += result.UsedGas
// If the result contains a revert reason, return it. // If the result contains a revert reason, return it.

View file

@ -24,7 +24,7 @@ import (
const ( const (
VersionMajor = 5 // Major version component of the current release VersionMajor = 5 // Major version component of the current release
VersionMinor = 1 // Minor version component of the current release VersionMinor = 1 // Minor version component of the current release
VersionPatch = 28 // Patch version component of the current release VersionPatch = 29 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string VersionMeta = "mainnet" // Version metadata to append to the version string
) )