From e870e61bc95cf40ca9956b2eb887976ef60dfd9c Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Sat, 11 Jul 2015 20:45:59 +0200 Subject: miner: smart mining Work is now handled and carried over multiple sessions. Previously one session only was assumed, potentially resulting in invalid (outdated) work * Larger work / result queue * Full validation option --- miner/worker.go | 134 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 74 insertions(+), 60 deletions(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 9f804bf30..b93444b1e 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -38,25 +38,20 @@ import ( var jsonlogger = logger.NewJsonLogger() -// Work holds the current work -type Work struct { - Number uint64 - Nonce uint64 - MixDigest []byte - SeedHash []byte -} +const ( + resultQueueSize = 10 + miningLogAtDepth = 5 +) // Agent can register themself with the worker type Agent interface { - Work() chan<- *types.Block - SetReturnCh(chan<- *types.Block) + Work() chan<- *Work + SetReturnCh(chan<- *Result) Stop() Start() GetHashRate() int64 } -const miningLogAtDepth = 5 - type uint64RingBuffer struct { ints []uint64 //array of all integers in buffer next int //where is the next insertion? assert 0 <= next < len(ints) @@ -64,7 +59,7 @@ type uint64RingBuffer struct { // environment is the workers current environment and holds // all of the current state information -type environment struct { +type Work struct { state *state.StateDB // apply state changes here coinbase *state.StateObject // the miner's account ancestors *set.Set // ancestor set (used for checking uncle parent validity) @@ -78,11 +73,18 @@ type environment struct { lowGasTxs types.Transactions localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion) - block *types.Block // the new block + Block *types.Block // the new block header *types.Header txs []*types.Transaction receipts []*types.Receipt + + createdAt time.Time +} + +type Result struct { + Work *Work + Block *types.Block } // worker is the main object which takes care of applying messages to the new state @@ -90,7 +92,7 @@ type worker struct { mu sync.Mutex agents []Agent - recv chan *types.Block + recv chan *Result mux *event.TypeMux quit chan struct{} pow pow.PoW @@ -105,7 +107,7 @@ type worker struct { extra []byte currentMu sync.Mutex - current *environment + current *Work uncleMu sync.Mutex possibleUncles map[common.Hash]*types.Block @@ -116,6 +118,8 @@ type worker struct { // atomic status counters mining int32 atWork int32 + + fullValidation bool } func newWorker(coinbase common.Address, eth core.Backend) *worker { @@ -123,7 +127,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { eth: eth, mux: eth.EventMux(), extraDb: eth.ExtraDb(), - recv: make(chan *types.Block), + recv: make(chan *Result, resultQueueSize), gasPrice: new(big.Int), chain: eth.ChainManager(), proc: eth.BlockProcessor(), @@ -131,6 +135,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { coinbase: coinbase, txQueue: make(map[common.Hash]*types.Transaction), quit: make(chan struct{}), + fullValidation: false, } go worker.update() go worker.wait() @@ -163,7 +168,7 @@ func (self *worker) pendingBlock() *types.Block { self.current.receipts, ) } - return self.current.block + return self.current.Block } func (self *worker) start() { @@ -250,34 +255,53 @@ func newLocalMinedBlock(blockNumber uint64, prevMinedBlocks *uint64RingBuffer) ( func (self *worker) wait() { for { - for block := range self.recv { + for result := range self.recv { atomic.AddInt32(&self.atWork, -1) - if block == nil { + if result == nil { continue } + block := result.Block - parent := self.chain.GetBlock(block.ParentHash()) - if parent == nil { - glog.V(logger.Error).Infoln("Invalid block found during mining") - continue - } - if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true); err != nil && err != core.BlockFutureErr { - glog.V(logger.Error).Infoln("Invalid header on mined block:", err) - continue - } + if self.fullValidation { + if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil { + glog.V(logger.Error).Infoln("mining err", err) + continue + } + go self.mux.Post(core.NewMinedBlockEvent{block}) + } else { + parent := self.chain.GetBlock(block.ParentHash()) + if parent == nil { + glog.V(logger.Error).Infoln("Invalid block found during mining") + continue + } + if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true); err != nil && err != core.BlockFutureErr { + glog.V(logger.Error).Infoln("Invalid header on mined block:", err) + continue + } - stat, err := self.chain.WriteBlock(block, false) - if err != nil { - glog.V(logger.Error).Infoln("error writing block to chain", err) - continue - } - // check if canon block and write transactions - if stat == core.CanonStatTy { - // This puts transactions in a extra db for rpc - core.PutTransactions(self.extraDb, block, block.Transactions()) - // store the receipts - core.PutReceipts(self.extraDb, self.current.receipts) + stat, err := self.chain.WriteBlock(block, false) + if err != nil { + glog.V(logger.Error).Infoln("error writing block to chain", err) + continue + } + // check if canon block and write transactions + if stat == core.CanonStatTy { + // This puts transactions in a extra db for rpc + core.PutTransactions(self.extraDb, block, block.Transactions()) + // store the receipts + core.PutReceipts(self.extraDb, self.current.receipts) + } + + // broadcast before waiting for validation + go func(block *types.Block, logs state.Logs) { + self.mux.Post(core.NewMinedBlockEvent{block}) + self.mux.Post(core.ChainEvent{block, block.Hash(), logs}) + if stat == core.CanonStatTy { + self.mux.Post(core.ChainHeadEvent{block}) + self.mux.Post(logs) + } + }(block, self.current.state.Logs()) } // check staleness and display confirmation @@ -289,19 +313,8 @@ func (self *worker) wait() { confirm = "Wait 5 blocks for confirmation" self.current.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), self.current.localMinedBlocks) } - glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) - // broadcast before waiting for validation - go func(block *types.Block, logs state.Logs) { - self.mux.Post(core.NewMinedBlockEvent{block}) - self.mux.Post(core.ChainEvent{block, block.Hash(), logs}) - if stat == core.CanonStatTy { - self.mux.Post(core.ChainHeadEvent{block}) - self.mux.Post(logs) - } - }(block, self.current.state.Logs()) - self.commitNewWork() } } @@ -320,7 +333,7 @@ func (self *worker) push() { atomic.AddInt32(&self.atWork, 1) if agent.Work() != nil { - agent.Work() <- self.current.block + agent.Work() <- self.current } } } @@ -329,13 +342,14 @@ func (self *worker) push() { // makeCurrent creates a new environment for the current cycle. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) { state := state.New(parent.Root(), self.eth.StateDb()) - current := &environment{ + current := &Work{ state: state, ancestors: set.New(), family: set.New(), uncles: set.New(), header: header, coinbase: state.GetOrNewStateObject(self.coinbase), + createdAt: time.Now(), } // when 08 is processed ancestors contain 07 (quick block) @@ -391,10 +405,10 @@ func (self *worker) isBlockLocallyMined(deepBlockNum uint64) bool { return block != nil && block.Coinbase() == self.coinbase } -func (self *worker) logLocalMinedBlocks(previous *environment) { +func (self *worker) logLocalMinedBlocks(previous *Work) { if previous != nil && self.current.localMinedBlocks != nil { - nextBlockNum := self.current.block.NumberU64() - for checkBlockNum := previous.block.NumberU64(); checkBlockNum < nextBlockNum; checkBlockNum++ { + nextBlockNum := self.current.Block.NumberU64() + for checkBlockNum := previous.Block.NumberU64(); checkBlockNum < nextBlockNum; checkBlockNum++ { inspectBlockNum := checkBlockNum - miningLogAtDepth if self.isBlockLocallyMined(inspectBlockNum) { glog.V(logger.Info).Infof("🔨 🔗 Mined %d blocks back: block #%v", miningLogAtDepth, inspectBlockNum) @@ -480,12 +494,12 @@ func (self *worker) commitNewWork() { } // create the new block whose nonce will be mined. - current.block = types.NewBlock(header, current.txs, uncles, current.receipts) - self.current.block.Td = new(big.Int).Set(core.CalcTD(self.current.block, self.chain.GetBlock(self.current.block.ParentHash()))) + current.Block = types.NewBlock(header, current.txs, uncles, current.receipts) + self.current.Block.Td = new(big.Int).Set(core.CalcTD(self.current.Block, self.chain.GetBlock(self.current.Block.ParentHash()))) // We only care about logging if we're actually mining. if atomic.LoadInt32(&self.mining) == 1 { - glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", current.block.Number(), current.tcount, len(uncles), time.Since(tstart)) + glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", current.Block.Number(), current.tcount, len(uncles), time.Since(tstart)) self.logLocalMinedBlocks(previous) } @@ -507,7 +521,7 @@ func (self *worker) commitUncle(uncle *types.Header) error { return nil } -func (env *environment) commitTransactions(transactions types.Transactions, gasPrice *big.Int, proc *core.BlockProcessor) { +func (env *Work) commitTransactions(transactions types.Transactions, gasPrice *big.Int, proc *core.BlockProcessor) { for _, tx := range transactions { // We can skip err. It has already been validated in the tx pool from, _ := tx.From() @@ -565,7 +579,7 @@ func (env *environment) commitTransactions(transactions types.Transactions, gasP } } -func (env *environment) commitTransaction(tx *types.Transaction, proc *core.BlockProcessor) error { +func (env *Work) commitTransaction(tx *types.Transaction, proc *core.BlockProcessor) error { snap := env.state.Copy() receipt, _, err := proc.ApplyTransaction(env.coinbase, env.state, env.header, tx, env.header.GasUsed, true) if err != nil { -- cgit v1.2.3 From 1b9ef0e52dea6b02ed070092de31422c4b67fe45 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 16 Jul 2015 14:19:27 +0200 Subject: miner: moved state sync Moved the state sync so it only syncs the state when the block mining yield a possitive result --- miner/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index b93444b1e..90ef79e3f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -263,6 +263,7 @@ func (self *worker) wait() { } block := result.Block + self.current.state.Sync() if self.fullValidation { if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil { glog.V(logger.Error).Infoln("mining err", err) @@ -489,7 +490,6 @@ func (self *worker) commitNewWork() { // commit state root after all state transitions. core.AccumulateRewards(self.current.state, header, uncles) current.state.SyncObjects() - self.current.state.Sync() header.Root = current.state.Root() } -- cgit v1.2.3 From 30293497617b19a2f55633ac0680fbe2234607e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 20 Jul 2015 14:37:43 +0300 Subject: miner: fix current work data race --- miner/worker.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 90ef79e3f..a4ebe6fe7 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -160,6 +160,7 @@ func (self *worker) pendingState() *state.StateDB { func (self *worker) pendingBlock() *types.Block { self.currentMu.Lock() defer self.currentMu.Unlock() + if atomic.LoadInt32(&self.mining) == 0 { return types.NewBlock( self.current.header, @@ -228,9 +229,9 @@ out: case core.TxPreEvent: // Apply transaction to the pending state if we're not mining if atomic.LoadInt32(&self.mining) == 0 { - self.mu.Lock() + self.currentMu.Lock() self.current.commitTransactions(types.Transactions{ev.Tx}, self.gasPrice, self.proc) - self.mu.Unlock() + self.currentMu.Unlock() } } case <-self.quit: -- cgit v1.2.3 From 3f047be5aa93b6222506445414ca909dd59c7eeb Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 22 Jul 2015 18:48:40 +0200 Subject: all: update license headers to distiguish GPL/LGPL All code outside of cmd/ is licensed as LGPL. The headers now reflect this by calling the whole work "the go-ethereum library". --- miner/worker.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index a4ebe6fe7..5d548584c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1,18 +1,18 @@ // Copyright 2015 The go-ethereum Authors -// This file is part of go-ethereum. +// This file is part of the go-ethereum library. // // 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, +// The go-ethereum library 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 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser 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 . +// along with the go-ethereum library. If not, see . package miner -- cgit v1.2.3 From bfbcfbe4a9dd9125391b56d6a13158cc5ec71c89 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Jul 2015 18:35:11 +0200 Subject: all: fix license headers one more time I forgot to update one instance of "go-ethereum" in commit 3f047be5a. --- miner/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 5d548584c..7fb40af78 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1,7 +1,7 @@ // Copyright 2015 The go-ethereum Authors // This file is part of the go-ethereum library. // -// go-ethereum is free software: you can redistribute it and/or modify +// The go-ethereum library 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. -- cgit v1.2.3 From d8590d97902ff9c9ddc4ce56856f64b417b64489 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 4 Aug 2015 20:38:57 +0200 Subject: miner: fixed worker race condition --- miner/worker.go | 87 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 43 deletions(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 7fb40af78..a703baada 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -263,8 +263,9 @@ func (self *worker) wait() { continue } block := result.Block + work := result.Work - self.current.state.Sync() + work.state.Sync() if self.fullValidation { if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil { glog.V(logger.Error).Infoln("mining err", err) @@ -292,7 +293,7 @@ func (self *worker) wait() { // This puts transactions in a extra db for rpc core.PutTransactions(self.extraDb, block, block.Transactions()) // store the receipts - core.PutReceipts(self.extraDb, self.current.receipts) + core.PutReceipts(self.extraDb, work.receipts) } // broadcast before waiting for validation @@ -303,7 +304,7 @@ func (self *worker) wait() { self.mux.Post(core.ChainHeadEvent{block}) self.mux.Post(logs) } - }(block, self.current.state.Logs()) + }(block, work.state.Logs()) } // check staleness and display confirmation @@ -313,7 +314,7 @@ func (self *worker) wait() { stale = "stale " } else { confirm = "Wait 5 blocks for confirmation" - self.current.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), self.current.localMinedBlocks) + work.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), work.localMinedBlocks) } glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) @@ -322,9 +323,9 @@ func (self *worker) wait() { } } -func (self *worker) push() { +func (self *worker) push(work *Work) { if atomic.LoadInt32(&self.mining) == 1 { - if core.Canary(self.current.state) { + if core.Canary(work.state) { glog.Infoln("Toxicity levels rising to deadly levels. Your canary has died. You can go back or continue down the mineshaft --more--") glog.Infoln("You turn back and abort mining") return @@ -335,7 +336,7 @@ func (self *worker) push() { atomic.AddInt32(&self.atWork, 1) if agent.Work() != nil { - agent.Work() <- self.current + agent.Work() <- work } } } @@ -344,7 +345,7 @@ func (self *worker) push() { // makeCurrent creates a new environment for the current cycle. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) { state := state.New(parent.Root(), self.eth.StateDb()) - current := &Work{ + work := &Work{ state: state, ancestors: set.New(), family: set.New(), @@ -357,23 +358,23 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) { // when 08 is processed ancestors contain 07 (quick block) for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) { for _, uncle := range ancestor.Uncles() { - current.family.Add(uncle.Hash()) + work.family.Add(uncle.Hash()) } - current.family.Add(ancestor.Hash()) - current.ancestors.Add(ancestor.Hash()) + work.family.Add(ancestor.Hash()) + work.ancestors.Add(ancestor.Hash()) } accounts, _ := self.eth.AccountManager().Accounts() // Keep track of transactions which return errors so they can be removed - current.remove = set.New() - current.tcount = 0 - current.ignoredTransactors = set.New() - current.lowGasTransactors = set.New() - current.ownedAccounts = accountAddressesSet(accounts) + work.remove = set.New() + work.tcount = 0 + work.ignoredTransactors = set.New() + work.lowGasTransactors = set.New() + work.ownedAccounts = accountAddressesSet(accounts) if self.current != nil { - current.localMinedBlocks = self.current.localMinedBlocks + work.localMinedBlocks = self.current.localMinedBlocks } - self.current = current + self.current = work } func (w *worker) setGasPrice(p *big.Int) { @@ -387,13 +388,13 @@ func (w *worker) setGasPrice(p *big.Int) { w.mux.Post(core.GasPriceChanged{w.gasPrice}) } -func (self *worker) isBlockLocallyMined(deepBlockNum uint64) bool { +func (self *worker) isBlockLocallyMined(current *Work, deepBlockNum uint64) bool { //Did this instance mine a block at {deepBlockNum} ? var isLocal = false - for idx, blockNum := range self.current.localMinedBlocks.ints { + for idx, blockNum := range current.localMinedBlocks.ints { if deepBlockNum == blockNum { isLocal = true - self.current.localMinedBlocks.ints[idx] = 0 //prevent showing duplicate logs + current.localMinedBlocks.ints[idx] = 0 //prevent showing duplicate logs break } } @@ -407,12 +408,12 @@ func (self *worker) isBlockLocallyMined(deepBlockNum uint64) bool { return block != nil && block.Coinbase() == self.coinbase } -func (self *worker) logLocalMinedBlocks(previous *Work) { - if previous != nil && self.current.localMinedBlocks != nil { - nextBlockNum := self.current.Block.NumberU64() +func (self *worker) logLocalMinedBlocks(current, previous *Work) { + if previous != nil && current.localMinedBlocks != nil { + nextBlockNum := current.Block.NumberU64() for checkBlockNum := previous.Block.NumberU64(); checkBlockNum < nextBlockNum; checkBlockNum++ { inspectBlockNum := checkBlockNum - miningLogAtDepth - if self.isBlockLocallyMined(inspectBlockNum) { + if self.isBlockLocallyMined(current, inspectBlockNum) { glog.V(logger.Info).Infof("🔨 🔗 Mined %d blocks back: block #%v", miningLogAtDepth, inspectBlockNum) } } @@ -454,14 +455,14 @@ func (self *worker) commitNewWork() { previous := self.current self.makeCurrent(parent, header) - current := self.current + work := self.current // commit transactions for this run. transactions := self.eth.TxPool().GetTransactions() sort.Sort(types.TxByNonce{transactions}) - current.coinbase.SetGasLimit(header.GasLimit) - current.commitTransactions(transactions, self.gasPrice, self.proc) - self.eth.TxPool().RemoveTransactions(current.lowGasTxs) + work.coinbase.SetGasLimit(header.GasLimit) + work.commitTransactions(transactions, self.gasPrice, self.proc) + self.eth.TxPool().RemoveTransactions(work.lowGasTxs) // compute uncles for the new block. var ( @@ -472,7 +473,7 @@ func (self *worker) commitNewWork() { if len(uncles) == 2 { break } - if err := self.commitUncle(uncle.Header()); err != nil { + if err := self.commitUncle(work, uncle.Header()); err != nil { if glog.V(logger.Ridiculousness) { glog.V(logger.Detail).Infof("Bad uncle found and will be removed (%x)\n", hash[:4]) glog.V(logger.Detail).Infoln(uncle) @@ -489,36 +490,36 @@ func (self *worker) commitNewWork() { if atomic.LoadInt32(&self.mining) == 1 { // commit state root after all state transitions. - core.AccumulateRewards(self.current.state, header, uncles) - current.state.SyncObjects() - header.Root = current.state.Root() + core.AccumulateRewards(work.state, header, uncles) + work.state.SyncObjects() + header.Root = work.state.Root() } // create the new block whose nonce will be mined. - current.Block = types.NewBlock(header, current.txs, uncles, current.receipts) - self.current.Block.Td = new(big.Int).Set(core.CalcTD(self.current.Block, self.chain.GetBlock(self.current.Block.ParentHash()))) + work.Block = types.NewBlock(header, work.txs, uncles, work.receipts) + work.Block.Td = new(big.Int).Set(core.CalcTD(work.Block, self.chain.GetBlock(work.Block.ParentHash()))) // We only care about logging if we're actually mining. if atomic.LoadInt32(&self.mining) == 1 { - glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", current.Block.Number(), current.tcount, len(uncles), time.Since(tstart)) - self.logLocalMinedBlocks(previous) + glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles. Took %v\n", work.Block.Number(), work.tcount, len(uncles), time.Since(tstart)) + self.logLocalMinedBlocks(work, previous) } - self.push() + self.push(work) } -func (self *worker) commitUncle(uncle *types.Header) error { +func (self *worker) commitUncle(work *Work, uncle *types.Header) error { hash := uncle.Hash() - if self.current.uncles.Has(hash) { + if work.uncles.Has(hash) { return core.UncleError("Uncle not unique") } - if !self.current.ancestors.Has(uncle.ParentHash) { + if !work.ancestors.Has(uncle.ParentHash) { return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) } - if self.current.family.Has(hash) { + if work.family.Has(hash) { return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", hash)) } - self.current.uncles.Add(uncle.Hash()) + work.uncles.Add(uncle.Hash()) return nil } -- cgit v1.2.3 From 71d32f54f70917c53fd3a691cface3bc73ffa1b7 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 4 Aug 2015 23:46:38 +0200 Subject: core, miner: added difficulty bomb --- miner/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 7fb40af78..d6d96601e 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -444,7 +444,7 @@ func (self *worker) commitNewWork() { header := &types.Header{ ParentHash: parent.Hash(), Number: num.Add(num, common.Big1), - Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Difficulty()), + Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Number(), parent.Difficulty()), GasLimit: core.CalcGasLimit(parent), GasUsed: new(big.Int), Coinbase: self.coinbase, -- cgit v1.2.3 From 35f271b264b8e4a0449eff325fb75a78299cf9a6 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 3 Aug 2015 21:57:09 +0200 Subject: miner, core: sort txs by price, nonce --- miner/worker.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'miner/worker.go') diff --git a/miner/worker.go b/miner/worker.go index 269219ba0..535ce5144 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -457,9 +457,42 @@ func (self *worker) commitNewWork() { self.makeCurrent(parent, header) work := self.current - // commit transactions for this run. + /* //approach 1 transactions := self.eth.TxPool().GetTransactions() sort.Sort(types.TxByNonce{transactions}) + */ + + //approach 2 + transactions := self.eth.TxPool().GetTransactions() + sort.Sort(types.TxByPriceAndNonce{transactions}) + + /* // approach 3 + // commit transactions for this run. + txPerOwner := make(map[common.Address]types.Transactions) + // Sort transactions by owner + for _, tx := range self.eth.TxPool().GetTransactions() { + from, _ := tx.From() // we can ignore the sender error + txPerOwner[from] = append(txPerOwner[from], tx) + } + var ( + singleTxOwner types.Transactions + multiTxOwner types.Transactions + ) + // Categorise transactions by + // 1. 1 owner tx per block + // 2. multi txs owner per block + for _, txs := range txPerOwner { + if len(txs) == 1 { + singleTxOwner = append(singleTxOwner, txs[0]) + } else { + multiTxOwner = append(multiTxOwner, txs...) + } + } + sort.Sort(types.TxByPrice{singleTxOwner}) + sort.Sort(types.TxByNonce{multiTxOwner}) + transactions := append(singleTxOwner, multiTxOwner...) + */ + work.coinbase.SetGasLimit(header.GasLimit) work.commitTransactions(transactions, self.gasPrice, self.proc) self.eth.TxPool().RemoveTransactions(work.lowGasTxs) -- cgit v1.2.3