Skip to content

Commit 13ddf20

Browse files
committed
miner, cmd/geth: settable gas price from flags and console
* --gasprice "<num>" flag * admin.miner.setGasPrice( <num> )
1 parent cb1fa52 commit 13ddf20

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

cmd/geth/admin.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (js *jsre) adminBindings() {
7070
miner.Set("stop", js.stopMining)
7171
miner.Set("hashrate", js.hashrate)
7272
miner.Set("setExtra", js.setExtra)
73+
miner.Set("setGasPrice", js.setGasPrice)
7374

7475
admin.Set("debug", struct{}{})
7576
t, _ = admin.Get("debug")
@@ -236,6 +237,17 @@ func (js *jsre) setExtra(call otto.FunctionCall) otto.Value {
236237
return otto.UndefinedValue()
237238
}
238239

240+
func (js *jsre) setGasPrice(call otto.FunctionCall) otto.Value {
241+
gasPrice, err := call.Argument(0).ToString()
242+
if err != nil {
243+
fmt.Println(err)
244+
return otto.UndefinedValue()
245+
}
246+
247+
js.ethereum.Miner().SetGasPrice(common.String2Big(gasPrice))
248+
return otto.UndefinedValue()
249+
}
250+
239251
func (js *jsre) hashrate(otto.FunctionCall) otto.Value {
240252
return js.re.ToVal(js.ethereum.Miner().HashRate())
241253
}

miner/miner.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (self *Miner) Mining() bool {
3737
return self.mining
3838
}
3939

40+
func (m *Miner) SetGasPrice(price *big.Int) {
41+
m.worker.gasPrice = price
42+
}
43+
4044
func (self *Miner) Start(coinbase common.Address) {
4145
self.mining = true
4246
self.worker.coinbase = coinbase

miner/worker.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type worker struct {
7272
proc *core.BlockProcessor
7373

7474
coinbase common.Address
75+
gasPrice *big.Int
7576
extra []byte
7677

7778
currentMu sync.Mutex
@@ -93,6 +94,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker {
9394
eth: eth,
9495
mux: eth.EventMux(),
9596
recv: make(chan *types.Block),
97+
gasPrice: new(big.Int),
9698
chain: eth.ChainManager(),
9799
proc: eth.BlockProcessor(),
98100
possibleUncles: make(map[common.Hash]*types.Block),
@@ -239,6 +241,19 @@ func (self *worker) makeCurrent() {
239241
self.current.coinbase.SetGasPool(core.CalcGasLimit(parent))
240242
}
241243

244+
func (w *worker) setGasPrice(p *big.Int) {
245+
w.mu.Lock()
246+
defer w.mu.Unlock()
247+
w.gasPrice = p
248+
}
249+
250+
func (w *worker) gasprice(pct int64) *big.Int {
251+
p := new(big.Int).Set(w.gasPrice)
252+
p.Div(p, big.NewInt(100))
253+
p.Mul(p, big.NewInt(pct))
254+
return p
255+
}
256+
242257
func (self *worker) commitNewWork() {
243258
self.mu.Lock()
244259
defer self.mu.Unlock()
@@ -259,9 +274,22 @@ func (self *worker) commitNewWork() {
259274
ignoredTransactors = set.New()
260275
)
261276

277+
var pct int64 = 90
278+
minprice := self.gasprice(pct)
262279
for _, tx := range transactions {
263280
// We can skip err. It has already been validated in the tx pool
264281
from, _ := tx.From()
282+
283+
// check if it falls within margin
284+
if tx.GasPrice().Cmp(minprice) < 0 {
285+
// ignore the transaction and transactor. We ignore the transactor
286+
// because nonce will fail after ignoring this transaction so there's
287+
// no point
288+
ignoredTransactors.Add(from)
289+
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])
290+
continue
291+
}
292+
265293
// Move on to the next transaction when the transactor is in ignored transactions set
266294
// This may occur when a transaction hits the gas limit. When a gas limit is hit and
267295
// the transaction is processed (that could potentially be included in the block) it

0 commit comments

Comments
 (0)