From fca3333f7f586832daba9e70a7ecd711baa163a9 Mon Sep 17 00:00:00 2001 From: Vitalik Buterin Date: Wed, 13 May 2015 20:46:23 -0400 Subject: Created separate family and ancestors environment objects --- miner/worker.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index f737be507..e714b5063 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()), @@ -265,6 +267,12 @@ func (self *worker) makeCurrent() { current := env(block, self.eth) for _, ancestor := range self.chain.GetAncestors(block, 7) { + current.ancestors.Add(ancestor.Hash()) + } + for _, ancestor := range self.chain.GetAncestors(block, 7) { + for _, uncle := range ancestor.Uncles() { + current.family.Add(uncle.Hash()) + } current.family.Add(ancestor.Hash()) } accounts, _ := self.eth.AccountManager().Accounts() @@ -363,7 +371,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])) } -- cgit v1.2.3 From b24f16fa532483e0714700d687f134fb373f2487 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 14 May 2015 03:39:07 +0200 Subject: Make read of ethash hashrate atomic and update ethash godep --- miner/miner.go | 2 +- miner/worker.go | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 09342e250..8143fcef7 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -70,7 +70,7 @@ func (self *Miner) Register(agent Agent) { } 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..4316038d2 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -173,7 +173,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) } @@ -453,13 +452,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 -- cgit v1.2.3 From bdec8c3e41acf56f1cb7914b216c1d12b15b657b Mon Sep 17 00:00:00 2001 From: Vitalik Buterin Date: Thu, 14 May 2015 05:02:15 -0400 Subject: Combined two loops --- miner/worker.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index e714b5063..c70ded434 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -266,14 +266,12 @@ func (self *worker) makeCurrent() { block.Header().Extra = self.extra current := env(block, self.eth) - for _, ancestor := range self.chain.GetAncestors(block, 7) { - current.ancestors.Add(ancestor.Hash()) - } 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 -- cgit v1.2.3 From b71091e337fef7e3cfad56c61c97a42094e87531 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 00:43:00 +0200 Subject: eth, eth/downloader, miner: use download events to check miner start --- miner/miner.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 13 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 8143fcef7..359be4032 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -2,33 +2,63 @@ 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() + 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,34 +71,46 @@ 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 atomic.LoadInt32(&self.mining) == 0 { agent.Start() } self.worker.register(agent) } +func (self *Miner) Mining() bool { + return atomic.LoadInt32(&self.mining) > 0 +} + func (self *Miner) HashRate() int64 { return self.pow.GetHashrate() } -- cgit v1.2.3 From 5cec1aad152115502f8ba0d7fcc1c3e40b915d7a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 15:30:34 +0200 Subject: core, miner: fork resolving and restart miner after sync op Fork resolving fixes #940 --- miner/miner.go | 1 + 1 file changed, 1 insertion(+) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 359be4032..6c220b1a6 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -47,6 +47,7 @@ func (self *Miner) update() { 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: -- cgit v1.2.3 From d3e84cc8b4d995b4cec505b1b183b44d55f61453 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 16:20:50 +0200 Subject: miner: properly check for mining operation on Register --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 6c220b1a6..19d39a605 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -101,7 +101,7 @@ func (self *Miner) Stop() { } func (self *Miner) Register(agent Agent) { - if atomic.LoadInt32(&self.mining) == 0 { + if self.Mining() { agent.Start() } -- cgit v1.2.3