|
|
@ -85,11 +85,7 @@ func (miner *Miner) listener() { |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case event, isopen := <-miner.events.Chan(): |
|
|
|
case event := <-miner.events.Chan(): |
|
|
|
if !isopen { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch event := event.(type) { |
|
|
|
switch event := event.(type) { |
|
|
|
case ethchain.NewBlockEvent: |
|
|
|
case ethchain.NewBlockEvent: |
|
|
|
miner.stopMining() |
|
|
|
miner.stopMining() |
|
|
@ -114,16 +110,13 @@ func (miner *Miner) listener() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
miner.txs = newtxs |
|
|
|
miner.txs = newtxs |
|
|
|
|
|
|
|
|
|
|
|
// Setup a fresh state to mine on
|
|
|
|
|
|
|
|
//miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase, miner.txs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { |
|
|
|
if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { |
|
|
|
logger.Infoln("Adding uncle block") |
|
|
|
logger.Infoln("Adding uncle block") |
|
|
|
miner.uncles = append(miner.uncles, block) |
|
|
|
miner.uncles = append(miner.uncles, block) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
miner.startMining() |
|
|
|
|
|
|
|
|
|
|
|
case ethchain.TxEvent: |
|
|
|
case ethchain.TxEvent: |
|
|
|
if event.Type == ethchain.TxPre { |
|
|
|
if event.Type == ethchain.TxPre { |
|
|
@ -141,6 +134,8 @@ func (miner *Miner) listener() { |
|
|
|
// Apply new transactions
|
|
|
|
// Apply new transactions
|
|
|
|
miner.txs = append(miner.txs, event.Tx) |
|
|
|
miner.txs = append(miner.txs, event.Tx) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
miner.startMining() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -159,8 +154,12 @@ func (miner *Miner) startMining() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (miner *Miner) stopMining() { |
|
|
|
func (miner *Miner) stopMining() { |
|
|
|
close(miner.powQuitChan) |
|
|
|
println("stop mining") |
|
|
|
<-miner.powDone |
|
|
|
_, isopen := <-miner.powQuitChan |
|
|
|
|
|
|
|
if isopen { |
|
|
|
|
|
|
|
close(miner.powQuitChan) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//<-miner.powDone
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Miner) mineNewBlock() { |
|
|
|
func (self *Miner) mineNewBlock() { |
|
|
@ -187,10 +186,9 @@ func (self *Miner) mineNewBlock() { |
|
|
|
} |
|
|
|
} |
|
|
|
self.ethereum.TxPool().RemoveSet(erroneous) |
|
|
|
self.ethereum.TxPool().RemoveSet(erroneous) |
|
|
|
self.txs = append(txs, unhandledTxs...) |
|
|
|
self.txs = append(txs, unhandledTxs...) |
|
|
|
self.block.SetReceiptHash(receipts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the transactions to the block so the new SHA3 can be calculated
|
|
|
|
self.block.SetReceipts(receipts) |
|
|
|
self.block.SetReceipts(receipts, txs) |
|
|
|
self.block.SetTransactions(txs) |
|
|
|
|
|
|
|
|
|
|
|
// Accumulate the rewards included for this block
|
|
|
|
// Accumulate the rewards included for this block
|
|
|
|
stateManager.AccumelateRewards(self.block.State(), self.block, parent) |
|
|
|
stateManager.AccumelateRewards(self.block.State(), self.block, parent) |
|
|
@ -203,7 +201,7 @@ func (self *Miner) mineNewBlock() { |
|
|
|
nonce := self.pow.Search(self.block, self.powQuitChan) |
|
|
|
nonce := self.pow.Search(self.block, self.powQuitChan) |
|
|
|
if nonce != nil { |
|
|
|
if nonce != nil { |
|
|
|
self.block.Nonce = nonce |
|
|
|
self.block.Nonce = nonce |
|
|
|
err := self.ethereum.StateManager().Process(self.block, false) |
|
|
|
err := self.ethereum.StateManager().Process(self.block) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logger.Infoln(err) |
|
|
|
logger.Infoln(err) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -212,7 +210,10 @@ func (self *Miner) mineNewBlock() { |
|
|
|
logger.Infoln(self.block) |
|
|
|
logger.Infoln(self.block) |
|
|
|
// Gather the new batch of transactions currently in the tx pool
|
|
|
|
// Gather the new batch of transactions currently in the tx pool
|
|
|
|
self.txs = self.ethereum.TxPool().CurrentTransactions() |
|
|
|
self.txs = self.ethereum.TxPool().CurrentTransactions() |
|
|
|
|
|
|
|
self.ethereum.EventMux().Post(ethchain.NewBlockEvent{self.block}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Continue mining on the next block
|
|
|
|
|
|
|
|
self.startMining() |
|
|
|
} |
|
|
|
} |
|
|
|
self.powDone <- struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|