diff options
Diffstat (limited to 'miner/miner.go')
-rw-r--r-- | miner/miner.go | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/miner/miner.go b/miner/miner.go index 795385424..d909c228b 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -27,15 +27,15 @@ import ( "math/big" "sort" - "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/pow/ezp" - "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/chain/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/wire" ) type LocalTx struct { @@ -60,7 +60,7 @@ type Miner struct { localTxs map[int]*LocalTx localTxId int - pow chain.PoW + pow pow.PoW quitCh chan struct{} powQuitCh chan struct{} @@ -75,7 +75,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { return &Miner{ eth: eth, powQuitCh: make(chan struct{}), - pow: &chain.EasyPow{}, + pow: ezp.New(), mining: false, localTxs: make(map[int]*LocalTx), MinAcceptedGasPrice: big.NewInt(10000000000000), @@ -83,7 +83,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { } } -func (self *Miner) GetPow() chain.PoW { +func (self *Miner) GetPow() pow.PoW { return self.pow } @@ -117,7 +117,7 @@ func (self *Miner) Start() { self.powQuitCh = make(chan struct{}) mux := self.eth.EventMux() - self.events = mux.Subscribe(chain.NewBlockEvent{}, chain.TxPreEvent{}, &LocalTx{}) + self.events = mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{}) go self.update() go self.mine() @@ -148,7 +148,7 @@ out: select { case event := <-self.events.Chan(): switch event := event.(type) { - case chain.NewBlockEvent: + case core.NewBlockEvent: block := event.Block if self.eth.ChainManager().HasBlock(block.Hash()) { self.reset() @@ -157,7 +157,7 @@ out: } else if true { // do uncle stuff } - case chain.TxPreEvent, *LocalTx: + case core.TxPreEvent, *LocalTx: self.reset() go self.mine() } @@ -168,7 +168,6 @@ out: } func (self *Miner) reset() { - println("reset") close(self.powQuitCh) self.powQuitCh = make(chan struct{}) } @@ -179,7 +178,6 @@ func (self *Miner) mine() { chainMan = self.eth.ChainManager() block = chainMan.NewBlock(self.Coinbase) ) - block.MinGasPrice = self.MinAcceptedGasPrice // Apply uncles if len(self.uncles) > 0 { @@ -194,7 +192,7 @@ func (self *Miner) mine() { // 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 := blockManager.ProcessTransactions(coinbase, block.State(), block, block, transactions) + receipts, txs, _, erroneous, err := blockManager.ApplyTransactions(coinbase, block.State(), block, transactions, true) if err != nil { minerlogger.Debugln(err) } @@ -206,7 +204,7 @@ func (self *Miner) mine() { // Accumulate the rewards included for this block blockManager.AccumelateRewards(block.State(), block, parent) - block.State().Update() + block.State().Update(ethutil.Big0) minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) @@ -214,16 +212,11 @@ func (self *Miner) mine() { nonce := self.pow.Search(block, self.powQuitCh) if nonce != nil { block.Nonce = nonce - lchain := chain.NewChain(types.Blocks{block}) - _, err := chainMan.TestChain(lchain) + err := chainMan.InsertChain(types.Blocks{block}) if err != nil { minerlogger.Infoln(err) } else { - chainMan.InsertChain(lchain, func(block *types.Block, _ state.Messages) { - self.eth.EventMux().Post(chain.NewBlockEvent{block}) - }) - - self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val}) + self.eth.EventMux().Post(core.NewMinedBlockEvent{block}) minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) minerlogger.Infoln(block) @@ -235,23 +228,33 @@ func (self *Miner) mine() { func (self *Miner) finiliseTxs() types.Transactions { // Sort the transactions by nonce in case of odd network propagation - var txs types.Transactions + actualSize := len(self.localTxs) // See copy below + txs := make(types.Transactions, actualSize+self.eth.TxPool().Size()) - state := self.eth.BlockManager().TransState() + state := self.eth.ChainManager().TransState() // XXX This has to change. Coinbase is, for new, same as key. key := self.eth.KeyManager() - for _, ltx := range self.localTxs { + 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.Nonce = state.GetNonce(self.Coinbase) - state.SetNonce(self.Coinbase, tx.Nonce+1) + tx.SetNonce(state.GetNonce(self.Coinbase)) + state.SetNonce(self.Coinbase, tx.Nonce()+1) tx.Sign(key.PrivateKey()) - txs = append(txs, tx) + txs[i] = tx + } + + // Faster than append + for _, tx := range self.eth.TxPool().GetTransactions() { + if tx.GasPrice().Cmp(self.MinAcceptedGasPrice) >= 0 { + txs[actualSize] = tx + actualSize++ + } } - txs = append(txs, self.eth.TxPool().CurrentTransactions()...) - sort.Sort(types.TxByNonce{txs}) + newTransactions := make(types.Transactions, actualSize) + copy(newTransactions, txs[:actualSize]) + sort.Sort(types.TxByNonce{newTransactions}) - return txs + return newTransactions } |