mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
* add core/rawdb/accessors_row_consumption.go * update core/rawdb/schema.go * update eth/ethconfig/config.go * update eth/ethconfig/gen_config.go * update miner/miner.go * update eth/backend.go * update core/types.go * update cmd/geth/main.go * update cmd/utils/flags.go * update core/types/block.go * update core/block_validator.go * WIP: update miner/worker.go * WIP: update miner/worker.go * WIP: update miner/worker.go * WIP: update miner/worker.go * WIP: update miner/worker.go * WIP: update miner/worker.go add withTimer * WIP: update miner/worker.go * WIP: update miner/worker.go add some func * WIP: update miner/worker.go `makeEnv` * fix * fix * minor * init traces return * init accRows * update `applyTransaction` * fix `TestPriorityClient` * miner: update metrics * minor * add `l2CommitTxTraceStateRevertTimer` * update `calcAndSetAccRowsForEnv` * start to work on `commitTransactions` * seal block early if we're over time * If we have collected enough transactions then we're done * check L1 message queue index * A single L1 message leads to out-of-gas * more on nil err * more error * skip RemoveTx for now * minor * l1TxStrangeErr * fix interface * start to work on `commitWork` * minor * draft `fillTransactions` * fix some types * refactor * fix * add `circuitCapacityReached` * minor * fix `txToLazyTx` * fix `(m *mockBackend) ChainDb()` * fix `removeTx` * update comments
362 lines
13 KiB
Go
362 lines
13 KiB
Go
// Copyright 2015 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package core
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
"github.com/ethereum/go-ethereum/rollup/circuitcapacitychecker"
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
)
|
|
|
|
// BlockValidator is responsible for validating block headers, uncles and
|
|
// processed state.
|
|
//
|
|
// BlockValidator implements Validator.
|
|
type BlockValidator struct {
|
|
config *params.ChainConfig // Chain configuration options
|
|
bc *BlockChain // Canonical block chain
|
|
engine consensus.Engine // Consensus engine used for validating
|
|
|
|
// circuit capacity checker related fields
|
|
checkCircuitCapacity bool // whether enable circuit capacity check
|
|
cMu sync.Mutex // mutex for circuit capacity checker
|
|
tracer tracerWrapper // scroll tracer wrapper
|
|
circuitCapacityChecker *circuitcapacitychecker.CircuitCapacityChecker // circuit capacity checker instance
|
|
}
|
|
|
|
// NewBlockValidator returns a new block validator which is safe for re-use
|
|
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
|
|
validator := &BlockValidator{
|
|
config: config,
|
|
engine: engine,
|
|
bc: blockchain,
|
|
}
|
|
return validator
|
|
}
|
|
|
|
type tracerWrapper interface {
|
|
CreateTraceEnvAndGetBlockTrace(*params.ChainConfig, ChainContext, consensus.Engine, ethdb.Database, *state.StateDB, *types.Header, *types.Block, bool) (*types.BlockTrace, error)
|
|
}
|
|
|
|
func (v *BlockValidator) SetupTracerAndCircuitCapacityChecker(tracer tracerWrapper) {
|
|
v.checkCircuitCapacity = true
|
|
v.tracer = tracer
|
|
v.circuitCapacityChecker = circuitcapacitychecker.NewCircuitCapacityChecker(true)
|
|
log.Info("new CircuitCapacityChecker in BlockValidator", "ID", v.circuitCapacityChecker.ID)
|
|
}
|
|
|
|
// ValidateBody validates the given block's uncles and verifies the block
|
|
// header's transaction and uncle roots. The headers are assumed to be already
|
|
// validated at this point.
|
|
func (v *BlockValidator) ValidateBody(block *types.Block) error {
|
|
// Check whether the block is already imported.
|
|
if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
|
|
return ErrKnownBlock
|
|
}
|
|
// Check if block tx count is is smaller than the max count
|
|
if !v.config.Scroll.IsValidTxCount(len(block.Transactions())) {
|
|
return consensus.ErrInvalidTxCount
|
|
}
|
|
// Check if block payload size is smaller than the max size
|
|
if !v.config.Scroll.IsValidBlockSize(block.PayloadSize()) {
|
|
return ErrInvalidBlockPayloadSize
|
|
}
|
|
|
|
// Header validity is known at this point. Here we verify that uncles, transactions
|
|
// and withdrawals given in the block body match the header.
|
|
header := block.Header()
|
|
if err := v.engine.VerifyUncles(v.bc, block); err != nil {
|
|
return err
|
|
}
|
|
if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
|
|
return fmt.Errorf("uncle root hash mismatch (header value %x, calculated %x)", header.UncleHash, hash)
|
|
}
|
|
if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash {
|
|
return fmt.Errorf("transaction root hash mismatch (header value %x, calculated %x)", header.TxHash, hash)
|
|
}
|
|
|
|
// Withdrawals are present after the Shanghai fork.
|
|
if header.WithdrawalsHash != nil {
|
|
// Withdrawals list must be present in body after Shanghai.
|
|
if block.Withdrawals() == nil {
|
|
return errors.New("missing withdrawals in block body")
|
|
}
|
|
if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash {
|
|
return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash)
|
|
}
|
|
} else if block.Withdrawals() != nil {
|
|
// Withdrawals are not allowed prior to Shanghai fork
|
|
return errors.New("withdrawals present in block body")
|
|
}
|
|
|
|
// Blob transactions may be present after the Cancun fork.
|
|
var blobs int
|
|
for i, tx := range block.Transactions() {
|
|
// Count the number of blobs to validate against the header's blobGasUsed
|
|
blobs += len(tx.BlobHashes())
|
|
|
|
// If the tx is a blob tx, it must NOT have a sidecar attached to be valid in a block.
|
|
if tx.BlobTxSidecar() != nil {
|
|
return fmt.Errorf("unexpected blob sidecar in transaction at index %d", i)
|
|
}
|
|
|
|
// The individual checks for blob validity (version-check + not empty)
|
|
// happens in StateTransition.
|
|
}
|
|
|
|
// Check blob gas usage.
|
|
if header.BlobGasUsed != nil {
|
|
if want := *header.BlobGasUsed / params.BlobTxBlobGasPerBlob; uint64(blobs) != want { // div because the header is surely good vs the body might be bloated
|
|
return fmt.Errorf("blob gas used mismatch (header %v, calculated %v)", *header.BlobGasUsed, blobs*params.BlobTxBlobGasPerBlob)
|
|
}
|
|
} else {
|
|
if blobs > 0 {
|
|
return errors.New("data blobs present in block body")
|
|
}
|
|
}
|
|
|
|
// Ancestor block must be known.
|
|
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
|
|
if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
|
|
return consensus.ErrUnknownAncestor
|
|
}
|
|
return consensus.ErrPrunedAncestor
|
|
}
|
|
|
|
if err := v.ValidateL1Messages(block); err != nil {
|
|
return err
|
|
}
|
|
|
|
if v.checkCircuitCapacity {
|
|
// if a block's RowConsumption has been stored, which means it has been processed before,
|
|
// (e.g., in miner/worker.go or in insertChain),
|
|
// we simply skip its calculation and validation
|
|
if rawdb.ReadBlockRowConsumption(v.bc.db, block.Hash()) != nil {
|
|
return nil
|
|
}
|
|
rowConsumption, err := v.validateCircuitRowConsumption(block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Trace(
|
|
"Validator write block row consumption",
|
|
"id", v.circuitCapacityChecker.ID,
|
|
"number", block.NumberU64(),
|
|
"hash", block.Hash().String(),
|
|
"rowConsumption", rowConsumption,
|
|
)
|
|
rawdb.WriteBlockRowConsumption(v.bc.db, block.Hash(), rowConsumption)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ValidateL1Messages validates L1 messages contained in a block.
|
|
// We check the following conditions:
|
|
// - L1 messages are in a contiguous section at the front of the block.
|
|
// - The first L1 message's QueueIndex is right after the last L1 message included in the chain.
|
|
// - L1 messages follow the QueueIndex order.
|
|
// - The L1 messages included in the block match the node's view of the L1 ledger.
|
|
func (v *BlockValidator) ValidateL1Messages(block *types.Block) error {
|
|
// skip DB read if the block contains no L1 messages
|
|
if !block.ContainsL1Messages() {
|
|
return nil
|
|
}
|
|
|
|
blockHash := block.Hash()
|
|
|
|
if v.config.Scroll.L1Config == nil {
|
|
// TODO: should we allow follower nodes to skip L1 message verification?
|
|
panic("Running on L1Message-enabled network but no l1Config was provided")
|
|
}
|
|
|
|
nextQueueIndex := rawdb.ReadFirstQueueIndexNotInL2Block(v.bc.db, block.ParentHash())
|
|
if nextQueueIndex == nil {
|
|
// we'll reprocess this block at a later time
|
|
return consensus.ErrMissingL1MessageData
|
|
}
|
|
queueIndex := *nextQueueIndex
|
|
|
|
L1SectionOver := false
|
|
it := rawdb.IterateL1MessagesFrom(v.bc.db, queueIndex)
|
|
|
|
for _, tx := range block.Transactions() {
|
|
if !tx.IsL1MessageTx() {
|
|
L1SectionOver = true
|
|
continue // we do not verify L2 transactions here
|
|
}
|
|
|
|
// check that L1 messages are before L2 transactions
|
|
if L1SectionOver {
|
|
return consensus.ErrInvalidL1MessageOrder
|
|
}
|
|
|
|
// queue index cannot decrease
|
|
txQueueIndex := tx.AsL1MessageTx().QueueIndex
|
|
|
|
if txQueueIndex < queueIndex {
|
|
return consensus.ErrInvalidL1MessageOrder
|
|
}
|
|
|
|
// skipped messages
|
|
// TODO: consider verifying that skipped messages overflow
|
|
for index := queueIndex; index < txQueueIndex; index++ {
|
|
if exists := it.Next(); !exists {
|
|
// the message in this block is not available in our local db.
|
|
// we'll reprocess this block at a later time.
|
|
return consensus.ErrMissingL1MessageData
|
|
}
|
|
|
|
l1msg := it.L1Message()
|
|
skippedTx := types.NewTx(&l1msg)
|
|
log.Debug("Skipped L1 message", "queueIndex", index, "tx", skippedTx.Hash().String(), "block", blockHash.String())
|
|
rawdb.WriteSkippedTransaction(v.bc.db, skippedTx, nil, "unknown", block.NumberU64(), &blockHash)
|
|
}
|
|
|
|
queueIndex = txQueueIndex + 1
|
|
|
|
if exists := it.Next(); !exists {
|
|
// the message in this block is not available in our local db.
|
|
// we'll reprocess this block at a later time.
|
|
return consensus.ErrMissingL1MessageData
|
|
}
|
|
|
|
// check that the L1 message in the block is the same that we collected from L1
|
|
msg := it.L1Message()
|
|
expectedHash := types.NewTx(&msg).Hash()
|
|
|
|
if tx.Hash() != expectedHash {
|
|
return consensus.ErrUnknownL1Message
|
|
}
|
|
}
|
|
|
|
// TODO: consider adding a rule to enforce L1Config.NumL1MessagesPerBlock.
|
|
// If there are L1 messages available, sequencer nodes should include them.
|
|
// However, this is hard to enforce as different nodes might have different views of L1.
|
|
|
|
return nil
|
|
}
|
|
|
|
// ValidateState validates the various changes that happen after a state transition,
|
|
// such as amount of used gas, the receipt roots and the state root itself.
|
|
func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, receipts types.Receipts, usedGas uint64) error {
|
|
header := block.Header()
|
|
if block.GasUsed() != usedGas {
|
|
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
|
|
}
|
|
// Validate the received block's bloom with the one derived from the generated receipts.
|
|
// For valid blocks this should always validate to true.
|
|
rbloom := types.CreateBloom(receipts)
|
|
if rbloom != header.Bloom {
|
|
return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
|
|
}
|
|
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
|
|
receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
|
|
if receiptSha != header.ReceiptHash {
|
|
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
|
|
}
|
|
// Validate the state root against the received state root and throw
|
|
// an error if they don't match.
|
|
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
|
|
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CalcGasLimit computes the gas limit of the next block after parent. It aims
|
|
// to keep the baseline gas close to the provided target, and increase it towards
|
|
// the target if the baseline gas is lower.
|
|
func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
|
|
delta := parentGasLimit/params.GasLimitBoundDivisor - 1
|
|
limit := parentGasLimit
|
|
if desiredLimit < params.MinGasLimit {
|
|
desiredLimit = params.MinGasLimit
|
|
}
|
|
// If we're outside our allowed gas range, we try to hone towards them
|
|
if limit < desiredLimit {
|
|
limit = parentGasLimit + delta
|
|
if limit > desiredLimit {
|
|
limit = desiredLimit
|
|
}
|
|
return limit
|
|
}
|
|
if limit > desiredLimit {
|
|
limit = parentGasLimit - delta
|
|
if limit < desiredLimit {
|
|
limit = desiredLimit
|
|
}
|
|
}
|
|
return limit
|
|
}
|
|
|
|
func (v *BlockValidator) createTraceEnvAndGetBlockTrace(block *types.Block) (*types.BlockTrace, error) {
|
|
parent := v.bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
if parent == nil {
|
|
return nil, errors.New("validateCircuitRowConsumption: no parent block found")
|
|
}
|
|
|
|
statedb, err := v.bc.StateAt(parent.Root())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return v.tracer.CreateTraceEnvAndGetBlockTrace(v.config, v.bc, v.engine, v.bc.db, statedb, parent.Header(), block, true)
|
|
}
|
|
|
|
func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) {
|
|
log.Trace(
|
|
"Validator apply ccc for block",
|
|
"id", v.circuitCapacityChecker.ID,
|
|
"number", block.NumberU64(),
|
|
"hash", block.Hash().String(),
|
|
"len(txs)", block.Transactions().Len(),
|
|
)
|
|
|
|
traces, err := v.createTraceEnvAndGetBlockTrace(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
v.cMu.Lock()
|
|
defer v.cMu.Unlock()
|
|
|
|
v.circuitCapacityChecker.Reset()
|
|
log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID)
|
|
rc, err := v.circuitCapacityChecker.ApplyBlock(traces)
|
|
|
|
log.Trace(
|
|
"Validator apply ccc for block result",
|
|
"id", v.circuitCapacityChecker.ID,
|
|
"number", block.NumberU64(),
|
|
"hash", block.Hash().String(),
|
|
"len(txs)", block.Transactions().Len(),
|
|
"rc", rc,
|
|
"err", err,
|
|
)
|
|
|
|
return rc, err
|
|
}
|