From 699dcaf65ced99517724984f5930845417cfdfca Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 4 Nov 2014 12:46:33 +0100 Subject: Reworked chain handling process * Forks * Rename * Moved inserting of blocks & processing * Added chain testing method for validating pieces of a **a** chain. --- miner/miner.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index fd74da4d1..7491ab373 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -162,8 +162,9 @@ func (miner *Miner) stopMining() { func (self *Miner) mineNewBlock() { blockManager := self.ethereum.BlockManager() + chainMan := self.ethereum.ChainManager() - self.block = self.ethereum.ChainManager().NewBlock(self.coinbase) + self.block = chainMan.NewBlock(self.coinbase) // Apply uncles if len(self.uncles) > 0 { @@ -199,10 +200,12 @@ func (self *Miner) mineNewBlock() { nonce := self.pow.Search(self.block, self.powQuitChan) if nonce != nil { self.block.Nonce = nonce - err := self.ethereum.BlockManager().Process(self.block) + lchain := chain.NewChain(chain.Blocks{self.block}) + _, err := chainMan.TestChain(lchain) if err != nil { minerlogger.Infoln(err) } else { + self.ethereum.ChainManager().InsertChain(lchain) self.ethereum.Broadcast(wire.MsgBlockTy, []interface{}{self.block.Value().Val}) minerlogger.Infof("🔨 Mined block %x\n", self.block.Hash()) minerlogger.Infoln(self.block) -- cgit v1.2.3 From 429dd2a100f3b9e2b612b59bcb48f79a805cd6f9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 7 Nov 2014 12:18:48 +0100 Subject: Implemented new miner w/ ui interface for merged mining. Closes #177 * Miner has been rewritten * Added new miner pane * Added option for local txs * Added option to read from MergeMining contract and list them for merged mining --- miner/miner.go | 298 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 154 insertions(+), 144 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 7491ab373..2ab74e516 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -1,220 +1,230 @@ package miner import ( - "bytes" + "math/big" "sort" + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/wire" ) +type LocalTx struct { + To []byte `json:"to"` + Data []byte `json:"data"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + Value string `json:"value"` +} + +func (self *LocalTx) Sign(key []byte) *chain.Transaction { + return nil +} + var minerlogger = logger.NewLogger("MINER") type Miner struct { - pow chain.PoW - ethereum chain.EthManager - coinbase []byte - txs chain.Transactions - uncles []*chain.Block - block *chain.Block - - events event.Subscription - powQuitChan chan struct{} - powDone chan struct{} - - turbo bool -} + eth *eth.Ethereum + events event.Subscription -const ( - Started = iota - Stopped -) + uncles chain.Blocks + localTxs map[int]*LocalTx + localTxId int + + pow chain.PoW + quitCh chan struct{} + powQuitCh chan struct{} + + Coinbase []byte -type Event struct { - Type int // Started || Stopped - Miner *Miner + mining bool + + MinAcceptedGasPrice *big.Int +} + +func New(coinbase []byte, eth *eth.Ethereum) *Miner { + return &Miner{ + eth: eth, + powQuitCh: make(chan struct{}), + pow: &chain.EasyPow{}, + mining: false, + localTxs: make(map[int]*LocalTx), + MinAcceptedGasPrice: big.NewInt(10000000000000), + Coinbase: coinbase, + } } func (self *Miner) GetPow() chain.PoW { return self.pow } -func NewDefaultMiner(coinbase []byte, ethereum chain.EthManager) *Miner { - miner := Miner{ - pow: &chain.EasyPow{}, - ethereum: ethereum, - coinbase: coinbase, +func (self *Miner) AddLocalTx(tx *LocalTx) int { + minerlogger.Infof("Added local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value) + + self.localTxId++ + self.localTxs[self.localTxId] = tx + self.eth.EventMux().Post(tx) + + return self.localTxId +} + +func (self *Miner) RemoveLocalTx(id int) { + if tx := self.localTxs[id]; tx != nil { + minerlogger.Infof("Removed local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value) } + self.eth.EventMux().Post(&LocalTx{}) - return &miner + delete(self.localTxs, id) } -func (self *Miner) ToggleTurbo() { - self.turbo = !self.turbo +func (self *Miner) Start() { + if self.mining { + return + } - self.pow.Turbo(self.turbo) + minerlogger.Infoln("Starting mining operations") + self.mining = true + self.quitCh = make(chan struct{}) + self.powQuitCh = make(chan struct{}) + + mux := self.eth.EventMux() + self.events = mux.Subscribe(chain.NewBlockEvent{}, chain.TxPreEvent{}, &LocalTx{}) + + go self.update() + go self.mine() } -func (miner *Miner) Start() { +func (self *Miner) Stop() { + if !self.mining { + return + } - // Insert initial TXs in our little miner 'pool' - miner.txs = miner.ethereum.TxPool().Flush() - miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase) + self.mining = false - mux := miner.ethereum.EventMux() - miner.events = mux.Subscribe(chain.NewBlockEvent{}, chain.TxPreEvent{}) + minerlogger.Infoln("Stopping mining operations") - // Prepare inital block - //miner.ethereum.BlockManager().Prepare(miner.block.State(), miner.block.State()) - go miner.listener() + self.events.Unsubscribe() - minerlogger.Infoln("Started") - mux.Post(Event{Started, miner}) + close(self.quitCh) + close(self.powQuitCh) } -func (miner *Miner) Stop() { - minerlogger.Infoln("Stopping...") - miner.events.Unsubscribe() - miner.ethereum.EventMux().Post(Event{Stopped, miner}) +func (self *Miner) Mining() bool { + return self.mining } -func (miner *Miner) listener() { - miner.startMining() - +func (self *Miner) update() { +out: for { select { - case event := <-miner.events.Chan(): + case event := <-self.events.Chan(): switch event := event.(type) { case chain.NewBlockEvent: - miner.stopMining() - block := event.Block - //minerlogger.Infoln("Got new block via Reactor") - if bytes.Compare(miner.ethereum.ChainManager().CurrentBlock.Hash(), block.Hash()) == 0 { - // TODO: Perhaps continue mining to get some uncle rewards - //minerlogger.Infoln("New top block found resetting state") - - // Filter out which Transactions we have that were not in this block - var newtxs []*chain.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 - } else { - if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { - minerlogger.Infoln("Adding uncle block") - miner.uncles = append(miner.uncles, block) - } - } - miner.startMining() - - case chain.TxPreEvent: - miner.stopMining() - - found := false - for _, ctx := range miner.txs { - if found = bytes.Compare(ctx.Hash(), event.Tx.Hash()) == 0; found { - break - } - - miner.startMining() - } - if found == false { - // Undo all previous commits - miner.block.Undo() - // Apply new transactions - miner.txs = append(miner.txs, event.Tx) + if self.eth.ChainManager().HasBlock(block.Hash()) { + self.reset() + self.eth.TxPool().RemoveSet(block.Transactions()) + go self.mine() + } else if true { + // do uncle stuff } + case chain.TxPreEvent, *LocalTx: + self.reset() + go self.mine() } - - case <-miner.powDone: - miner.startMining() + case <-self.quitCh: + break out } } } -func (miner *Miner) startMining() { - if miner.powDone == nil { - miner.powDone = make(chan struct{}) - } - miner.powQuitChan = make(chan struct{}) - go miner.mineNewBlock() -} - -func (miner *Miner) stopMining() { - println("stop mining") - _, isopen := <-miner.powQuitChan - if isopen { - close(miner.powQuitChan) - } - //<-miner.powDone +func (self *Miner) reset() { + println("reset") + close(self.powQuitCh) + self.powQuitCh = make(chan struct{}) } -func (self *Miner) mineNewBlock() { - blockManager := self.ethereum.BlockManager() - chainMan := self.ethereum.ChainManager() - - self.block = chainMan.NewBlock(self.coinbase) +func (self *Miner) mine() { + var ( + blockManager = self.eth.BlockManager() + chainMan = self.eth.ChainManager() + block = chainMan.NewBlock(self.Coinbase) + ) + block.MinGasPrice = self.MinAcceptedGasPrice // Apply uncles if len(self.uncles) > 0 { - self.block.SetUncles(self.uncles) + block.SetUncles(self.uncles) } - // Sort the transactions by nonce in case of odd network propagation - sort.Sort(chain.TxByNonce{self.txs}) + parent := chainMan.GetBlock(block.PrevHash) + coinbase := block.State().GetOrNewStateObject(block.Coinbase) + coinbase.SetGasPool(block.CalcGasLimit(parent)) + + transactions := self.finiliseTxs() // Accumulate all valid transactions and apply them to the new state // Error may be ignored. It's not important during mining - 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, erroneous, err := blockManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) + receipts, txs, _, erroneous, err := blockManager.ProcessTransactions(coinbase, block.State(), block, block, transactions) if err != nil { minerlogger.Debugln(err) } - self.ethereum.TxPool().RemoveSet(erroneous) - self.txs = append(txs, unhandledTxs...) + self.eth.TxPool().RemoveSet(erroneous) - self.block.SetTransactions(txs) - self.block.SetReceipts(receipts) + block.SetTransactions(txs) + block.SetReceipts(receipts) // Accumulate the rewards included for this block - blockManager.AccumelateRewards(self.block.State(), self.block, parent) + blockManager.AccumelateRewards(block.State(), block, parent) - self.block.State().Update() + block.State().Update() - minerlogger.Infof("Mining on block. Includes %v transactions", len(self.txs)) + minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) // Find a valid nonce - nonce := self.pow.Search(self.block, self.powQuitChan) + nonce := self.pow.Search(block, self.powQuitCh) if nonce != nil { - self.block.Nonce = nonce - lchain := chain.NewChain(chain.Blocks{self.block}) - _, err := chainMan.TestChain(lchain) + block.Nonce = nonce + lchain := chain.NewChain(chain.Blocks{block}) + _, err := chainMan.TestChain(lchain, true) if err != nil { minerlogger.Infoln(err) } else { - self.ethereum.ChainManager().InsertChain(lchain) - self.ethereum.Broadcast(wire.MsgBlockTy, []interface{}{self.block.Value().Val}) - minerlogger.Infof("🔨 Mined block %x\n", self.block.Hash()) - minerlogger.Infoln(self.block) - // Gather the new batch of transactions currently in the tx pool - self.txs = self.ethereum.TxPool().CurrentTransactions() - self.ethereum.EventMux().Post(chain.NewBlockEvent{self.block}) + //chainMan.InsertChain(lchain) + self.eth.EventMux().Post(chain.NewBlockEvent{block}) + self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val}) + + minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) + minerlogger.Infoln(block) } - // Continue mining on the next block - self.startMining() + go self.mine() } } + +func (self *Miner) finiliseTxs() chain.Transactions { + // Sort the transactions by nonce in case of odd network propagation + var txs chain.Transactions + + state := self.eth.BlockManager().TransState() + // XXX This has to change. Coinbase is, for new, same as key. + key := self.eth.KeyManager() + for _, ltx := range self.localTxs { + tx := chain.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.Sign(key.PrivateKey()) + + txs = append(txs, tx) + } + + txs = append(txs, self.eth.TxPool().CurrentTransactions()...) + sort.Sort(chain.TxByNonce{txs}) + + return txs +} -- cgit v1.2.3 From cbeebcd47da846e1b8990313f1ff1ffe7d0bf00f Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 10 Nov 2014 01:17:31 +0100 Subject: Fixed bloom, updated mining & block processing * Reverted back to process blocks in batches method * Bloom generation and lookup fix * Minor UI changed (mainly debug) --- miner/miner.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 2ab74e516..a678a6895 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -1,3 +1,26 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with go-ethereum. If not, see . +*/ +/** + * @authors + * Jeffrey Wilcke + * @date 2014 + * + */ + package miner import ( @@ -190,12 +213,12 @@ func (self *Miner) mine() { if nonce != nil { block.Nonce = nonce lchain := chain.NewChain(chain.Blocks{block}) - _, err := chainMan.TestChain(lchain, true) + _, err := chainMan.TestChain(lchain) if err != nil { minerlogger.Infoln(err) } else { - //chainMan.InsertChain(lchain) - self.eth.EventMux().Post(chain.NewBlockEvent{block}) + chainMan.InsertChain(lchain) + //self.eth.EventMux().Post(chain.NewBlockEvent{block}) self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val}) minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) -- cgit v1.2.3 From a1b6a9ac29d0aa8d29a2c0535bafdb5fe4d4830b Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 18 Nov 2014 16:58:22 +0100 Subject: Begin of moving objects to types package * Block(s) * Transaction(s) --- miner/miner.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index a678a6895..b25e25357 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/wire" @@ -44,7 +45,7 @@ type LocalTx struct { Value string `json:"value"` } -func (self *LocalTx) Sign(key []byte) *chain.Transaction { +func (self *LocalTx) Sign(key []byte) *types.Transaction { return nil } @@ -54,7 +55,7 @@ type Miner struct { eth *eth.Ethereum events event.Subscription - uncles chain.Blocks + uncles types.Blocks localTxs map[int]*LocalTx localTxId int @@ -212,7 +213,7 @@ func (self *Miner) mine() { nonce := self.pow.Search(block, self.powQuitCh) if nonce != nil { block.Nonce = nonce - lchain := chain.NewChain(chain.Blocks{block}) + lchain := chain.NewChain(types.Blocks{block}) _, err := chainMan.TestChain(lchain) if err != nil { minerlogger.Infoln(err) @@ -229,15 +230,15 @@ func (self *Miner) mine() { } } -func (self *Miner) finiliseTxs() chain.Transactions { +func (self *Miner) finiliseTxs() types.Transactions { // Sort the transactions by nonce in case of odd network propagation - var txs chain.Transactions + var txs types.Transactions state := self.eth.BlockManager().TransState() // XXX This has to change. Coinbase is, for new, same as key. key := self.eth.KeyManager() for _, ltx := range self.localTxs { - tx := chain.NewTransactionMessage(ltx.To, ethutil.Big(ltx.Value), ethutil.Big(ltx.Gas), ethutil.Big(ltx.GasPrice), ltx.Data) + 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) @@ -247,7 +248,7 @@ func (self *Miner) finiliseTxs() chain.Transactions { } txs = append(txs, self.eth.TxPool().CurrentTransactions()...) - sort.Sort(chain.TxByNonce{txs}) + sort.Sort(types.TxByNonce{txs}) return txs } -- cgit v1.2.3 From f8d0cd9906a1ec4a4a1e95868a279312363f8b49 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 18 Nov 2014 19:44:17 +0100 Subject: Added a callback mechanism to chain adding. Not sure if this is the right approach. Why? BlockChain shouldn't need the "Ethereum" object. BlockChain shouldn't need to worry about notifying listeners or message propagation. --- miner/miner.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index b25e25357..795385424 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain/types" @@ -218,8 +219,10 @@ func (self *Miner) mine() { if err != nil { minerlogger.Infoln(err) } else { - chainMan.InsertChain(lchain) - //self.eth.EventMux().Post(chain.NewBlockEvent{block}) + 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}) minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) -- cgit v1.2.3 From 61556ef01de20a23951690376662ca58b345eca4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 28 Nov 2014 20:47:24 +0100 Subject: GasData changes & removed min gas price --- miner/miner.go | 1 - 1 file changed, 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index a678a6895..e3435f5a6 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -177,7 +177,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 { -- cgit v1.2.3 From c8d0f8adc5145f650ced3ad5c8c008eb4b4094e2 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 28 Nov 2014 21:20:32 +0100 Subject: Changed refund --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index e3435f5a6..9152d532b 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -203,7 +203,7 @@ func (self *Miner) mine() { // Accumulate the rewards included for this block blockManager.AccumelateRewards(block.State(), block, parent) - block.State().Update() + block.State().Update(nil) minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) -- cgit v1.2.3 From 6dc46d3341dc5fa25bd005f9606de258874139be Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 1 Dec 2014 20:18:09 +0100 Subject: Changed the way transactions are being added to the transaction pool --- miner/miner.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 9152d532b..6fe5a18ac 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -203,7 +203,7 @@ func (self *Miner) mine() { // Accumulate the rewards included for this block blockManager.AccumelateRewards(block.State(), block, parent) - block.State().Update(nil) + block.State().Update(ethutil.Big0) minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) @@ -211,12 +211,13 @@ func (self *Miner) mine() { nonce := self.pow.Search(block, self.powQuitCh) if nonce != nil { block.Nonce = nonce - lchain := chain.NewChain(chain.Blocks{block}) - _, err := chainMan.TestChain(lchain) + //lchain := chain.NewChain(chain.Blocks{block}) + //_, err := chainMan.TestChain(lchain) + err := chainMan.InsertChain(chain.Blocks{block}) if err != nil { minerlogger.Infoln(err) } else { - chainMan.InsertChain(lchain) + //chainMan.InsertChain(lchain) //self.eth.EventMux().Post(chain.NewBlockEvent{block}) self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val}) -- cgit v1.2.3 From 9008b155d3c8d2a32c4c8945f1174243d48d4e90 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 10:28:02 +0100 Subject: Renamed `chain` => `core` --- miner/miner.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index c39de18e3..589144c0c 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -30,8 +30,8 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/ethutil" - "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" @@ -59,7 +59,7 @@ type Miner struct { localTxs map[int]*LocalTx localTxId int - pow chain.PoW + pow core.PoW quitCh chan struct{} powQuitCh chan struct{} @@ -74,7 +74,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { return &Miner{ eth: eth, powQuitCh: make(chan struct{}), - pow: &chain.EasyPow{}, + pow: &core.EasyPow{}, mining: false, localTxs: make(map[int]*LocalTx), MinAcceptedGasPrice: big.NewInt(10000000000000), @@ -82,7 +82,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { } } -func (self *Miner) GetPow() chain.PoW { +func (self *Miner) GetPow() core.PoW { return self.pow } @@ -116,7 +116,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() @@ -147,7 +147,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() @@ -156,7 +156,7 @@ out: } else if true { // do uncle stuff } - case chain.TxPreEvent, *LocalTx: + case core.TxPreEvent, *LocalTx: self.reset() go self.mine() } -- cgit v1.2.3 From a5b27bbc10d6a145152fc2629043c46ef4a9ca71 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 16:44:43 +0100 Subject: Improved and simplified wallet functions and behaviour --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 589144c0c..f9b8a9c22 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -192,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) } -- cgit v1.2.3 From 8dbca75d85553f2d9451ee563a919850f05ea1dd Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 23:54:07 +0100 Subject: Skip mining on transactions that don't meet the min accepted gas price --- miner/miner.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index f9b8a9c22..c350eb1a8 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -228,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() // 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.Sign(key.PrivateKey()) - txs = append(txs, tx) + txs[i] = tx } - txs = append(txs, self.eth.TxPool().CurrentTransactions()...) - sort.Sort(types.TxByNonce{txs}) + // Faster than append + for _, tx := range self.eth.TxPool().CurrentTransactions() { + if tx.GasPrice.Cmp(self.MinAcceptedGasPrice) >= 0 { + txs[actualSize] = tx + actualSize++ + } + } + + newTransactions := make(types.Transactions, actualSize) + copy(newTransactions, txs[:actualSize]) + sort.Sort(types.TxByNonce{newTransactions}) - return txs + return newTransactions } -- cgit v1.2.3 From 9925916851c00323336e213fc18c83da5fceee94 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 5 Dec 2014 16:26:39 +0100 Subject: upped proto version and modified block pool --- miner/miner.go | 1 - 1 file changed, 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index c350eb1a8..4f677cbef 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -167,7 +167,6 @@ out: } func (self *Miner) reset() { - println("reset") close(self.powQuitCh) self.powQuitCh = make(chan struct{}) } -- cgit v1.2.3 From 1b98cbbfa4f587107fa15fccde7d22102ea4b1c0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Dec 2014 16:45:16 +0100 Subject: Moved pow --- miner/miner.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 4f677cbef..82a92cc20 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -29,6 +29,8 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -59,7 +61,7 @@ type Miner struct { localTxs map[int]*LocalTx localTxId int - pow core.PoW + pow pow.PoW quitCh chan struct{} powQuitCh chan struct{} @@ -74,7 +76,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { return &Miner{ eth: eth, powQuitCh: make(chan struct{}), - pow: &core.EasyPow{}, + pow: ezp.New(), mining: false, localTxs: make(map[int]*LocalTx), MinAcceptedGasPrice: big.NewInt(10000000000000), @@ -82,7 +84,7 @@ func New(coinbase []byte, eth *eth.Ethereum) *Miner { } } -func (self *Miner) GetPow() core.PoW { +func (self *Miner) GetPow() pow.PoW { return self.pow } -- cgit v1.2.3 From 5553e5aaed5c3f4e303b7d6671d2c92a45aa487e Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Dec 2014 19:59:12 +0100 Subject: states moved to chain --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 82a92cc20..dc69dddc0 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -232,7 +232,7 @@ func (self *Miner) finiliseTxs() 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 i, ltx := range self.localTxs { -- cgit v1.2.3 From 118862f1ba058fa90414ab9ec6deb1ab5aac6590 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 14 Dec 2014 18:10:48 +0000 Subject: adapt miner to new backend. use events to broadcast new mined blocks --- miner/miner.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index dc69dddc0..6ba3b1eba 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -27,7 +27,7 @@ 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/pow" "github.com/ethereum/go-ethereum/pow/ezp" @@ -36,7 +36,6 @@ import ( "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 { @@ -217,7 +216,7 @@ func (self *Miner) mine() { if err != nil { minerlogger.Infoln(err) } else { - 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) @@ -246,7 +245,7 @@ func (self *Miner) finiliseTxs() types.Transactions { } // Faster than append - for _, tx := range self.eth.TxPool().CurrentTransactions() { + for _, tx := range self.eth.TxPool().GetTransactions() { if tx.GasPrice.Cmp(self.MinAcceptedGasPrice) >= 0 { txs[actualSize] = tx actualSize++ -- cgit v1.2.3 From db494170dc819b1eb0d267b6e1ab36c6cfb63569 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 15:18:13 +0100 Subject: Created generic message (easy for testing) --- miner/miner.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index dc69dddc0..f63096b63 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -237,8 +237,8 @@ func (self *Miner) finiliseTxs() types.Transactions { key := self.eth.KeyManager() for i, ltx := range self.localTxs { tx := types.NewTransactionMessage(ltx.To, ethutil.Big(ltx.Value), ethutil.Big(ltx.Gas), ethutil.Big(ltx.GasPrice), ltx.Data) - tx.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()) @@ -247,7 +247,7 @@ func (self *Miner) finiliseTxs() types.Transactions { // Faster than append for _, tx := range self.eth.TxPool().CurrentTransactions() { - if tx.GasPrice.Cmp(self.MinAcceptedGasPrice) >= 0 { + if tx.GasPrice().Cmp(self.MinAcceptedGasPrice) >= 0 { txs[actualSize] = tx actualSize++ } -- cgit v1.2.3 From 4cd79d8ddd7608d60344b13fe4bda7315429d1d9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 23 Dec 2014 13:48:44 +0100 Subject: Refactored block & Transaction * Includes new rlp decoder --- miner/miner.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index d909c228b..c9a6922bb 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -56,7 +56,7 @@ type Miner struct { eth *eth.Ethereum events event.Subscription - uncles types.Blocks + uncles []*types.Header localTxs map[int]*LocalTx localTxId int @@ -184,9 +184,9 @@ func (self *Miner) mine() { block.SetUncles(self.uncles) } - parent := chainMan.GetBlock(block.PrevHash) - coinbase := block.State().GetOrNewStateObject(block.Coinbase) - coinbase.SetGasPool(block.CalcGasLimit(parent)) + parent := chainMan.GetBlock(block.ParentHash()) + coinbase := block.State().GetOrNewStateObject(block.Coinbase()) + coinbase.SetGasPool(core.CalcGasLimit(parent, block)) transactions := self.finiliseTxs() @@ -211,7 +211,7 @@ func (self *Miner) mine() { // Find a valid nonce nonce := self.pow.Search(block, self.powQuitCh) if nonce != nil { - block.Nonce = nonce + block.Header().Nonce = nonce err := chainMan.InsertChain(types.Blocks{block}) if err != nil { minerlogger.Infoln(err) -- cgit v1.2.3 From ce68ac695981523aca5cf83e051597f46070547d Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 30 Dec 2014 13:18:19 +0100 Subject: Updated miner to new block api --- miner/miner.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index c9a6922bb..aefcadab8 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -190,9 +190,11 @@ func (self *Miner) mine() { transactions := self.finiliseTxs() + state := block.State() + // 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.ApplyTransactions(coinbase, block.State(), block, transactions, true) + receipts, txs, _, erroneous, err := blockManager.ApplyTransactions(coinbase, state, block, transactions, true) if err != nil { minerlogger.Debugln(err) } @@ -202,9 +204,10 @@ func (self *Miner) mine() { block.SetReceipts(receipts) // Accumulate the rewards included for this block - blockManager.AccumelateRewards(block.State(), block, parent) + blockManager.AccumelateRewards(state, block, parent) - block.State().Update(ethutil.Big0) + state.Update(ethutil.Big0) + block.SetRoot(state.Root()) minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) -- cgit v1.2.3 From c1dee151445d1d9700e0c623916299370868490c Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 5 Jan 2015 00:18:44 +0100 Subject: BlockManager => BlockProcessor --- miner/miner.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index aefcadab8..949227d98 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -174,9 +174,9 @@ func (self *Miner) reset() { func (self *Miner) mine() { var ( - blockManager = self.eth.BlockManager() - chainMan = self.eth.ChainManager() - block = chainMan.NewBlock(self.Coinbase) + blockProcessor = self.eth.BlockProcessor() + chainMan = self.eth.ChainManager() + block = chainMan.NewBlock(self.Coinbase) ) // Apply uncles @@ -194,7 +194,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.ApplyTransactions(coinbase, state, block, transactions, true) + receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true) if err != nil { minerlogger.Debugln(err) } @@ -204,7 +204,7 @@ func (self *Miner) mine() { block.SetReceipts(receipts) // Accumulate the rewards included for this block - blockManager.AccumelateRewards(state, block, parent) + blockProcessor.AccumelateRewards(state, block, parent) state.Update(ethutil.Big0) block.SetRoot(state.Root()) -- cgit v1.2.3 From 47e6b2cef8ee0164e82d119c6a9359b55259d645 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 6 Jan 2015 00:19:07 +0100 Subject: Allow extra to be set for mined blocks --- miner/miner.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 949227d98..f80ae51c6 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -69,6 +69,7 @@ type Miner struct { mining bool MinAcceptedGasPrice *big.Int + Extra string } func New(coinbase []byte, eth *eth.Ethereum) *Miner { @@ -178,6 +179,7 @@ func (self *Miner) mine() { chainMan = self.eth.ChainManager() block = chainMan.NewBlock(self.Coinbase) ) + block.Header().Extra = self.Extra // Apply uncles if len(self.uncles) > 0 { -- cgit v1.2.3 From fed3e6a808921fb8274b50043c5c39a24a1bbccf Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 7 Jan 2015 13:17:48 +0100 Subject: Refactored ethutil.Config.Db out --- miner/miner.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index f80ae51c6..52dd5687d 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow/ezp" + "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -178,6 +179,7 @@ func (self *Miner) mine() { blockProcessor = self.eth.BlockProcessor() chainMan = self.eth.ChainManager() block = chainMan.NewBlock(self.Coinbase) + state = state.New(block.Root(), self.eth.Db()) ) block.Header().Extra = self.Extra @@ -187,13 +189,11 @@ func (self *Miner) mine() { } parent := chainMan.GetBlock(block.ParentHash()) - coinbase := block.State().GetOrNewStateObject(block.Coinbase()) + coinbase := state.GetOrNewStateObject(block.Coinbase()) coinbase.SetGasPool(core.CalcGasLimit(parent, block)) transactions := self.finiliseTxs() - state := block.State() - // Accumulate all valid transactions and apply them to the new state // Error may be ignored. It's not important during mining receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true) -- cgit v1.2.3