Skip to content

Commit ee7299b

Browse files
eth, miner: fix enforcing the minimum miner tip (ethereum#28933) (ethereum#1209)
* eth, miner: fix enforcing the minimum miner tip * ethclient/simulated: fix failing test due the min tip change * accounts/abi/bind: fix simulater gas tip issue Co-authored-by: Péter Szilágyi <[email protected]>
1 parent 694eadb commit ee7299b

File tree

7 files changed

+39
-13
lines changed

7 files changed

+39
-13
lines changed

accounts/abi/bind/util_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/core"
3030
"github.com/ethereum/go-ethereum/core/types"
3131
"github.com/ethereum/go-ethereum/crypto"
32+
"github.com/ethereum/go-ethereum/params"
3233
)
3334

3435
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
@@ -64,7 +65,7 @@ func TestWaitDeployed(t *testing.T) {
6465

6566
// Create the transaction
6667
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
67-
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
68+
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(params.GWei))
6869

6970
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code))
7071
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)

eth/api_miner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (api *MinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
6464
api.e.lock.Unlock()
6565

6666
api.e.txPool.SetGasTip((*big.Int)(&gasPrice))
67+
api.e.Miner().SetGasTip((*big.Int)(&gasPrice))
6768
return true
6869
}
6970

miner/miner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ func (miner *Miner) SetExtra(extra []byte) error {
219219
return nil
220220
}
221221

222+
func (miner *Miner) SetGasTip(tip *big.Int) error {
223+
miner.worker.setGasTip(tip)
224+
return nil
225+
}
226+
222227
// SetRecommitInterval sets the interval for sealing work resubmitting.
223228
func (miner *Miner) SetRecommitInterval(interval time.Duration) {
224229
miner.worker.setRecommitInterval(interval)

miner/ordering.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ func newTransactionsByPriceAndNonce(signer types.Signer, txs map[common.Address]
119119
}
120120

121121
// Peek returns the next transaction by price.
122-
func (t *transactionsByPriceAndNonce) Peek() *txpool.LazyTransaction {
122+
func (t *transactionsByPriceAndNonce) Peek() (*txpool.LazyTransaction, *big.Int) {
123123
if len(t.heads) == 0 {
124-
return nil
124+
return nil, nil
125125
}
126-
return t.heads[0].tx
126+
return t.heads[0].tx, t.heads[0].fees
127127
}
128128

129129
// Shift replaces the current best head with the next one from the same account.

miner/ordering_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func testTransactionPriceNonceSort(t *testing.T, baseFee *big.Int) {
102102
txset := newTransactionsByPriceAndNonce(signer, groups, baseFee)
103103

104104
txs := types.Transactions{}
105-
for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
105+
for tx, _ := txset.Peek(); tx != nil; tx, _ = txset.Peek() {
106106
txs = append(txs, tx.Tx)
107107
txset.Shift()
108108
}
@@ -167,7 +167,7 @@ func TestTransactionTimeSort(t *testing.T) {
167167
txset := newTransactionsByPriceAndNonce(signer, groups, nil)
168168

169169
txs := types.Transactions{}
170-
for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
170+
for tx, _ := txset.Peek(); tx != nil; tx, _ = txset.Peek() {
171171
txs = append(txs, tx.Tx)
172172
txset.Shift()
173173
}

miner/test_backend.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"math/big"
78
"os"
89
"sync"
910
"sync/atomic"
@@ -175,7 +176,7 @@ func (w *worker) mainLoopWithDelay(ctx context.Context, delay uint, opcodeDelay
175176
}
176177
txset := newTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee)
177178
tcount := w.current.tcount
178-
w.commitTransactions(w.current, txset, nil, context.Background())
179+
w.commitTransactions(w.current, txset, nil, new(big.Int), context.Background())
179180

180181
// Only update the snapshot if any new transactons were added
181182
// to the pending block
@@ -587,7 +588,7 @@ mainloop:
587588
break
588589
}
589590
// Retrieve the next transaction and abort if all done.
590-
ltx := txs.Peek()
591+
ltx, _ := txs.Peek()
591592
if ltx == nil {
592593
breakCause = "all transactions has been included"
593594
break

miner/worker.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ type worker struct {
237237
mu sync.RWMutex // The lock used to protect the coinbase and extra fields
238238
coinbase common.Address
239239
extra []byte
240+
tip *big.Int // Minimum tip needed for non-local transaction to include them
240241

241242
pendingMu sync.RWMutex
242243
pendingTasks map[common.Hash]*task
@@ -295,6 +296,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
295296
isLocalBlock: isLocalBlock,
296297
coinbase: config.Etherbase,
297298
extra: config.ExtraData,
299+
tip: config.GasPrice,
298300
pendingTasks: make(map[common.Hash]*task),
299301
txsCh: make(chan core.NewTxsEvent, txChanSize),
300302
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
@@ -395,6 +397,13 @@ func (w *worker) setExtra(extra []byte) {
395397
w.extra = extra
396398
}
397399

400+
// setGasTip sets the minimum miner tip needed to include a non-local transaction.
401+
func (w *worker) setGasTip(tip *big.Int) {
402+
w.mu.Lock()
403+
defer w.mu.Unlock()
404+
w.tip = tip
405+
}
406+
398407
// setRecommitInterval updates the interval for miner sealing work recommitting.
399408
func (w *worker) setRecommitInterval(interval time.Duration) {
400409
select {
@@ -648,7 +657,7 @@ func (w *worker) mainLoop(ctx context.Context) {
648657
}
649658
txset := newTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee)
650659
tcount := w.current.tcount
651-
w.commitTransactions(w.current, txset, nil, context.Background())
660+
w.commitTransactions(w.current, txset, nil, new(big.Int), context.Background())
652661

653662
// Only update the snapshot if any new transactons were added
654663
// to the pending block
@@ -908,7 +917,7 @@ func (w *worker) commitTransaction(env *environment, tx *types.Transaction, inte
908917
return receipt.Logs, nil
909918
}
910919

911-
func (w *worker) commitTransactions(env *environment, txs *transactionsByPriceAndNonce, interrupt *atomic.Int32, interruptCtx context.Context) error {
920+
func (w *worker) commitTransactions(env *environment, txs *transactionsByPriceAndNonce, interrupt *atomic.Int32, minTip *big.Int, interruptCtx context.Context) error {
912921
gasLimit := env.header.GasLimit
913922
if env.gasPool == nil {
914923
env.gasPool = new(core.GasPool).AddGas(gasLimit)
@@ -996,7 +1005,7 @@ mainloop:
9961005
break
9971006
}
9981007
// Retrieve the next transaction and abort if all done.
999-
ltx := txs.Peek()
1008+
ltx, tip := txs.Peek()
10001009
if ltx == nil {
10011010
breakCause = "all transactions has been included"
10021011
break
@@ -1012,6 +1021,11 @@ mainloop:
10121021
txs.Pop()
10131022
continue
10141023
}
1024+
// If we don't receive enough tip for the next transaction, skip the account
1025+
if tip.Cmp(minTip) < 0 {
1026+
log.Trace("Not enough tip for transaction", "hash", ltx.Hash, "tip", tip, "needed", minTip)
1027+
break // If the next-best is too low, surely no better will be available
1028+
}
10151029
// Transaction seems to fit, pull it up from the pool
10161030
tx := ltx.Resolve()
10171031
if tx == nil {
@@ -1469,6 +1483,10 @@ func (w *worker) fillTransactions(ctx context.Context, interrupt *atomic.Int32,
14691483
err error
14701484
)
14711485

1486+
w.mu.RLock()
1487+
tip := w.tip
1488+
w.mu.RUnlock()
1489+
14721490
if len(localTxs) > 0 {
14731491
var txs *transactionsByPriceAndNonce
14741492

@@ -1487,7 +1505,7 @@ func (w *worker) fillTransactions(ctx context.Context, interrupt *atomic.Int32,
14871505
})
14881506

14891507
tracing.Exec(ctx, "", "worker.LocalCommitTransactions", func(ctx context.Context, span trace.Span) {
1490-
err = w.commitTransactions(env, txs, interrupt, interruptCtx)
1508+
err = w.commitTransactions(env, txs, interrupt, new(big.Int), interruptCtx)
14911509
})
14921510

14931511
if err != nil {
@@ -1515,7 +1533,7 @@ func (w *worker) fillTransactions(ctx context.Context, interrupt *atomic.Int32,
15151533
})
15161534

15171535
tracing.Exec(ctx, "", "worker.RemoteCommitTransactions", func(ctx context.Context, span trace.Span) {
1518-
err = w.commitTransactions(env, txs, interrupt, interruptCtx)
1536+
err = w.commitTransactions(env, txs, interrupt, tip, interruptCtx)
15191537
})
15201538

15211539
if err != nil {

0 commit comments

Comments
 (0)