Skip to content

Commit 6d817e1

Browse files
committed
core, miner: tx pool drops txs below ask price
1 parent 6f5c615 commit 6d817e1

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

core/transaction_pool.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
// Transaction Pool Errors
2020
ErrInvalidSender = errors.New("Invalid sender")
2121
ErrNonce = errors.New("Nonce too low")
22+
ErrCheap = errors.New("Gas price too low for acceptance")
2223
ErrBalance = errors.New("Insufficient balance")
2324
ErrNonExistentAccount = errors.New("Account does not exist or account balance too low")
2425
ErrInsufficientFunds = errors.New("Insufficient funds for gas * price + value")
@@ -41,6 +42,7 @@ type TxPool struct {
4142
currentState stateFn // The state function which will allow us to do some pre checkes
4243
pendingState *state.ManagedState
4344
gasLimit func() *big.Int // The current gas limit function callback
45+
minGasPrice *big.Int
4446
eventMux *event.TypeMux
4547
events event.Subscription
4648

@@ -57,8 +59,9 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func(
5759
eventMux: eventMux,
5860
currentState: currentStateFn,
5961
gasLimit: gasLimitFn,
62+
minGasPrice: new(big.Int),
6063
pendingState: state.ManageState(currentStateFn()),
61-
events: eventMux.Subscribe(ChainEvent{}),
64+
events: eventMux.Subscribe(ChainEvent{}, GasPriceChanged{}),
6265
}
6366
go pool.eventLoop()
6467

@@ -69,10 +72,15 @@ func (pool *TxPool) eventLoop() {
6972
// Track chain events. When a chain events occurs (new chain canon block)
7073
// we need to know the new state. The new state will help us determine
7174
// the nonces in the managed state
72-
for _ = range pool.events.Chan() {
75+
for ev := range pool.events.Chan() {
7376
pool.mu.Lock()
7477

75-
pool.resetState()
78+
switch ev := ev.(type) {
79+
case ChainEvent:
80+
pool.resetState()
81+
case GasPriceChanged:
82+
pool.minGasPrice = ev.Price
83+
}
7684

7785
pool.mu.Unlock()
7886
}
@@ -124,6 +132,11 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
124132
err error
125133
)
126134

135+
// Drop transactions under our own minimal accepted gas price
136+
if pool.minGasPrice.Cmp(tx.GasPrice()) > 0 {
137+
return ErrCheap
138+
}
139+
127140
// Validate the transaction sender and it's sig. Throw
128141
// if the from fields is invalid.
129142
if from, err = tx.From(); err != nil {

eth/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ func New(config *Config) (*Ethereum, error) {
292292
}
293293
eth.downloader = downloader.New(eth.EventMux(), eth.chainManager.HasBlock, eth.chainManager.GetBlock)
294294
eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit)
295+
295296
eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.chainManager, eth.EventMux())
296297
eth.chainManager.SetProcessor(eth.blockProcessor)
297298
eth.miner = miner.New(eth, eth.EventMux(), eth.pow)

miner/miner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (m *Miner) SetGasPrice(price *big.Int) {
7777
return
7878
}
7979

80-
m.worker.gasPrice = price
80+
m.worker.setGasPrice(price)
8181
}
8282

8383
func (self *Miner) Start(coinbase common.Address, threads int) {

miner/worker.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sort"
77
"sync"
88
"sync/atomic"
9+
"time"
910

1011
"github.com/ethereum/go-ethereum/accounts"
1112
"github.com/ethereum/go-ethereum/common"
@@ -374,6 +375,8 @@ func (self *worker) commitNewWork() {
374375
self.currentMu.Lock()
375376
defer self.currentMu.Unlock()
376377

378+
tstart := time.Now()
379+
377380
previous := self.current
378381
self.makeCurrent()
379382
current := self.current
@@ -409,7 +412,7 @@ func (self *worker) commitNewWork() {
409412

410413
// We only care about logging if we're actually mining
411414
if atomic.LoadInt32(&self.mining) == 1 {
412-
glog.V(logger.Info).Infof("commit new work on block %v with %d txs & %d uncles\n", current.block.Number(), current.tcount, len(uncles))
415+
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))
413416
self.logLocalMinedBlocks(previous)
414417
}
415418

@@ -437,7 +440,6 @@ func (self *worker) commitUncle(uncle *types.Header) error {
437440
// Error not unique
438441
return core.UncleError("Uncle not unique")
439442
}
440-
self.current.uncles.Add(uncle.Hash())
441443

442444
if !self.current.ancestors.Has(uncle.ParentHash) {
443445
return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
@@ -446,6 +448,7 @@ func (self *worker) commitUncle(uncle *types.Header) error {
446448
if self.current.family.Has(uncle.Hash()) {
447449
return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", uncle.Hash()))
448450
}
451+
self.current.uncles.Add(uncle.Hash())
449452

450453
return nil
451454
}

0 commit comments

Comments
 (0)