plugeth/ethminer/miner.go

215 lines
5.4 KiB
Go
Raw Normal View History

2014-03-20 10:20:29 +00:00
package ethminer
import (
"bytes"
2014-08-21 16:14:49 +00:00
"sort"
2014-03-20 10:20:29 +00:00
"github.com/ethereum/eth-go/ethchain"
2014-06-26 17:45:57 +00:00
"github.com/ethereum/eth-go/ethlog"
2014-03-20 10:20:29 +00:00
"github.com/ethereum/eth-go/ethwire"
"github.com/ethereum/eth-go/event"
2014-03-20 10:20:29 +00:00
)
var logger = ethlog.NewLogger("MINER")
2014-03-20 10:20:29 +00:00
type Miner struct {
pow ethchain.PoW
ethereum ethchain.EthManager
coinbase []byte
txs ethchain.Transactions
uncles []*ethchain.Block
block *ethchain.Block
events event.Subscription
powQuitChan chan struct{}
powDone chan struct{}
2014-08-21 18:13:26 +00:00
turbo bool
2014-03-20 10:20:29 +00:00
}
const (
Started = iota
Stopped
)
type Event struct {
Type int // Started || Stopped
Miner *Miner
}
func (self *Miner) GetPow() ethchain.PoW {
return self.pow
2014-07-18 10:01:08 +00:00
}
func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) *Miner {
2014-03-20 10:20:29 +00:00
miner := Miner{
pow: &ethchain.EasyPow{},
ethereum: ethereum,
coinbase: coinbase,
2014-03-20 10:20:29 +00:00
}
return &miner
2014-03-20 10:20:29 +00:00
}
2014-06-26 17:45:57 +00:00
2014-08-21 18:13:26 +00:00
func (self *Miner) ToggleTurbo() {
self.turbo = !self.turbo
self.pow.Turbo(self.turbo)
}
2014-03-20 10:20:29 +00:00
func (miner *Miner) Start() {
2014-07-21 12:26:29 +00:00
// Insert initial TXs in our little miner 'pool'
miner.txs = miner.ethereum.TxPool().Flush()
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase)
mux := miner.ethereum.EventMux()
miner.events = mux.Subscribe(ethchain.NewBlockEvent{}, ethchain.TxEvent{})
2014-03-20 10:20:29 +00:00
// Prepare inital block
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
go miner.listener()
2014-06-26 17:45:57 +00:00
logger.Infoln("Started")
mux.Post(Event{Started, miner})
}
func (miner *Miner) Stop() {
logger.Infoln("Stopping...")
miner.events.Unsubscribe()
miner.ethereum.EventMux().Post(Event{Stopped, miner})
2014-03-20 10:20:29 +00:00
}
2014-06-26 17:45:57 +00:00
2014-03-20 10:20:29 +00:00
func (miner *Miner) listener() {
for {
miner.startMining()
2014-03-20 10:20:29 +00:00
select {
case event, isopen := <-miner.events.Chan():
miner.stopMining()
if !isopen {
return
}
2014-06-26 17:45:57 +00:00
switch event := event.(type) {
case ethchain.NewBlockEvent:
block := event.Block
//logger.Infoln("Got new block via Reactor")
2014-03-20 10:20:29 +00: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 10:20:29 +00: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 10:20:29 +00:00
} else {
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
logger.Infoln("Adding uncle block")
2014-03-20 10:20:29 +00:00
miner.uncles = append(miner.uncles, block)
}
}
case ethchain.TxEvent:
if event.Type == ethchain.TxPre {
found := false
for _, ctx := range miner.txs {
if found = bytes.Compare(ctx.Hash(), event.Tx.Hash()) == 0; found {
break
}
}
if found == false {
// Undo all previous commits
miner.block.Undo()
// Apply new transactions
miner.txs = append(miner.txs, event.Tx)
2014-03-20 10:20:29 +00:00
}
}
}
case <-miner.powDone:
// next iteration will start mining again
}
}
}
func (miner *Miner) startMining() {
if miner.powDone == nil {
miner.powDone = make(chan struct{})
}
miner.powQuitChan = make(chan struct{})
go miner.mineNewBlock()
}
2014-07-01 09:55:50 +00:00
func (miner *Miner) stopMining() {
close(miner.powQuitChan)
<-miner.powDone
2014-05-21 22:25:34 +00:00
}
func (self *Miner) mineNewBlock() {
stateManager := self.ethereum.StateManager()
self.block = self.ethereum.BlockChain().NewBlock(self.coinbase)
2014-03-20 10:20:29 +00:00
// Apply uncles
if len(self.uncles) > 0 {
self.block.SetUncles(self.uncles)
}
2014-03-20 10:20:29 +00:00
// Sort the transactions by nonce in case of odd network propagation
sort.Sort(ethchain.TxByNonce{self.txs})
2014-06-14 09:46:09 +00:00
// Accumulate all valid transactions and apply them to the new state
2014-06-14 09:46:09 +00:00
// Error may be ignored. It's not important during mining
2014-06-16 09:51:16 +00:00
parent := self.ethereum.BlockChain().GetBlock(self.block.PrevHash)
2014-06-16 09:14:01 +00:00
coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase)
2014-06-16 09:51:16 +00:00
coinbase.SetGasPool(self.block.CalcGasLimit(parent))
2014-06-16 09:14:01 +00:00
receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs)
2014-06-14 09:46:09 +00:00
if err != nil {
logger.Debugln(err)
2014-06-14 09:46:09 +00:00
}
self.txs = append(txs, unhandledTxs...)
2014-07-21 10:21:34 +00:00
self.block.SetTxHash(receipts)
2014-06-14 09:46:09 +00:00
// Set the transactions to the block so the new SHA3 can be calculated
self.block.SetReceipts(receipts, txs)
2014-06-14 09:46:09 +00:00
// Accumulate the rewards included for this block
2014-09-14 23:11:01 +00:00
stateManager.AccumelateRewards(self.block.State(), self.block, parent)
self.block.State().Update()
2014-06-25 17:43:35 +00:00
logger.Infof("Mining on block. Includes %v transactions", len(self.txs))
// Find a valid nonce
nonce := self.pow.Search(self.block, self.powQuitChan)
if nonce != nil {
self.block.Nonce = nonce
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 10:20:29 +00:00
}
}
self.powDone <- struct{}{}
2014-03-20 10:20:29 +00:00
}