Skip to content

Commit da8845e

Browse files
committed
core, eth, miner: Istanbul consensus integration
1 parent 0837971 commit da8845e

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

core/blockchain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,11 @@ func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
12251225
return headers, nil
12261226
}
12271227

1228+
// HasBadBlock returns whether the block with the hash is a bad block
1229+
func (bc *BlockChain) HasBadBlock(hash common.Hash) bool {
1230+
return bc.badBlocks.Contains(hash)
1231+
}
1232+
12281233
// addBadBlock adds a bad block to the bad-block LRU cache
12291234
func (bc *BlockChain) addBadBlock(block *types.Block) {
12301235
bc.badBlocks.Add(block.Header().Hash(), block.Header())

eth/backend.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ import (
3131
"github.com/ethereum/go-ethereum/consensus"
3232
"github.com/ethereum/go-ethereum/consensus/clique"
3333
"github.com/ethereum/go-ethereum/consensus/ethash"
34+
"github.com/ethereum/go-ethereum/consensus/istanbul"
35+
istanbulBackend "github.com/ethereum/go-ethereum/consensus/istanbul/backend"
3436
"github.com/ethereum/go-ethereum/core"
3537
"github.com/ethereum/go-ethereum/core/bloombits"
3638
"github.com/ethereum/go-ethereum/core/types"
3739
"github.com/ethereum/go-ethereum/core/vm"
40+
"github.com/ethereum/go-ethereum/crypto"
3841
"github.com/ethereum/go-ethereum/eth/downloader"
3942
"github.com/ethereum/go-ethereum/eth/filters"
4043
"github.com/ethereum/go-ethereum/eth/gasprice"
@@ -133,6 +136,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
133136
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks),
134137
}
135138

139+
// force to set the istanbul etherbase to node key address
140+
if chainConfig.Istanbul != nil {
141+
eth.etherbase = crypto.PubkeyToAddress(ctx.NodeKey().PublicKey)
142+
}
143+
136144
log.Info("Initialising Ethereum protocol", "versions", eth.engine.Protocol().Versions, "network", config.NetworkId)
137145

138146
if !config.SkipBcVersionCheck {
@@ -212,6 +220,15 @@ func CreateConsensusEngine(ctx *node.ServiceContext, config *Config, chainConfig
212220
if chainConfig.Clique != nil {
213221
return clique.New(chainConfig.Clique, db)
214222
}
223+
// If Istanbul is requested, set it up
224+
if chainConfig.Istanbul != nil {
225+
if chainConfig.Istanbul.Epoch != 0 {
226+
config.Istanbul.Epoch = chainConfig.Istanbul.Epoch
227+
}
228+
config.Istanbul.ProposerPolicy = istanbul.ProposerPolicy(chainConfig.Istanbul.ProposerPolicy)
229+
return istanbulBackend.New(&config.Istanbul, ctx.NodeKey(), db)
230+
}
231+
215232
// Otherwise assume proof-of-work
216233
switch {
217234
case config.PowFake:
@@ -311,6 +328,10 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
311328
// set in js console via admin interface or wrapper from cli flags
312329
func (self *Ethereum) SetEtherbase(etherbase common.Address) {
313330
self.lock.Lock()
331+
if _, ok := self.engine.(consensus.Istanbul); ok {
332+
log.Error("Cannot set etherbase in Istanbul consensus")
333+
return
334+
}
314335
self.etherbase = etherbase
315336
self.lock.Unlock()
316337

miner/worker.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func (self *worker) start() {
206206

207207
atomic.StoreInt32(&self.mining, 1)
208208

209+
if istanbul, ok := self.engine.(consensus.Istanbul); ok {
210+
istanbul.Start(self.chain, self.chain.CurrentBlock, self.chain.HasBadBlock)
211+
}
212+
209213
// spin up agents
210214
for agent := range self.agents {
211215
agent.Start()
@@ -222,6 +226,11 @@ func (self *worker) stop() {
222226
agent.Stop()
223227
}
224228
}
229+
230+
if istanbul, ok := self.engine.(consensus.Istanbul); ok {
231+
istanbul.Stop()
232+
}
233+
225234
atomic.StoreInt32(&self.mining, 0)
226235
atomic.StoreInt32(&self.atWork, 0)
227236
}
@@ -250,6 +259,9 @@ func (self *worker) update() {
250259
select {
251260
// Handle ChainHeadEvent
252261
case <-self.chainHeadCh:
262+
if h, ok := self.engine.(consensus.Handler); ok {
263+
h.NewChainHead()
264+
}
253265
self.commitNewWork()
254266

255267
// Handle ChainSideEvent

0 commit comments

Comments
 (0)