Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmountaintop committed Jan 11, 2024
1 parent ab2f776 commit 6925368
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
29 changes: 18 additions & 11 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type BlockValidator struct {
engine consensus.Engine // Consensus engine used for validating

// circuit capacity checker related fields
checkCircuitCapacity bool // whether enable circuit capacity check
db ethdb.Database // db to store row consumption
cMu sync.Mutex // mutex for circuit capacity checker
checkCircuitCapacity bool // whether enable circuit capacity check
db ethdb.Database // TODO: remove this and use bc.db
cMu sync.Mutex // mutex for circuit capacity checker
tracer tracerWrapper
circuitCapacityChecker *circuitcapacitychecker.CircuitCapacityChecker // circuit capacity checker instance
}

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

type tracerWrapper interface {
CreateTraceEnvAndGetBlockTrace(*params.ChainConfig, ChainContext, consensus.Engine, ethdb.Database, *state.StateDB, *types.Block, *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("created new BlockValidator", "CircuitCapacityChecker 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.
Expand Down Expand Up @@ -260,7 +272,7 @@ func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
return limit
}

func (v *BlockValidator) createTraceEnv(block *types.Block) (*TraceEnv, error) {
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")
Expand All @@ -271,7 +283,7 @@ func (v *BlockValidator) createTraceEnv(block *types.Block) (*TraceEnv, error) {
return nil, err
}

return CreateTraceEnv(v.config, v.bc, v.engine, v.db, statedb, parent, block, true)
return v.tracer.CreateTraceEnvAndGetBlockTrace(v.config, v.bc, v.engine, v.db, statedb, parent, block, true)
}

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

env, err := v.createTraceEnv(block)
if err != nil {
return nil, err
}

traces, err := env.GetBlockTrace(block)
traces, err := v.createTraceEnvAndGetBlockTrace(block)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Validator interface {
// ValidateState validates the given statedb and optionally the receipts and
// gas used.
ValidateState(block *types.Block, state *state.StateDB, receipts types.Receipts, usedGas uint64) error

// TODO: add comments
SetupTracerAndCircuitCapacityChecker(tracer tracerWrapper)
}

// Prefetcher is an interface for pre-caching transaction signatures and state.
Expand Down
6 changes: 6 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/scroll-tech/go-ethereum/eth/protocols/snap"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/event"
"github.com/scroll-tech/go-ethereum/hack"
"github.com/scroll-tech/go-ethereum/internal/ethapi"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/miner"
Expand Down Expand Up @@ -198,6 +199,11 @@ func New(stack *node.Node, config *ethconfig.Config, l1Client sync_service.EthCl
if err != nil {
return nil, err
}
if config.CheckCircuitCapacity {
tracer := hack.NewTracerWrapper()
eth.blockchain.Validator().SetupTracerAndCircuitCapacityChecker(tracer)
}

// Rewind the chain in case of an incompatible config upgrade.
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
Expand Down
15 changes: 15 additions & 0 deletions hack/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ import (
"github.com/scroll-tech/go-ethereum/trie/zkproof"
)

type TracerWrapper struct{}

func NewTracerWrapper() *TracerWrapper {
return &TracerWrapper{}
}

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) {
traceEnv, err := CreateTraceEnv(chainConfig, chainContext, engine, chaindb, statedb, parent, block, commitAfterApply)
if err != nil {
return nil, err
}

return traceEnv.GetBlockTrace(block)
}

type TraceEnv struct {
logConfig *vm.LogConfig
commitAfterApply bool
Expand Down

0 comments on commit 6925368

Please sign in to comment.