forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_txmgr.go
94 lines (83 loc) · 2.47 KB
/
test_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
package txmgr
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type TestTxManager struct {
*SimpleTxManager
ss *SendState
tx *types.Transaction
}
// JamTxPool sends a transaction intended to get stuck in the txpool, and should be used ONLY for testing.
// It is non-blocking. See WaitOnJammingTx if you wish to wait on the transaction to clear.
func (m *TestTxManager) JamTxPool(ctx context.Context, candidate TxCandidate) error {
var err error
m.tx, err = m.makeStuckTx(ctx, candidate)
if err != nil {
return err
}
m.ss = NewSendState(m.cfg.SafeAbortNonceTooLowCount, m.cfg.TxNotInMempoolTimeout)
if err := m.backend.SendTransaction(ctx, m.tx); err != nil {
return err
}
return nil
}
// WaitOnJammingTx can be called after JamTxPool in order to wait on the jam transaction clearing.
func (m *TestTxManager) WaitOnJammingTx(ctx context.Context) error {
if m.ss == nil {
return errors.New("WaitOnJammingTx called without first calling JamTxPool")
}
_, err := m.waitMined(ctx, m.tx, m.ss)
return err
}
func (m *TestTxManager) makeStuckTx(ctx context.Context, candidate TxCandidate) (*types.Transaction, error) {
gasTipCap, _, blobBaseFee, err := m.SuggestGasPriceCaps(ctx)
if err != nil {
return nil, err
}
// override with minimal fees to make sure tx gets stuck in the pool
gasFeeCap := big.NewInt(2)
gasTipCap.SetUint64(1)
var sidecar *types.BlobTxSidecar
var blobHashes []common.Hash
if len(candidate.Blobs) > 0 {
if sidecar, blobHashes, err = MakeSidecar(candidate.Blobs); err != nil {
return nil, err
}
}
nonce, err := m.backend.NonceAt(ctx, m.cfg.From, nil)
if err != nil {
return nil, err
}
var txMessage types.TxData
if sidecar != nil {
blobFeeCap := m.calcBlobFeeCap(blobBaseFee)
message := &types.BlobTx{
To: *candidate.To,
Data: candidate.TxData,
Gas: candidate.GasLimit,
BlobHashes: blobHashes,
Sidecar: sidecar,
Nonce: nonce,
}
if err := finishBlobTx(message, m.chainID, gasTipCap, gasFeeCap, blobFeeCap, candidate.Value); err != nil {
return nil, err
}
txMessage = message
} else {
txMessage = &types.DynamicFeeTx{
ChainID: m.chainID,
To: candidate.To,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: candidate.Value,
Data: candidate.TxData,
Gas: candidate.GasLimit,
Nonce: nonce,
}
}
return m.cfg.Signer(ctx, m.cfg.From, types.NewTx(txMessage))
}