Skip to content

Commit cc0b451

Browse files
committed
Merge pull request #1260 from obscuren/tx-drop-low-tx
core: drop low gas tx
2 parents f2a2164 + 2628103 commit cc0b451

File tree

8 files changed

+60
-24
lines changed

8 files changed

+60
-24
lines changed

common/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package common
22

33
import (
4+
"fmt"
45
"math/big"
56
"math/rand"
67
"reflect"
@@ -95,3 +96,13 @@ func (a *Address) Set(other Address) {
9596
a[i] = v
9697
}
9798
}
99+
100+
// PP Pretty Prints a byte slice in the following format:
101+
// hex(value[:4])...(hex[len(value)-4:])
102+
func PP(value []byte) string {
103+
if len(value) <= 8 {
104+
return Bytes2Hex(value)
105+
}
106+
107+
return fmt.Sprintf("%x...%x", value[:4], value[len(value)-4])
108+
}

core/chain_manager.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"math/big"
8-
"os"
98
"runtime"
109
"sync"
1110
"sync/atomic"
@@ -235,15 +234,8 @@ func (bc *ChainManager) setLastState() {
235234
if block != nil {
236235
bc.currentBlock = block
237236
bc.lastBlockHash = block.Hash()
238-
} else { // TODO CLEAN THIS UP TMP CODE
239-
block = bc.GetBlockByNumber(400000)
240-
if block == nil {
241-
fmt.Println("Fatal. LastBlock not found. Report this issue")
242-
os.Exit(1)
243-
}
244-
bc.currentBlock = block
245-
bc.lastBlockHash = block.Hash()
246-
bc.insert(block)
237+
} else {
238+
glog.Fatalf("Fatal. LastBlock not found. Please run removedb and resync")
247239
}
248240
} else {
249241
bc.Reset()

core/transaction_pool.go

Lines changed: 30 additions & 4 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")
@@ -27,6 +28,10 @@ var (
2728
ErrNegativeValue = errors.New("Negative value")
2829
)
2930

31+
const (
32+
maxQueued = 200 // max limit of queued txs per address
33+
)
34+
3035
type 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
}

eth/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func New(config *Config) (*Ethereum, error) {
310310
return nil, err
311311
}
312312
eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit)
313+
313314
eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.chainManager, eth.EventMux())
314315
eth.chainManager.SetProcessor(eth.blockProcessor)
315316
eth.protocolManager = NewProtocolManager(config.ProtocolVersion, config.NetworkId, eth.eventMux, eth.txPool, eth.chainManager)

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
}

rpc/api/eth.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ func (self *ethApi) SendTransaction(req *shared.Request) (interface{}, error) {
259259
nonce = args.Nonce.String()
260260
}
261261

262-
v, err := self.xeth.Transact(args.From, args.To, nonce, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
262+
var gas, price string
263+
if args.Gas != nil {
264+
gas = args.Gas.String()
265+
}
266+
if args.GasPrice != nil {
267+
price = args.GasPrice.String()
268+
}
269+
v, err := self.xeth.Transact(args.From, args.To, nonce, args.Value.String(), gas, price, args.Data)
263270
if err != nil {
264271
return nil, err
265272
}

rpc/api/eth_args.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,15 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
333333
args.Value = num
334334

335335
num = nil
336-
if ext.Gas == nil {
337-
num = big.NewInt(0)
338-
} else {
336+
if ext.Gas != nil {
339337
if num, err = numString(ext.Gas); err != nil {
340338
return err
341339
}
342340
}
343341
args.Gas = num
344342

345343
num = nil
346-
if ext.GasPrice == nil {
347-
num = big.NewInt(0)
348-
} else {
344+
if ext.GasPrice != nil {
349345
if num, err = numString(ext.GasPrice); err != nil {
350346
return err
351347
}

0 commit comments

Comments
 (0)