Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/syncUpstream/active' into feat/l…
Browse files Browse the repository at this point in the history
…1-state-tracker
  • Loading branch information
jonastheis committed Oct 23, 2024
2 parents 294b9c6 + 0942cb7 commit 6464296
Show file tree
Hide file tree
Showing 80 changed files with 2,853 additions and 883 deletions.
1 change: 1 addition & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
AccessList: call.AccessList,
SkipAccountChecks: true,
IsL1MessageTx: false,
TxSize: 0,
}

// Create a new environment which holds all relevant information
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var (
utils.L1ConfirmationsFlag,
utils.L1DeploymentBlockFlag,
utils.CircuitCapacityCheckEnabledFlag,
utils.CircuitCapacityCheckWorkersFlag,
utils.RollupVerifyEnabledFlag,
utils.ShadowforkPeersFlag,
utils.DASyncEnabledFlag,
Expand Down Expand Up @@ -458,6 +459,7 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, isCon
// Set the gas price to the limits from the CLI and start mining
gasprice := flags.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
ethBackend.TxPool().SetGasTip(gasprice)
ethBackend.TxPool().SetIsMiner(true)
if err := ethBackend.StartMining(); err != nil {
utils.Fatalf("Failed to start mining: %v", err)
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
godebug "runtime/debug"
"strconv"
"strings"
Expand Down Expand Up @@ -1002,6 +1003,12 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Usage: "Enable circuit capacity check during block validation",
}

CircuitCapacityCheckWorkersFlag = &cli.UintFlag{
Name: "ccc.numworkers",
Usage: "Set the number of workers that will be used for background CCC tasks",
Value: uint(runtime.GOMAXPROCS(0)),
}

// Rollup verify service settings
RollupVerifyEnabledFlag = &cli.BoolFlag{
Name: "rollup.verify",
Expand All @@ -1025,11 +1032,10 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Name: "da.sync",
Usage: "Enable node syncing from DA",
}
defaultDA = ethconfig.Defaults.DA.FetcherMode
DAModeFlag = &flags.TextMarshalerFlag{
Name: "da.mode",
Usage: `DA sync mode ("l1rpc" or "snapshot")`,
Value: &defaultDA,
Value: &ethconfig.Defaults.DA.FetcherMode,
}
DASnapshotFileFlag = &cli.StringFlag{
Name: "da.snapshot.file",
Expand Down Expand Up @@ -1742,6 +1748,10 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
if ctx.IsSet(MinerMaxAccountsNumFlag.Name) {
cfg.MaxAccountsNum = ctx.Int(MinerMaxAccountsNumFlag.Name)
}
cfg.CCCMaxWorkers = runtime.GOMAXPROCS(0)
if ctx.IsSet(CircuitCapacityCheckWorkersFlag.Name) {
cfg.CCCMaxWorkers = int(ctx.Uint(CircuitCapacityCheckWorkersFlag.Name))
}
}

func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
Expand Down Expand Up @@ -1775,6 +1785,10 @@ func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
func setCircuitCapacityCheck(ctx *cli.Context, cfg *ethconfig.Config) {
if ctx.IsSet(CircuitCapacityCheckEnabledFlag.Name) {
cfg.CheckCircuitCapacity = ctx.Bool(CircuitCapacityCheckEnabledFlag.Name)
cfg.CCCMaxWorkers = runtime.GOMAXPROCS(0)
if ctx.IsSet(CircuitCapacityCheckWorkersFlag.Name) {
cfg.CCCMaxWorkers = int(ctx.Uint(CircuitCapacityCheckWorkersFlag.Name))
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions consensus/misc/eip1559/eip1559_scroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header, parentL1BaseF
return big.NewInt(10000000) // 0.01 Gwei
}
l2SequencerFee := big.NewInt(1000000) // 0.001 Gwei
provingFee := big.NewInt(33700000) // 0.0337 Gwei
provingFee := big.NewInt(38200000) // 0.0382 Gwei

// L1_base_fee * 0.0034
// L1_base_fee * 0.00017
verificationFee := parentL1BaseFee
verificationFee = new(big.Int).Mul(verificationFee, big.NewInt(34))
verificationFee = new(big.Int).Div(verificationFee, big.NewInt(10000))
verificationFee = new(big.Int).Mul(verificationFee, big.NewInt(17))
verificationFee = new(big.Int).Div(verificationFee, big.NewInt(100000))

baseFee := big.NewInt(0)
baseFee.Add(baseFee, l2SequencerFee)
Expand Down
14 changes: 7 additions & 7 deletions consensus/misc/eip1559/eip1559_scroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func TestCalcBaseFee(t *testing.T) {
parentL1BaseFee int64
expectedL2BaseFee int64
}{
{0, 34700000},
{1000000000, 38100000},
{2000000000, 41500000},
{100000000000, 374700000},
{111111111111, 412477777},
{2164000000000, 7392300000},
{2931000000000, 10000000000}, // cap at max L2 base fee
{0, 39200000},
{1000000000, 39370000},
{2000000000, 39540000},
{100000000000, 56200000},
{111111111111, 58088888},
{2164000000000, 407080000},
{58592942000000, 10000000000}, // cap at max L2 base fee
}
for i, test := range tests {
if have, want := CalcBaseFee(config(), nil, big.NewInt(test.parentL1BaseFee)), big.NewInt(test.expectedL2BaseFee); have.Cmp(want) != 0 {
Expand Down
111 changes: 13 additions & 98 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,22 @@ package core
import (
"errors"
"fmt"
"sync"
"time"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/consensus"
"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/core/state"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethdb"
"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/rollup/ccc"
"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)
validateL1MessagesTimer = metrics.NewRegisteredTimer("validator/l1msg", nil)
asyncValidatorTimer = metrics.NewRegisteredTimer("validator/async", nil)
)

// BlockValidator is responsible for validating block headers, uncles and
Expand All @@ -51,11 +46,7 @@ type BlockValidator struct {
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 *ccc.Checker // circuit capacity checker instance
asyncValidator func(*types.Block) error // Asynchronously run a validation task
}

// NewBlockValidator returns a new block validator which is safe for re-use
Expand All @@ -68,15 +59,10 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engin
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 = ccc.NewChecker(true)
log.Info("new Checker in BlockValidator", "ID", v.circuitCapacityChecker.ID)
// WithAsyncValidator sets up an async validator to be triggered on each new block
func (v *BlockValidator) WithAsyncValidator(asyncValidator func(*types.Block) error) Validator {
v.asyncValidator = asyncValidator
return v
}

// ValidateBody validates the given block's uncles and verifies the block
Expand All @@ -92,7 +78,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
return consensus.ErrInvalidTxCount
}
// Check if block payload size is smaller than the max size
if !v.config.Scroll.IsValidBlockSize(block.PayloadSize()) {
if !v.config.Scroll.IsValidBlockSize(common.StorageSize(block.PayloadSize())) {
return ErrInvalidBlockPayloadSize
}

Expand Down Expand Up @@ -161,25 +147,12 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
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 {
if v.asyncValidator != nil {
asyncStart := time.Now()
if err := v.asyncValidator(block); 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)
asyncValidatorTimer.UpdateSince(asyncStart)
}

return nil
Expand Down Expand Up @@ -332,61 +305,3 @@ func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
}
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) {
defer func(t0 time.Time) {
validateRowConsumptionTimer.Update(time.Since(t0))
}(time.Now())

log.Trace(
"Validator apply ccc for block",
"id", v.circuitCapacityChecker.ID,
"number", block.NumberU64(),
"hash", block.Hash().String(),
"len(txs)", block.Transactions().Len(),
)

traceStartTime := time.Now()
traces, err := v.createTraceEnvAndGetBlockTrace(block)
if err != nil {
return nil, err
}
validateTraceTimer.Update(time.Since(traceStartTime))

lockStartTime := time.Now()
v.cMu.Lock()
defer v.cMu.Unlock()
validateLockTimer.Update(time.Since(lockStartTime))

cccStartTime := time.Now()
v.circuitCapacityChecker.Reset()
log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID)
rc, err := v.circuitCapacityChecker.ApplyBlock(traces)
validateCccTimer.Update(time.Since(cccStartTime))

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
}
1 change: 1 addition & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4728,6 +4728,7 @@ func TestCurieTransition(t *testing.T) {
json.Unmarshal(b, &config)
config.CurieBlock = big.NewInt(2)
config.DarwinTime = nil
config.DarwinV2Time = nil

var (
db = rawdb.NewMemoryDatabase()
Expand Down
5 changes: 4 additions & 1 deletion core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, chainConfig *p
func NewEVMTxContext(msg *Message) vm.TxContext {
return vm.TxContext{
Origin: msg.From,
To: msg.To,
GasPrice: new(big.Int).Set(msg.GasPrice),
BlobHashes: msg.BlobHashes,

To: msg.To,
IsL1MessageTx: msg.IsL1MessageTx,
TxSize: msg.TxSize,
}
}

Expand Down
Loading

0 comments on commit 6464296

Please sign in to comment.