Removed some logging and refactored a bit

This commit is contained in:
obscuren 2014-05-21 14:00:13 +02:00
parent 86cf69648e
commit f5852b47d1
2 changed files with 46 additions and 48 deletions

View File

@ -29,7 +29,7 @@ func (pow *EasyPow) Search(block *Block, reactChan chan ethutil.React) []byte {
for { for {
select { select {
case <-reactChan: case <-reactChan:
ethutil.Config.Log.Infoln("[POW] Received reactor event; breaking out.") //ethutil.Config.Log.Infoln("[POW] Received reactor event; breaking out.")
return nil return nil
default: default:
i++ i++

View File

@ -60,10 +60,10 @@ func (miner *Miner) listener() {
select { select {
case chanMessage := <-miner.reactChan: case chanMessage := <-miner.reactChan:
if block, ok := chanMessage.Resource.(*ethchain.Block); ok { if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor") //ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor")
if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 {
// TODO: Perhaps continue mining to get some uncle rewards // TODO: Perhaps continue mining to get some uncle rewards
ethutil.Config.Log.Infoln("[MINER] New top block found resetting state") //ethutil.Config.Log.Infoln("[MINER] New top block found resetting state")
// Filter out which Transactions we have that were not in this block // Filter out which Transactions we have that were not in this block
var newtxs []*ethchain.Transaction var newtxs []*ethchain.Transaction
@ -87,13 +87,11 @@ func (miner *Miner) listener() {
if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
ethutil.Config.Log.Infoln("[MINER] Adding uncle block") ethutil.Config.Log.Infoln("[MINER] Adding uncle block")
miner.uncles = append(miner.uncles, block) miner.uncles = append(miner.uncles, block)
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
} }
} }
} }
if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok {
//log.Infoln("[MINER] Got new transaction from Reactor", tx)
found := false found := false
for _, ctx := range miner.txs { for _, ctx := range miner.txs {
if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found {
@ -102,54 +100,54 @@ func (miner *Miner) listener() {
} }
if found == false { if found == false {
// Undo all previous commits
miner.block.Undo() miner.block.Undo()
//log.Infoln("[MINER] We did not know about this transaction, adding") // Apply new transactions
miner.txs = append(miner.txs, tx) miner.txs = append(miner.txs, tx)
} else {
//log.Infoln("[MINER] We already had this transaction, ignoring")
} }
} }
default: default:
stateManager := miner.ethereum.StateManager() miner.mineNewBlock()
}
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) }
}
// Apply uncles
if len(miner.uncles) > 0 { func (self *Miner) mineNewBlock() {
miner.block.SetUncles(miner.uncles) stateManager := self.ethereum.StateManager()
}
self.block = self.ethereum.BlockChain().NewBlock(self.coinbase, self.txs)
// Apply all transactions to the block
txs := miner.txs // Apply uncles
miner.txs = nil if len(self.uncles) > 0 {
for _, tx := range txs { self.block.SetUncles(self.uncles)
if err := stateManager.ApplyTransaction(miner.block.State(), miner.block, tx); err == nil { }
miner.txs = append(miner.txs, tx)
} // Accumulate all valid transaction and apply them to the new state
} var txs []*ethchain.Transaction
miner.block.SetTransactions(miner.txs) for _, tx := range self.txs {
stateManager.AccumelateRewards(miner.block.State(), miner.block) if err := stateManager.ApplyTransaction(self.block.State(), self.block, tx); err == nil {
txs = append(txs, tx)
ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(miner.txs), "transactions") }
}
//miner.ethereum.StateManager().ApplyTransactions(miner.block.State(), miner.block, miner.block.Transactions()) self.txs = txs
// Set the transactions to the block so the new SHA3 can be calculated
// Search the nonce self.block.SetTransactions(self.txs)
miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) // Accumulate the rewards included for this block
if miner.block.Nonce != nil { stateManager.AccumelateRewards(self.block.State(), self.block)
err := miner.ethereum.StateManager().Process(miner.block, true)
if err != nil { ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions")
ethutil.Config.Log.Infoln(err)
miner.txs = []*ethchain.Transaction{} // Move this somewhere neat // Find a valid nonce
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) self.block.Nonce = self.pow.Search(self.block, self.quitChan)
} else { if self.block.Nonce != nil {
miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) err := self.ethereum.StateManager().Process(self.block, true)
ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) if err != nil {
ethutil.Config.Log.Infoln(err)
miner.txs = []*ethchain.Transaction{} // Move this somewhere neat } else {
miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val})
} ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", self.block.Hash())
} // Gather the new batch of transactions currently in the tx pool
self.txs = self.ethereum.TxPool().CurrentTransactions()
} }
} }
} }