17
17
package core
18
18
19
19
import (
20
- "errors"
21
20
"fmt"
22
- "sync"
23
21
"time"
24
22
25
23
"github.com/scroll-tech/go-ethereum/consensus"
26
24
"github.com/scroll-tech/go-ethereum/core/rawdb"
27
25
"github.com/scroll-tech/go-ethereum/core/state"
28
26
"github.com/scroll-tech/go-ethereum/core/types"
29
- "github.com/scroll-tech/go-ethereum/ethdb"
30
27
"github.com/scroll-tech/go-ethereum/log"
31
28
"github.com/scroll-tech/go-ethereum/metrics"
32
29
"github.com/scroll-tech/go-ethereum/params"
33
- "github.com/scroll-tech/go-ethereum/rollup/ccc"
34
30
"github.com/scroll-tech/go-ethereum/trie"
35
31
)
36
32
37
33
var (
38
- validateL1MessagesTimer = metrics .NewRegisteredTimer ("validator/l1msg" , nil )
39
- validateRowConsumptionTimer = metrics .NewRegisteredTimer ("validator/rowconsumption" , nil )
40
- validateTraceTimer = metrics .NewRegisteredTimer ("validator/trace" , nil )
41
- validateLockTimer = metrics .NewRegisteredTimer ("validator/lock" , nil )
42
- validateCccTimer = metrics .NewRegisteredTimer ("validator/ccc" , nil )
34
+ validateL1MessagesTimer = metrics .NewRegisteredTimer ("validator/l1msg" , nil )
35
+ asyncValidatorTimer = metrics .NewRegisteredTimer ("validator/async" , nil )
43
36
)
44
37
45
38
// BlockValidator is responsible for validating block headers, uncles and
46
39
// processed state.
47
40
//
48
41
// BlockValidator implements Validator.
49
42
type BlockValidator struct {
50
- config * params.ChainConfig // Chain configuration options
51
- bc * BlockChain // Canonical block chain
52
- engine consensus.Engine // Consensus engine used for validating
53
-
54
- // circuit capacity checker related fields
55
- checkCircuitCapacity bool // whether enable circuit capacity check
56
- cMu sync.Mutex // mutex for circuit capacity checker
57
- tracer tracerWrapper // scroll tracer wrapper
58
- circuitCapacityChecker * ccc.Checker // circuit capacity checker instance
43
+ config * params.ChainConfig // Chain configuration options
44
+ bc * BlockChain // Canonical block chain
45
+ engine consensus.Engine // Consensus engine used for validating
46
+ asyncValidator func (* types.Block ) error // Asynchronously run a validation task
59
47
}
60
48
61
49
// NewBlockValidator returns a new block validator which is safe for re-use
@@ -68,15 +56,10 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engin
68
56
return validator
69
57
}
70
58
71
- type tracerWrapper interface {
72
- CreateTraceEnvAndGetBlockTrace (* params.ChainConfig , ChainContext , consensus.Engine , ethdb.Database , * state.StateDB , * types.Block , * types.Block , bool ) (* types.BlockTrace , error )
73
- }
74
-
75
- func (v * BlockValidator ) SetupTracerAndCircuitCapacityChecker (tracer tracerWrapper ) {
76
- v .checkCircuitCapacity = true
77
- v .tracer = tracer
78
- v .circuitCapacityChecker = ccc .NewChecker (true )
79
- log .Info ("new CircuitCapacityChecker in BlockValidator" , "ID" , v .circuitCapacityChecker .ID )
59
+ // WithAsyncValidator sets up an async validator to be triggered on each new block
60
+ func (v * BlockValidator ) WithAsyncValidator (asyncValidator func (* types.Block ) error ) Validator {
61
+ v .asyncValidator = asyncValidator
62
+ return v
80
63
}
81
64
82
65
// ValidateBody validates the given block's uncles and verifies the block
@@ -114,25 +97,13 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
114
97
if err := v .ValidateL1Messages (block ); err != nil {
115
98
return err
116
99
}
117
- if v .checkCircuitCapacity {
118
- // if a block's RowConsumption has been stored, which means it has been processed before,
119
- // (e.g., in miner/worker.go or in insertChain),
120
- // we simply skip its calculation and validation
121
- if rawdb .ReadBlockRowConsumption (v .bc .db , block .Hash ()) != nil {
122
- return nil
123
- }
124
- rowConsumption , err := v .validateCircuitRowConsumption (block )
125
- if err != nil {
100
+
101
+ if v .asyncValidator != nil {
102
+ asyncStart := time .Now ()
103
+ if err := v .asyncValidator (block ); err != nil {
126
104
return err
127
105
}
128
- log .Trace (
129
- "Validator write block row consumption" ,
130
- "id" , v .circuitCapacityChecker .ID ,
131
- "number" , block .NumberU64 (),
132
- "hash" , block .Hash ().String (),
133
- "rowConsumption" , rowConsumption ,
134
- )
135
- rawdb .WriteBlockRowConsumption (v .bc .db , block .Hash (), rowConsumption )
106
+ asyncValidatorTimer .UpdateSince (asyncStart )
136
107
}
137
108
return nil
138
109
}
@@ -286,61 +257,3 @@ func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
286
257
}
287
258
return limit
288
259
}
289
-
290
- func (v * BlockValidator ) createTraceEnvAndGetBlockTrace (block * types.Block ) (* types.BlockTrace , error ) {
291
- parent := v .bc .GetBlock (block .ParentHash (), block .NumberU64 ()- 1 )
292
- if parent == nil {
293
- return nil , errors .New ("validateCircuitRowConsumption: no parent block found" )
294
- }
295
-
296
- statedb , err := v .bc .StateAt (parent .Root ())
297
- if err != nil {
298
- return nil , err
299
- }
300
-
301
- return v .tracer .CreateTraceEnvAndGetBlockTrace (v .config , v .bc , v .engine , v .bc .db , statedb , parent , block , true )
302
- }
303
-
304
- func (v * BlockValidator ) validateCircuitRowConsumption (block * types.Block ) (* types.RowConsumption , error ) {
305
- defer func (t0 time.Time ) {
306
- validateRowConsumptionTimer .Update (time .Since (t0 ))
307
- }(time .Now ())
308
-
309
- log .Trace (
310
- "Validator apply ccc for block" ,
311
- "id" , v .circuitCapacityChecker .ID ,
312
- "number" , block .NumberU64 (),
313
- "hash" , block .Hash ().String (),
314
- "len(txs)" , block .Transactions ().Len (),
315
- )
316
-
317
- traceStartTime := time .Now ()
318
- traces , err := v .createTraceEnvAndGetBlockTrace (block )
319
- if err != nil {
320
- return nil , err
321
- }
322
- validateTraceTimer .Update (time .Since (traceStartTime ))
323
-
324
- lockStartTime := time .Now ()
325
- v .cMu .Lock ()
326
- defer v .cMu .Unlock ()
327
- validateLockTimer .Update (time .Since (lockStartTime ))
328
-
329
- cccStartTime := time .Now ()
330
- v .circuitCapacityChecker .Reset ()
331
- log .Trace ("Validator reset ccc" , "id" , v .circuitCapacityChecker .ID )
332
- rc , err := v .circuitCapacityChecker .ApplyBlock (traces )
333
- validateCccTimer .Update (time .Since (cccStartTime ))
334
-
335
- log .Trace (
336
- "Validator apply ccc for block result" ,
337
- "id" , v .circuitCapacityChecker .ID ,
338
- "number" , block .NumberU64 (),
339
- "hash" , block .Hash ().String (),
340
- "len(txs)" , block .Transactions ().Len (),
341
- "rc" , rc ,
342
- "err" , err ,
343
- )
344
-
345
- return rc , err
346
- }
0 commit comments