Skip to content

Commit 159fba7

Browse files
committed
core, les, eth, miner: Istanbul consensus integration
1 parent 3718bc0 commit 159fba7

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

core/blockchain.go

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

1409+
// HasBadBlock returns whether the block with the hash is a bad block
1410+
func (bc *BlockChain) HasBadBlock(hash common.Hash) bool {
1411+
return bc.badBlocks.Contains(hash)
1412+
}
1413+
14091414
// addBadBlock adds a bad block to the bad-block LRU cache
14101415
func (bc *BlockChain) addBadBlock(block *types.Block) {
14111416
bc.badBlocks.Add(block.Header().Hash(), block.Header())

eth/backend.go

Lines changed: 33 additions & 11 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"
@@ -125,7 +128,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
125128
chainConfig: chainConfig,
126129
eventMux: ctx.EventMux,
127130
accountManager: ctx.AccountManager,
128-
engine: CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
131+
engine: CreateConsensusEngine(ctx, config, chainConfig, chainDb),
129132
shutdownChan: make(chan bool),
130133
stopDbUpgrade: stopDbUpgrade,
131134
networkId: config.NetworkId,
@@ -135,6 +138,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
135138
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks),
136139
}
137140

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

140148
if !config.SkipBcVersionCheck {
@@ -211,30 +219,40 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data
211219
}
212220

213221
// CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
214-
func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
222+
func CreateConsensusEngine(ctx *node.ServiceContext, config *Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
215223
// If proof-of-authority is requested, set it up
216224
if chainConfig.Clique != nil {
217225
return clique.New(chainConfig.Clique, db)
218226
}
227+
// If Istanbul is requested, set it up
228+
if chainConfig.Istanbul != nil {
229+
if chainConfig.Istanbul.Epoch != 0 {
230+
config.Istanbul.Epoch = chainConfig.Istanbul.Epoch
231+
}
232+
config.Istanbul.ProposerPolicy = istanbul.ProposerPolicy(chainConfig.Istanbul.ProposerPolicy)
233+
return istanbulBackend.New(&config.Istanbul, ctx.NodeKey(), db)
234+
}
235+
219236
// Otherwise assume proof-of-work
237+
ethConfig := config.Ethash
220238
switch {
221-
case config.PowMode == ethash.ModeFake:
239+
case ethConfig.PowMode == ethash.ModeFake:
222240
log.Warn("Ethash used in fake mode")
223241
return ethash.NewFaker()
224-
case config.PowMode == ethash.ModeTest:
242+
case ethConfig.PowMode == ethash.ModeTest:
225243
log.Warn("Ethash used in test mode")
226244
return ethash.NewTester()
227-
case config.PowMode == ethash.ModeShared:
245+
case ethConfig.PowMode == ethash.ModeShared:
228246
log.Warn("Ethash used in shared mode")
229247
return ethash.NewShared()
230248
default:
231249
engine := ethash.New(ethash.Config{
232-
CacheDir: ctx.ResolvePath(config.CacheDir),
233-
CachesInMem: config.CachesInMem,
234-
CachesOnDisk: config.CachesOnDisk,
235-
DatasetDir: config.DatasetDir,
236-
DatasetsInMem: config.DatasetsInMem,
237-
DatasetsOnDisk: config.DatasetsOnDisk,
250+
CacheDir: ctx.ResolvePath(ethConfig.CacheDir),
251+
CachesInMem: ethConfig.CachesInMem,
252+
CachesOnDisk: ethConfig.CachesOnDisk,
253+
DatasetDir: ethConfig.DatasetDir,
254+
DatasetsInMem: ethConfig.DatasetsInMem,
255+
DatasetsOnDisk: ethConfig.DatasetsOnDisk,
238256
})
239257
engine.SetThreads(-1) // Disable CPU mining
240258
return engine
@@ -328,6 +346,10 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
328346
// set in js console via admin interface or wrapper from cli flags
329347
func (self *Ethereum) SetEtherbase(etherbase common.Address) {
330348
self.lock.Lock()
349+
if _, ok := self.engine.(consensus.Istanbul); ok {
350+
log.Error("Cannot set etherbase in Istanbul consensus")
351+
return
352+
}
331353
self.etherbase = etherbase
332354
self.lock.Unlock()
333355

les/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
101101
peers: peers,
102102
reqDist: newRequestDistributor(peers, quitSync),
103103
accountManager: ctx.AccountManager,
104-
engine: eth.CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
104+
engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb),
105105
shutdownChan: make(chan bool),
106106
networkId: config.NetworkId,
107107
bloomRequests: make(chan chan *bloombits.Retrieval),

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)