aboutsummaryrefslogtreecommitdiffstats
path: root/miner
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-05-16 06:27:13 +0800
committerobscuren <geffobscura@gmail.com>2015-05-16 06:27:13 +0800
commit1564f1a020b9edc78bc672f8f2df64b3d0dc55c3 (patch)
treed898e2b20a6c2e0b5613ae7f669499c5db23b719 /miner
parent8e24378cc1acb074b56de75bf0baf6feb7927677 (diff)
parent7ea76fcf993f3fecb55233bdcc2409618d9080b9 (diff)
downloaddexon-1564f1a020b9edc78bc672f8f2df64b3d0dc55c3.tar.gz
dexon-1564f1a020b9edc78bc672f8f2df64b3d0dc55c3.tar.zst
dexon-1564f1a020b9edc78bc672f8f2df64b3d0dc55c3.zip
Merge branch 'release/0.9.21'
Diffstat (limited to 'miner')
-rw-r--r--miner/miner.go71
-rw-r--r--miner/worker.go19
2 files changed, 67 insertions, 23 deletions
diff --git a/miner/miner.go b/miner/miner.go
index 09342e250..19d39a605 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -2,33 +2,64 @@ package miner
import (
"math/big"
+ "sync/atomic"
"github.com/ethereum/go-ethereum/common"
"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/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/pow"
)
type Miner struct {
+ mux *event.TypeMux
+
worker *worker
MinAcceptedGasPrice *big.Int
- threads int
- mining bool
- eth core.Backend
- pow pow.PoW
+ threads int
+ coinbase common.Address
+ mining int32
+ eth core.Backend
+ pow pow.PoW
+
+ canStart int32 // can start indicates whether we can start the mining operation
+ shouldStart int32 // should start indicates whether we should start after sync
}
-func New(eth core.Backend, pow pow.PoW) *Miner {
- return &Miner{eth: eth, pow: pow, worker: newWorker(common.Address{}, eth)}
+func New(eth core.Backend, mux *event.TypeMux, pow pow.PoW) *Miner {
+ miner := &Miner{eth: eth, mux: mux, pow: pow, worker: newWorker(common.Address{}, eth), canStart: 1}
+ go miner.update()
+
+ return miner
}
-func (self *Miner) Mining() bool {
- return self.mining
+func (self *Miner) update() {
+ events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
+ for ev := range events.Chan() {
+ switch ev.(type) {
+ case downloader.StartEvent:
+ atomic.StoreInt32(&self.canStart, 0)
+ if self.Mining() {
+ self.Stop()
+ atomic.StoreInt32(&self.shouldStart, 1)
+ glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation")
+ }
+ 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, self.threads)
+ }
+ }
+ }
}
func (m *Miner) SetGasPrice(price *big.Int) {
@@ -41,36 +72,48 @@ func (m *Miner) SetGasPrice(price *big.Int) {
}
func (self *Miner) Start(coinbase common.Address, threads int) {
+ atomic.StoreInt32(&self.shouldStart, 1)
+ self.threads = threads
+ self.worker.coinbase = coinbase
+
+ if atomic.LoadInt32(&self.canStart) == 0 {
+ glog.V(logger.Info).Infoln("Can not start mining operation due to network sync (starts when finished)")
+ return
+ }
- self.mining = true
+ atomic.StoreInt32(&self.mining, 1)
for i := 0; i < threads; i++ {
self.worker.register(NewCpuAgent(i, self.pow))
}
- self.threads = threads
glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
- self.worker.coinbase = coinbase
self.worker.start()
+
self.worker.commitNewWork()
}
func (self *Miner) Stop() {
self.worker.stop()
- self.mining = false
+ atomic.StoreInt32(&self.mining, 0)
+ atomic.StoreInt32(&self.shouldStart, 0)
}
func (self *Miner) Register(agent Agent) {
- if self.mining {
+ if self.Mining() {
agent.Start()
}
self.worker.register(agent)
}
+func (self *Miner) Mining() bool {
+ return atomic.LoadInt32(&self.mining) > 0
+}
+
func (self *Miner) HashRate() int64 {
- return self.worker.HashRate()
+ return self.pow.GetHashrate()
}
func (self *Miner) SetExtra(extra []byte) {
diff --git a/miner/worker.go b/miner/worker.go
index f737be507..d5f9dd8c5 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -45,7 +45,8 @@ type environment struct {
state *state.StateDB // apply state changes here
coinbase *state.StateObject // the miner's account
block *types.Block // the new block
- family *set.Set // family set (used for checking uncles)
+ ancestors *set.Set // ancestor set (used for checking uncle parent validity)
+ family *set.Set // family set (used for checking uncle invalidity)
uncles *set.Set // uncle set
remove *set.Set // tx which will be removed
tcount int // tx count in cycle
@@ -62,6 +63,7 @@ func env(block *types.Block, eth core.Backend) *environment {
totalUsedGas: new(big.Int),
state: state,
block: block,
+ ancestors: set.New(),
family: set.New(),
uncles: set.New(),
coinbase: state.GetOrNewStateObject(block.Coinbase()),
@@ -173,7 +175,6 @@ func (self *worker) stop() {
func (self *worker) register(agent Agent) {
self.mu.Lock()
defer self.mu.Unlock()
-
self.agents = append(self.agents, agent)
agent.SetReturnCh(self.recv)
}
@@ -265,7 +266,11 @@ func (self *worker) makeCurrent() {
current := env(block, self.eth)
for _, ancestor := range self.chain.GetAncestors(block, 7) {
+ for _, uncle := range ancestor.Uncles() {
+ current.family.Add(uncle.Hash())
+ }
current.family.Add(ancestor.Hash())
+ current.ancestors.Add(ancestor.Hash())
}
accounts, _ := self.eth.AccountManager().Accounts()
// Keep track of transactions which return errors so they can be removed
@@ -363,7 +368,7 @@ func (self *worker) commitUncle(uncle *types.Header) error {
}
self.current.uncles.Add(uncle.Hash())
- if !self.current.family.Has(uncle.ParentHash) {
+ if !self.current.ancestors.Has(uncle.ParentHash) {
return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
}
@@ -453,13 +458,9 @@ func (self *worker) commitTransaction(tx *types.Transaction) error {
return nil
}
+// TODO: remove or use
func (self *worker) HashRate() int64 {
- var tot int64
- for _, agent := range self.agents {
- tot += agent.GetHashRate()
- }
-
- return tot
+ return 0
}
// gasprice calculates a reduced gas price based on the pct