Skip to content

Commit

Permalink
core, eth, miner: Istanbul consensus integration
Browse files Browse the repository at this point in the history
  • Loading branch information
markya0616 committed Oct 2, 2017
1 parent 0837971 commit da8845e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,11 @@ func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
return headers, nil
}

// HasBadBlock returns whether the block with the hash is a bad block
func (bc *BlockChain) HasBadBlock(hash common.Hash) bool {
return bc.badBlocks.Contains(hash)
}

// addBadBlock adds a bad block to the bad-block LRU cache
func (bc *BlockChain) addBadBlock(block *types.Block) {
bc.badBlocks.Add(block.Header().Hash(), block.Header())
Expand Down
21 changes: 21 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import (
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/consensus/istanbul"
istanbulBackend "github.com/ethereum/go-ethereum/consensus/istanbul/backend"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/eth/gasprice"
Expand Down Expand Up @@ -133,6 +136,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks),
}

// force to set the istanbul etherbase to node key address
if chainConfig.Istanbul != nil {
eth.etherbase = crypto.PubkeyToAddress(ctx.NodeKey().PublicKey)
}

log.Info("Initialising Ethereum protocol", "versions", eth.engine.Protocol().Versions, "network", config.NetworkId)

if !config.SkipBcVersionCheck {
Expand Down Expand Up @@ -212,6 +220,15 @@ func CreateConsensusEngine(ctx *node.ServiceContext, config *Config, chainConfig
if chainConfig.Clique != nil {
return clique.New(chainConfig.Clique, db)
}
// If Istanbul is requested, set it up
if chainConfig.Istanbul != nil {
if chainConfig.Istanbul.Epoch != 0 {
config.Istanbul.Epoch = chainConfig.Istanbul.Epoch
}
config.Istanbul.ProposerPolicy = istanbul.ProposerPolicy(chainConfig.Istanbul.ProposerPolicy)
return istanbulBackend.New(&config.Istanbul, ctx.NodeKey(), db)
}

// Otherwise assume proof-of-work
switch {
case config.PowFake:
Expand Down Expand Up @@ -311,6 +328,10 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
// set in js console via admin interface or wrapper from cli flags
func (self *Ethereum) SetEtherbase(etherbase common.Address) {
self.lock.Lock()
if _, ok := self.engine.(consensus.Istanbul); ok {
log.Error("Cannot set etherbase in Istanbul consensus")
return
}
self.etherbase = etherbase
self.lock.Unlock()

Expand Down
12 changes: 12 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func (self *worker) start() {

atomic.StoreInt32(&self.mining, 1)

if istanbul, ok := self.engine.(consensus.Istanbul); ok {
istanbul.Start(self.chain, self.chain.CurrentBlock, self.chain.HasBadBlock)
}

// spin up agents
for agent := range self.agents {
agent.Start()
Expand All @@ -222,6 +226,11 @@ func (self *worker) stop() {
agent.Stop()
}
}

if istanbul, ok := self.engine.(consensus.Istanbul); ok {
istanbul.Stop()
}

atomic.StoreInt32(&self.mining, 0)
atomic.StoreInt32(&self.atWork, 0)
}
Expand Down Expand Up @@ -250,6 +259,9 @@ func (self *worker) update() {
select {
// Handle ChainHeadEvent
case <-self.chainHeadCh:
if h, ok := self.engine.(consensus.Handler); ok {
h.NewChainHead()
}
self.commitNewWork()

// Handle ChainSideEvent
Expand Down

0 comments on commit da8845e

Please sign in to comment.