Skip to content

Commit d704571

Browse files
committed
core, les, eth, miner: Istanbul consensus integration
1 parent 3eb61f2 commit d704571

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
@@ -1226,6 +1226,11 @@ func (bc *BlockChain) BadBlocks() ([]BadBlockArgs, error) {
12261226
return headers, nil
12271227
}
12281228

1229+
// HasBadBlock returns whether the block with the hash is a bad block
1230+
func (bc *BlockChain) HasBadBlock(hash common.Hash) bool {
1231+
return bc.badBlocks.Contains(hash)
1232+
}
1233+
12291234
// addBadBlock adds a bad block to the bad-block LRU cache
12301235
func (bc *BlockChain) addBadBlock(block *types.Block) {
12311236
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 {
@@ -209,30 +217,40 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data
209217
}
210218

211219
// CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
212-
func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
220+
func CreateConsensusEngine(ctx *node.ServiceContext, config *Config, chainConfig *params.ChainConfig, db ethdb.Database) consensus.Engine {
213221
// If proof-of-authority is requested, set it up
214222
if chainConfig.Clique != nil {
215223
return clique.New(chainConfig.Clique, db)
216224
}
225+
// If Istanbul is requested, set it up
226+
if chainConfig.Istanbul != nil {
227+
if chainConfig.Istanbul.Epoch != 0 {
228+
config.Istanbul.Epoch = chainConfig.Istanbul.Epoch
229+
}
230+
config.Istanbul.ProposerPolicy = istanbul.ProposerPolicy(chainConfig.Istanbul.ProposerPolicy)
231+
return istanbulBackend.New(&config.Istanbul, ctx.NodeKey(), db)
232+
}
233+
217234
// Otherwise assume proof-of-work
235+
ethConfig := config.Ethash
218236
switch {
219-
case config.PowMode == ethash.ModeFake:
237+
case ethConfig.PowMode == ethash.ModeFake:
220238
log.Warn("Ethash used in fake mode")
221239
return ethash.NewFaker()
222-
case config.PowMode == ethash.ModeTest:
240+
case ethConfig.PowMode == ethash.ModeTest:
223241
log.Warn("Ethash used in test mode")
224242
return ethash.NewTester()
225-
case config.PowMode == ethash.ModeShared:
243+
case ethConfig.PowMode == ethash.ModeShared:
226244
log.Warn("Ethash used in shared mode")
227245
return ethash.NewShared()
228246
default:
229247
engine := ethash.New(ethash.Config{
230-
CacheDir: ctx.ResolvePath(config.CacheDir),
231-
CachesInMem: config.CachesInMem,
232-
CachesOnDisk: config.CachesOnDisk,
233-
DatasetDir: config.DatasetDir,
234-
DatasetsInMem: config.DatasetsInMem,
235-
DatasetsOnDisk: config.DatasetsOnDisk,
248+
CacheDir: ctx.ResolvePath(ethConfig.CacheDir),
249+
CachesInMem: ethConfig.CachesInMem,
250+
CachesOnDisk: ethConfig.CachesOnDisk,
251+
DatasetDir: ethConfig.DatasetDir,
252+
DatasetsInMem: ethConfig.DatasetsInMem,
253+
DatasetsOnDisk: ethConfig.DatasetsOnDisk,
236254
})
237255
engine.SetThreads(-1) // Disable CPU mining
238256
return engine
@@ -326,6 +344,10 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
326344
// set in js console via admin interface or wrapper from cli flags
327345
func (self *Ethereum) SetEtherbase(etherbase common.Address) {
328346
self.lock.Lock()
347+
if _, ok := self.engine.(consensus.Istanbul); ok {
348+
log.Error("Cannot set etherbase in Istanbul consensus")
349+
return
350+
}
329351
self.etherbase = etherbase
330352
self.lock.Unlock()
331353

les/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
9898
peers: peers,
9999
reqDist: newRequestDistributor(peers, quitSync),
100100
accountManager: ctx.AccountManager,
101-
engine: eth.CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
101+
engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb),
102102
shutdownChan: make(chan bool),
103103
networkId: config.NetworkId,
104104
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)