@@ -108,6 +108,7 @@ def exact_size_transactions(
108108 block_size_limit : int ,
109109 fork : Fork ,
110110 pre : Alloc ,
111+ gas_limit : int ,
111112 emit_logs : bool = False ,
112113 specific_transaction_to_include : Transaction | None = None ,
113114) -> Tuple [List [Transaction ], int ]:
@@ -122,6 +123,7 @@ def exact_size_transactions(
122123 block_size_limit: The target block RLP size limit
123124 fork: The fork to generate transactions for
124125 pre: Required if emit_logs is True, used to deploy the log contract
126+ gas_limit: The gas limit for the block
125127 emit_logs: If True, transactions will call a contract that emits logs
126128 specific_transaction_to_include: If provided, this transaction will be included
127129
@@ -153,45 +155,53 @@ def exact_size_transactions(
153155
154156 if not specific_transaction_to_include :
155157 # use cached version when possible for performance
156- stubbed_transactions , gas_used = _exact_size_transactions_cached (
157- block_size_limit , fork , emit_logs_contract = log_contract
158+ transactions , gas_used = _exact_size_transactions_cached (
159+ block_size_limit ,
160+ fork ,
161+ gas_limit ,
162+ sender ,
163+ emit_logs_contract = log_contract ,
158164 )
159165 else :
160166 # Direct calculation, no cache, since `Transaction` is not hashable
161- stubbed_transactions , gas_used = _exact_size_transactions_impl (
167+ transactions , gas_used = _exact_size_transactions_impl (
162168 block_size_limit ,
163169 fork ,
170+ gas_limit ,
171+ sender ,
164172 specific_transaction_to_include = specific_transaction_to_include ,
165173 )
166174
167- test_transactions = []
168- for tx in stubbed_transactions :
169- # Create a new transaction with the correct sender, preserving all other fields
170- tx_dict = tx .model_dump (exclude_unset = True )
171- tx_dict .pop ("r" , None )
172- tx_dict .pop ("s" , None )
173- tx_dict .pop ("v" , None )
174- tx_dict ["sender" ] = sender
175- test_transactions .append (Transaction (** tx_dict ))
176- return test_transactions , gas_used
175+ return transactions , gas_used
177176
178177
179178@lru_cache (maxsize = 128 )
180179def _exact_size_transactions_cached (
181180 block_size_limit : int ,
182181 fork : Fork ,
182+ gas_limit : int ,
183+ sender : EOA ,
183184 emit_logs_contract : Address | None = None ,
184185) -> Tuple [List [Transaction ], int ]:
185186 """
186187 Generate transactions that fill a block to exactly the RLP size limit. Abstracted
187188 with hashable arguments for caching block calculations.
188189 """
189- return _exact_size_transactions_impl (block_size_limit , fork , None , emit_logs_contract )
190+ return _exact_size_transactions_impl (
191+ block_size_limit ,
192+ fork ,
193+ gas_limit ,
194+ sender ,
195+ None ,
196+ emit_logs_contract ,
197+ )
190198
191199
192200def _exact_size_transactions_impl (
193201 block_size_limit : int ,
194202 fork : Fork ,
203+ block_gas_limit : int ,
204+ sender : EOA ,
195205 specific_transaction_to_include : Transaction | None = None ,
196206 emit_logs_contract : Address | None = None ,
197207) -> Tuple [List [Transaction ], int ]:
@@ -200,10 +210,8 @@ def _exact_size_transactions_impl(
200210 non-cached paths.
201211 """
202212 transactions = []
203- sender = EOA ("0x" + "00" * 20 , key = 123 )
204213 nonce = 0
205214 total_gas_used = 0
206- max_block_gas = 100_000_000
207215
208216 calculator = fork .transaction_intrinsic_cost_calculator ()
209217
@@ -268,7 +276,7 @@ def _exact_size_transactions_impl(
268276
269277 current_size = get_block_rlp_size (transactions , gas_used = total_gas_used )
270278 remaining_bytes = block_size_limit - current_size
271- remaining_gas = max_block_gas - total_gas_used
279+ remaining_gas = block_gas_limit - total_gas_used
272280
273281 if remaining_bytes > 0 and remaining_gas > 50_000 :
274282 # create an empty transaction to measure base contribution
@@ -360,7 +368,9 @@ def _exact_size_transactions_impl(
360368 final_gas = sum (tx .gas_limit for tx in transactions )
361369
362370 assert final_size == block_size_limit , (
363- f"Size mismatch: got { final_size } , expected { block_size_limit } "
371+ f"Size mismatch: got { final_size } , "
372+ f"expected { block_size_limit } "
373+ f"({ final_size - block_size_limit } bytes diff)"
364374 )
365375 return transactions , final_gas
366376
@@ -395,6 +405,7 @@ def test_block_at_rlp_size_limit_boundary(
395405 block_size_limit ,
396406 fork ,
397407 pre ,
408+ env .gas_limit ,
398409 )
399410 block_rlp_size = get_block_rlp_size (transactions , gas_used = gas_used )
400411 assert block_rlp_size == block_size_limit , (
@@ -441,6 +452,7 @@ def test_block_rlp_size_at_limit_with_all_typed_transactions(
441452 block_size_limit ,
442453 fork ,
443454 pre ,
455+ env .gas_limit ,
444456 specific_transaction_to_include = typed_transaction ,
445457 )
446458 block_rlp_size = get_block_rlp_size (transactions , gas_used = gas_used )
@@ -477,6 +489,7 @@ def test_block_at_rlp_limit_with_logs(
477489 block_size_limit ,
478490 fork ,
479491 pre ,
492+ env .gas_limit ,
480493 emit_logs = True ,
481494 )
482495
0 commit comments