|
| 1 | +package mempool |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/ethereum/go-ethereum/ethclient" |
| 11 | + "github.com/ethereum/go-ethereum/log" |
| 12 | +) |
| 13 | + |
| 14 | +// ReplayMempool fetches transactions from an external node and replays them. |
| 15 | +// It iterates through blocks from a source node and provides transactions |
| 16 | +// block-by-block for the benchmark to replay. |
| 17 | +type ReplayMempool struct { |
| 18 | + log log.Logger |
| 19 | + client *ethclient.Client |
| 20 | + |
| 21 | + lock sync.Mutex |
| 22 | + |
| 23 | + // startBlock is the first block to fetch transactions from |
| 24 | + startBlock uint64 |
| 25 | + |
| 26 | + // currentBlock tracks which block we're fetching next |
| 27 | + currentBlock uint64 |
| 28 | + |
| 29 | + // chainID for transaction signing validation |
| 30 | + chainID *big.Int |
| 31 | + |
| 32 | + // addressNonce tracks the latest nonce for each address |
| 33 | + addressNonce map[common.Address]uint64 |
| 34 | +} |
| 35 | + |
| 36 | +// NewReplayMempool creates a new ReplayMempool that fetches transactions |
| 37 | +// from the given RPC endpoint starting from the specified block. |
| 38 | +func NewReplayMempool(log log.Logger, rpcURL string, startBlock uint64, chainID *big.Int) (*ReplayMempool, error) { |
| 39 | + client, err := ethclient.Dial(rpcURL) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + return &ReplayMempool{ |
| 45 | + log: log, |
| 46 | + client: client, |
| 47 | + startBlock: startBlock, |
| 48 | + currentBlock: startBlock, |
| 49 | + chainID: chainID, |
| 50 | + addressNonce: make(map[common.Address]uint64), |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +// AddTransactions is a no-op for ReplayMempool since transactions come from the source node. |
| 55 | +func (m *ReplayMempool) AddTransactions(_ []*types.Transaction) { |
| 56 | + // No-op: transactions are fetched from the source node, not added manually |
| 57 | +} |
| 58 | + |
| 59 | +// NextBlock fetches the next block from the source node and returns its transactions. |
| 60 | +// Returns (mempoolTxs, sequencerTxs) where: |
| 61 | +// - mempoolTxs: regular transactions to be sent via eth_sendRawTransaction |
| 62 | +// - sequencerTxs: deposit transactions to be included in payload attributes |
| 63 | +func (m *ReplayMempool) NextBlock() ([][]byte, [][]byte) { |
| 64 | + m.lock.Lock() |
| 65 | + defer m.lock.Unlock() |
| 66 | + |
| 67 | + ctx := context.Background() |
| 68 | + |
| 69 | + block, err := m.client.BlockByNumber(ctx, big.NewInt(int64(m.currentBlock))) |
| 70 | + if err != nil { |
| 71 | + m.log.Warn("Failed to fetch block", "block", m.currentBlock, "error", err) |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + m.log.Info("Fetched block for replay", |
| 76 | + "block", m.currentBlock, |
| 77 | + "txs", len(block.Transactions()), |
| 78 | + "gas_used", block.GasUsed(), |
| 79 | + ) |
| 80 | + |
| 81 | + m.currentBlock++ |
| 82 | + |
| 83 | + mempoolTxs := make([][]byte, 0) |
| 84 | + sequencerTxs := make([][]byte, 0) |
| 85 | + |
| 86 | + for _, tx := range block.Transactions() { |
| 87 | + // Track nonces for GetTransactionCount |
| 88 | + from, err := types.Sender(types.NewIsthmusSigner(m.chainID), tx) |
| 89 | + if err != nil { |
| 90 | + // Try with London signer for older transactions |
| 91 | + from, err = types.Sender(types.NewLondonSigner(m.chainID), tx) |
| 92 | + if err != nil { |
| 93 | + m.log.Warn("Failed to get sender", "tx", tx.Hash(), "error", err) |
| 94 | + continue |
| 95 | + } |
| 96 | + } |
| 97 | + m.addressNonce[from] = tx.Nonce() |
| 98 | + |
| 99 | + txBytes, err := tx.MarshalBinary() |
| 100 | + if err != nil { |
| 101 | + m.log.Warn("Failed to marshal transaction", "tx", tx.Hash(), "error", err) |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + // Deposit transactions go to sequencer, others go to mempool |
| 106 | + if tx.Type() == types.DepositTxType { |
| 107 | + sequencerTxs = append(sequencerTxs, txBytes) |
| 108 | + } else { |
| 109 | + mempoolTxs = append(mempoolTxs, txBytes) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return mempoolTxs, sequencerTxs |
| 114 | +} |
| 115 | + |
| 116 | +// GetTransactionCount returns the latest nonce for an address. |
| 117 | +func (m *ReplayMempool) GetTransactionCount(address common.Address) uint64 { |
| 118 | + m.lock.Lock() |
| 119 | + defer m.lock.Unlock() |
| 120 | + return m.addressNonce[address] |
| 121 | +} |
| 122 | + |
| 123 | +// CurrentBlock returns the current block number being replayed. |
| 124 | +func (m *ReplayMempool) CurrentBlock() uint64 { |
| 125 | + m.lock.Lock() |
| 126 | + defer m.lock.Unlock() |
| 127 | + return m.currentBlock |
| 128 | +} |
| 129 | + |
| 130 | +// Close closes the underlying RPC client connection. |
| 131 | +func (m *ReplayMempool) Close() { |
| 132 | + m.client.Close() |
| 133 | +} |
| 134 | + |
| 135 | +var _ FakeMempool = &ReplayMempool{} |
| 136 | + |
0 commit comments