Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/miner/miner.go

266 lines
6.4 KiB

/*
This file is part of go-ethereum
go-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @authors
* Jeffrey Wilcke <i@jev.io>
* @date 2014
*
*/
10 years ago
package miner
import (
"math/big"
10 years ago
"sort"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethutil"
10 years ago
"github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/pow/ezp"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
10 years ago
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
)
type LocalTx struct {
To []byte `json:"to"`
Data []byte `json:"data"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Value string `json:"value"`
}
func (self *LocalTx) Sign(key []byte) *types.Transaction {
return nil
}
10 years ago
var minerlogger = logger.NewLogger("MINER")
type Miner struct {
eth *eth.Ethereum
events event.Subscription
10 years ago
uncles []*types.Header
localTxs map[int]*LocalTx
localTxId int
10 years ago
pow pow.PoW
quitCh chan struct{}
powQuitCh chan struct{}
Coinbase []byte
10 years ago
mining bool
MinAcceptedGasPrice *big.Int
Extra string
}
func New(coinbase []byte, eth *eth.Ethereum) *Miner {
return &Miner{
eth: eth,
powQuitCh: make(chan struct{}),
10 years ago
pow: ezp.New(),
mining: false,
localTxs: make(map[int]*LocalTx),
MinAcceptedGasPrice: big.NewInt(10000000000000),
Coinbase: coinbase,
}
10 years ago
}
10 years ago
func (self *Miner) GetPow() pow.PoW {
10 years ago
return self.pow
}
func (self *Miner) AddLocalTx(tx *LocalTx) int {
minerlogger.Infof("Added local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value)
self.localTxId++
self.localTxs[self.localTxId] = tx
self.eth.EventMux().Post(tx)
return self.localTxId
}
func (self *Miner) RemoveLocalTx(id int) {
if tx := self.localTxs[id]; tx != nil {
minerlogger.Infof("Removed local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value)
10 years ago
}
self.eth.EventMux().Post(&LocalTx{})
10 years ago
delete(self.localTxs, id)
10 years ago
}
func (self *Miner) Start() {
if self.mining {
return
}
10 years ago
minerlogger.Infoln("Starting mining operations")
self.mining = true
self.quitCh = make(chan struct{})
self.powQuitCh = make(chan struct{})
mux := self.eth.EventMux()
self.events = mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{})
go self.update()
go self.mine()
10 years ago
}
func (self *Miner) Stop() {
if !self.mining {
return
}
10 years ago
self.mining = false
10 years ago
minerlogger.Infoln("Stopping mining operations")
10 years ago
self.events.Unsubscribe()
10 years ago
close(self.quitCh)
close(self.powQuitCh)
10 years ago
}
func (self *Miner) Mining() bool {
return self.mining
10 years ago
}
func (self *Miner) update() {
out:
10 years ago
for {
select {
case event := <-self.events.Chan():
10 years ago
switch event := event.(type) {
case core.NewBlockEvent:
10 years ago
block := event.Block
if self.eth.ChainManager().HasBlock(block.Hash()) {
self.reset()
self.eth.TxPool().RemoveSet(block.Transactions())
go self.mine()
} else if true {
// do uncle stuff
10 years ago
}
case core.TxPreEvent, *LocalTx:
self.reset()
go self.mine()
10 years ago
}
case <-self.quitCh:
break out
10 years ago
}
}
}
func (self *Miner) reset() {
close(self.powQuitCh)
self.powQuitCh = make(chan struct{})
10 years ago
}
func (self *Miner) mine() {
var (
blockProcessor = self.eth.BlockProcessor()
chainMan = self.eth.ChainManager()
block = chainMan.NewBlock(self.Coinbase)
state = state.New(block.Root(), self.eth.Db())
)
block.Header().Extra = self.Extra
10 years ago
// Apply uncles
if len(self.uncles) > 0 {
block.SetUncles(self.uncles)
10 years ago
}
parent := chainMan.GetBlock(block.ParentHash())
coinbase := state.GetOrNewStateObject(block.Coinbase())
coinbase.SetGasPool(core.CalcGasLimit(parent, block))
transactions := self.finiliseTxs()
10 years ago
// Accumulate all valid transactions and apply them to the new state
// Error may be ignored. It's not important during mining
receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true)
10 years ago
if err != nil {
minerlogger.Debugln(err)
}
self.eth.TxPool().RemoveSet(erroneous)
10 years ago
block.SetTransactions(txs)
block.SetReceipts(receipts)
10 years ago
// Accumulate the rewards included for this block
blockProcessor.AccumelateRewards(state, block, parent)
10 years ago
state.Update(ethutil.Big0)
block.SetRoot(state.Root())
10 years ago
minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions))
10 years ago
// Find a valid nonce
nonce := self.pow.Search(block, self.powQuitCh)
10 years ago
if nonce != nil {
block.Header().Nonce = nonce
err := chainMan.InsertChain(types.Blocks{block})
10 years ago
if err != nil {
minerlogger.Infoln(err)
} else {
self.eth.EventMux().Post(core.NewMinedBlockEvent{block})
minerlogger.Infof("🔨 Mined block %x\n", block.Hash())
minerlogger.Infoln(block)
10 years ago
}
go self.mine()
10 years ago
}
}
func (self *Miner) finiliseTxs() types.Transactions {
// Sort the transactions by nonce in case of odd network propagation
actualSize := len(self.localTxs) // See copy below
txs := make(types.Transactions, actualSize+self.eth.TxPool().Size())
state := self.eth.ChainManager().TransState()
// XXX This has to change. Coinbase is, for new, same as key.
key := self.eth.KeyManager()
for i, ltx := range self.localTxs {
tx := types.NewTransactionMessage(ltx.To, ethutil.Big(ltx.Value), ethutil.Big(ltx.Gas), ethutil.Big(ltx.GasPrice), ltx.Data)
tx.SetNonce(state.GetNonce(self.Coinbase))
state.SetNonce(self.Coinbase, tx.Nonce()+1)
tx.Sign(key.PrivateKey())
txs[i] = tx
}
// Faster than append
for _, tx := range self.eth.TxPool().GetTransactions() {
if tx.GasPrice().Cmp(self.MinAcceptedGasPrice) >= 0 {
txs[actualSize] = tx
actualSize++
}
}
newTransactions := make(types.Transactions, actualSize)
copy(newTransactions, txs[:actualSize])
sort.Sort(types.TxByNonce{newTransactions})
return newTransactions
}