quorum/ethminer/miner.go

185 lines
5.5 KiB
Go
Raw Normal View History

2014-03-20 03:20:29 -07:00
package ethminer
import (
"bytes"
"github.com/ethereum/eth-go/ethchain"
2014-06-26 10:45:57 -07:00
"github.com/ethereum/eth-go/ethlog"
2014-03-20 03:20:29 -07:00
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
"sort"
2014-03-20 03:20:29 -07:00
)
var logger = ethlog.NewLogger("MINER")
2014-03-20 03:20:29 -07:00
type Miner struct {
2014-05-21 15:25:34 -07:00
pow ethchain.PoW
ethereum ethchain.EthManager
coinbase []byte
reactChan chan ethutil.React
txs ethchain.Transactions
2014-05-21 15:25:34 -07:00
uncles []*ethchain.Block
block *ethchain.Block
powChan chan []byte
powQuitChan chan ethutil.React
quitChan chan bool
2014-03-20 03:20:29 -07:00
}
func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
2014-05-21 15:25:34 -07:00
reactChan := make(chan ethutil.React, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in
powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block
powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread
quitChan := make(chan bool, 1)
2014-03-20 03:20:29 -07:00
ethereum.Reactor().Subscribe("newBlock", reactChan)
2014-05-15 06:00:25 -07:00
ethereum.Reactor().Subscribe("newTx:pre", reactChan)
2014-03-20 03:20:29 -07:00
// We need the quit chan to be a Reactor event.
// The POW search method is actually blocking and if we don't
// listen to the reactor events inside of the pow itself
// The miner overseer will never get the reactor events themselves
// Only after the miner will find the sha
2014-05-21 15:25:34 -07:00
ethereum.Reactor().Subscribe("newBlock", powQuitChan)
ethereum.Reactor().Subscribe("newTx:pre", powQuitChan)
2014-03-20 03:20:29 -07:00
miner := Miner{
2014-05-21 15:25:34 -07:00
pow: &ethchain.EasyPow{},
ethereum: ethereum,
coinbase: coinbase,
reactChan: reactChan,
powChan: powChan,
powQuitChan: powQuitChan,
quitChan: quitChan,
2014-03-20 03:20:29 -07:00
}
// Insert initial TXs in our little miner 'pool'
miner.txs = ethereum.TxPool().Flush()
miner.block = ethereum.BlockChain().NewBlock(miner.coinbase)
2014-03-20 03:20:29 -07:00
return miner
}
2014-06-26 10:45:57 -07:00
2014-03-20 03:20:29 -07:00
func (miner *Miner) Start() {
// Prepare inital block
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
go miner.listener()
2014-06-26 10:45:57 -07:00
logger.Infoln("Started")
2014-03-20 03:20:29 -07:00
}
2014-06-26 10:45:57 -07:00
2014-03-20 03:20:29 -07:00
func (miner *Miner) listener() {
2014-05-21 15:25:34 -07:00
out:
2014-03-20 03:20:29 -07:00
for {
select {
2014-05-21 15:25:34 -07:00
case <-miner.quitChan:
2014-06-26 10:45:57 -07:00
logger.Infoln("Stopped")
2014-05-21 15:25:34 -07:00
break out
2014-03-20 03:20:29 -07:00
case chanMessage := <-miner.reactChan:
2014-06-26 10:45:57 -07:00
2014-03-20 03:20:29 -07:00
if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
//logger.Infoln("Got new block via Reactor")
2014-03-20 03:20:29 -07:00
if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 {
// TODO: Perhaps continue mining to get some uncle rewards
//logger.Infoln("New top block found resetting state")
2014-03-20 03:20:29 -07:00
// Filter out which Transactions we have that were not in this block
var newtxs []*ethchain.Transaction
for _, tx := range miner.txs {
found := false
for _, othertx := range block.Transactions() {
if bytes.Compare(tx.Hash(), othertx.Hash()) == 0 {
found = true
}
}
if found == false {
newtxs = append(newtxs, tx)
}
}
miner.txs = newtxs
// Setup a fresh state to mine on
//miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
2014-03-20 03:20:29 -07:00
} else {
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
logger.Infoln("Adding uncle block")
2014-03-20 03:20:29 -07:00
miner.uncles = append(miner.uncles, block)
}
}
}
if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok {
found := false
for _, ctx := range miner.txs {
if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found {
break
}
}
if found == false {
// Undo all previous commits
miner.block.Undo()
// Apply new transactions
2014-03-20 03:20:29 -07:00
miner.txs = append(miner.txs, tx)
}
}
default:
miner.mineNewBlock()
}
}
}
2014-05-21 15:25:34 -07:00
func (self *Miner) Stop() {
2014-06-26 10:45:57 -07:00
logger.Infoln("Stopping...")
2014-05-21 15:25:34 -07:00
self.quitChan <- true
2014-06-26 10:45:57 -07:00
self.powQuitChan <- ethutil.React{}
2014-05-21 15:25:34 -07:00
}
func (self *Miner) mineNewBlock() {
stateManager := self.ethereum.StateManager()
self.block = self.ethereum.BlockChain().NewBlock(self.coinbase)
2014-03-20 03:20:29 -07:00
// Apply uncles
if len(self.uncles) > 0 {
self.block.SetUncles(self.uncles)
}
2014-03-20 03:20:29 -07:00
// Sort the transactions by nonce in case of odd network propagation
sort.Sort(ethchain.TxByNonce{self.txs})
2014-06-14 02:46:09 -07:00
// Accumulate all valid transactions and apply them to the new state
2014-06-14 02:46:09 -07:00
// Error may be ignored. It's not important during mining
2014-06-16 02:51:16 -07:00
parent := self.ethereum.BlockChain().GetBlock(self.block.PrevHash)
2014-06-16 02:14:01 -07:00
coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase)
2014-06-16 02:51:16 -07:00
coinbase.SetGasPool(self.block.CalcGasLimit(parent))
2014-06-16 02:14:01 -07:00
receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs)
2014-06-14 02:46:09 -07:00
if err != nil {
logger.Debugln(err)
2014-06-14 02:46:09 -07:00
}
self.txs = append(txs, unhandledTxs...)
// Set the transactions to the block so the new SHA3 can be calculated
self.block.SetReceipts(receipts, txs)
2014-06-14 02:46:09 -07:00
// Accumulate the rewards included for this block
stateManager.AccumelateRewards(self.block.State(), self.block)
self.block.State().Update()
2014-06-25 10:43:35 -07:00
logger.Infof("Mining on block. Includes %v transactions", len(self.txs))
// Find a valid nonce
2014-05-21 15:25:34 -07:00
self.block.Nonce = self.pow.Search(self.block, self.powQuitChan)
if self.block.Nonce != nil {
err := self.ethereum.StateManager().Process(self.block, false)
if err != nil {
logger.Infoln(err)
} else {
self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val})
logger.Infof("🔨 Mined block %x\n", self.block.Hash())
logger.Infoln(self.block)
// Gather the new batch of transactions currently in the tx pool
self.txs = self.ethereum.TxPool().CurrentTransactions()
2014-03-20 03:20:29 -07:00
}
}
}