Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
miner: move agent logic to worker (#17351)
Browse files Browse the repository at this point in the history
* miner: move agent logic to worker

* miner: polish

* core: persist block before reorg
  • Loading branch information
rjl493456442 authored and karalabe committed Aug 14, 2018
1 parent e0e0e53 commit a1783d1
Show file tree
Hide file tree
Showing 5 changed files with 646 additions and 474 deletions.
7 changes: 4 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
return NonStatTy, err
}
// Write other block data using a batch.
batch := bc.db.NewBatch()
rawdb.WriteBlock(batch, block)
rawdb.WriteBlock(bc.db, block)

root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
if err != nil {
Expand Down Expand Up @@ -955,6 +953,9 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
}
}
}

// Write other block data using a batch.
batch := bc.db.NewBatch()
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)

// If the total difficulty is higher than our known, add it to the canonical chain
Expand Down
116 changes: 0 additions & 116 deletions miner/agent.go

This file was deleted.

64 changes: 32 additions & 32 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ import (
"fmt"
"sync/atomic"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)

// Backend wraps all methods required for mining.
type Backend interface {
AccountManager() *accounts.Manager
BlockChain() *core.BlockChain
TxPool() *core.TxPool
ChainDb() ethdb.Database
}

// Miner creates blocks and searches for proof-of-work values.
Expand All @@ -49,6 +45,7 @@ type Miner struct {
coinbase common.Address
eth Backend
engine consensus.Engine
exitCh chan struct{}

canStart int32 // can start indicates whether we can start the mining operation
shouldStart int32 // should start indicates whether we should start after sync
Expand All @@ -59,10 +56,10 @@ func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine con
eth: eth,
mux: mux,
engine: engine,
exitCh: make(chan struct{}),
worker: newWorker(config, engine, eth, mux),
canStart: 1,
}
miner.Register(NewCpuAgent(eth.BlockChain(), engine))
go miner.update()

return miner
Expand All @@ -74,28 +71,35 @@ func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine con
// and halt your mining operation for as long as the DOS continues.
func (self *Miner) update() {
events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
out:
for ev := range events.Chan() {
switch ev.Data.(type) {
case downloader.StartEvent:
atomic.StoreInt32(&self.canStart, 0)
if self.Mining() {
self.Stop()
atomic.StoreInt32(&self.shouldStart, 1)
log.Info("Mining aborted due to sync")
}
case downloader.DoneEvent, downloader.FailedEvent:
shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
defer events.Unsubscribe()

atomic.StoreInt32(&self.canStart, 1)
atomic.StoreInt32(&self.shouldStart, 0)
if shouldStart {
self.Start(self.coinbase)
for {
select {
case ev := <-events.Chan():
if ev == nil {
return
}
switch ev.Data.(type) {
case downloader.StartEvent:
atomic.StoreInt32(&self.canStart, 0)
if self.Mining() {
self.Stop()
atomic.StoreInt32(&self.shouldStart, 1)
log.Info("Mining aborted due to sync")
}
case downloader.DoneEvent, downloader.FailedEvent:
shouldStart := atomic.LoadInt32(&self.shouldStart) == 1

atomic.StoreInt32(&self.canStart, 1)
atomic.StoreInt32(&self.shouldStart, 0)
if shouldStart {
self.Start(self.coinbase)
}
// stop immediately and ignore all further pending events
return
}
// unsubscribe. we're only interested in this event once
events.Unsubscribe()
// stop immediately and ignore all further pending events
break out
case <-self.exitCh:
return
}
}
}
Expand All @@ -109,20 +113,16 @@ func (self *Miner) Start(coinbase common.Address) {
return
}
self.worker.start()
self.worker.commitNewWork()
}

func (self *Miner) Stop() {
self.worker.stop()
atomic.StoreInt32(&self.shouldStart, 0)
}

func (self *Miner) Register(agent Agent) {
self.worker.register(agent)
}

func (self *Miner) Unregister(agent Agent) {
self.worker.unregister(agent)
func (self *Miner) Close() {
self.worker.close()
close(self.exitCh)
}

func (self *Miner) Mining() bool {
Expand Down
Loading

0 comments on commit a1783d1

Please sign in to comment.