From ae837c4719855384921fcaadb1a575942dc9833d Mon Sep 17 00:00:00 2001 From: Maran Date: Thu, 20 Mar 2014 11:20:29 +0100 Subject: More mining rework --- ethminer/miner.go | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 ethminer/miner.go (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go new file mode 100644 index 000000000..f4f697aba --- /dev/null +++ b/ethminer/miner.go @@ -0,0 +1,149 @@ +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) + ethereum.Reactor().Subscribe("newTx", reactChan) + + // 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) + ethereum.Reactor().Subscribe("newTx", quitChan) + + 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 { + log.Println("[miner] Got new block via Reactor") + if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { + // TODO: Perhaps continue mining to get some uncle rewards + log.Println("[miner] New top block found resetting state") + + // 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 { + log.Println("[miner] Adding uncle 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 { + log.Println("[miner] Got new transaction from Reactor", tx) + found := false + for _, ctx := range miner.txs { + if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { + break + } + + } + if found == false { + log.Println("[miner] We did not know about this transaction, adding") + miner.txs = append(miner.txs, tx) + miner.block.SetTransactions(miner.txs) + } else { + log.Println("[miner] We already had this transaction, ignoring") + } + } + default: + log.Println("[miner] Mining on block. Includes", len(miner.txs), "transactions") + + // Apply uncles + if len(miner.uncles) > 0 { + miner.block.SetUncles(miner.uncles) + } + + // Apply all transactions to the block + miner.ethereum.StateManager().ApplyTransactions(miner.block, miner.block.Transactions()) + miner.ethereum.StateManager().AccumelateRewards(miner.block) + + // Search the nonce + log.Println("[miner] Initialision complete, starting mining") + 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 { + log.Println("Error result from process block:", err) + log.Println(miner.block) + } else { + + 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)) + } + miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) + log.Printf("[miner] 🔨 Mined block %x\n", miner.block.Hash()) + log.Println(miner.block) + + miner.txs = []*ethchain.Transaction{} // Move this somewhere neat + miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) + } + } + } + } +} -- cgit v1.2.3 From b52b1fca89fd56549ecc0f086d96a39d6009e568 Mon Sep 17 00:00:00 2001 From: Maran Date: Fri, 21 Mar 2014 15:06:23 +0100 Subject: Initial block reorganisation code --- ethminer/miner.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index f4f697aba..cb752e3de 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -61,10 +61,10 @@ func (miner *Miner) listener() { select { case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - log.Println("[miner] Got new block via Reactor") + log.Println("[MINER] Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards - log.Println("[miner] New top block found resetting state") + log.Println("[MINER] New top block found resetting state") // Filter out which Transactions we have that were not in this block var newtxs []*ethchain.Transaction @@ -86,7 +86,7 @@ func (miner *Miner) listener() { } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - log.Println("[miner] Adding uncle block") + log.Println("[MINER] Adding uncle block") miner.uncles = append(miner.uncles, block) miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) } @@ -94,7 +94,7 @@ func (miner *Miner) listener() { } if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { - log.Println("[miner] Got new transaction from Reactor", tx) + log.Println("[MINER] Got new transaction from Reactor", tx) found := false for _, ctx := range miner.txs { if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { @@ -103,15 +103,15 @@ func (miner *Miner) listener() { } if found == false { - log.Println("[miner] We did not know about this transaction, adding") + log.Println("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) miner.block.SetTransactions(miner.txs) } else { - log.Println("[miner] We already had this transaction, ignoring") + log.Println("[MINER] We already had this transaction, ignoring") } } default: - log.Println("[miner] Mining on block. Includes", len(miner.txs), "transactions") + log.Println("[MINER] Mining on block. Includes", len(miner.txs), "transactions") // Apply uncles if len(miner.uncles) > 0 { @@ -123,7 +123,7 @@ func (miner *Miner) listener() { miner.ethereum.StateManager().AccumelateRewards(miner.block) // Search the nonce - log.Println("[miner] Initialision complete, starting mining") + log.Println("[MINER] Initialision complete, starting mining") miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) if miner.block.Nonce != nil { miner.ethereum.StateManager().PrepareDefault(miner.block) @@ -137,8 +137,7 @@ func (miner *Miner) listener() { log.Printf("Second stage verification error: Block's nonce is invalid (= %v)\n", ethutil.Hex(miner.block.Nonce)) } miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) - log.Printf("[miner] 🔨 Mined block %x\n", miner.block.Hash()) - log.Println(miner.block) + log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From 6253d109389d49e47772597de24cd11874b91338 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 24 Mar 2014 15:04:29 +0100 Subject: initial testcode for canonical chain --- ethminer/miner.go | 1 + 1 file changed, 1 insertion(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index cb752e3de..125eb6fb1 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -138,6 +138,7 @@ func (miner *Miner) listener() { } miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) + log.Println(miner.block) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From 782910eaa76bb31be4c2bcd0f4505b8085acb57c Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 1 Apr 2014 15:54:29 +0200 Subject: Small tweaks --- ethminer/miner.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 125eb6fb1..60af3ab31 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -130,7 +130,6 @@ func (miner *Miner) listener() { err := miner.ethereum.StateManager().ProcessBlock(miner.block, true) if err != nil { log.Println("Error result from process block:", err) - log.Println(miner.block) } else { if !miner.ethereum.StateManager().Pow.Verify(miner.block.HashNoNonce(), miner.block.Difficulty, miner.block.Nonce) { @@ -138,7 +137,6 @@ func (miner *Miner) listener() { } miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) - log.Println(miner.block) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From a83db489dfddb16786b1de3d28aee4a4af9e12d5 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 9 Apr 2014 09:48:17 -0400 Subject: Fix transaction on new blocks --- ethminer/miner.go | 1 + 1 file changed, 1 insertion(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 60af3ab31..d84977342 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -105,6 +105,7 @@ func (miner *Miner) listener() { if found == false { log.Println("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) + miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) miner.block.SetTransactions(miner.txs) } else { log.Println("[MINER] We already had this transaction, ignoring") -- cgit v1.2.3 From d811920d8b5643b944a7df58d5d75095539242c4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 16 Apr 2014 04:07:21 +0200 Subject: Hack for miner problem added w/ note @maranh please check --- ethminer/miner.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index d84977342..5bbf6b977 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -119,6 +119,9 @@ func (miner *Miner) listener() { miner.block.SetUncles(miner.uncles) } + // 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()) // Apply all transactions to the block miner.ethereum.StateManager().ApplyTransactions(miner.block, miner.block.Transactions()) miner.ethereum.StateManager().AccumelateRewards(miner.block) -- cgit v1.2.3 From 6b08efabf837c9c763e116b91dc9b566a2c76d80 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 23 Apr 2014 12:14:28 +0200 Subject: @maranh see comment --- ethminer/miner.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 5bbf6b977..d1636ccee 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -61,10 +61,10 @@ func (miner *Miner) listener() { select { case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - log.Println("[MINER] Got new block via Reactor") + //log.Println("[MINER] Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards - log.Println("[MINER] New top block found resetting state") + //log.Println("[MINER] New top block found resetting state") // Filter out which Transactions we have that were not in this block var newtxs []*ethchain.Transaction @@ -86,7 +86,7 @@ func (miner *Miner) listener() { } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - log.Println("[MINER] Adding uncle block") + //log.Println("[MINER] Adding uncle block") miner.uncles = append(miner.uncles, block) miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) } @@ -94,7 +94,7 @@ func (miner *Miner) listener() { } if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { - log.Println("[MINER] Got new transaction from Reactor", tx) + //log.Println("[MINER] Got new transaction from Reactor", tx) found := false for _, ctx := range miner.txs { if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { @@ -103,12 +103,12 @@ func (miner *Miner) listener() { } if found == false { - log.Println("[MINER] We did not know about this transaction, adding") + //log.Println("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) miner.block.SetTransactions(miner.txs) } else { - log.Println("[MINER] We already had this transaction, ignoring") + //log.Println("[MINER] We already had this transaction, ignoring") } } default: @@ -127,7 +127,7 @@ func (miner *Miner) listener() { miner.ethereum.StateManager().AccumelateRewards(miner.block) // Search the nonce - log.Println("[MINER] Initialision complete, starting mining") + //log.Println("[MINER] Initialision complete, starting mining") miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) if miner.block.Nonce != nil { miner.ethereum.StateManager().PrepareDefault(miner.block) @@ -136,6 +136,7 @@ func (miner *Miner) listener() { log.Println("Error result from process block:", err) } else { + // 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)) } -- cgit v1.2.3 From 0651af9dfd701ba09e6c734f21eff85f61454476 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 23 Apr 2014 15:54:15 +0200 Subject: Removed some log statements and disabled additional validation checks --- ethminer/miner.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index d1636ccee..08a4626e4 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -134,14 +134,18 @@ func (miner *Miner) listener() { err := miner.ethereum.StateManager().ProcessBlock(miner.block, true) if err != nil { log.Println("Error result from process block:", err) + miner.block.State().Reset() } else { - // 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)) - } + /* + // 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)) + } + */ miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) + log.Println(miner.block) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From f3818478e2601df1d9cfc9cc36b021366f870856 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 24 Apr 2014 13:48:33 +0200 Subject: Removed debug & unused functions --- ethminer/miner.go | 1 - 1 file changed, 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 08a4626e4..791e8e402 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -145,7 +145,6 @@ func (miner *Miner) listener() { */ miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) - log.Println(miner.block) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From 38d6b67b5cfbfb63620a244ea01b5b534917128f Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 29 Apr 2014 12:36:27 +0200 Subject: Fixed state problem --- ethminer/miner.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 791e8e402..c93267161 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -2,6 +2,7 @@ package ethminer import ( "bytes" + "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" @@ -61,10 +62,10 @@ func (miner *Miner) listener() { select { case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - //log.Println("[MINER] Got new block via Reactor") + log.Println("[MINER] Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards - //log.Println("[MINER] New top block found resetting state") + log.Println("[MINER] New top block found resetting state") // Filter out which Transactions we have that were not in this block var newtxs []*ethchain.Transaction @@ -86,7 +87,7 @@ func (miner *Miner) listener() { } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - //log.Println("[MINER] Adding uncle block") + log.Println("[MINER] Adding uncle block") miner.uncles = append(miner.uncles, block) miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) } @@ -133,8 +134,9 @@ func (miner *Miner) listener() { miner.ethereum.StateManager().PrepareDefault(miner.block) err := miner.ethereum.StateManager().ProcessBlock(miner.block, true) if err != nil { - log.Println("Error result from process block:", err) - miner.block.State().Reset() + log.Println(err) + miner.txs = []*ethchain.Transaction{} // Move this somewhere neat + miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) } else { /* -- cgit v1.2.3 From 21724f7ef960f0f2df0d2b0f3cccfd030a4aaee8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 30 Apr 2014 14:43:32 +0200 Subject: Added manifest changes and changed closures --- ethminer/miner.go | 1 - 1 file changed, 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index c93267161..3796c873e 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -2,7 +2,6 @@ package ethminer import ( "bytes" - "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" -- cgit v1.2.3 From 7bf2ae0b116fff0fede5b1455c5fda20caf98252 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 15 May 2014 14:05:15 +0200 Subject: Removed old tx pool notification system. Fixes #19 --- ethminer/miner.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 3796c873e..f1d0caae9 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -26,7 +26,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { quitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread ethereum.Reactor().Subscribe("newBlock", reactChan) - ethereum.Reactor().Subscribe("newTx", reactChan) + ethereum.Reactor().Subscribe("newTx:post", reactChan) // We need the quit chan to be a Reactor event. // The POW search method is actually blocking and if we don't @@ -34,7 +34,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { // The miner overseer will never get the reactor events themselves // Only after the miner will find the sha ethereum.Reactor().Subscribe("newBlock", quitChan) - ethereum.Reactor().Subscribe("newTx", quitChan) + ethereum.Reactor().Subscribe("newTx:post", quitChan) miner := Miner{ pow: ðchain.EasyPow{}, -- cgit v1.2.3 From 88686cbed27ad2e7d5c111051e06c270b1b352a3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 15 May 2014 15:00:25 +0200 Subject: listen to pre instead of post --- ethminer/miner.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index f1d0caae9..bc29b2588 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -26,7 +26,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { quitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread ethereum.Reactor().Subscribe("newBlock", reactChan) - ethereum.Reactor().Subscribe("newTx:post", reactChan) + ethereum.Reactor().Subscribe("newTx:pre", reactChan) // We need the quit chan to be a Reactor event. // The POW search method is actually blocking and if we don't @@ -34,7 +34,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { // The miner overseer will never get the reactor events themselves // Only after the miner will find the sha ethereum.Reactor().Subscribe("newBlock", quitChan) - ethereum.Reactor().Subscribe("newTx:post", quitChan) + ethereum.Reactor().Subscribe("newTx:pre", quitChan) miner := Miner{ pow: ðchain.EasyPow{}, -- cgit v1.2.3 From 8730dfdcc2e2b40410a57385e4864d15f2f0336b Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 17 May 2014 14:07:52 +0200 Subject: Changed how changes are being applied to states --- ethminer/miner.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index bc29b2588..233a8bc32 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -53,8 +53,8 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { } func (miner *Miner) Start() { // Prepare inital block - miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) - go func() { miner.listener() }() + //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) + go miner.listener() } func (miner *Miner) listener() { for { @@ -88,7 +88,7 @@ func (miner *Miner) listener() { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { log.Println("[MINER] Adding uncle block") miner.uncles = append(miner.uncles, block) - miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) + //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) } } } @@ -119,31 +119,19 @@ func (miner *Miner) listener() { miner.block.SetUncles(miner.uncles) } - // 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()) // Apply all transactions to the block - miner.ethereum.StateManager().ApplyTransactions(miner.block, miner.block.Transactions()) - miner.ethereum.StateManager().AccumelateRewards(miner.block) + miner.ethereum.StateManager().ApplyTransactions(miner.block.State(), miner.block, miner.block.Transactions()) + miner.ethereum.StateManager().AccumelateRewards(miner.block.State(), miner.block) // Search the nonce - //log.Println("[MINER] Initialision complete, starting mining") 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) + err := miner.ethereum.StateManager().ProcessBlock(miner.ethereum.StateManager().CurrentState(), miner.block, true) if err != nil { log.Println(err) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) } else { - - /* - // 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)) - } - */ miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) -- cgit v1.2.3 From bd48690f63d07d9a0568f0d8092006ebaa12af5f Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 19 May 2014 11:25:27 +0200 Subject: Testing different mining state --- ethminer/miner.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 233a8bc32..294bc7b3d 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -5,7 +5,6 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" - "log" ) type Miner struct { @@ -61,10 +60,10 @@ func (miner *Miner) listener() { select { case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - log.Println("[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 { // TODO: Perhaps continue mining to get some uncle rewards - log.Println("[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 var newtxs []*ethchain.Transaction @@ -86,7 +85,7 @@ func (miner *Miner) listener() { } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - log.Println("[MINER] Adding uncle block") + ethutil.Config.Log.Infoln("[MINER] Adding uncle block") miner.uncles = append(miner.uncles, block) //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) } @@ -94,7 +93,7 @@ func (miner *Miner) listener() { } if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { - //log.Println("[MINER] Got new transaction from Reactor", tx) + //log.Infoln("[MINER] Got new transaction from Reactor", tx) found := false for _, ctx := range miner.txs { if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { @@ -103,16 +102,16 @@ func (miner *Miner) listener() { } if found == false { - //log.Println("[MINER] We did not know about this transaction, adding") + //log.Infoln("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) miner.block.SetTransactions(miner.txs) } else { - //log.Println("[MINER] We already had this transaction, ignoring") + //log.Infoln("[MINER] We already had this transaction, ignoring") } } default: - log.Println("[MINER] Mining on block. Includes", len(miner.txs), "transactions") + ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(miner.txs), "transactions") // Apply uncles if len(miner.uncles) > 0 { @@ -128,12 +127,12 @@ func (miner *Miner) listener() { if miner.block.Nonce != nil { err := miner.ethereum.StateManager().ProcessBlock(miner.ethereum.StateManager().CurrentState(), miner.block, true) if err != nil { - log.Println(err) + ethutil.Config.Log.Infoln(err) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) } else { miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) - log.Printf("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) + ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From 5ceb1620e93e1999c6f72e6164c7c65af63244ec Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 00:17:50 +0200 Subject: Fixed couple issues * (imp) Lock / RLock tries * (fix) stack --- ethminer/miner.go | 1 + 1 file changed, 1 insertion(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 294bc7b3d..26b28d82f 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -102,6 +102,7 @@ func (miner *Miner) listener() { } if found == false { + miner.block.Undo() //log.Infoln("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) -- cgit v1.2.3 From cbf221f6b7a48ece543d6141d8a7e9dbf9b8d86d Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 11:42:20 +0200 Subject: Fixed competing block method --- ethminer/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 26b28d82f..e052d0207 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -126,7 +126,7 @@ func (miner *Miner) listener() { // Search the nonce miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) if miner.block.Nonce != nil { - err := miner.ethereum.StateManager().ProcessBlock(miner.ethereum.StateManager().CurrentState(), miner.block, true) + err := miner.ethereum.StateManager().Process(miner.block, true) if err != nil { ethutil.Config.Log.Infoln(err) miner.txs = []*ethchain.Transaction{} // Move this somewhere neat -- cgit v1.2.3 From 86cf69648efc5029abffbf39f1be7308acb1531e Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 13:04:40 +0200 Subject: Improved miner so it won't include invalid transactions --- ethminer/miner.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index e052d0207..8d6486e90 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -105,14 +105,14 @@ func (miner *Miner) listener() { miner.block.Undo() //log.Infoln("[MINER] We did not know about this transaction, adding") miner.txs = append(miner.txs, tx) - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) - miner.block.SetTransactions(miner.txs) } else { //log.Infoln("[MINER] We already had this transaction, ignoring") } } default: - ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(miner.txs), "transactions") + stateManager := miner.ethereum.StateManager() + + miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) // Apply uncles if len(miner.uncles) > 0 { @@ -120,8 +120,19 @@ func (miner *Miner) listener() { } // Apply all transactions to the block - miner.ethereum.StateManager().ApplyTransactions(miner.block.State(), miner.block, miner.block.Transactions()) - miner.ethereum.StateManager().AccumelateRewards(miner.block.State(), miner.block) + txs := miner.txs + miner.txs = nil + for _, tx := range txs { + if err := stateManager.ApplyTransaction(miner.block.State(), miner.block, tx); err == nil { + miner.txs = append(miner.txs, tx) + } + } + miner.block.SetTransactions(miner.txs) + stateManager.AccumelateRewards(miner.block.State(), miner.block) + + 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()) // Search the nonce miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) -- cgit v1.2.3 From f5852b47d1008e3b1752031900e8f4cdd982ee61 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 14:00:13 +0200 Subject: Removed some logging and refactored a bit --- ethminer/miner.go | 84 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 8d6486e90..2e31dcead 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -60,10 +60,10 @@ func (miner *Miner) listener() { select { case chanMessage := <-miner.reactChan: 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 { // 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 var newtxs []*ethchain.Transaction @@ -87,13 +87,11 @@ func (miner *Miner) listener() { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { ethutil.Config.Log.Infoln("[MINER] Adding uncle 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 { - //log.Infoln("[MINER] Got new transaction from Reactor", tx) found := false for _, ctx := range miner.txs { if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { @@ -102,54 +100,54 @@ func (miner *Miner) listener() { } if found == false { + // Undo all previous commits miner.block.Undo() - //log.Infoln("[MINER] We did not know about this transaction, adding") + // Apply new transactions miner.txs = append(miner.txs, tx) - } else { - //log.Infoln("[MINER] We already had this transaction, ignoring") } } default: - stateManager := miner.ethereum.StateManager() - - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) - - // Apply uncles - if len(miner.uncles) > 0 { - miner.block.SetUncles(miner.uncles) - } - - // Apply all transactions to the block - txs := miner.txs - miner.txs = nil - for _, tx := range txs { - if err := stateManager.ApplyTransaction(miner.block.State(), miner.block, tx); err == nil { - miner.txs = append(miner.txs, tx) - } - } - miner.block.SetTransactions(miner.txs) - stateManager.AccumelateRewards(miner.block.State(), miner.block) + miner.mineNewBlock() + } + } +} - ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(miner.txs), "transactions") +func (self *Miner) mineNewBlock() { + stateManager := self.ethereum.StateManager() - //miner.ethereum.StateManager().ApplyTransactions(miner.block.State(), miner.block, miner.block.Transactions()) + self.block = self.ethereum.BlockChain().NewBlock(self.coinbase, self.txs) - // Search the nonce - miner.block.Nonce = miner.pow.Search(miner.block, miner.quitChan) - if miner.block.Nonce != nil { - err := miner.ethereum.StateManager().Process(miner.block, true) - if err != nil { - ethutil.Config.Log.Infoln(err) - miner.txs = []*ethchain.Transaction{} // Move this somewhere neat - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) - } else { - miner.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{miner.block.Value().Val}) - ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", miner.block.Hash()) + // Apply uncles + if len(self.uncles) > 0 { + self.block.SetUncles(self.uncles) + } - miner.txs = []*ethchain.Transaction{} // Move this somewhere neat - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) - } - } + // Accumulate all valid transaction and apply them to the new state + var txs []*ethchain.Transaction + for _, tx := range self.txs { + if err := stateManager.ApplyTransaction(self.block.State(), self.block, tx); err == nil { + txs = append(txs, tx) + } + } + self.txs = txs + // Set the transactions to the block so the new SHA3 can be calculated + self.block.SetTransactions(self.txs) + // Accumulate the rewards included for this block + stateManager.AccumelateRewards(self.block.State(), self.block) + + ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions") + + // Find a valid nonce + self.block.Nonce = self.pow.Search(self.block, self.quitChan) + if self.block.Nonce != nil { + err := self.ethereum.StateManager().Process(self.block, true) + if err != nil { + ethutil.Config.Log.Infoln(err) + } else { + 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() } } } -- cgit v1.2.3 From 4e1c6a8a22924d06a2a972c024891cebcf8ea054 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 22 May 2014 00:25:34 +0200 Subject: Added start / stopping methods --- ethminer/miner.go | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 2e31dcead..166f7bc2f 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -8,21 +8,22 @@ import ( ) 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 + pow ethchain.PoW + ethereum ethchain.EthManager + coinbase []byte + reactChan chan ethutil.React + txs []*ethchain.Transaction + uncles []*ethchain.Block + block *ethchain.Block + powChan chan []byte + powQuitChan chan ethutil.React + quitChan chan bool } 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 + 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 + powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread ethereum.Reactor().Subscribe("newBlock", reactChan) ethereum.Reactor().Subscribe("newTx:pre", reactChan) @@ -32,16 +33,17 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { // 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) - ethereum.Reactor().Subscribe("newTx:pre", quitChan) + ethereum.Reactor().Subscribe("newBlock", powQuitChan) + ethereum.Reactor().Subscribe("newTx:pre", powQuitChan) miner := Miner{ - pow: ðchain.EasyPow{}, - ethereum: ethereum, - coinbase: coinbase, - reactChan: reactChan, - powChan: powChan, - quitChan: quitChan, + pow: ðchain.EasyPow{}, + ethereum: ethereum, + coinbase: coinbase, + reactChan: reactChan, + powChan: powChan, + powQuitChan: powQuitChan, + quitChan: make(chan bool), } // Insert initial TXs in our little miner 'pool' @@ -56,8 +58,11 @@ func (miner *Miner) Start() { go miner.listener() } func (miner *Miner) listener() { +out: for { select { + case <-miner.quitChan: + break out case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { //ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor") @@ -112,6 +117,11 @@ func (miner *Miner) listener() { } } +func (self *Miner) Stop() { + self.powQuitChan <- ethutil.React{} + self.quitChan <- true +} + func (self *Miner) mineNewBlock() { stateManager := self.ethereum.StateManager() @@ -138,7 +148,7 @@ func (self *Miner) mineNewBlock() { ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions") // Find a valid nonce - self.block.Nonce = self.pow.Search(self.block, self.quitChan) + self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) if self.block.Nonce != nil { err := self.ethereum.StateManager().Process(self.block, true) if err != nil { -- cgit v1.2.3 From 230aafbf66ba747fb3796810adf3b1680f368e73 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 22 May 2014 17:35:26 +0200 Subject: Working on interop * Receipts after each transaction * Fee structure * Applying fees to miners --- ethminer/miner.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 166f7bc2f..00e04cde2 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -48,7 +48,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { // Insert initial TXs in our little miner 'pool' miner.txs = ethereum.TxPool().Flush() - miner.block = ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) + miner.block = ethereum.BlockChain().NewBlock(miner.coinbase) return miner } @@ -86,7 +86,7 @@ out: miner.txs = newtxs // Setup a fresh state to mine on - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) + //miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { @@ -125,7 +125,7 @@ func (self *Miner) Stop() { func (self *Miner) mineNewBlock() { stateManager := self.ethereum.StateManager() - self.block = self.ethereum.BlockChain().NewBlock(self.coinbase, self.txs) + self.block = self.ethereum.BlockChain().NewBlock(self.coinbase) // Apply uncles if len(self.uncles) > 0 { @@ -133,15 +133,10 @@ func (self *Miner) mineNewBlock() { } // Accumulate all valid transaction and apply them to the new state - var txs []*ethchain.Transaction - for _, tx := range self.txs { - if err := stateManager.ApplyTransaction(self.block.State(), self.block, tx); err == nil { - txs = append(txs, tx) - } - } + receipts, txs := stateManager.ApplyTransactions(self.block.State(), self.block, self.txs) self.txs = txs // Set the transactions to the block so the new SHA3 can be calculated - self.block.SetTransactions(self.txs) + self.block.SetReceipts(receipts, txs) // Accumulate the rewards included for this block stateManager.AccumelateRewards(self.block.State(), self.block) -- cgit v1.2.3 From 9988b1a04710e03ce7ed4b23393e2e90f06889f9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 28 May 2014 12:06:09 +0200 Subject: Sort transactions based on the nonce * Added a transaction sorter --- ethminer/miner.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 00e04cde2..9396d33f9 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -5,6 +5,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" + "sort" ) type Miner struct { @@ -12,7 +13,7 @@ type Miner struct { ethereum ethchain.EthManager coinbase []byte reactChan chan ethutil.React - txs []*ethchain.Transaction + txs ethchain.Transactions uncles []*ethchain.Block block *ethchain.Block powChan chan []byte @@ -132,6 +133,8 @@ func (self *Miner) mineNewBlock() { self.block.SetUncles(self.uncles) } + // Sort the transactions by nonce in case of odd network propagation + sort.Sort(ethchain.TxByNonce{self.txs}) // Accumulate all valid transaction and apply them to the new state receipts, txs := stateManager.ApplyTransactions(self.block.State(), self.block, self.txs) self.txs = txs -- cgit v1.2.3 From e0b6a31613bc48bc5785f2bea655f832848392d8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 13:27:56 +0200 Subject: Buffered channel to fix not ready (blocking) --- ethminer/miner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 9396d33f9..e7237bae2 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -25,6 +25,7 @@ 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 powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread + quitChan := make(chan bool, 1) ethereum.Reactor().Subscribe("newBlock", reactChan) ethereum.Reactor().Subscribe("newTx:pre", reactChan) @@ -44,7 +45,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { reactChan: reactChan, powChan: powChan, powQuitChan: powQuitChan, - quitChan: make(chan bool), + quitChan: quitChan, } // Insert initial TXs in our little miner 'pool' -- cgit v1.2.3 From 17c825f53a2676ffe17fd7731f8f550aebcb56b0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 16:57:58 +0200 Subject: Peer changes broadcasting and minor miner fix --- ethminer/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index e7237bae2..19ff5dd9e 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -149,7 +149,7 @@ func (self *Miner) mineNewBlock() { // Find a valid nonce self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) if self.block.Nonce != nil { - err := self.ethereum.StateManager().Process(self.block, true) + err := self.ethereum.StateManager().Process(self.block, false) if err != nil { ethutil.Config.Log.Infoln(err) } else { -- cgit v1.2.3 From 9ee6295c752a518603de01e4feaec787c61a5dcf Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 11 Jun 2014 21:55:45 +0200 Subject: Minor changes --- ethminer/miner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 19ff5dd9e..d05405391 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -137,7 +137,7 @@ func (self *Miner) mineNewBlock() { // Sort the transactions by nonce in case of odd network propagation sort.Sort(ethchain.TxByNonce{self.txs}) // Accumulate all valid transaction and apply them to the new state - receipts, txs := stateManager.ApplyTransactions(self.block.State(), self.block, self.txs) + receipts, txs := stateManager.ApplyTransactions(self.block.Coinbase, self.block.State(), self.block, self.txs) self.txs = txs // Set the transactions to the block so the new SHA3 can be calculated self.block.SetReceipts(receipts, txs) @@ -155,6 +155,7 @@ func (self *Miner) mineNewBlock() { } else { self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val}) ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", self.block.Hash()) + ethutil.Config.Log.Infoln(self.block) // Gather the new batch of transactions currently in the tx pool self.txs = self.ethereum.TxPool().CurrentTransactions() } -- cgit v1.2.3 From 63883bf27d8b87f601e1603e9024a279b91bffb7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Jun 2014 11:46:09 +0200 Subject: Moving closer to interop --- ethminer/miner.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index d05405391..30b7ef35d 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -136,11 +136,18 @@ func (self *Miner) mineNewBlock() { // Sort the transactions by nonce in case of odd network propagation sort.Sort(ethchain.TxByNonce{self.txs}) + // Accumulate all valid transaction and apply them to the new state - receipts, txs := stateManager.ApplyTransactions(self.block.Coinbase, self.block.State(), self.block, self.txs) - self.txs = txs + // Error may be ignored. It's not important during mining + receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(self.block.Coinbase, self.block.State(), self.block, self.block, self.txs) + if err != nil { + ethutil.Config.Log.Debugln("[MINER]", err) + } + self.txs = append(txs, unhandledTxs...) + // Set the transactions to the block so the new SHA3 can be calculated self.block.SetReceipts(receipts, txs) + // Accumulate the rewards included for this block stateManager.AccumelateRewards(self.block.State(), self.block) -- cgit v1.2.3 From 9f62d441a7c785b88f89d52643a9deaa822af15e Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 16 Jun 2014 11:14:01 +0200 Subject: Moved gas limit err check to buy gas --- ethminer/miner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 30b7ef35d..8ea6c51e5 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -139,7 +139,8 @@ func (self *Miner) mineNewBlock() { // Accumulate all valid transaction and apply them to the new state // Error may be ignored. It's not important during mining - receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(self.block.Coinbase, self.block.State(), self.block, self.block, self.txs) + coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase) + receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) if err != nil { ethutil.Config.Log.Debugln("[MINER]", err) } -- cgit v1.2.3 From 48bca30e61f869a00111abe5d818ac7379854616 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 16 Jun 2014 11:51:16 +0200 Subject: Fixed minor issue with the gas pool --- ethminer/miner.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 8ea6c51e5..1ef9ca229 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -139,7 +139,9 @@ func (self *Miner) mineNewBlock() { // Accumulate all valid transaction and apply them to the new state // Error may be ignored. It's not important during mining + parent := self.ethereum.BlockChain().GetBlock(self.block.PrevHash) coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase) + coinbase.SetGasPool(self.block.CalcGasLimit(parent)) receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) if err != nil { ethutil.Config.Log.Debugln("[MINER]", err) -- cgit v1.2.3 From 53e30f750dd0c91279bfebe01bb12fd170cb74ff Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 17 Jun 2014 11:06:06 +0200 Subject: Removal of manual updating of state objects * You'll only ever need to update the state by calling Update. Update will take care of the updating of it's child state objects. --- ethminer/miner.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 1ef9ca229..4343b4333 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -154,6 +154,8 @@ func (self *Miner) mineNewBlock() { // Accumulate the rewards included for this block stateManager.AccumelateRewards(self.block.State(), self.block) + self.block.State().Update() + ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions") // Find a valid nonce -- cgit v1.2.3 From b9e8a3e02493d5bbf23cfcab259e66f6ae166612 Mon Sep 17 00:00:00 2001 From: zelig Date: Mon, 23 Jun 2014 12:54:10 +0100 Subject: modified logging API - package vars for tagged loggers - weed out spurious fmt.PrintX and log.PrintX logging - tried to second guess loglevel for some :) --- ethminer/miner.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 4343b4333..5f5c40134 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -5,9 +5,12 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" + "github.com/ethereum/eth-go/ethlog" "sort" ) +var logger = ethlog.NewLogger("MINER") + type Miner struct { pow ethchain.PoW ethereum ethchain.EthManager @@ -67,10 +70,10 @@ out: break out case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - //ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor") + //logger.Infoln("Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards - //ethutil.Config.Log.Infoln("[MINER] New top block found resetting state") + //logger.Infoln("New top block found resetting state") // Filter out which Transactions we have that were not in this block var newtxs []*ethchain.Transaction @@ -92,7 +95,7 @@ out: } else { if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - ethutil.Config.Log.Infoln("[MINER] Adding uncle block") + logger.Infoln("Adding uncle block") miner.uncles = append(miner.uncles, block) } } @@ -137,14 +140,14 @@ func (self *Miner) mineNewBlock() { // Sort the transactions by nonce in case of odd network propagation sort.Sort(ethchain.TxByNonce{self.txs}) - // Accumulate all valid transaction and apply them to the new state + // Accumulate all valid transactions and apply them to the new state // Error may be ignored. It's not important during mining parent := self.ethereum.BlockChain().GetBlock(self.block.PrevHash) coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase) coinbase.SetGasPool(self.block.CalcGasLimit(parent)) receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) if err != nil { - ethutil.Config.Log.Debugln("[MINER]", err) + logger.Debugln(err) } self.txs = append(txs, unhandledTxs...) @@ -156,18 +159,18 @@ func (self *Miner) mineNewBlock() { self.block.State().Update() - ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions") + logger.Infoln("Mining on block. Includes", len(self.txs), "transactions") // Find a valid nonce self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) if self.block.Nonce != nil { err := self.ethereum.StateManager().Process(self.block, false) if err != nil { - ethutil.Config.Log.Infoln(err) + logger.Infoln(err) } else { self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val}) - ethutil.Config.Log.Infof("[MINER] 🔨 Mined block %x\n", self.block.Hash()) - ethutil.Config.Log.Infoln(self.block) + 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() } -- cgit v1.2.3 From 782f780476afb3c895c30583fc5cbd6d7d830d3d Mon Sep 17 00:00:00 2001 From: zelig Date: Wed, 25 Jun 2014 18:43:35 +0100 Subject: space in miner logging message --- ethminer/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 5f5c40134..2c1645672 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -159,7 +159,7 @@ func (self *Miner) mineNewBlock() { self.block.State().Update() - logger.Infoln("Mining on block. Includes", len(self.txs), "transactions") + logger.Infof("Mining on block. Includes %v transactions", len(self.txs)) // Find a valid nonce self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) -- cgit v1.2.3 From 853053a3b204ddf4ae935e70e0aa5b5d8994493e Mon Sep 17 00:00:00 2001 From: zelig Date: Thu, 26 Jun 2014 18:45:57 +0100 Subject: go fmt --- ethminer/miner.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 2c1645672..66388723e 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -3,9 +3,9 @@ package ethminer import ( "bytes" "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" - "github.com/ethereum/eth-go/ethlog" "sort" ) @@ -57,18 +57,23 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { return miner } + func (miner *Miner) Start() { // Prepare inital block //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) go miner.listener() + logger.Infoln("Started") } + func (miner *Miner) listener() { out: for { select { case <-miner.quitChan: + logger.Infoln("Stopped") break out case chanMessage := <-miner.reactChan: + if block, ok := chanMessage.Resource.(*ethchain.Block); ok { //logger.Infoln("Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { @@ -123,8 +128,9 @@ out: } func (self *Miner) Stop() { - self.powQuitChan <- ethutil.React{} + logger.Infoln("Stopping...") self.quitChan <- true + self.powQuitChan <- ethutil.React{} } func (self *Miner) mineNewBlock() { -- cgit v1.2.3 From 2bbc204328cf674e8ccd591bb4d08179225b396a Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 1 Jul 2014 11:55:50 +0200 Subject: Close pow chat. Fixes #95 --- ethminer/miner.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 66388723e..71d4b2428 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -130,7 +130,9 @@ out: func (self *Miner) Stop() { logger.Infoln("Stopping...") self.quitChan <- true - self.powQuitChan <- ethutil.React{} + + close(self.powQuitChan) + close(self.quitChan) } func (self *Miner) mineNewBlock() { -- cgit v1.2.3 From 584d1c61ec93df3417f2ce8ece041b81a5ec63a6 Mon Sep 17 00:00:00 2001 From: zelig Date: Fri, 4 Jul 2014 19:38:44 +0100 Subject: use ethreact.Event and ethreact.ReactorEngine --- ethminer/miner.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 71d4b2428..8224c5441 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -4,7 +4,7 @@ import ( "bytes" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" - "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethwire" "sort" ) @@ -15,19 +15,19 @@ type Miner struct { pow ethchain.PoW ethereum ethchain.EthManager coinbase []byte - reactChan chan ethutil.React + reactChan chan ethreact.Event txs ethchain.Transactions uncles []*ethchain.Block block *ethchain.Block powChan chan []byte - powQuitChan chan ethutil.React + powQuitChan chan ethreact.Event quitChan chan bool } 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 - powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread + reactChan := make(chan ethreact.Event, 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 + powQuitChan := make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread quitChan := make(chan bool, 1) ethereum.Reactor().Subscribe("newBlock", reactChan) -- cgit v1.2.3 From 6fe9b4ab5e839be96eb1c4a619bc14fab622d8d1 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 7 Jul 2014 10:59:16 +0200 Subject: Revert "ethreact - Feature/ethutil refactor" --- ethminer/miner.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 8224c5441..71d4b2428 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -4,7 +4,7 @@ import ( "bytes" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" - "github.com/ethereum/eth-go/ethreact" + "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" "sort" ) @@ -15,19 +15,19 @@ type Miner struct { pow ethchain.PoW ethereum ethchain.EthManager coinbase []byte - reactChan chan ethreact.Event + reactChan chan ethutil.React txs ethchain.Transactions uncles []*ethchain.Block block *ethchain.Block powChan chan []byte - powQuitChan chan ethreact.Event + powQuitChan chan ethutil.React quitChan chan bool } func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { - reactChan := make(chan ethreact.Event, 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 - powQuitChan := make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread + 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 + powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread quitChan := make(chan bool, 1) ethereum.Reactor().Subscribe("newBlock", reactChan) -- cgit v1.2.3 From 1735ec0362e84455126d8c1bd380ecae436d1167 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 15 Jul 2014 01:11:06 +0100 Subject: use ethreact.Event and ethreact.ReactorEngine --- ethminer/miner.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 71d4b2428..8224c5441 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -4,7 +4,7 @@ import ( "bytes" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" - "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethwire" "sort" ) @@ -15,19 +15,19 @@ type Miner struct { pow ethchain.PoW ethereum ethchain.EthManager coinbase []byte - reactChan chan ethutil.React + reactChan chan ethreact.Event txs ethchain.Transactions uncles []*ethchain.Block block *ethchain.Block powChan chan []byte - powQuitChan chan ethutil.React + powQuitChan chan ethreact.Event quitChan chan bool } 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 - powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread + reactChan := make(chan ethreact.Event, 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 + powQuitChan := make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread quitChan := make(chan bool, 1) ethereum.Reactor().Subscribe("newBlock", reactChan) -- cgit v1.2.3 From 017d36e6b2e127084448dfb38bd1b8de7424e1c9 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 15 Jul 2014 01:12:45 +0100 Subject: properly unsubscribe react channels when miner stops - fixes write on closed chan crash --- ethminer/miner.go | 58 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 8224c5441..5151ee885 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -25,30 +25,10 @@ type Miner struct { } func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { - reactChan := make(chan ethreact.Event, 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 - powQuitChan := make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread - quitChan := make(chan bool, 1) - - ethereum.Reactor().Subscribe("newBlock", reactChan) - ethereum.Reactor().Subscribe("newTx:pre", reactChan) - - // 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", powQuitChan) - ethereum.Reactor().Subscribe("newTx:pre", powQuitChan) - miner := Miner{ - pow: ðchain.EasyPow{}, - ethereum: ethereum, - coinbase: coinbase, - reactChan: reactChan, - powChan: powChan, - powQuitChan: powQuitChan, - quitChan: quitChan, + pow: ðchain.EasyPow{}, + ethereum: ethereum, + coinbase: coinbase, } // Insert initial TXs in our little miner 'pool' @@ -59,9 +39,27 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { } func (miner *Miner) Start() { + miner.reactChan = make(chan ethreact.Event, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in + miner.powChan = make(chan []byte, 1) // This is the channel that receives valid sha hashes for a given block + miner.powQuitChan = make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread + miner.quitChan = make(chan bool, 1) + // Prepare inital block //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) go miner.listener() + + reactor := miner.ethereum.Reactor() + reactor.Subscribe("newBlock", miner.reactChan) + reactor.Subscribe("newTx:pre", miner.reactChan) + + // 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 + reactor.Subscribe("newBlock", miner.powQuitChan) + reactor.Subscribe("newTx:pre", miner.powQuitChan) + logger.Infoln("Started") } @@ -127,12 +125,18 @@ out: } } -func (self *Miner) Stop() { +func (miner *Miner) Stop() { logger.Infoln("Stopping...") - self.quitChan <- true + miner.quitChan <- true + + reactor := miner.ethereum.Reactor() + reactor.Unsubscribe("newBlock", miner.powQuitChan) + reactor.Unsubscribe("newTx:pre", miner.powQuitChan) + reactor.Unsubscribe("newBlock", miner.reactChan) + reactor.Unsubscribe("newTx:pre", miner.reactChan) - close(self.powQuitChan) - close(self.quitChan) + close(miner.powQuitChan) + close(miner.quitChan) } func (self *Miner) mineNewBlock() { -- cgit v1.2.3 From db8170def31e03ecb7086dd257d7c8fce084313f Mon Sep 17 00:00:00 2001 From: Maran Date: Fri, 18 Jul 2014 12:01:08 +0200 Subject: WIP to expose hashrate to gui --- ethminer/miner.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 71d4b2428..f45615b62 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -24,6 +24,10 @@ type Miner struct { quitChan chan bool } +func (self Miner) GetPow() *ethchain.PoW { + return &self.pow +} + 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 -- cgit v1.2.3 From dad29bcaa12d7b170d81a9f5a44a5faa119bd210 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 18 Jul 2014 12:21:11 +0200 Subject: Added channel for starting/stopping miner --- ethminer/miner.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index f45615b62..fd14571cd 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -24,11 +24,11 @@ type Miner struct { quitChan chan bool } -func (self Miner) GetPow() *ethchain.PoW { - return &self.pow +func (self *Miner) GetPow() ethchain.PoW { + return self.pow } -func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { +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 powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread @@ -59,7 +59,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { miner.txs = ethereum.TxPool().Flush() miner.block = ethereum.BlockChain().NewBlock(miner.coinbase) - return miner + return &miner } func (miner *Miner) Start() { @@ -67,6 +67,8 @@ func (miner *Miner) Start() { //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) go miner.listener() logger.Infoln("Started") + + miner.ethereum.Reactor().Post("miner:start", miner) } func (miner *Miner) listener() { @@ -137,6 +139,8 @@ func (self *Miner) Stop() { close(self.powQuitChan) close(self.quitChan) + + self.ethereum.Reactor().Post("miner:stop", self) } func (self *Miner) mineNewBlock() { -- cgit v1.2.3 From 2762ec22d0693b406ead2f0c07b62e9b66d395e4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 18 Jul 2014 13:50:15 +0200 Subject: Fixed miner and logger --- ethminer/miner.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index fd14571cd..a50b3712f 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -135,10 +135,9 @@ out: func (self *Miner) Stop() { logger.Infoln("Stopping...") - self.quitChan <- true - close(self.powQuitChan) - close(self.quitChan) + self.quitChan <- true + self.powQuitChan <- ethutil.React{} self.ethereum.Reactor().Post("miner:stop", self) } -- cgit v1.2.3 From 5d2669dbd35b9449cbbb249dcec89e2a64c90f30 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 21 Jul 2014 12:21:34 +0200 Subject: Fixed tx sha creation --- ethminer/miner.go | 1 + 1 file changed, 1 insertion(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index a50b3712f..bfea8e580 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -165,6 +165,7 @@ func (self *Miner) mineNewBlock() { logger.Debugln(err) } self.txs = append(txs, unhandledTxs...) + self.block.SetTxHash(receipts) // Set the transactions to the block so the new SHA3 can be calculated self.block.SetReceipts(receipts, txs) -- cgit v1.2.3 From 194c58858cd230a9a08b0eb14650720341a5580e Mon Sep 17 00:00:00 2001 From: zelig Date: Mon, 21 Jul 2014 19:12:04 +0100 Subject: send zero event to miner.powQuitChan fixes miner hanging --- ethminer/miner.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 8659d2889..e51b37e05 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -133,6 +133,9 @@ func (miner *Miner) listener() { func (miner *Miner) Stop() { logger.Infoln("Stopping...") + + miner.powQuitChan <- ethreact.Event{} + status := make(chan error) miner.quitChan <- status <-status @@ -143,9 +146,6 @@ func (miner *Miner) Stop() { reactor.Unsubscribe("newBlock", miner.reactChan) reactor.Unsubscribe("newTx:pre", miner.reactChan) - close(miner.powQuitChan) - close(miner.quitChan) - reactor.Post("miner:stop", miner) } -- cgit v1.2.3 From 3def9258be3c212bf405502f84654f45b0306543 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 21 Aug 2014 18:14:49 +0200 Subject: Turbo mode --- ethminer/miner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index e51b37e05..74f5bc7af 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -2,11 +2,12 @@ package ethminer import ( "bytes" + "sort" + "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethwire" - "sort" ) var logger = ethlog.NewLogger("MINER") -- cgit v1.2.3 From 732573ba512aa215e88aed3f20393c3c42c1aeb0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 21 Aug 2014 20:13:26 +0200 Subject: Turbo mining --- ethminer/miner.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 74f5bc7af..799db79f1 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -23,6 +23,8 @@ type Miner struct { powChan chan []byte powQuitChan chan ethreact.Event quitChan chan chan error + + turbo bool } func (self *Miner) GetPow() ethchain.PoW { @@ -39,6 +41,12 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) *Miner { return &miner } +func (self *Miner) ToggleTurbo() { + self.turbo = !self.turbo + + self.pow.Turbo(self.turbo) +} + func (miner *Miner) Start() { miner.reactChan = make(chan ethreact.Event, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in miner.powChan = make(chan []byte, 1) // This is the channel that receives valid sha hashes for a given block -- cgit v1.2.3 From 2f614900e82036e3e8f6f6a714efc43e09aca830 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Sep 2014 01:11:01 +0200 Subject: Updated GHOST --- ethminer/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 799db79f1..083d9ecde 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -187,7 +187,7 @@ func (self *Miner) mineNewBlock() { self.block.SetReceipts(receipts, txs) // Accumulate the rewards included for this block - stateManager.AccumelateRewards(self.block.State(), self.block) + stateManager.AccumelateRewards(self.block.State(), self.block, parent) self.block.State().Update() -- cgit v1.2.3 From 48fd23dc107c06aea63e75a7a07612a5f10b1dd0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Sep 2014 10:20:55 +0200 Subject: don't mine without transactions --- ethminer/miner.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 083d9ecde..b74f90e0c 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -3,6 +3,7 @@ package ethminer import ( "bytes" "sort" + "time" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" @@ -135,6 +136,12 @@ func (miner *Miner) listener() { } } default: + // This hack is only temporarily + if len(miner.txs) == 0 { + time.Sleep(2 * time.Second) + continue + } + miner.mineNewBlock() } } @@ -159,6 +166,7 @@ func (miner *Miner) Stop() { } func (self *Miner) mineNewBlock() { + stateManager := self.ethereum.StateManager() self.block = self.ethereum.BlockChain().NewBlock(self.coinbase) -- cgit v1.2.3 From 60a8c9527cba4298be4bdde39ff1e0e3fd7fe637 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 24 Sep 2014 19:54:37 +0200 Subject: Mine without txs --- ethminer/miner.go | 7 ------- 1 file changed, 7 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index b74f90e0c..299a5204a 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -3,7 +3,6 @@ package ethminer import ( "bytes" "sort" - "time" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" @@ -136,12 +135,6 @@ func (miner *Miner) listener() { } } default: - // This hack is only temporarily - if len(miner.txs) == 0 { - time.Sleep(2 * time.Second) - continue - } - miner.mineNewBlock() } } -- cgit v1.2.3 From 36cdab206849c7e363e0b9911553098c3e8ca644 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 14 Oct 2014 01:58:31 +0200 Subject: all: use (blocking) event package instead of ethreact --- ethminer/miner.go | 137 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 67 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index 299a5204a..ffc49f096 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -6,27 +6,37 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" - "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethwire" + "github.com/ethereum/eth-go/event" ) var logger = ethlog.NewLogger("MINER") type Miner struct { - pow ethchain.PoW - ethereum ethchain.EthManager - coinbase []byte - reactChan chan ethreact.Event - txs ethchain.Transactions - uncles []*ethchain.Block - block *ethchain.Block - powChan chan []byte - powQuitChan chan ethreact.Event - quitChan chan chan error + 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{} turbo bool } +const ( + Started = iota + Stopped +) + +type Event struct { + Type int // Started || Stopped + Miner *Miner +} + func (self *Miner) GetPow() ethchain.PoW { return self.pow } @@ -48,46 +58,42 @@ func (self *Miner) ToggleTurbo() { } func (miner *Miner) Start() { - miner.reactChan = make(chan ethreact.Event, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in - miner.powChan = make(chan []byte, 1) // This is the channel that receives valid sha hashes for a given block - miner.powQuitChan = make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread - miner.quitChan = make(chan chan error, 1) // 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{}) + // Prepare inital block //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) go miner.listener() - reactor := miner.ethereum.Reactor() - reactor.Subscribe("newBlock", miner.reactChan) - reactor.Subscribe("newTx:pre", miner.reactChan) - - // 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 - reactor.Subscribe("newBlock", miner.powQuitChan) - reactor.Subscribe("newTx:pre", miner.powQuitChan) - logger.Infoln("Started") + mux.Post(Event{Started, miner}) +} - reactor.Post("miner:start", miner) +func (miner *Miner) Stop() { + logger.Infoln("Stopping...") + miner.events.Unsubscribe() + miner.ethereum.EventMux().Post(Event{Stopped, miner}) } func (miner *Miner) listener() { for { + miner.startMining() + select { - case status := <-miner.quitChan: - logger.Infoln("Stopped") - status <- nil - return - case chanMessage := <-miner.reactChan: + case event, isopen := <-miner.events.Chan(): + miner.stopMining() + if !isopen { + return + } - if block, ok := chanMessage.Resource.(*ethchain.Block); ok { + switch event := event.(type) { + case ethchain.NewBlockEvent: + block := event.Block //logger.Infoln("Got new block via Reactor") if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards @@ -117,49 +123,44 @@ func (miner *Miner) listener() { miner.uncles = append(miner.uncles, block) } } - } - if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { - found := false - for _, ctx := range miner.txs { - if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { - break + 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) } - - } - if found == false { - // Undo all previous commits - miner.block.Undo() - // Apply new transactions - miner.txs = append(miner.txs, tx) } } - default: - miner.mineNewBlock() + + case <-miner.powDone: + // next iteration will start mining again } } } -func (miner *Miner) Stop() { - logger.Infoln("Stopping...") - - miner.powQuitChan <- ethreact.Event{} - - status := make(chan error) - miner.quitChan <- status - <-status - - reactor := miner.ethereum.Reactor() - reactor.Unsubscribe("newBlock", miner.powQuitChan) - reactor.Unsubscribe("newTx:pre", miner.powQuitChan) - reactor.Unsubscribe("newBlock", miner.reactChan) - reactor.Unsubscribe("newTx:pre", miner.reactChan) +func (miner *Miner) startMining() { + if miner.powDone == nil { + miner.powDone = make(chan struct{}) + } + miner.powQuitChan = make(chan struct{}) + go miner.mineNewBlock() +} - reactor.Post("miner:stop", miner) +func (miner *Miner) stopMining() { + close(miner.powQuitChan) + <-miner.powDone } func (self *Miner) mineNewBlock() { - stateManager := self.ethereum.StateManager() self.block = self.ethereum.BlockChain().NewBlock(self.coinbase) @@ -195,8 +196,9 @@ func (self *Miner) mineNewBlock() { logger.Infof("Mining on block. Includes %v transactions", len(self.txs)) // Find a valid nonce - self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) - if self.block.Nonce != nil { + 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) @@ -208,4 +210,5 @@ func (self *Miner) mineNewBlock() { self.txs = self.ethereum.TxPool().CurrentTransactions() } } + self.powDone <- struct{}{} } -- cgit v1.2.3 From 097ba56df59293f9225a8ecdc9e1c43a5ad891bb Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 20 Oct 2014 11:53:11 +0200 Subject: Renamed block_chain to chain_manager --- ethminer/miner.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ethminer') diff --git a/ethminer/miner.go b/ethminer/miner.go index ffc49f096..69f6e4bf6 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -61,7 +61,7 @@ func (miner *Miner) Start() { // Insert initial TXs in our little miner 'pool' miner.txs = miner.ethereum.TxPool().Flush() - miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase) + miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase) mux := miner.ethereum.EventMux() miner.events = mux.Subscribe(ethchain.NewBlockEvent{}, ethchain.TxEvent{}) @@ -95,7 +95,7 @@ func (miner *Miner) listener() { case ethchain.NewBlockEvent: block := event.Block //logger.Infoln("Got new block via Reactor") - if bytes.Compare(miner.ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { + if bytes.Compare(miner.ethereum.ChainManager().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards //logger.Infoln("New top block found resetting state") @@ -115,10 +115,10 @@ func (miner *Miner) listener() { miner.txs = newtxs // Setup a fresh state to mine on - //miner.block = miner.ethereum.BlockChain().NewBlock(miner.coinbase, miner.txs) + //miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase, miner.txs) } else { - if bytes.Compare(block.PrevHash, miner.ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { + if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { logger.Infoln("Adding uncle block") miner.uncles = append(miner.uncles, block) } @@ -163,7 +163,7 @@ func (miner *Miner) stopMining() { func (self *Miner) mineNewBlock() { stateManager := self.ethereum.StateManager() - self.block = self.ethereum.BlockChain().NewBlock(self.coinbase) + self.block = self.ethereum.ChainManager().NewBlock(self.coinbase) // Apply uncles if len(self.uncles) > 0 { @@ -175,7 +175,7 @@ func (self *Miner) mineNewBlock() { // Accumulate all valid transactions and apply them to the new state // Error may be ignored. It's not important during mining - parent := self.ethereum.BlockChain().GetBlock(self.block.PrevHash) + parent := self.ethereum.ChainManager().GetBlock(self.block.PrevHash) coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase) coinbase.SetGasPool(self.block.CalcGasLimit(parent)) receipts, txs, unhandledTxs, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) -- cgit v1.2.3