Skip to content

Commit

Permalink
setup default validator for blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
dean65 committed Apr 28, 2022
1 parent ef3ec13 commit 7490499
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
24 changes: 15 additions & 9 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 7490499

Please sign in to comment.