forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxmgr.go
1075 lines (957 loc) · 37 KB
/
txmgr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package txmgr
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/ethereum-optimism/optimism/op-service/errutil"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/holiman/uint256"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum-optimism/optimism/op-service/txmgr/metrics"
)
const (
// geth requires a minimum fee bump of 10% for regular tx resubmission
priceBump int64 = 10
// geth requires a minimum fee bump of 100% for blob tx resubmission
blobPriceBump int64 = 100
)
var (
priceBumpPercent = big.NewInt(100 + priceBump)
blobPriceBumpPercent = big.NewInt(100 + blobPriceBump)
oneHundred = big.NewInt(100)
ninetyNine = big.NewInt(99)
two = big.NewInt(2)
ErrBlobFeeLimit = errors.New("blob fee limit reached")
ErrClosed = errors.New("transaction manager is closed")
)
type SendResponse struct {
Receipt *types.Receipt
Nonce uint64
Err error
}
// TxManager is an interface that allows callers to reliably publish txs,
// bumping the gas price if needed, and obtain the receipt of the resulting tx.
//
//go:generate mockery --name TxManager --output ./mocks
type TxManager interface {
// Send is used to create & send a transaction. It will handle increasing
// the gas price & ensuring that the transaction remains in the transaction pool.
// It can be stopped by canceling the provided context; however, the transaction
// may be included on L1 even if the context is canceled.
//
// NOTE: Send can be called concurrently, the nonce will be managed internally.
//
// Callers using both Blob and non-Blob transactions should check to see if the returned error
// is ErrAlreadyReserved, which indicates an incompatible transaction may be stuck in the
// mempool and is in need of replacement or cancellation.
Send(ctx context.Context, candidate TxCandidate) (*types.Receipt, error)
// SendAsync is used to create & send a transaction asynchronously. It has similar internal
// semantics to Send, however it returns a channel that will receive the result of the
// send operation once it completes. Transactions crafted synchronously - that is, nonce
// management and gas estimation happen prior to the method returning. This allows callers
// that rely on predictable nonces to send multiple transactions in parallel while preserving
// the order of nonce increments.
SendAsync(ctx context.Context, candidate TxCandidate, ch chan SendResponse)
// From returns the sending address associated with the instance of the transaction manager.
// It is static for a single instance of a TxManager.
From() common.Address
// BlockNumber returns the most recent block number from the underlying network.
BlockNumber(ctx context.Context) (uint64, error)
// API returns an rpc api interface which can be customized for each TxManager implementation
API() rpc.API
// Close the underlying connection
Close()
IsClosed() bool
// SuggestGasPriceCaps suggests what the new tip, base fee, and blob base fee should be based on
// the current L1 conditions. `blobBaseFee` will be nil if 4844 is not yet active.
SuggestGasPriceCaps(ctx context.Context) (tipCap *big.Int, baseFee *big.Int, blobBaseFee *big.Int, err error)
}
// ETHBackend is the set of methods that the transaction manager uses to resubmit gas & determine
// when transactions are included on L1.
type ETHBackend interface {
// BlockNumber returns the most recent block number.
BlockNumber(ctx context.Context) (uint64, error)
// CallContract executes an eth_call against the provided contract.
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
// TransactionReceipt queries the backend for a receipt associated with
// txHash. If lookup does not fail, but the transaction is not found,
// nil should be returned for both values.
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
// SendTransaction submits a signed transaction to L1.
SendTransaction(ctx context.Context, tx *types.Transaction) error
// These functions are used to estimate what the base fee & priority fee should be set to.
// TODO: Maybe need a generic interface to support different RPC providers
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
// NonceAt returns the account nonce of the given account.
// The block number can be nil, in which case the nonce is taken from the latest known block.
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
// PendingNonceAt returns the pending nonce.
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
// EstimateGas returns an estimate of the amount of gas needed to execute the given
// transaction against the current pending block.
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
// Close the underlying eth connection
Close()
}
// SimpleTxManager is a implementation of TxManager that performs linear fee
// bumping of a tx until it confirms.
type SimpleTxManager struct {
cfg *Config // embed the config directly
name string
chainID *big.Int
backend ETHBackend
l log.Logger
metr metrics.TxMetricer
gasPriceEstimatorFn GasPriceEstimatorFn
nonce *uint64
nonceLock sync.RWMutex
pending atomic.Int64
closed atomic.Bool
}
// NewSimpleTxManager initializes a new SimpleTxManager with the passed Config.
func NewSimpleTxManager(name string, l log.Logger, m metrics.TxMetricer, cfg CLIConfig) (*SimpleTxManager, error) {
conf, err := NewConfig(cfg, l)
if err != nil {
return nil, err
}
return NewSimpleTxManagerFromConfig(name, l, m, conf)
}
// NewSimpleTxManager initializes a new SimpleTxManager with the passed Config.
func NewSimpleTxManagerFromConfig(name string, l log.Logger, m metrics.TxMetricer, conf *Config) (*SimpleTxManager, error) {
if err := conf.Check(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return &SimpleTxManager{
chainID: conf.ChainID,
name: name,
cfg: conf,
backend: conf.Backend,
l: l.New("service", name),
metr: m,
gasPriceEstimatorFn: conf.GasPriceEstimatorFn,
}, nil
}
func (m *SimpleTxManager) From() common.Address {
return m.cfg.From
}
func (m *SimpleTxManager) BlockNumber(ctx context.Context) (uint64, error) {
return m.backend.BlockNumber(ctx)
}
func (m *SimpleTxManager) API() rpc.API {
return rpc.API{
Namespace: "txmgr",
Service: &SimpleTxmgrAPI{
mgr: m,
l: m.l,
},
}
}
// Close closes the underlying connection, and sets the closed flag.
// once closed, the tx manager will refuse to send any new transactions, and may abandon pending ones.
func (m *SimpleTxManager) Close() {
m.backend.Close()
m.closed.Store(true)
}
func (m *SimpleTxManager) txLogger(tx *types.Transaction, logGas bool) log.Logger {
fields := []any{"tx", tx.Hash(), "nonce", tx.Nonce()}
if logGas {
fields = append(fields, "gasTipCap", tx.GasTipCap(), "gasFeeCap", tx.GasFeeCap(), "gasLimit", tx.Gas())
}
if len(tx.BlobHashes()) != 0 {
// log the number of blobs a tx has only if it's a blob tx
fields = append(fields, "blobs", len(tx.BlobHashes()), "blobFeeCap", tx.BlobGasFeeCap())
}
return m.l.New(fields...)
}
// TxCandidate is a transaction candidate that can be submitted to ask the
// [TxManager] to construct a transaction with gas price bounds.
type TxCandidate struct {
// TxData is the transaction calldata to be used in the constructed tx.
TxData []byte
// Blobs to send along in the tx (optional). If len(Blobs) > 0 then a blob tx
// will be sent instead of a DynamicFeeTx.
Blobs []*eth.Blob
// To is the recipient of the constructed tx. Nil means contract creation.
To *common.Address
// GasLimit is the gas limit to be used in the constructed tx.
GasLimit uint64
// Value is the value to be used in the constructed tx.
Value *big.Int
}
// Send is used to publish a transaction with incrementally higher gas prices
// until the transaction eventually confirms. This method blocks until an
// invocation of sendTx returns (called with differing gas prices). The method
// may be canceled using the passed context.
//
// The transaction manager handles all signing. If and only if the gas limit is 0, the
// transaction manager will do a gas estimation.
//
// NOTE: Send can be called concurrently, the nonce will be managed internally.
func (m *SimpleTxManager) Send(ctx context.Context, candidate TxCandidate) (*types.Receipt, error) {
// refuse new requests if the tx manager is closed
if m.closed.Load() {
return nil, ErrClosed
}
m.metr.RecordPendingTx(m.pending.Add(1))
defer m.metr.RecordPendingTx(m.pending.Add(-1))
var cancel context.CancelFunc
if m.cfg.TxSendTimeout == 0 {
ctx, cancel = context.WithCancel(ctx)
} else {
ctx, cancel = context.WithTimeout(ctx, m.cfg.TxSendTimeout)
}
defer cancel()
tx, err := m.prepare(ctx, candidate)
if err != nil {
m.resetNonce()
return nil, err
}
receipt, err := m.sendTx(ctx, tx)
if err != nil {
m.resetNonce()
return nil, err
}
return receipt, err
}
func (m *SimpleTxManager) SendAsync(ctx context.Context, candidate TxCandidate, ch chan SendResponse) {
if cap(ch) == 0 {
panic("SendAsync: channel must be buffered")
}
// refuse new requests if the tx manager is closed
if m.closed.Load() {
ch <- SendResponse{
Receipt: nil,
Err: ErrClosed,
}
return
}
m.metr.RecordPendingTx(m.pending.Add(1))
var cancel context.CancelFunc
if m.cfg.TxSendTimeout == 0 {
ctx, cancel = context.WithCancel(ctx)
} else {
ctx, cancel = context.WithTimeout(ctx, m.cfg.TxSendTimeout)
}
tx, err := m.prepare(ctx, candidate)
if err != nil {
m.resetNonce()
cancel()
m.metr.RecordPendingTx(m.pending.Add(-1))
ch <- SendResponse{
Receipt: nil,
Err: err,
}
return
}
go func() {
defer m.metr.RecordPendingTx(m.pending.Add(-1))
defer cancel()
receipt, err := m.sendTx(ctx, tx)
if err != nil {
m.resetNonce()
}
ch <- SendResponse{
Receipt: receipt,
Nonce: tx.Nonce(),
Err: err,
}
}()
}
// prepare prepares the transaction for sending.
func (m *SimpleTxManager) prepare(ctx context.Context, candidate TxCandidate) (*types.Transaction, error) {
tx, err := retry.Do(ctx, 30, retry.Fixed(2*time.Second), func() (*types.Transaction, error) {
if m.closed.Load() {
return nil, ErrClosed
}
tx, err := m.craftTx(ctx, candidate)
if err != nil {
m.l.Warn("Failed to create a transaction, will retry", "err", err)
}
return tx, err
})
if err != nil {
return nil, fmt.Errorf("failed to create the tx: %w", err)
}
return tx, nil
}
// craftTx creates the signed transaction
// It queries L1 for the current fee market conditions as well as for the nonce.
// NOTE: This method SHOULD NOT publish the resulting transaction.
// NOTE: If the [TxCandidate.GasLimit] is non-zero, it will be used as the transaction's gas.
// NOTE: Otherwise, the [SimpleTxManager] will query the specified backend for an estimate.
func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (*types.Transaction, error) {
m.l.Debug("crafting Transaction", "blobs", len(candidate.Blobs), "calldata_size", len(candidate.TxData))
gasTipCap, baseFee, blobBaseFee, err := m.SuggestGasPriceCaps(ctx)
if err != nil {
m.metr.RPCError()
return nil, fmt.Errorf("failed to get gas price info: %w", err)
}
gasFeeCap := calcGasFeeCap(baseFee, gasTipCap)
gasLimit := candidate.GasLimit
var sidecar *types.BlobTxSidecar
var blobHashes []common.Hash
if len(candidate.Blobs) > 0 {
if candidate.To == nil {
return nil, errors.New("blob txs cannot deploy contracts")
}
if sidecar, blobHashes, err = MakeSidecar(candidate.Blobs); err != nil {
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}
}
// Calculate the intrinsic gas for the transaction
callMsg := ethereum.CallMsg{
From: m.cfg.From,
To: candidate.To,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Data: candidate.TxData,
Value: candidate.Value,
}
if len(blobHashes) > 0 {
callMsg.BlobGasFeeCap = blobBaseFee
callMsg.BlobHashes = blobHashes
}
// If the gas limit is set, we can use that as the gas
if gasLimit == 0 {
gas, err := m.backend.EstimateGas(ctx, callMsg)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas: %w", errutil.TryAddRevertReason(err))
}
gasLimit = gas
} else {
callMsg.Gas = gasLimit
_, err := m.backend.CallContract(ctx, callMsg, nil)
if err != nil {
return nil, fmt.Errorf("failed to call: %w", errutil.TryAddRevertReason(err))
}
}
var txMessage types.TxData
if sidecar != nil {
if blobBaseFee == nil {
return nil, errors.New("expected non-nil blobBaseFee")
}
blobFeeCap := m.calcBlobFeeCap(blobBaseFee)
message := &types.BlobTx{
To: *candidate.To,
Data: candidate.TxData,
Gas: gasLimit,
BlobHashes: blobHashes,
Sidecar: sidecar,
}
if err := finishBlobTx(message, m.chainID, gasTipCap, gasFeeCap, blobFeeCap, candidate.Value); err != nil {
return nil, fmt.Errorf("failed to create blob transaction: %w", err)
}
txMessage = message
} else {
txMessage = &types.DynamicFeeTx{
ChainID: m.chainID,
To: candidate.To,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: candidate.Value,
Data: candidate.TxData,
Gas: gasLimit,
}
}
return m.signWithNextNonce(ctx, txMessage) // signer sets the nonce field of the tx
}
func (m *SimpleTxManager) GetMinBaseFee() *big.Int {
return m.cfg.MinBaseFee.Load()
}
func (m *SimpleTxManager) SetMinBaseFee(val *big.Int) {
m.cfg.MinBaseFee.Store(val)
m.l.Info("txmgr config val changed: SetMinBaseFee", "newVal", val)
}
func (m *SimpleTxManager) GetMinPriorityFee() *big.Int {
return m.cfg.MinTipCap.Load()
}
func (m *SimpleTxManager) SetMinPriorityFee(val *big.Int) {
m.cfg.MinTipCap.Store(val)
m.l.Info("txmgr config val changed: SetMinPriorityFee", "newVal", val)
}
func (m *SimpleTxManager) GetMinBlobFee() *big.Int {
return m.cfg.MinBlobTxFee.Load()
}
func (m *SimpleTxManager) SetMinBlobFee(val *big.Int) {
m.cfg.MinBlobTxFee.Store(val)
m.l.Info("txmgr config val changed: SetMinBlobFee", "newVal", val)
}
func (m *SimpleTxManager) GetFeeLimitMultiplier() uint64 {
return m.cfg.FeeLimitMultiplier.Load()
}
func (m *SimpleTxManager) SetFeeLimitMultiplier(val uint64) {
m.cfg.FeeLimitMultiplier.Store(val)
m.l.Info("txmgr config val changed: SetFeeLimitMultiplier", "newVal", val)
}
func (m *SimpleTxManager) GetFeeThreshold() *big.Int {
return m.cfg.FeeLimitThreshold.Load()
}
func (m *SimpleTxManager) SetFeeThreshold(val *big.Int) {
m.cfg.FeeLimitThreshold.Store(val)
m.l.Info("txmgr config val changed: SetFeeThreshold", "newVal", val)
}
func (m *SimpleTxManager) GetBumpFeeRetryTime() time.Duration {
return time.Duration(m.cfg.ResubmissionTimeout.Load())
}
func (m *SimpleTxManager) SetBumpFeeRetryTime(val time.Duration) {
m.cfg.ResubmissionTimeout.Store(int64(val))
m.l.Info("txmgr config val changed: SetBumpFeeRetryTime", "newVal", val)
}
// MakeSidecar builds & returns the BlobTxSidecar and corresponding blob hashes from the raw blob
// data.
func MakeSidecar(blobs []*eth.Blob) (*types.BlobTxSidecar, []common.Hash, error) {
sidecar := &types.BlobTxSidecar{}
blobHashes := make([]common.Hash, 0, len(blobs))
for i, blob := range blobs {
rawBlob := blob.KZGBlob()
sidecar.Blobs = append(sidecar.Blobs, *rawBlob)
commitment, err := kzg4844.BlobToCommitment(rawBlob)
if err != nil {
return nil, nil, fmt.Errorf("cannot compute KZG commitment of blob %d in tx candidate: %w", i, err)
}
sidecar.Commitments = append(sidecar.Commitments, commitment)
proof, err := kzg4844.ComputeBlobProof(rawBlob, commitment)
if err != nil {
return nil, nil, fmt.Errorf("cannot compute KZG proof for fast commitment verification of blob %d in tx candidate: %w", i, err)
}
sidecar.Proofs = append(sidecar.Proofs, proof)
blobHashes = append(blobHashes, eth.KZGToVersionedHash(commitment))
}
return sidecar, blobHashes, nil
}
// signWithNextNonce returns a signed transaction with the next available nonce.
// The nonce is fetched once using eth_getTransactionCount with "latest", and
// then subsequent calls simply increment this number. If the transaction manager
// is reset, it will query the eth_getTransactionCount nonce again. If signing
// fails, the nonce is not incremented.
func (m *SimpleTxManager) signWithNextNonce(ctx context.Context, txMessage types.TxData) (*types.Transaction, error) {
m.nonceLock.Lock()
defer m.nonceLock.Unlock()
if m.nonce == nil {
// Fetch the sender's nonce from the latest known block (nil `blockNumber`)
childCtx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
nonce, err := m.backend.NonceAt(childCtx, m.cfg.From, nil)
if err != nil {
m.metr.RPCError()
return nil, fmt.Errorf("failed to get nonce: %w", err)
}
m.nonce = &nonce
} else {
*m.nonce++
}
switch x := txMessage.(type) {
case *types.DynamicFeeTx:
x.Nonce = *m.nonce
case *types.BlobTx:
x.Nonce = *m.nonce
default:
return nil, fmt.Errorf("unrecognized tx type: %T", x)
}
ctx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
tx, err := m.cfg.Signer(ctx, m.cfg.From, types.NewTx(txMessage))
if err != nil {
// decrement the nonce, so we can retry signing with the same nonce next time
// signWithNextNonce is called
*m.nonce--
} else {
m.metr.RecordNonce(*m.nonce)
}
return tx, err
}
// resetNonce resets the internal nonce tracking. This is called if any pending send
// returns an error.
func (m *SimpleTxManager) resetNonce() {
m.nonceLock.Lock()
defer m.nonceLock.Unlock()
m.nonce = nil
}
// send submits the same transaction several times with increasing gas prices as necessary.
// It waits for the transaction to be confirmed on chain.
func (m *SimpleTxManager) sendTx(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
sendState := NewSendState(m.cfg.SafeAbortNonceTooLowCount, m.cfg.TxNotInMempoolTimeout)
receiptChan := make(chan *types.Receipt, 1)
resubmissionTimeout := m.GetBumpFeeRetryTime()
ticker := time.NewTicker(resubmissionTimeout)
defer ticker.Stop()
for {
if !sendState.IsWaitingForConfirmation() {
if m.closed.Load() {
// the tx manager closed and no txs are waiting to be confirmed, give up
m.txLogger(tx, false).Warn("TxManager closed, aborting transaction submission")
return nil, ErrClosed
}
var published bool
if tx, published = m.publishTx(ctx, tx, sendState); published {
wg.Add(1)
go func() {
defer wg.Done()
m.waitForTx(ctx, tx, sendState, receiptChan)
}()
}
}
if err := sendState.CriticalError(); err != nil {
m.txLogger(tx, false).Warn("Aborting transaction submission", "err", err)
return nil, fmt.Errorf("aborted tx send due to critical error: %w", err)
}
select {
case <-ticker.C:
case <-ctx.Done():
return nil, ctx.Err()
case receipt := <-receiptChan:
m.metr.RecordGasBumpCount(sendState.bumpCount)
m.metr.TxConfirmed(receipt)
return receipt, nil
}
}
}
// publishTx publishes the transaction to the transaction pool. If it receives any underpriced errors
// it will bump the fees and retry.
// Returns the latest fee bumped tx, and a boolean indicating whether the tx was sent or not
func (m *SimpleTxManager) publishTx(ctx context.Context, tx *types.Transaction, sendState *SendState) (*types.Transaction, bool) {
l := m.txLogger(tx, true)
l.Info("Publishing transaction")
for {
if sendState.bumpFees {
if newTx, err := m.increaseGasPrice(ctx, tx); err != nil {
l.Warn("unable to increase gas, will try to re-publish the tx", "err", err)
m.metr.TxPublished("bump_failed")
// Even if we are unable to bump fees, we must still resubmit the transaction
// because a previously successfully published tx can get dropped from the
// mempool. If we don't try to resubmit it to either force a failure (eg. from
// nonce to low errors) or get it back into the mempool, we can end up waiting on
// it to get mined indefinitely.
} else {
if sendState.IsWaitingForConfirmation() {
// A previously published tx might get mined during the increaseGasPrice call
// above, in which case we can abort trying to replace it with a higher fee tx.
return tx, false
}
sendState.bumpCount++
tx = newTx
l = m.txLogger(tx, true)
// Disable bumping fees again until the new transaction is successfully published,
// or we immediately get another underpriced error.
sendState.bumpFees = false
}
}
cCtx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
err := m.backend.SendTransaction(cCtx, tx)
cancel()
sendState.ProcessSendError(err)
if err == nil {
m.metr.TxPublished("")
l.Info("Transaction successfully published", "tx", tx.Hash())
// Tx made it into the mempool, so we'll need a fee bump if we end up trying to replace
// it with another publish attempt.
sendState.bumpFees = true
return tx, true
}
switch {
case errStringMatch(err, txpool.ErrAlreadyReserved):
// this can happen if, say, a blob transaction is stuck in the mempool and we try to
// send a non-blob transaction (and vice-versa).
l.Warn("txpool contains pending tx of incompatible type", "err", err)
m.metr.TxPublished("pending_tx_of_incompatible_type")
case errStringMatch(err, core.ErrNonceTooLow):
l.Warn("nonce too low", "err", err)
m.metr.TxPublished("nonce_too_low")
case errStringMatch(err, context.Canceled):
m.metr.RPCError()
l.Warn("transaction send canceled", "err", err)
m.metr.TxPublished("context_canceled")
case errStringMatch(err, txpool.ErrAlreadyKnown):
l.Warn("resubmitted already known transaction", "err", err)
m.metr.TxPublished("tx_already_known")
case errStringMatch(err, txpool.ErrReplaceUnderpriced):
l.Warn("transaction replacement is underpriced", "err", err)
m.metr.TxPublished("tx_replacement_underpriced")
// retry tx with fee bump, unless we already just tried to bump them
if !sendState.bumpFees {
sendState.bumpFees = true
continue
}
case errStringMatch(err, txpool.ErrUnderpriced):
l.Warn("transaction is underpriced", "err", err)
m.metr.TxPublished("tx_underpriced")
// retry tx with fee bump, unless we already just tried to bump them
if !sendState.bumpFees {
sendState.bumpFees = true
continue
}
default:
m.metr.RPCError()
l.Error("unable to publish transaction", "err", err)
m.metr.TxPublished("unknown_error")
}
return tx, false
}
}
// waitForTx calls waitMined, and then sends the receipt to receiptChan in a non-blocking way if a receipt is found
// for the transaction. It should be called in a separate goroutine.
func (m *SimpleTxManager) waitForTx(ctx context.Context, tx *types.Transaction, sendState *SendState, receiptChan chan *types.Receipt) {
t := time.Now()
// Poll for the transaction to be ready & then send the result to receiptChan
receipt, err := m.waitMined(ctx, tx, sendState)
if err != nil {
// this will happen if the tx was successfully replaced by a tx with bumped fees
m.txLogger(tx, true).Info("Transaction receipt not found", "err", err)
return
}
select {
case receiptChan <- receipt:
m.metr.RecordTxConfirmationLatency(time.Since(t).Milliseconds())
default:
}
}
// waitMined waits for the transaction to be mined or for the context to be canceled.
func (m *SimpleTxManager) waitMined(ctx context.Context, tx *types.Transaction, sendState *SendState) (*types.Receipt, error) {
txHash := tx.Hash()
queryTicker := time.NewTicker(m.cfg.ReceiptQueryInterval)
defer queryTicker.Stop()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-queryTicker.C:
if receipt := m.queryReceipt(ctx, txHash, sendState); receipt != nil {
return receipt, nil
}
}
}
}
// queryReceipt queries for the receipt and returns the receipt if it has passed the confirmation depth
func (m *SimpleTxManager) queryReceipt(ctx context.Context, txHash common.Hash, sendState *SendState) *types.Receipt {
ctx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
receipt, err := m.backend.TransactionReceipt(ctx, txHash)
if errors.Is(err, ethereum.NotFound) {
sendState.TxNotMined(txHash)
m.l.Trace("Transaction not yet mined", "tx", txHash)
return nil
} else if err != nil {
m.metr.RPCError()
m.l.Info("Receipt retrieval failed", "tx", txHash, "err", err)
return nil
} else if receipt == nil {
m.metr.RPCError()
m.l.Warn("Receipt and error are both nil", "tx", txHash)
return nil
}
// Receipt is confirmed to be valid from this point on
sendState.TxMined(txHash)
txHeight := receipt.BlockNumber.Uint64()
tip, err := m.backend.HeaderByNumber(ctx, nil)
if err != nil {
m.metr.RPCError()
m.l.Error("Unable to fetch tip", "err", err)
return nil
}
m.metr.RecordBaseFee(tip.BaseFee)
if tip.ExcessBlobGas != nil {
blobFee := eip4844.CalcBlobFee(*tip.ExcessBlobGas)
m.metr.RecordBlobBaseFee(blobFee)
}
m.l.Debug("Transaction mined, checking confirmations", "tx", txHash,
"block", eth.ReceiptBlockID(receipt), "tip", eth.HeaderBlockID(tip),
"numConfirmations", m.cfg.NumConfirmations)
// The transaction is considered confirmed when
// txHeight+numConfirmations-1 <= tipHeight. Note that the -1 is
// needed to account for the fact that confirmations have an
// inherent off-by-one, i.e. when using 1 confirmation the
// transaction should be confirmed when txHeight is equal to
// tipHeight. The equation is rewritten in this form to avoid
// underflows.
tipHeight := tip.Number.Uint64()
if txHeight+m.cfg.NumConfirmations <= tipHeight+1 {
m.l.Info("Transaction confirmed", "tx", txHash,
"block", eth.ReceiptBlockID(receipt),
"effectiveGasPrice", receipt.EffectiveGasPrice)
return receipt
}
// Safe to subtract since we know the LHS above is greater.
confsRemaining := (txHeight + m.cfg.NumConfirmations) - (tipHeight + 1)
m.l.Debug("Transaction not yet confirmed", "tx", txHash, "confsRemaining", confsRemaining)
return nil
}
// increaseGasPrice returns a new transaction that is equivalent to the input transaction but with
// higher fees that should satisfy geth's tx replacement rules. It also computes an updated gas
// limit estimate. To avoid runaway price increases, fees are capped at a `feeLimitMultiplier`
// multiple of the suggested values.
func (m *SimpleTxManager) increaseGasPrice(ctx context.Context, tx *types.Transaction) (*types.Transaction, error) {
m.txLogger(tx, true).Info("bumping gas price for transaction")
tip, baseFee, blobBaseFee, err := m.SuggestGasPriceCaps(ctx)
if err != nil {
m.txLogger(tx, false).Warn("failed to get suggested gas tip and base fee", "err", err)
return nil, err
}
bumpedTip, bumpedFee := updateFees(tx.GasTipCap(), tx.GasFeeCap(), tip, baseFee, tx.Type() == types.BlobTxType, m.l)
if err := m.checkLimits(tip, baseFee, bumpedTip, bumpedFee); err != nil {
return nil, err
}
// Re-estimate gaslimit in case things have changed or a previous gaslimit estimate was wrong
callMsg := ethereum.CallMsg{
From: m.cfg.From,
To: tx.To(),
GasTipCap: bumpedTip,
GasFeeCap: bumpedFee,
Data: tx.Data(),
Value: tx.Value(),
}
var bumpedBlobFee *big.Int
if tx.Type() == types.BlobTxType {
// Blob transactions have an additional blob gas price we must specify, so we must make sure it is
// getting bumped appropriately.
bumpedBlobFee = calcThresholdValue(tx.BlobGasFeeCap(), true)
if bumpedBlobFee.Cmp(blobBaseFee) < 0 {
bumpedBlobFee = blobBaseFee
}
if err := m.checkBlobFeeLimits(blobBaseFee, bumpedBlobFee); err != nil {
return nil, err
}
callMsg.BlobGasFeeCap = bumpedBlobFee
callMsg.BlobHashes = tx.BlobHashes()
}
gas, err := m.backend.EstimateGas(ctx, callMsg)
if err != nil {
// If this is a transaction resubmission, we sometimes see this outcome because the
// original tx can get included in a block just before the above call. In this case the
// error is due to the tx reverting with message "block number must be equal to next
// expected block number"
m.l.Warn("failed to re-estimate gas", "err", err, "tx", tx.Hash(), "gaslimit", tx.Gas(),
"gasFeeCap", bumpedFee, "gasTipCap", bumpedTip)
return nil, err
}
if tx.Gas() != gas {
// non-determinism in gas limit estimation happens regularly due to underlying state
// changes across calls, and is even more common now that geth uses an in-exact estimation
// approach as of v1.13.6.
m.l.Debug("re-estimated gas differs", "tx", tx.Hash(), "oldgas", tx.Gas(), "newgas", gas,
"gasFeeCap", bumpedFee, "gasTipCap", bumpedTip)
}
if tx.Gas() > gas {
// Don't bump the gas limit down if the passed-in gas limit is higher than
// what was originally specified.
gas = tx.Gas()
}
var newTx *types.Transaction
if tx.Type() == types.BlobTxType {
message := &types.BlobTx{
Nonce: tx.Nonce(),
To: *tx.To(),
Data: tx.Data(),
Gas: gas,
BlobHashes: tx.BlobHashes(),
Sidecar: tx.BlobTxSidecar(),
}
if err := finishBlobTx(message, tx.ChainId(), bumpedTip, bumpedFee, bumpedBlobFee, tx.Value()); err != nil {
return nil, err
}
newTx = types.NewTx(message)
} else {
newTx = types.NewTx(&types.DynamicFeeTx{
ChainID: tx.ChainId(),
Nonce: tx.Nonce(),
To: tx.To(),
GasTipCap: bumpedTip,
GasFeeCap: bumpedFee,
Value: tx.Value(),
Data: tx.Data(),
Gas: gas,
})
}
ctx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
signedTx, err := m.cfg.Signer(ctx, m.cfg.From, newTx)
if err != nil {
m.l.Warn("failed to sign new transaction", "err", err, "tx", tx.Hash())
return tx, nil
}
return signedTx, nil
}
// SuggestGasPriceCaps suggests what the new tip, base fee, and blob base fee should be based on
// the current L1 conditions. `blobBaseFee` will be nil if 4844 is not yet active.
func (m *SimpleTxManager) SuggestGasPriceCaps(ctx context.Context) (*big.Int, *big.Int, *big.Int, error) {
cCtx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
estimatorFn := m.gasPriceEstimatorFn
if estimatorFn == nil {
estimatorFn = DefaultGasPriceEstimatorFn
}
tip, baseFee, blobFee, err := estimatorFn(cCtx, m.backend)
if err != nil {
m.metr.RPCError()
return nil, nil, nil, fmt.Errorf("failed to get gas price estimates: %w", err)
}
m.metr.RecordTipCap(tip)
m.metr.RecordBaseFee(baseFee)
m.metr.RecordBlobBaseFee(blobFee)
// Enforce minimum base fee and tip cap
minTipCap := m.cfg.MinTipCap.Load()
minBaseFee := m.cfg.MinBaseFee.Load()
if minTipCap != nil && tip.Cmp(minTipCap) == -1 {
m.l.Debug("Enforcing min tip cap", "minTipCap", minTipCap, "origTipCap", tip)
tip = new(big.Int).Set(minTipCap)
}
if minBaseFee != nil && baseFee.Cmp(minBaseFee) == -1 {
m.l.Debug("Enforcing min base fee", "minBaseFee", minBaseFee, "origBaseFee", baseFee)
baseFee = new(big.Int).Set(minBaseFee)
}
return tip, baseFee, blobFee, nil
}
// checkLimits checks that the tip and baseFee have not increased by more than the configured multipliers
// if FeeLimitThreshold is specified in config, any increase which stays under the threshold are allowed
func (m *SimpleTxManager) checkLimits(tip, baseFee, bumpedTip, bumpedFee *big.Int) (errs error) {
threshold := m.cfg.FeeLimitThreshold.Load()
feeLimitMultiplier := m.cfg.FeeLimitMultiplier.Load()
limit := big.NewInt(int64(feeLimitMultiplier))
maxTip := new(big.Int).Mul(tip, limit)
maxFee := calcGasFeeCap(new(big.Int).Mul(baseFee, limit), maxTip)
// generic check function to check tip and fee, and build up an error
check := func(v, max *big.Int, name string) {
// if threshold is specified and the value is under the threshold, no need to check the max
if threshold != nil && threshold.Cmp(v) > 0 {
return
}
// if the value is over the max, add an error message
if v.Cmp(max) > 0 {
errs = errors.Join(errs, fmt.Errorf("bumped %s cap %v is over %dx multiple of the suggested value", name, v, limit))
}
}
check(bumpedTip, maxTip, "tip")
check(bumpedFee, maxFee, "fee")
return errs
}
func (m *SimpleTxManager) checkBlobFeeLimits(blobBaseFee, bumpedBlobFee *big.Int) error {
// If below threshold, don't apply multiplier limit. Note we use same threshold parameter here
// used for non-blob fee limiting.
feeLimitThreshold := m.cfg.FeeLimitThreshold.Load()
feeLimitMultiplier := m.cfg.FeeLimitMultiplier.Load()
if feeLimitThreshold != nil && feeLimitThreshold.Cmp(bumpedBlobFee) == 1 {
return nil
}
maxBlobFee := new(big.Int).Mul(m.calcBlobFeeCap(blobBaseFee), big.NewInt(int64(feeLimitMultiplier)))
if bumpedBlobFee.Cmp(maxBlobFee) > 0 {
return fmt.Errorf(
"bumped blob fee %v is over %dx multiple of the suggested value: %w",
bumpedBlobFee, feeLimitMultiplier, ErrBlobFeeLimit)
}
return nil
}
// IsClosed returns true if the tx manager is closed.
func (m *SimpleTxManager) IsClosed() bool {
return m.closed.Load()
}
// calcThresholdValue returns ceil(x * priceBumpPercent / 100) for non-blob txs, or
// ceil(x * blobPriceBumpPercent / 100) for blob txs.
// It guarantees that x is increased by at least 1
func calcThresholdValue(x *big.Int, isBlobTx bool) *big.Int {
threshold := new(big.Int)
if isBlobTx {
threshold.Set(blobPriceBumpPercent)
} else {
threshold.Set(priceBumpPercent)
}
return threshold.Mul(threshold, x).Add(threshold, ninetyNine).Div(threshold, oneHundred)
}
// updateFees takes an old transaction's tip & fee cap plus a new tip & base fee, and returns
// a suggested tip and fee cap such that:
//
// (a) each satisfies geth's required tx-replacement fee bumps, and
// (b) gasTipCap is no less than new tip, and
// (c) gasFeeCap is no less than calcGasFee(newBaseFee, newTip)
func updateFees(oldTip, oldFeeCap, newTip, newBaseFee *big.Int, isBlobTx bool, lgr log.Logger) (*big.Int, *big.Int) {
newFeeCap := calcGasFeeCap(newBaseFee, newTip)
lgr = lgr.New("old_gasTipCap", oldTip, "old_gasFeeCap", oldFeeCap,
"new_gasTipCap", newTip, "new_gasFeeCap", newFeeCap, "new_baseFee", newBaseFee)
thresholdTip := calcThresholdValue(oldTip, isBlobTx)