Merge pull request #989 from obscuren/develop

core, miner: fork resolving and restart miner after sync op
pull/1011/head
Jeffrey Wilcke 9 years ago
commit d116b9595c
  1. 2
      cmd/geth/admin.go
  2. 18
      core/chain_manager.go
  3. 2
      eth/backend.go
  4. 3
      miner/miner.go

@ -288,7 +288,7 @@ func (js *jsre) startMining(call otto.FunctionCall) otto.Value {
return otto.FalseValue() return otto.FalseValue()
} }
} else { } else {
threads = 4 threads = int64(js.ethereum.MinerThreads)
} }
err = js.ethereum.StartMining(int(threads)) err = js.ethereum.StartMining(int(threads))

@ -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. // 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. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
if block.Td.Cmp(self.td) > 0 { 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 // chain fork
if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() { if block.ParentHash() != cblock.Hash() {
// during split we merge two different chains and create the new canonical chain // 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} queue[i] = ChainSplitEvent{block, logs}
queueEvent.splitCount++ queueEvent.splitCount++
@ -641,10 +641,18 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks {
oldStart = oldBlock oldStart = oldBlock
newStart = newBlock newStart = newBlock
) )
// first find common number
// 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()) { for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
newChain = append(newChain, newBlock) newChain = append(newChain, newBlock)
} }
}
numSplit := newBlock.Number() numSplit := newBlock.Number()
for { for {
@ -669,7 +677,7 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks {
func (self *ChainManager) merge(oldBlock, newBlock *types.Block) { func (self *ChainManager) merge(oldBlock, newBlock *types.Block) {
newChain := self.diff(oldBlock, newBlock) 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 { for _, block := range newChain {
self.insert(block) self.insert(block)
} }

@ -190,6 +190,7 @@ type Ethereum struct {
// logger logger.LogSystem // logger logger.LogSystem
Mining bool Mining bool
MinerThreads int
NatSpec bool NatSpec bool
DataDir string DataDir string
etherbase common.Address etherbase common.Address
@ -262,6 +263,7 @@ func New(config *Config) (*Ethereum, error) {
ethVersionId: config.ProtocolVersion, ethVersionId: config.ProtocolVersion,
netVersionId: config.NetworkId, netVersionId: config.NetworkId,
NatSpec: config.NatSpec, NatSpec: config.NatSpec,
MinerThreads: config.MinerThreads,
} }
eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux()) eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())

@ -47,6 +47,7 @@ func (self *Miner) update() {
atomic.StoreInt32(&self.canStart, 0) atomic.StoreInt32(&self.canStart, 0)
if self.Mining() { if self.Mining() {
self.Stop() self.Stop()
atomic.StoreInt32(&self.shouldStart, 1)
glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation") glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation")
} }
case downloader.DoneEvent, downloader.FailedEvent: case downloader.DoneEvent, downloader.FailedEvent:
@ -100,7 +101,7 @@ func (self *Miner) Stop() {
} }
func (self *Miner) Register(agent Agent) { func (self *Miner) Register(agent Agent) {
if atomic.LoadInt32(&self.mining) == 0 { if self.Mining() {
agent.Start() agent.Start()
} }

Loading…
Cancel
Save