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"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Miner struct {
|
|
|
|
pow ethchain.PoW
|
|
|
|
ethereum ethchain.EthManager
|
|
|
|
coinbase []byte
|
|
|
|
reactChan chan ethutil.React
|
|
|
|
txs []*ethchain.Transaction
|
|
|
|
uncles []*ethchain.Block
|
|
|
|
block *ethchain.Block
|
|
|
|
powChan chan []byte
|
|
|
|
quitChan chan ethutil.React
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
|
|
|
|
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
|
|
|
|
quitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread
|
|
|
|
|
|
|
|
ethereum.Reactor().Subscribe("newBlock", reactChan)
|
2014-05-15 15:05:15 +03:00
|
|
|
ethereum.Reactor().Subscribe("newTx:post", 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
|
|
|
|
ethereum.Reactor().Subscribe("newBlock", quitChan)
|
2014-05-15 15:05:15 +03:00
|
|
|
ethereum.Reactor().Subscribe("newTx:post", quitChan)
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
miner := Miner{
|
|
|
|
pow: ðchain.EasyPow{},
|
|
|
|
ethereum: ethereum,
|
|
|
|
coinbase: coinbase,
|
|
|
|
reactChan: reactChan,
|
|
|
|
powChan: powChan,
|
|
|
|
quitChan: quitChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert initial TXs in our little miner 'pool'
|
|
|
|
miner.txs = ethereum.TxPool().Flush()
|
|
|
|
miner.block = ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
|
|
|
|
|
|
|
return miner
|
|
|
|
}
|
|
|
|
func (miner *Miner) Start() {
|
|
|
|
// Prepare inital block
|
|
|
|
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
|
|
|
|
go func() { miner.listener() }()
|
|
|
|
}
|
|
|
|
func (miner *Miner) listener() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case chanMessage := <-miner.reactChan:
|
|
|
|
if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
|
2014-04-29 13:36:27 +03:00
|
|
|
log.Println("[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-04-29 13:36:27 +03:00
|
|
|
log.Println("[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
|
|
|
|
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
|
2014-04-29 13:36:27 +03:00
|
|
|
log.Println("[MINER] Adding uncle block")
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.uncles = append(miner.uncles, block)
|
|
|
|
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok {
|
2014-04-23 13:14:28 +03:00
|
|
|
//log.Println("[MINER] Got new transaction from Reactor", tx)
|
2014-03-20 12:20:29 +02:00
|
|
|
found := false
|
|
|
|
for _, ctx := range miner.txs {
|
|
|
|
if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if found == false {
|
2014-04-23 13:14:28 +03:00
|
|
|
//log.Println("[MINER] We did not know about this transaction, adding")
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.txs = append(miner.txs, tx)
|
2014-04-09 16:48:17 +03:00
|
|
|
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.block.SetTransactions(miner.txs)
|
|
|
|
} else {
|
2014-04-23 13:14:28 +03:00
|
|
|
//log.Println("[MINER] We already had this transaction, ignoring")
|
2014-03-20 12:20:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2014-03-21 16:06:23 +02:00
|
|
|
log.Println("[MINER] Mining on block. Includes", len(miner.txs), "transactions")
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
// Apply uncles
|
|
|
|
if len(miner.uncles) > 0 {
|
|
|
|
miner.block.SetUncles(miner.uncles)
|
|
|
|
}
|
|
|
|
|
2014-04-16 05:07:21 +03:00
|
|
|
// FIXME @ maranh, first block doesn't need this. Everything after the first block does.
|
|
|
|
// Please check and fix
|
|
|
|
miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
|
2014-03-20 12:20:29 +02:00
|
|
|
// Apply all transactions to the block
|
|
|
|
miner.ethereum.StateManager().ApplyTransactions(miner.block, miner.block.Transactions())
|
|
|
|
miner.ethereum.StateManager().AccumelateRewards(miner.block)
|
|
|
|
|
|
|
|
// Search the nonce
|
2014-04-23 13:14:28 +03:00
|
|
|
//log.Println("[MINER] Initialision complete, starting mining")
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan)
|
|
|
|
if miner.block.Nonce != nil {
|
|
|
|
miner.ethereum.StateManager().PrepareDefault(miner.block)
|
|
|
|
err := miner.ethereum.StateManager().ProcessBlock(miner.block, true)
|
|
|
|
if err != nil {
|
2014-04-29 13:36:27 +03:00
|
|
|
log.Println(err)
|
|
|
|
miner.txs = []*ethchain.Transaction{} // Move this somewhere neat
|
|
|
|
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
2014-03-20 12:20:29 +02:00
|
|
|
} else {
|
|
|
|
|
2014-04-23 16:54:15 +03:00
|
|
|
/*
|
|
|
|
// XXX @maranh This is already done in the state manager, why a 2nd time?
|
|
|
|
if !miner.ethereum.StateManager().Pow.Verify(miner.block.HashNoNonce(), miner.block.Difficulty, miner.block.Nonce) {
|
|
|
|
log.Printf("Second stage verification error: Block's nonce is invalid (= %v)\n", ethutil.Hex(miner.block.Nonce))
|
|
|
|
}
|
|
|
|
*/
|
2014-03-20 12:20:29 +02:00
|
|
|
miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val})
|
2014-03-21 16:06:23 +02:00
|
|
|
log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash())
|
2014-03-20 12:20:29 +02:00
|
|
|
|
|
|
|
miner.txs = []*ethchain.Transaction{} // Move this somewhere neat
|
|
|
|
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|