diff --git a/core/block_validator.go b/core/block_validator.go index 6f2c40b2ec..876c6e43a2 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -29,6 +29,15 @@ import ( const badBlockCacheExpire = 30 * time.Second +type BlockValidatorOption func(*BlockValidator) *BlockValidator + +func EnableRemoteVerifyManager(remoteValidator *remoteVerifyManager) BlockValidatorOption { + return func(bv *BlockValidator) *BlockValidator { + bv.remoteValidator = remoteValidator + return bv + } +} + // BlockValidator is responsible for validating block headers, uncles and // processed state. // @@ -41,21 +50,18 @@ type BlockValidator struct { } // NewBlockValidator returns a new block validator which is safe for re-use -func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode VerifyMode, peers verifyPeers) (*BlockValidator, error) { +func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, opts ...BlockValidatorOption) *BlockValidator { validator := &BlockValidator{ config: config, engine: engine, bc: blockchain, } - if mode.NeedRemoteVerify() { - remoteValidator, err := NewVerifyManager(blockchain, peers, mode == InsecureVerify) - if err != nil { - return nil, err - } - validator.remoteValidator = remoteValidator - go validator.remoteValidator.mainLoop() + + for _, opt := range opts { + validator = opt(validator) } - return validator, nil + + return validator } // ValidateBody validates the given block's uncles and verifies the block diff --git a/core/blockchain.go b/core/blockchain.go index 1ca43ec7c8..465235fdd2 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -317,6 +317,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par } bc.prefetcher = NewStatePrefetcher(chainConfig, bc, engine) + bc.validator = NewBlockValidator(chainConfig, bc, engine) bc.processor = NewStateProcessor(chainConfig, bc, engine) var err error @@ -3165,11 +3166,14 @@ func EnablePersistDiff(limit uint64) BlockChainOption { func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode VerifyMode, peers verifyPeers) BlockChainOption { return func(bc *BlockChain) (*BlockChain, error) { - validator, err := NewBlockValidator(chainConfig, bc, engine, mode, peers) - if err != nil { - return bc, err + if mode.NeedRemoteVerify() { + vm, err := NewVerifyManager(bc, peers, mode == InsecureVerify) + if err != nil { + return nil, err + } + go vm.mainLoop() + bc.validator = NewBlockValidator(chainConfig, bc, engine, EnableRemoteVerifyManager(vm)) } - bc.validator = validator return bc, nil } }