Skip to content

Commit e289b0a

Browse files
committed
Merge pull request #888 from obscuren/develop
miner, flags: Configurable gas price & log flag change
2 parents c8fc4ce + 658ac3c commit e289b0a

File tree

8 files changed

+88
-15
lines changed

8 files changed

+88
-15
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
}

cmd/geth/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import _ "net/http/pprof"
5151

5252
const (
5353
ClientIdentifier = "Geth"
54-
Version = "0.9.17"
54+
Version = "0.9.18"
5555
)
5656

5757
var (
@@ -244,6 +244,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
244244
utils.MaxPeersFlag,
245245
utils.MaxPendingPeersFlag,
246246
utils.EtherbaseFlag,
247+
utils.GasPriceFlag,
247248
utils.MinerThreadsFlag,
248249
utils.MiningEnabledFlag,
249250
utils.NATFlag,
@@ -258,7 +259,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
258259
utils.ProtocolVersionFlag,
259260
utils.NetworkIdFlag,
260261
utils.RPCCORSDomainFlag,
261-
utils.LogLevelFlag,
262+
utils.VerbosityFlag,
262263
utils.BacktraceAtFlag,
263264
utils.LogToStdErrFlag,
264265
utils.LogVModuleFlag,

cmd/mist/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func init() {
7373
utils.DataDirFlag,
7474
utils.ListenPortFlag,
7575
utils.LogFileFlag,
76-
utils.LogLevelFlag,
76+
utils.VerbosityFlag,
7777
utils.MaxPeersFlag,
7878
utils.MaxPendingPeersFlag,
7979
utils.MinerThreadsFlag,

cmd/utils/flags.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/ecdsa"
55
"fmt"
66
"log"
7+
"math/big"
78
"net/http"
89
"os"
910
"path"
@@ -116,6 +117,11 @@ var (
116117
Usage: "Public address for block mining rewards. By default the address of your primary account is used",
117118
Value: "primary",
118119
}
120+
GasPriceFlag = cli.StringFlag{
121+
Name: "gasprice",
122+
Usage: "Sets the minimal gasprice when mining transactions",
123+
Value: new(big.Int).Mul(big.NewInt(10), common.Szabo).String(),
124+
}
119125

120126
UnlockedAccountFlag = cli.StringFlag{
121127
Name: "unlock",
@@ -133,8 +139,8 @@ var (
133139
Name: "logfile",
134140
Usage: "Send log output to a file",
135141
}
136-
LogLevelFlag = cli.IntFlag{
137-
Name: "loglevel",
142+
VerbosityFlag = cli.IntFlag{
143+
Name: "verbosity",
138144
Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
139145
Value: int(logger.InfoLevel),
140146
}
@@ -270,7 +276,7 @@ func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
270276

271277
func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
272278
// Set verbosity on glog
273-
glog.SetV(ctx.GlobalInt(LogLevelFlag.Name))
279+
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
274280
// Set the log type
275281
//glog.SetToStderr(ctx.GlobalBool(LogToStdErrFlag.Name))
276282
glog.SetToStderr(true)
@@ -290,7 +296,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
290296
SkipBcVersionCheck: false,
291297
NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
292298
LogFile: ctx.GlobalString(LogFileFlag.Name),
293-
LogLevel: ctx.GlobalInt(LogLevelFlag.Name),
299+
Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
294300
LogJSON: ctx.GlobalString(LogJSONFlag.Name),
295301
Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
296302
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
@@ -305,6 +311,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
305311
Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
306312
Dial: true,
307313
BootNodes: ctx.GlobalString(BootnodesFlag.Name),
314+
GasPrice: common.String2Big(ctx.GlobalString(GasPriceFlag.Name)),
308315
}
309316

310317
}

eth/backend.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"math/big"
89
"os"
910
"path"
1011
"path/filepath"
@@ -53,12 +54,12 @@ type Config struct {
5354
BlockChainVersion int
5455
SkipBcVersionCheck bool // e.g. blockchain export
5556

56-
DataDir string
57-
LogFile string
58-
LogLevel int
59-
LogJSON string
60-
VmDebug bool
61-
NatSpec bool
57+
DataDir string
58+
LogFile string
59+
Verbosity int
60+
LogJSON string
61+
VmDebug bool
62+
NatSpec bool
6263

6364
MaxPeers int
6465
MaxPendingPeers int
@@ -76,6 +77,7 @@ type Config struct {
7677
Dial bool
7778

7879
Etherbase string
80+
GasPrice *big.Int
7981
MinerThreads int
8082
AccountManager *accounts.Manager
8183

@@ -200,7 +202,7 @@ type Ethereum struct {
200202

201203
func New(config *Config) (*Ethereum, error) {
202204
// Bootstrap database
203-
logger.New(config.DataDir, config.LogFile, config.LogLevel)
205+
logger.New(config.DataDir, config.LogFile, config.Verbosity)
204206
if len(config.LogJSON) > 0 {
205207
logger.NewJSONsystem(config.DataDir, config.LogJSON)
206208
}
@@ -266,6 +268,8 @@ func New(config *Config) (*Ethereum, error) {
266268
eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.txPool, eth.chainManager, eth.EventMux())
267269
eth.chainManager.SetProcessor(eth.blockProcessor)
268270
eth.miner = miner.New(eth, eth.pow, config.MinerThreads)
271+
eth.miner.SetGasPrice(config.GasPrice)
272+
269273
eth.protocolManager = NewProtocolManager(config.ProtocolVersion, config.NetworkId, eth.eventMux, eth.txPool, eth.chainManager, eth.downloader)
270274
if config.Shh {
271275
eth.whisper = whisper.New()

miner/miner.go

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

40+
func (m *Miner) SetGasPrice(price *big.Int) {
41+
// FIXME block tests set a nil gas price. Quick dirty fix
42+
if price == nil {
43+
return
44+
}
45+
46+
m.worker.gasPrice = price
47+
}
48+
4049
func (self *Miner) Start(coinbase common.Address) {
4150
self.mining = true
4251
self.worker.coinbase = coinbase

miner/worker.go

Lines changed: 40 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),
@@ -123,6 +125,9 @@ func (self *worker) pendingBlock() *types.Block {
123125
}
124126

125127
func (self *worker) start() {
128+
self.mu.Lock()
129+
defer self.mu.Unlock()
130+
126131
// spin up agents
127132
for _, agent := range self.agents {
128133
agent.Start()
@@ -132,6 +137,9 @@ func (self *worker) start() {
132137
}
133138

134139
func (self *worker) stop() {
140+
self.mu.Lock()
141+
defer self.mu.Unlock()
142+
135143
if atomic.LoadInt32(&self.mining) == 1 {
136144
// stop all agents
137145
for _, agent := range self.agents {
@@ -144,6 +152,9 @@ func (self *worker) stop() {
144152
}
145153

146154
func (self *worker) register(agent Agent) {
155+
self.mu.Lock()
156+
defer self.mu.Unlock()
157+
147158
self.agents = append(self.agents, agent)
148159
agent.SetReturnCh(self.recv)
149160
}
@@ -239,6 +250,12 @@ func (self *worker) makeCurrent() {
239250
self.current.coinbase.SetGasPool(core.CalcGasLimit(parent))
240251
}
241252

253+
func (w *worker) setGasPrice(p *big.Int) {
254+
w.mu.Lock()
255+
defer w.mu.Unlock()
256+
w.gasPrice = p
257+
}
258+
242259
func (self *worker) commitNewWork() {
243260
self.mu.Lock()
244261
defer self.mu.Unlock()
@@ -259,9 +276,23 @@ func (self *worker) commitNewWork() {
259276
ignoredTransactors = set.New()
260277
)
261278

279+
const pct = int64(90)
280+
// calculate the minimal gas price the miner accepts when sorting out transactions.
281+
minprice := gasprice(self.gasPrice, pct)
262282
for _, tx := range transactions {
263283
// We can skip err. It has already been validated in the tx pool
264284
from, _ := tx.From()
285+
286+
// check if it falls within margin
287+
if tx.GasPrice().Cmp(minprice) < 0 {
288+
// ignore the transaction and transactor. We ignore the transactor
289+
// because nonce will fail after ignoring this transaction so there's
290+
// no point
291+
ignoredTransactors.Add(from)
292+
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])
293+
continue
294+
}
295+
265296
// Move on to the next transaction when the transactor is in ignored transactions set
266297
// This may occur when a transaction hits the gas limit. When a gas limit is hit and
267298
// the transaction is processed (that could potentially be included in the block) it
@@ -383,3 +414,12 @@ func (self *worker) HashRate() int64 {
383414

384415
return tot
385416
}
417+
418+
// gasprice calculates a reduced gas price based on the pct
419+
// XXX Use big.Rat?
420+
func gasprice(price *big.Int, pct int64) *big.Int {
421+
p := new(big.Int).Set(price)
422+
p.Div(p, big.NewInt(100))
423+
p.Mul(p, big.NewInt(pct))
424+
return p
425+
}

tests/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func testEthConfig() *eth.Config {
103103

104104
return &eth.Config{
105105
DataDir: common.DefaultDataDir(),
106-
LogLevel: 5,
106+
Verbosity: 5,
107107
Etherbase: "primary",
108108
AccountManager: accounts.NewManager(ks),
109109
NewDB: func(path string) (common.Database, error) { return ethdb.NewMemDatabase() },

0 commit comments

Comments
 (0)