diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-04-05 06:16:29 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-04-05 06:16:29 +0800 |
commit | 09777952ee476ff80d4b6e63b5041ff5ca0e441b (patch) | |
tree | e85320f88f548201e3476b3e7095e96fd071617b /core/blockchain.go | |
parent | e50a5b77712d891ff409aa942a5cbc24e721b332 (diff) | |
download | dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar.gz dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar.bz2 dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar.lz dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar.xz dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.tar.zst dexon-09777952ee476ff80d4b6e63b5041ff5ca0e441b.zip |
core, consensus: pluggable consensus engines (#3817)
This commit adds pluggable consensus engines to go-ethereum. In short, it
introduces a generic consensus interface, and refactors the entire codebase to
use this interface.
Diffstat (limited to 'core/blockchain.go')
-rw-r--r-- | core/blockchain.go | 62 |
1 files changed, 26 insertions, 36 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index a57832df0..4793431d8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/mclock" + "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" @@ -39,7 +40,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" "github.com/hashicorp/golang-lru" @@ -104,7 +104,7 @@ type BlockChain struct { procInterrupt int32 // interrupt signaler for block processing wg sync.WaitGroup // chain processing wait group for shutting down - pow pow.PoW + engine consensus.Engine processor Processor // block processor interface validator Validator // block and state validator interface vmConfig vm.Config @@ -115,7 +115,7 @@ type BlockChain struct { // NewBlockChain returns a fully initialised block chain using information // available in the database. It initialiser the default Ethereum Validator and // Processor. -func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, pow pow.PoW, mux *event.TypeMux, vmConfig vm.Config) (*BlockChain, error) { +func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, engine consensus.Engine, mux *event.TypeMux, vmConfig vm.Config) (*BlockChain, error) { bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) blockCache, _ := lru.New(blockCacheLimit) @@ -131,25 +131,22 @@ func NewBlockChain(chainDb ethdb.Database, config *params.ChainConfig, pow pow.P bodyRLPCache: bodyRLPCache, blockCache: blockCache, futureBlocks: futureBlocks, - pow: pow, + engine: engine, vmConfig: vmConfig, badBlocks: badBlocks, } - bc.SetValidator(NewBlockValidator(config, bc, pow)) - bc.SetProcessor(NewStateProcessor(config, bc)) + bc.SetValidator(NewBlockValidator(config, bc, engine)) + bc.SetProcessor(NewStateProcessor(config, bc, engine)) - gv := func() HeaderValidator { return bc.Validator() } var err error - bc.hc, err = NewHeaderChain(chainDb, config, gv, bc.getProcInterrupt) + bc.hc, err = NewHeaderChain(chainDb, config, engine, bc.getProcInterrupt) if err != nil { return nil, err } - bc.genesisBlock = bc.GetBlockByNumber(0) if bc.genesisBlock == nil { return nil, ErrNoGenesis } - if err := bc.loadLastState(); err != nil { return nil, err } @@ -233,9 +230,6 @@ func (self *BlockChain) loadLastState() error { log.Info("Loaded most recent local full block", "number", self.currentBlock.Number(), "hash", self.currentBlock.Hash(), "td", blockTd) log.Info("Loaded most recent local fast block", "number", self.currentFastBlock.Number(), "hash", self.currentFastBlock.Hash(), "td", fastTd) - // Try to be smart and issue a pow verification for the head to pre-generate caches - go self.pow.Verify(types.NewBlockWithHeader(currentHeader)) - return nil } @@ -383,9 +377,6 @@ func (self *BlockChain) Processor() Processor { return self.processor } -// AuxValidator returns the auxiliary validator (Proof of work atm) -func (self *BlockChain) AuxValidator() pow.PoW { return self.pow } - // State returns a new mutable state based on the current HEAD block. func (self *BlockChain) State() (*state.StateDB, error) { return self.StateAt(self.CurrentBlock().Root()) @@ -906,38 +897,38 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) { stats = insertStats{startTime: mclock.Now()} events = make([]interface{}, 0, len(chain)) coalescedLogs []*types.Log - nonceChecked = make([]bool, len(chain)) ) + // Start the parallel header verifier + headers := make([]*types.Header, len(chain)) + seals := make([]bool, len(chain)) - // Start the parallel nonce verifier. - nonceAbort, nonceResults := verifyNoncesFromBlocks(self.pow, chain) - defer close(nonceAbort) + for i, block := range chain { + headers[i] = block.Header() + seals[i] = true + } + abort, results := self.engine.VerifyHeaders(self, headers, seals) + defer close(abort) + // Iterate over the blocks and insert when the verifier permits for i, block := range chain { + // If the chain is terminating, stop processing blocks if atomic.LoadInt32(&self.procInterrupt) == 1 { log.Debug("Premature abort during blocks processing") break } - bstart := time.Now() - // Wait for block i's nonce to be verified before processing - // its state transition. - for !nonceChecked[i] { - r := <-nonceResults - nonceChecked[r.index] = true - if !r.valid { - invalid := chain[r.index] - return r.index, &BlockNonceErr{Hash: invalid.Hash(), Number: invalid.Number(), Nonce: invalid.Nonce()} - } - } - + // If the header is a banned one, straight out abort if BadHashes[block.Hash()] { err := BadHashError(block.Hash()) self.reportBlock(block, nil, err) return i, err } - // Stage 1 validation of the block using the chain's validator - // interface. - err := self.Validator().ValidateBlock(block) + // Wait for the block's verification to complete + bstart := time.Now() + + err := <-results + if err == nil { + err = self.Validator().ValidateBody(block) + } if err != nil { if IsKnownBlockErr(err) { stats.ignored++ @@ -952,7 +943,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) { if block.Time().Cmp(max) == 1 { return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) } - self.futureBlocks.Add(block.Hash(), block) stats.queued++ continue |