From 13ddf20bd20707141309fbfa969183a7afbe5a80 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 9 May 2015 12:04:00 +0200 Subject: miner, cmd/geth: settable gas price from flags and console * --gasprice "" flag * admin.miner.setGasPrice( ) --- miner/miner.go | 4 ++++ miner/worker.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index bff0026dc..52b9f2a69 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -37,6 +37,10 @@ func (self *Miner) Mining() bool { return self.mining } +func (m *Miner) SetGasPrice(price *big.Int) { + m.worker.gasPrice = price +} + func (self *Miner) Start(coinbase common.Address) { self.mining = true self.worker.coinbase = coinbase diff --git a/miner/worker.go b/miner/worker.go index 87d17dfd6..bb8b70c86 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -72,6 +72,7 @@ type worker struct { proc *core.BlockProcessor coinbase common.Address + gasPrice *big.Int extra []byte currentMu sync.Mutex @@ -93,6 +94,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { eth: eth, mux: eth.EventMux(), recv: make(chan *types.Block), + gasPrice: new(big.Int), chain: eth.ChainManager(), proc: eth.BlockProcessor(), possibleUncles: make(map[common.Hash]*types.Block), @@ -239,6 +241,19 @@ func (self *worker) makeCurrent() { self.current.coinbase.SetGasPool(core.CalcGasLimit(parent)) } +func (w *worker) setGasPrice(p *big.Int) { + w.mu.Lock() + defer w.mu.Unlock() + w.gasPrice = p +} + +func (w *worker) gasprice(pct int64) *big.Int { + p := new(big.Int).Set(w.gasPrice) + p.Div(p, big.NewInt(100)) + p.Mul(p, big.NewInt(pct)) + return p +} + func (self *worker) commitNewWork() { self.mu.Lock() defer self.mu.Unlock() @@ -259,9 +274,22 @@ func (self *worker) commitNewWork() { ignoredTransactors = set.New() ) + var pct int64 = 90 + minprice := self.gasprice(pct) for _, tx := range transactions { // We can skip err. It has already been validated in the tx pool from, _ := tx.From() + + // check if it falls within margin + if tx.GasPrice().Cmp(minprice) < 0 { + // ignore the transaction and transactor. We ignore the transactor + // because nonce will fail after ignoring this transaction so there's + // no point + ignoredTransactors.Add(from) + glog.V(logger.Info).Infof("transaction(%x) below gas price (<%d%% ask price). All sequential txs from this address(%x) will fail\n", tx.Hash().Bytes()[:4], pct, from[:4]) + continue + } + // Move on to the next transaction when the transactor is in ignored transactions set // This may occur when a transaction hits the gas limit. When a gas limit is hit and // the transaction is processed (that could potentially be included in the block) it -- cgit v1.2.3 From a7705fc2037836f037de5e5ea5aef8475424a1be Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 9 May 2015 12:13:46 +0200 Subject: miner: moved gasprice to non-method --- miner/worker.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index bb8b70c86..7adf298ed 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -247,13 +247,6 @@ func (w *worker) setGasPrice(p *big.Int) { w.gasPrice = p } -func (w *worker) gasprice(pct int64) *big.Int { - p := new(big.Int).Set(w.gasPrice) - p.Div(p, big.NewInt(100)) - p.Mul(p, big.NewInt(pct)) - return p -} - func (self *worker) commitNewWork() { self.mu.Lock() defer self.mu.Unlock() @@ -274,8 +267,9 @@ func (self *worker) commitNewWork() { ignoredTransactors = set.New() ) - var pct int64 = 90 - minprice := self.gasprice(pct) + const pct = int64(90) + // calculate the minimal gas price the miner accepts when sorting out transactions. + minprice := gasprice(self.gasPrice, pct) for _, tx := range transactions { // We can skip err. It has already been validated in the tx pool from, _ := tx.From() @@ -411,3 +405,12 @@ func (self *worker) HashRate() int64 { return tot } + +// gasprice calculates a reduced gas price based on the pct +// XXX Use big.Rat? +func gasprice(price *big.Int, pct int64) *big.Int { + p := new(big.Int).Set(price) + p.Div(p, big.NewInt(100)) + p.Mul(p, big.NewInt(pct)) + return p +} -- cgit v1.2.3 From e3a08875f6435b1674e62bd1d923941ec2092b69 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 9 May 2015 12:32:36 +0200 Subject: miner: start/stop mutex locked. Closes #887 --- miner/worker.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index 7adf298ed..22493c235 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -125,6 +125,9 @@ func (self *worker) pendingBlock() *types.Block { } func (self *worker) start() { + self.mu.Lock() + defer self.mu.Unlock() + // spin up agents for _, agent := range self.agents { agent.Start() @@ -134,6 +137,9 @@ func (self *worker) start() { } func (self *worker) stop() { + self.mu.Lock() + defer self.mu.Unlock() + if atomic.LoadInt32(&self.mining) == 1 { // stop all agents for _, agent := range self.agents { @@ -146,6 +152,9 @@ func (self *worker) stop() { } func (self *worker) register(agent Agent) { + self.mu.Lock() + defer self.mu.Unlock() + self.agents = append(self.agents, agent) agent.SetReturnCh(self.recv) } -- cgit v1.2.3 From 7eed47fad5ac971895a3853c83a22dff652dac02 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 9 May 2015 12:51:40 +0200 Subject: miner, tests: fixed block test --- miner/miner.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'miner') diff --git a/miner/miner.go b/miner/miner.go index 52b9f2a69..d5ea9a146 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -38,6 +38,11 @@ func (self *Miner) Mining() bool { } func (m *Miner) SetGasPrice(price *big.Int) { + // FIXME block tests set a nil gas price. Quick dirty fix + if price == nil { + return + } + m.worker.gasPrice = price } -- cgit v1.2.3