Skip to content

Commit 7490499

Browse files
committed
setup default validator for blockchain
1 parent ef3ec13 commit 7490499

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

core/block_validator.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ import (
2929

3030
const badBlockCacheExpire = 30 * time.Second
3131

32+
type BlockValidatorOption func(*BlockValidator) *BlockValidator
33+
34+
func EnableRemoteVerifyManager(remoteValidator *remoteVerifyManager) BlockValidatorOption {
35+
return func(bv *BlockValidator) *BlockValidator {
36+
bv.remoteValidator = remoteValidator
37+
return bv
38+
}
39+
}
40+
3241
// BlockValidator is responsible for validating block headers, uncles and
3342
// processed state.
3443
//
@@ -41,21 +50,18 @@ type BlockValidator struct {
4150
}
4251

4352
// NewBlockValidator returns a new block validator which is safe for re-use
44-
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode VerifyMode, peers verifyPeers) (*BlockValidator, error) {
53+
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, opts ...BlockValidatorOption) *BlockValidator {
4554
validator := &BlockValidator{
4655
config: config,
4756
engine: engine,
4857
bc: blockchain,
4958
}
50-
if mode.NeedRemoteVerify() {
51-
remoteValidator, err := NewVerifyManager(blockchain, peers, mode == InsecureVerify)
52-
if err != nil {
53-
return nil, err
54-
}
55-
validator.remoteValidator = remoteValidator
56-
go validator.remoteValidator.mainLoop()
59+
60+
for _, opt := range opts {
61+
validator = opt(validator)
5762
}
58-
return validator, nil
63+
64+
return validator
5965
}
6066

6167
// ValidateBody validates the given block's uncles and verifies the block

core/blockchain.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
317317
}
318318

319319
bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine)
320+
bc.validator = NewBlockValidator(chainConfig, bc, engine)
320321
bc.processor = NewStateProcessor(chainConfig, bc, engine)
321322

322323
var err error
@@ -3165,11 +3166,14 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
31653166

31663167
func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode VerifyMode, peers verifyPeers) BlockChainOption {
31673168
return func(bc *BlockChain) (*BlockChain, error) {
3168-
validator, err := NewBlockValidator(chainConfig, bc, engine, mode, peers)
3169-
if err != nil {
3170-
return bc, err
3169+
if mode.NeedRemoteVerify() {
3170+
vm, err := NewVerifyManager(bc, peers, mode == InsecureVerify)
3171+
if err != nil {
3172+
return nil, err
3173+
}
3174+
go vm.mainLoop()
3175+
bc.validator = NewBlockValidator(chainConfig, bc, engine, EnableRemoteVerifyManager(vm))
31713176
}
3172-
bc.validator = validator
31733177
return bc, nil
31743178
}
31753179
}

0 commit comments

Comments
 (0)