2014-03-20 12:20:29 +02:00
|
|
|
package ethminer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/ethereum/eth-go/ethwire"
|
2014-05-28 13:06:09 +03:00
|
|
|
"sort"
|
2014-03-20 12:20:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Miner struct {
|
2014-05-22 01:25:34 +03:00
|
|
|
pow ethchain.PoW
|
|
|
|
ethereum ethchain.EthManager
|
|
|
|
coinbase []byte
|
|
|
|
reactChan chan ethutil.React
|
2014-05-28 13:06:09 +03:00
|
|
|
txs ethchain.Transactions
|
2014-05-22 01:25:34 +03:00
|
|
|
uncles []*ethchain.Block
|
|
|
|
block *ethchain.Block
|
|
|
|
powChan chan []byte
|
|
|
|
powQuitChan chan ethutil.React
|
|
|
|
quitChan chan bool
|
2014-03-20 12:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
|
2014-05-22 01:25:34 +03: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
|
2014-05-30 14:27:56 +03:00
|
|
|
quitChan := make(chan bool, 1)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
ethereum.Reactor().Subscribe("newBlock", reactChan)
|
2014-05-15 16:00:25 +03:00
|
|
|
ethereum.Reactor().Subscribe("newTx:pre", reactChan)
|
2014-03-20 12:20:29 +02: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-22 01:25:34 +03:00
|
|
|
ethereum.Reactor().Subscribe("newBlock", powQuitChan)
|
|
|
|
ethereum.Reactor().Subscribe("newTx:pre", powQuitChan)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
miner := Miner{
|
2014-05-22 01:25:34 +03:00
|
|
|
pow: ðchain.EasyPow{},
|
|
|
|
ethereum: ethereum,
|
|
|
|
coinbase: coinbase,
|
|
|
|
reactChan: reactChan,
|
|
|
|
powChan: powChan,
|
|
|
|
powQuitChan: powQuitChan,
|
2014-05-30 14:27:56 +03:00
|
|
|
quitChan: quitChan,
|
2014-03-20 12:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert initial TXs in our little miner 'pool'
|
|
|
|
miner.txs = ethereum.TxPool().Flush()
|
2014-05-22 18:35:26 +03:00
|
|
|
miner.block = ethereum.BlockChain().NewBlock(miner.coinbase)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
return miner
|
|
|
|
}
|
|
|
|
func (miner *Miner) Start() {
|
|
|
|
// Prepare inital block
|
2014-05-17 15:07:52 +03:00
|
|
|
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
|
|
|
|
go miner.listener()
|
2014-03-20 12:20:29 +02:00
|
|
|
}
|
|
|
|
func (miner *Miner) listener() {
|
2014-05-22 01:25:34 +03:00
|
|
|
out:
|
2014-03-20 12:20:29 +02:00
|
|
|
for {
|
|
|
|
select {
|
2014-05-22 01:25:34 +03:00
|
|
|
case <-miner.quitChan:
|
|
|
|
break out
|
2014-03-20 12:20:29 +02:00
|
|
|
case chanMessage := <-miner.reactChan:
|
|
|
|
if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
|
2014-05-21 15:00:13 +03:00
|
|
|
//ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor")
|
2014-03-20 12:20:29 +02:00
|
|
|
if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 {
|
|
|
|
// TODO: Perhaps continue mining to get some uncle rewards
|
2014-05-21 15:00:13 +03:00
|
|
|
//ethutil.Config.Log.Infoln("[MINER] New top block found resetting state")
|
2014-03-20 12:20:29 +02: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
|
2014-05-22 18:35:26 +03:00
|
|
|
//miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
|
2014-05-19 12:25:27 +03:00
|
|
|
ethutil.Config.Log.Infoln("[MINER] Adding uncle block")
|
2014-03-20 12:20:29 +02: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 {
|
2014-05-21 15:00:13 +03:00
|
|
|
// Undo all previous commits
|
2014-05-21 01:17:50 +03:00
|
|
|
miner.block.Undo()
|
2014-05-21 15:00:13 +03:00
|
|
|
// Apply new transactions
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.txs = append(miner.txs, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2014-05-21 15:00:13 +03:00
|
|
|
miner.mineNewBlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 14:04:40 +03:00
|
|
|
|
2014-05-22 01:25:34 +03:00
|
|
|
func (self *Miner) Stop() {
|
|
|
|
self.powQuitChan <- ethutil.React{}
|
|
|
|
self.quitChan <- true
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:00:13 +03:00
|
|
|
func (self *Miner) mineNewBlock() {
|
|
|
|
stateManager := self.ethereum.StateManager()
|
2014-05-21 14:04:40 +03:00
|
|
|
|
2014-05-22 18:35:26 +03:00
|
|
|
self.block = self.ethereum.BlockChain().NewBlock(self.coinbase)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
2014-05-21 15:00:13 +03:00
|
|
|
// Apply uncles
|
|
|
|
if len(self.uncles) > 0 {
|
|
|
|
self.block.SetUncles(self.uncles)
|
|
|
|
}
|
2014-03-20 12:20:29 +02:00
|
|
|
|
2014-05-28 13:06:09 +03:00
|
|
|
// Sort the transactions by nonce in case of odd network propagation
|
|
|
|
sort.Sort(ethchain.TxByNonce{self.txs})
|
2014-06-14 12:46:09 +03:00
|
|
|
|
2014-05-21 15:00:13 +03:00
|
|
|
// Accumulate all valid transaction and apply them to the new state
|
2014-06-14 12:46:09 +03:00
|
|
|
// Error may be ignored. It's not important during mining
|
2014-06-16 12:14:01 +03:00
|
|
|
coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase)
|
|
|
|
receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs)
|
2014-06-14 12:46:09 +03:00
|
|
|
if err != nil {
|
|
|
|
ethutil.Config.Log.Debugln("[MINER]", err)
|
|
|
|
}
|
|
|
|
self.txs = append(txs, unhandledTxs...)
|
|
|
|
|
2014-05-21 15:00:13 +03:00
|
|
|
// Set the transactions to the block so the new SHA3 can be calculated
|
2014-05-22 18:35:26 +03:00
|
|
|
self.block.SetReceipts(receipts, txs)
|
2014-06-14 12:46:09 +03:00
|
|
|
|
2014-05-21 15:00:13 +03:00
|
|
|
// Accumulate the rewards included for this block
|
|
|
|
stateManager.AccumelateRewards(self.block.State(), self.block)
|
|
|
|
|
|
|
|
ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions")
|
|
|
|
|
|
|
|
// Find a valid nonce
|
2014-05-22 01:25:34 +03:00
|
|
|
self.block.Nonce = self.pow.Search(self.block, self.powQuitChan)
|
2014-05-21 15:00:13 +03:00
|
|
|
if self.block.Nonce != nil {
|
2014-05-30 17:57:58 +03:00
|
|
|
err := self.ethereum.StateManager().Process(self.block, false)
|
2014-05-21 15:00:13 +03:00
|
|
|
if err != nil {
|
|
|
|
ethutil.Config.Log.Infoln(err)
|
|
|
|
} else {
|
|
|
|
self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val})
|
|
|
|
ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", self.block.Hash())
|
2014-06-11 22:55:45 +03:00
|
|
|
ethutil.Config.Log.Infoln(self.block)
|
2014-05-21 15:00:13 +03:00
|
|
|
// Gather the new batch of transactions currently in the tx pool
|
|
|
|
self.txs = self.ethereum.TxPool().CurrentTransactions()
|
2014-03-20 12:20:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|