From 5cec1aad152115502f8ba0d7fcc1c3e40b915d7a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 15:30:34 +0200 Subject: [PATCH 1/3] core, miner: fork resolving and restart miner after sync op Fork resolving fixes #940 --- core/chain_manager.go | 22 +++++++++++++++------- miner/miner.go | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index a0839ad41..2e8eb927d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -577,10 +577,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { // Compare the TD of the last known block in the canonical chain to make sure it's greater. // At this point it's possible that a different chain (fork) becomes the new canonical chain. if block.Td.Cmp(self.td) > 0 { - // Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging - if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() { + // chain fork + if block.ParentHash() != cblock.Hash() { // during split we merge two different chains and create the new canonical chain - self.merge(previous, block) + self.merge(cblock, block) queue[i] = ChainSplitEvent{block, logs} queueEvent.splitCount++ @@ -641,9 +641,17 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { oldStart = oldBlock newStart = newBlock ) - // first find common number - for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { - newChain = append(newChain, newBlock) + + // first reduce whoever is higher bound + if oldBlock.NumberU64() > newBlock.NumberU64() { + // reduce old chain + for oldBlock = oldBlock; oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) { + } + } else { + // reduce new chain and append new chain blocks for inserting later on + for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { + newChain = append(newChain, newBlock) + } } numSplit := newBlock.Number() @@ -669,7 +677,7 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { func (self *ChainManager) merge(oldBlock, newBlock *types.Block) { newChain := self.diff(oldBlock, newBlock) - // insert blocks + // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly for _, block := range newChain { self.insert(block) } 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: From d3e84cc8b4d995b4cec505b1b183b44d55f61453 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 16:20:50 +0200 Subject: [PATCH 2/3] miner: properly check for mining operation on Register --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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() } From 55d85d60fdad2cbd7947d87b2a81bd8df6a18025 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 May 2015 16:47:44 +0200 Subject: [PATCH 3/3] eth, cmd/geth: start mining from console respects CLI flag --- cmd/geth/admin.go | 2 +- eth/backend.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index 91a336cbb..ebdf3512a 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -288,7 +288,7 @@ func (js *jsre) startMining(call otto.FunctionCall) otto.Value { return otto.FalseValue() } } else { - threads = 4 + threads = int64(js.ethereum.MinerThreads) } err = js.ethereum.StartMining(int(threads)) diff --git a/eth/backend.go b/eth/backend.go index 064018955..a7107f8d8 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -190,6 +190,7 @@ type Ethereum struct { // logger logger.LogSystem Mining bool + MinerThreads int NatSpec bool DataDir string etherbase common.Address @@ -262,6 +263,7 @@ func New(config *Config) (*Ethereum, error) { ethVersionId: config.ProtocolVersion, netVersionId: config.NetworkId, NatSpec: config.NatSpec, + MinerThreads: config.MinerThreads, } eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())