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" )
2728 ErrNegativeValue = errors .New ("Negative value" )
2829)
2930
31+ const (
32+ maxQueued = 200 // max limit of queued txs per address
33+ )
34+
3035type stateFn func () * state.StateDB
3136
3237// TxPool contains all currently known transactions. Transactions
@@ -41,6 +46,7 @@ type TxPool struct {
4146 currentState stateFn // The state function which will allow us to do some pre checkes
4247 pendingState * state.ManagedState
4348 gasLimit func () * big.Int // The current gas limit function callback
49+ minGasPrice * big.Int
4450 eventMux * event.TypeMux
4551 events event.Subscription
4652
@@ -57,8 +63,9 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func(
5763 eventMux : eventMux ,
5864 currentState : currentStateFn ,
5965 gasLimit : gasLimitFn ,
66+ minGasPrice : new (big.Int ),
6067 pendingState : state .ManageState (currentStateFn ()),
61- events : eventMux .Subscribe (ChainEvent {}),
68+ events : eventMux .Subscribe (ChainEvent {}, GasPriceChanged {} ),
6269 }
6370 go pool .eventLoop ()
6471
@@ -69,10 +76,15 @@ func (pool *TxPool) eventLoop() {
6976 // Track chain events. When a chain events occurs (new chain canon block)
7077 // we need to know the new state. The new state will help us determine
7178 // the nonces in the managed state
72- for _ = range pool .events .Chan () {
79+ for ev : = range pool .events .Chan () {
7380 pool .mu .Lock ()
7481
75- pool .resetState ()
82+ switch ev := ev .(type ) {
83+ case ChainEvent :
84+ pool .resetState ()
85+ case GasPriceChanged :
86+ pool .minGasPrice = ev .Price
87+ }
7688
7789 pool .mu .Unlock ()
7890 }
@@ -124,6 +136,11 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
124136 err error
125137 )
126138
139+ // Drop transactions under our own minimal accepted gas price
140+ if pool .minGasPrice .Cmp (tx .GasPrice ()) > 0 {
141+ return ErrCheap
142+ }
143+
127144 // Validate the transaction sender and it's sig. Throw
128145 // if the from fields is invalid.
129146 if from , err = tx .From (); err != nil {
@@ -335,7 +352,16 @@ func (pool *TxPool) checkQueue() {
335352 // Find the next consecutive nonce range starting at the
336353 // current account nonce.
337354 sort .Sort (addq )
338- for _ , e := range addq {
355+ for i , e := range addq {
356+ // start deleting the transactions from the queue if they exceed the limit
357+ if i > maxQueued {
358+ if glog .V (logger .Debug ) {
359+ glog .Infof ("Queued tx limit exceeded for %s. Tx %s removed\n " , common .PP (address [:]), common .PP (e .hash [:]))
360+ }
361+ delete (pool .queue [address ], e .hash )
362+ continue
363+ }
364+
339365 if e .AccountNonce > guessedNonce {
340366 break
341367 }
0 commit comments