Skip to content

Commit 6925368

Browse files
committed
draft
1 parent ab2f776 commit 6925368

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

core/block_validator.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ type BlockValidator struct {
4242
engine consensus.Engine // Consensus engine used for validating
4343

4444
// circuit capacity checker related fields
45-
checkCircuitCapacity bool // whether enable circuit capacity check
46-
db ethdb.Database // db to store row consumption
47-
cMu sync.Mutex // mutex for circuit capacity checker
45+
checkCircuitCapacity bool // whether enable circuit capacity check
46+
db ethdb.Database // TODO: remove this and use bc.db
47+
cMu sync.Mutex // mutex for circuit capacity checker
48+
tracer tracerWrapper
4849
circuitCapacityChecker *circuitcapacitychecker.CircuitCapacityChecker // circuit capacity checker instance
4950
}
5051

@@ -62,6 +63,17 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engin
6263
return validator
6364
}
6465

66+
type tracerWrapper interface {
67+
CreateTraceEnvAndGetBlockTrace(*params.ChainConfig, ChainContext, consensus.Engine, ethdb.Database, *state.StateDB, *types.Block, *types.Block, bool) (*types.BlockTrace, error)
68+
}
69+
70+
func (v *BlockValidator) SetupTracerAndCircuitCapacityChecker(tracer tracerWrapper) {
71+
v.checkCircuitCapacity = true
72+
v.tracer = tracer
73+
v.circuitCapacityChecker = circuitcapacitychecker.NewCircuitCapacityChecker(true)
74+
log.Info("created new BlockValidator", "CircuitCapacityChecker ID", v.circuitCapacityChecker.ID)
75+
}
76+
6577
// ValidateBody validates the given block's uncles and verifies the block
6678
// header's transaction and uncle roots. The headers are assumed to be already
6779
// validated at this point.
@@ -260,7 +272,7 @@ func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
260272
return limit
261273
}
262274

263-
func (v *BlockValidator) createTraceEnv(block *types.Block) (*TraceEnv, error) {
275+
func (v *BlockValidator) createTraceEnvAndGetBlockTrace(block *types.Block) (*types.BlockTrace, error) {
264276
parent := v.bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
265277
if parent == nil {
266278
return nil, errors.New("validateCircuitRowConsumption: no parent block found")
@@ -271,7 +283,7 @@ func (v *BlockValidator) createTraceEnv(block *types.Block) (*TraceEnv, error) {
271283
return nil, err
272284
}
273285

274-
return CreateTraceEnv(v.config, v.bc, v.engine, v.db, statedb, parent, block, true)
286+
return v.tracer.CreateTraceEnvAndGetBlockTrace(v.config, v.bc, v.engine, v.db, statedb, parent, block, true)
275287
}
276288

277289
func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) {
@@ -283,12 +295,7 @@ func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*typ
283295
"len(txs)", block.Transactions().Len(),
284296
)
285297

286-
env, err := v.createTraceEnv(block)
287-
if err != nil {
288-
return nil, err
289-
}
290-
291-
traces, err := env.GetBlockTrace(block)
298+
traces, err := v.createTraceEnvAndGetBlockTrace(block)
292299
if err != nil {
293300
return nil, err
294301
}

core/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type Validator interface {
3232
// ValidateState validates the given statedb and optionally the receipts and
3333
// gas used.
3434
ValidateState(block *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error
35+
36+
// TODO: add comments
37+
SetupTracerAndCircuitCapacityChecker(tracer tracerWrapper)
3538
}
3639

3740
// Prefetcher is an interface for pre-caching transaction signatures and state.

eth/backend.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/scroll-tech/go-ethereum/eth/protocols/snap"
4747
"github.com/scroll-tech/go-ethereum/ethdb"
4848
"github.com/scroll-tech/go-ethereum/event"
49+
"github.com/scroll-tech/go-ethereum/hack"
4950
"github.com/scroll-tech/go-ethereum/internal/ethapi"
5051
"github.com/scroll-tech/go-ethereum/log"
5152
"github.com/scroll-tech/go-ethereum/miner"
@@ -198,6 +199,11 @@ func New(stack *node.Node, config *ethconfig.Config, l1Client sync_service.EthCl
198199
if err != nil {
199200
return nil, err
200201
}
202+
if config.CheckCircuitCapacity {
203+
tracer := hack.NewTracerWrapper()
204+
eth.blockchain.Validator().SetupTracerAndCircuitCapacityChecker(tracer)
205+
}
206+
201207
// Rewind the chain in case of an incompatible config upgrade.
202208
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
203209
log.Warn("Rewinding chain to upgrade configuration", "err", compat)

hack/trace.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ import (
2828
"github.com/scroll-tech/go-ethereum/trie/zkproof"
2929
)
3030

31+
type TracerWrapper struct{}
32+
33+
func NewTracerWrapper() *TracerWrapper {
34+
return &TracerWrapper{}
35+
}
36+
37+
func (tw *TracerWrapper) CreateTraceEnvAndGetBlockTrace(chainConfig *params.ChainConfig, chainContext core.ChainContext, engine consensus.Engine, chaindb ethdb.Database, statedb *state.StateDB, parent *types.Block, block *types.Block, commitAfterApply bool) (*types.BlockTrace, error) {
38+
traceEnv, err := CreateTraceEnv(chainConfig, chainContext, engine, chaindb, statedb, parent, block, commitAfterApply)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
return traceEnv.GetBlockTrace(block)
44+
}
45+
3146
type TraceEnv struct {
3247
logConfig *vm.LogConfig
3348
commitAfterApply bool

0 commit comments

Comments
 (0)