Skip to content

Commit

Permalink
core, les, eth, miner: Istanbul consensus integration
Browse files Browse the repository at this point in the history
  • Loading branch information
markya0616 committed Dec 18, 2017
1 parent 2e7ee8f commit 4520cf2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 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
44 changes: 33 additions & 11 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 @@ -125,7 +128,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
chainConfig: chainConfig,
eventMux: ctx.EventMux,
accountManager: ctx.AccountManager,
engine: CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
engine: CreateConsensusEngine(ctx, config, chainConfig, chainDb),
shutdownChan: make(chan bool),
stopDbUpgrade: stopDbUpgrade,
networkId: config.NetworkId,
Expand All @@ -135,6 +138,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 @@ -209,30 +217,40 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data
}

// CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
func CreateConsensusEngine(ctx *node.ServiceContext, config *Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
// If proof-of-authority is requested, set it up
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
ethConfig := config.Ethash
switch {
case config.PowMode == ethash.ModeFake:
case ethConfig.PowMode == ethash.ModeFake:
log.Warn("Ethash used in fake mode")
return ethash.NewFaker()
case config.PowMode == ethash.ModeTest:
case ethConfig.PowMode == ethash.ModeTest:
log.Warn("Ethash used in test mode")
return ethash.NewTester()
case config.PowMode == ethash.ModeShared:
case ethConfig.PowMode == ethash.ModeShared:
log.Warn("Ethash used in shared mode")
return ethash.NewShared()
default:
engine := ethash.New(ethash.Config{
CacheDir: ctx.ResolvePath(config.CacheDir),
CachesInMem: config.CachesInMem,
CachesOnDisk: config.CachesOnDisk,
DatasetDir: config.DatasetDir,
DatasetsInMem: config.DatasetsInMem,
DatasetsOnDisk: config.DatasetsOnDisk,
CacheDir: ctx.ResolvePath(ethConfig.CacheDir),
CachesInMem: ethConfig.CachesInMem,
CachesOnDisk: ethConfig.CachesOnDisk,
DatasetDir: ethConfig.DatasetDir,
DatasetsInMem: ethConfig.DatasetsInMem,
DatasetsOnDisk: ethConfig.DatasetsOnDisk,
})
engine.SetThreads(-1) // Disable CPU mining
return engine
Expand Down Expand Up @@ -326,6 +344,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
2 changes: 1 addition & 1 deletion les/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
peers: peers,
reqDist: newRequestDistributor(peers, quitSync),
accountManager: ctx.AccountManager,
engine: eth.CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb),
shutdownChan: make(chan bool),
networkId: config.NetworkId,
bloomRequests: make(chan chan *bloombits.Retrieval),
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 4520cf2

Please sign in to comment.