-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathtest_blob_txs.py
More file actions
1395 lines (1263 loc) · 40.9 KB
/
test_blob_txs.py
File metadata and controls
1395 lines (1263 loc) · 40.9 KB
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
"""
abstract: Tests blob type transactions for [EIP-4844: Shard Blob Transactions](https://eips.ethereum.org/EIPS/eip-4844)
Test blob type transactions for [EIP-4844: Shard Blob Transactions](https://eips.ethereum.org/EIPS/eip-4844).
note: Adding a new test
Add a function that is named `test_<test_name>` and takes at least the following arguments:
- blockchain_test or state_test
- pre
- env
- block or txs
All other `pytest.fixture` fixtures can be parametrized to generate new combinations and test cases.
""" # noqa: E501
from typing import List, Optional, Tuple
import pytest
from ethereum_test_forks import Fork
from ethereum_test_tools import (
EOA,
AccessList,
Account,
Address,
Alloc,
Block,
BlockchainTestFiller,
BlockException,
Bytecode,
EngineAPIError,
Environment,
Hash,
Header,
Removable,
StateTestFiller,
Storage,
Transaction,
TransactionException,
add_kzg_version,
)
from ethereum_test_tools import Opcodes as Op
from .spec import Spec, SpecHelpers, ref_spec_4844
REFERENCE_SPEC_GIT_PATH = ref_spec_4844.git_path
REFERENCE_SPEC_VERSION = ref_spec_4844.version
@pytest.fixture
def destination_account_code() -> Bytecode | None:
"""Code in the destination account for the blob transactions."""
return None
@pytest.fixture
def destination_account_balance() -> int:
"""Balance in the destination account for the blob transactions."""
return 0
@pytest.fixture
def destination_account(
pre: Alloc, destination_account_code: Bytecode | None, destination_account_balance: int
) -> Address:
"""Destination account for the blob transactions."""
if destination_account_code is not None:
return pre.deploy_contract(
code=destination_account_code,
balance=destination_account_balance,
)
return pre.fund_eoa(destination_account_balance)
@pytest.fixture
def tx_gas(
fork: Fork,
tx_calldata: bytes,
tx_access_list: List[AccessList],
) -> int:
"""Gas allocated to transactions sent during test."""
tx_intrinsic_cost_calculator = fork.transaction_intrinsic_cost_calculator()
return tx_intrinsic_cost_calculator(calldata=tx_calldata, access_list=tx_access_list)
@pytest.fixture
def blobs_per_tx() -> List[int]:
"""
Return list of integers that each represent the number of blobs in each
transaction in the block of the test.
Used to automatically generate a list of correctly versioned blob hashes.
Default is to have one transaction with one blob.
Can be overloaded by a test case to provide a custom list of blob counts.
"""
return [1]
@pytest.fixture
def blob_hashes_per_tx(blobs_per_tx: List[int]) -> List[List[Hash]]:
"""
Produce the list of blob hashes that are sent during the test.
Can be overloaded by a test case to provide a custom list of blob hashes.
"""
return [
add_kzg_version(
[Hash(x) for x in range(blob_count)],
Spec.BLOB_COMMITMENT_VERSION_KZG,
)
for blob_count in blobs_per_tx
]
@pytest.fixture
def total_account_minimum_balance( # noqa: D103
blob_gas_per_blob: int,
tx_gas: int,
tx_value: int,
tx_max_fee_per_gas: int,
tx_max_fee_per_blob_gas: int,
blob_hashes_per_tx: List[List[bytes]],
) -> int:
"""
Calculate minimum balance required for the account to be able to send
the transactions in the block of the test.
"""
minimum_cost = 0
for tx_blob_count in [len(x) for x in blob_hashes_per_tx]:
blob_cost = tx_max_fee_per_blob_gas * blob_gas_per_blob * tx_blob_count
minimum_cost += (tx_gas * tx_max_fee_per_gas) + tx_value + blob_cost
return minimum_cost
@pytest.fixture
def total_account_transactions_fee( # noqa: D103
tx_gas: int,
tx_value: int,
blob_gas_price: int,
block_base_fee_per_gas: int,
blob_gas_per_blob: int,
tx_max_fee_per_gas: int,
tx_max_priority_fee_per_gas: int,
blob_hashes_per_tx: List[List[bytes]],
) -> int:
"""Calculate actual fee for the blob transactions in the block of the test."""
total_cost = 0
for tx_blob_count in [len(x) for x in blob_hashes_per_tx]:
blob_cost = blob_gas_price * blob_gas_per_blob * tx_blob_count
block_producer_fee = (
tx_max_fee_per_gas - block_base_fee_per_gas if tx_max_priority_fee_per_gas else 0
)
total_cost += (
(tx_gas * (block_base_fee_per_gas + block_producer_fee)) + tx_value + blob_cost
)
return total_cost
@pytest.fixture
def tx_access_list() -> List[AccessList]:
"""
Access list for transactions sent during test.
Can be overloaded by a test case to provide a custom access list.
"""
return []
@pytest.fixture
def tx_error() -> Optional[TransactionException]:
"""
Error produced by the block transactions (no error).
Can be overloaded on test cases where the transactions are expected
to fail.
"""
return None
@pytest.fixture
def sender_initial_balance( # noqa: D103
total_account_minimum_balance: int, account_balance_modifier: int
) -> int:
return total_account_minimum_balance + account_balance_modifier
@pytest.fixture
def sender(pre: Alloc, sender_initial_balance: int) -> Address: # noqa: D103
return pre.fund_eoa(sender_initial_balance)
@pytest.fixture
def txs( # noqa: D103
sender: EOA,
destination_account: Optional[Address],
tx_gas: int,
tx_value: int,
tx_calldata: bytes,
tx_max_fee_per_gas: int,
tx_max_fee_per_blob_gas: int,
tx_max_priority_fee_per_gas: int,
tx_access_list: List[AccessList],
blob_hashes_per_tx: List[List[bytes]],
tx_error: Optional[TransactionException],
) -> List[Transaction]:
"""Prepare the list of transactions that are sent during the test."""
return [
Transaction(
ty=Spec.BLOB_TX_TYPE,
sender=sender,
to=destination_account,
value=tx_value,
gas_limit=tx_gas,
data=tx_calldata,
max_fee_per_gas=tx_max_fee_per_gas,
max_priority_fee_per_gas=tx_max_priority_fee_per_gas,
max_fee_per_blob_gas=tx_max_fee_per_blob_gas,
access_list=tx_access_list,
blob_versioned_hashes=blob_hashes,
error=tx_error if tx_i == (len(blob_hashes_per_tx) - 1) else None,
)
for tx_i, blob_hashes in enumerate(blob_hashes_per_tx)
]
@pytest.fixture
def account_balance_modifier() -> int:
"""
Account balance modifier for the source account of all tests.
See `pre` fixture.
"""
return 0
@pytest.fixture
def state_env(
excess_blob_gas: Optional[int],
) -> Environment:
"""
Prepare the environment for all state test cases.
Main difference is that the excess blob gas is not increased by the target, as
there is no genesis block -> block 1 transition, and therefore the excess blob gas
is not decreased by the target.
"""
return Environment(
excess_blob_gas=excess_blob_gas if excess_blob_gas else 0,
)
@pytest.fixture
def engine_api_error_code() -> Optional[EngineAPIError]:
"""
Engine API error code to be returned by the client on consumption
of the erroneous block in hive.
"""
return None
@pytest.fixture
def block_error(
tx_error: Optional[TransactionException],
) -> Optional[TransactionException | BlockException]:
"""
Error produced by the block transactions (no error).
Can be overloaded on test cases where the transactions are expected
to fail.
"""
return tx_error
@pytest.fixture
def block_number() -> int:
"""First block number."""
return 1
@pytest.fixture
def block_timestamp() -> int:
"""Timestamp of the first block."""
return 1
@pytest.fixture
def expected_blob_gas_used(
fork: Fork,
txs: List[Transaction],
block_number: int,
block_timestamp: int,
) -> Optional[int | Removable]:
"""Calculate blob gas used by the test block."""
if not fork.header_blob_gas_used_required(
block_number=block_number, timestamp=block_timestamp
):
return Header.EMPTY_FIELD
blob_gas_per_blob = fork.blob_gas_per_blob(
block_number=block_number,
timestamp=block_timestamp,
)
return sum([Spec.get_total_blob_gas(tx=tx, blob_gas_per_blob=blob_gas_per_blob) for tx in txs])
@pytest.fixture
def expected_excess_blob_gas(
fork: Fork,
parent_excess_blobs: Optional[int],
parent_blobs: Optional[int],
block_number: int,
block_timestamp: int,
block_base_fee_per_gas: int,
) -> Optional[int | Removable]:
"""Calculate blob gas used by the test block."""
if not fork.header_excess_blob_gas_required(
block_number=block_number, timestamp=block_timestamp
):
return Header.EMPTY_FIELD
excess_blob_gas = fork.excess_blob_gas_calculator()
return excess_blob_gas(
parent_excess_blobs=parent_excess_blobs if parent_excess_blobs else 0,
parent_blob_count=parent_blobs if parent_blobs else 0,
parent_base_fee_per_gas=block_base_fee_per_gas,
)
@pytest.fixture
def header_verify(
txs: List[Transaction],
expected_blob_gas_used: Optional[int | Removable],
expected_excess_blob_gas: Optional[int | Removable],
) -> Header:
"""Header fields to verify from the transition tool."""
header_verify = Header(
blob_gas_used=expected_blob_gas_used,
excess_blob_gas=expected_excess_blob_gas,
gas_used=0 if len([tx for tx in txs if not tx.error]) == 0 else None,
)
return header_verify
@pytest.fixture
def rlp_modifier(
expected_blob_gas_used: Optional[int | Removable],
) -> Optional[Header]:
"""Header fields to modify on the output block in the BlockchainTest."""
if expected_blob_gas_used == Header.EMPTY_FIELD:
return None
return Header(
blob_gas_used=expected_blob_gas_used,
)
@pytest.fixture
def block(
txs: List[Transaction],
block_error: Optional[TransactionException | BlockException],
engine_api_error_code: Optional[EngineAPIError],
header_verify: Optional[Header],
rlp_modifier: Optional[Header],
) -> Block:
"""Test block for all blockchain test cases."""
return Block(
txs=txs,
exception=block_error,
engine_api_error_code=engine_api_error_code,
header_verify=header_verify,
rlp_modifier=rlp_modifier,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
SpecHelpers.all_valid_blob_combinations,
)
@pytest.mark.parametrize("block_base_fee_per_gas", [7, 100])
@pytest.mark.valid_from("Cancun")
def test_valid_blob_tx_combinations(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
env: Environment,
block: Block,
):
"""
Test all valid blob combinations in a single block, assuming a given value of
`MAX_BLOBS_PER_BLOCK`.
This assumes a block can include from 1 and up to `MAX_BLOBS_PER_BLOCK` transactions where all
transactions contain at least 1 blob, and the sum of all blobs in a block is at
most `MAX_BLOBS_PER_BLOCK`.
This test is parametrized with all valid blob transaction combinations for a given block, and
therefore if value of `MAX_BLOBS_PER_BLOCK` changes, this test is automatically updated.
"""
blockchain_test(
pre=pre,
post={},
blocks=[block],
genesis_environment=env,
)
def generate_invalid_tx_max_fee_per_blob_gas_tests(
fork: Fork,
) -> List:
"""
Return a list of tests for invalid blob transactions due to insufficient max fee per blob gas
parametrized for each different fork.
"""
min_base_fee_per_blob_gas = fork.min_base_fee_per_blob_gas()
minimum_excess_blobs_for_first_increment = SpecHelpers.get_min_excess_blobs_for_blob_gas_price(
fork=fork,
blob_gas_price=min_base_fee_per_blob_gas + 1,
)
next_base_fee_per_blob_gas = fork.blob_gas_price_calculator()(
excess_blob_gas=minimum_excess_blobs_for_first_increment,
)
tests = []
tests.append(
pytest.param(
minimum_excess_blobs_for_first_increment - 1, # blob gas price is 1
fork.target_blobs_per_block() + 1, # blob gas cost increases to above the minimum
min_base_fee_per_blob_gas, # tx max_blob_gas_cost is the minimum
TransactionException.INSUFFICIENT_MAX_FEE_PER_BLOB_GAS,
id="insufficient_max_fee_per_blob_gas",
marks=pytest.mark.exception_test,
)
)
if (next_base_fee_per_blob_gas - min_base_fee_per_blob_gas) > 1:
tests.append(
pytest.param(
minimum_excess_blobs_for_first_increment
- 1, # blob gas price is one less than the minimum
fork.target_blobs_per_block() + 1, # blob gas cost increases to above the minimum
next_base_fee_per_blob_gas
- 1, # tx max_blob_gas_cost is one less than the minimum
TransactionException.INSUFFICIENT_MAX_FEE_PER_BLOB_GAS,
id="insufficient_max_fee_per_blob_gas_one_less_than_next",
marks=pytest.mark.exception_test,
)
)
if min_base_fee_per_blob_gas > 1:
tests.append(
pytest.param(
0, # blob gas price is the minimum
0, # blob gas cost stays put at 1
min_base_fee_per_blob_gas - 1, # tx max_blob_gas_cost is one less than the minimum
TransactionException.INSUFFICIENT_MAX_FEE_PER_BLOB_GAS,
id="insufficient_max_fee_per_blob_gas_one_less_than_min",
marks=pytest.mark.exception_test,
)
)
tests.append(
pytest.param(
0, # blob gas price is the minimum
0, # blob gas cost stays put at 1
0, # tx max_blob_gas_cost is 0
TransactionException.INSUFFICIENT_MAX_FEE_PER_BLOB_GAS,
id="invalid_max_fee_per_blob_gas",
marks=pytest.mark.exception_test,
)
)
return tests
@pytest.mark.parametrize_by_fork(
"parent_excess_blobs,parent_blobs,tx_max_fee_per_blob_gas,tx_error",
generate_invalid_tx_max_fee_per_blob_gas_tests,
)
@pytest.mark.parametrize(
"account_balance_modifier",
[1_000_000_000],
) # Extra balance to cover block blob gas cost
@pytest.mark.valid_from("Cancun")
def test_invalid_tx_max_fee_per_blob_gas(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
env: Environment,
block: Block,
non_zero_blob_gas_used_genesis_block: Optional[Block],
):
"""
Reject blocks with invalid blob txs.
- tx max_fee_per_blob_gas is barely not enough
- tx max_fee_per_blob_gas is zero
"""
if non_zero_blob_gas_used_genesis_block is not None:
blocks = [non_zero_blob_gas_used_genesis_block, block]
else:
blocks = [block]
blockchain_test(
pre=pre,
post={},
blocks=blocks,
genesis_environment=env,
)
@pytest.mark.parametrize_by_fork(
"parent_excess_blobs,parent_blobs,tx_max_fee_per_blob_gas,tx_error",
generate_invalid_tx_max_fee_per_blob_gas_tests,
)
@pytest.mark.state_test_only
@pytest.mark.valid_from("Cancun")
def test_invalid_tx_max_fee_per_blob_gas_state(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
):
"""
Reject an invalid blob transaction.
- tx max_fee_per_blob_gas is barely not enough
- tx max_fee_per_blob_gas is zero
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
)
@pytest.mark.parametrize(
"tx_max_fee_per_gas,tx_error",
[
# max blob gas is ok, but max fee per gas is less than base fee per gas
(
6,
TransactionException.INSUFFICIENT_MAX_FEE_PER_GAS,
),
],
ids=["insufficient_max_fee_per_gas"],
)
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_invalid_normal_gas(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
header_verify: Optional[Header],
rlp_modifier: Optional[Header],
):
"""
Reject an invalid blob transaction.
- Sufficient max fee per blob gas, but insufficient max fee per gas
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
blockchain_test_header_verify=header_verify,
blockchain_test_rlp_modifier=rlp_modifier,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
SpecHelpers.invalid_blob_combinations,
)
@pytest.mark.parametrize(
"tx_error",
[
[
TransactionException.TYPE_3_TX_MAX_BLOB_GAS_ALLOWANCE_EXCEEDED,
TransactionException.TYPE_3_TX_BLOB_COUNT_EXCEEDED,
]
],
ids=[""],
)
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_invalid_block_blob_count(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
env: Environment,
block: Block,
):
"""
Test all invalid blob combinations in a single block, where the sum of all blobs in a block is
at `MAX_BLOBS_PER_BLOCK + 1`.
This test is parametrized with all blob transaction combinations exceeding
`MAX_BLOBS_PER_BLOCK` by one for a given block, and
therefore if value of `MAX_BLOBS_PER_BLOCK` changes, this test is automatically updated.
"""
blockchain_test(
pre=pre,
post={},
blocks=[block],
genesis_environment=env,
)
@pytest.mark.parametrize(
"tx_access_list",
[[], [AccessList(address=100, storage_keys=[100, 200])]],
ids=["no_access_list", "access_list"],
)
@pytest.mark.parametrize("tx_max_fee_per_gas", [7, 14])
@pytest.mark.parametrize("tx_max_priority_fee_per_gas", [0, 7])
@pytest.mark.parametrize("tx_value", [0, 1])
@pytest.mark.parametrize(
"tx_calldata",
[b"", b"\x00", b"\x01"],
ids=["no_calldata", "single_zero_calldata", "single_one_calldata"],
)
@pytest.mark.parametrize("tx_max_fee_per_blob_gas_multiplier", [1, 100, 10000])
@pytest.mark.parametrize("account_balance_modifier", [-1], ids=["exact_balance_minus_1"])
@pytest.mark.parametrize("tx_error", [TransactionException.INSUFFICIENT_ACCOUNT_FUNDS], ids=[""])
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_insufficient_balance_blob_tx(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
):
"""
Reject blocks where user cannot afford the blob gas specified (but
max_fee_per_gas would be enough for current block).
- Transactions with max fee equal or higher than current block base fee
- Transactions with and without priority fee
- Transactions with and without value
- Transactions with and without calldata
- Transactions with max fee per blob gas lower or higher than the priority fee
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
lambda fork: [
pytest.param([1], id="single_blob"),
pytest.param([fork.max_blobs_per_tx()], id="max_blobs"),
],
)
@pytest.mark.parametrize(
"tx_access_list",
[[], [AccessList(address=100, storage_keys=[100, 200])]],
ids=["no_access_list", "access_list"],
)
@pytest.mark.parametrize("tx_max_fee_per_gas", [7, 14])
@pytest.mark.parametrize("tx_max_priority_fee_per_gas", [0, 7])
@pytest.mark.parametrize("tx_value", [0, 1])
@pytest.mark.parametrize(
"tx_calldata",
[b"", b"\x00", b"\x01"],
ids=["no_calldata", "single_zero_calldata", "single_one_calldata"],
)
@pytest.mark.parametrize("block_base_fee_per_gas", [7, 100])
@pytest.mark.parametrize("tx_max_fee_per_blob_gas_multiplier", [1, 100, 10000])
@pytest.mark.valid_from("Cancun")
def test_sufficient_balance_blob_tx(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
):
"""
Check that transaction is accepted when user can exactly afford the blob gas specified (and
max_fee_per_gas would be enough for current block).
- Transactions with max fee equal or higher than current block base fee
- Transactions with and without priority fee
- Transactions with and without value
- Transactions with and without calldata
- Transactions with max fee per blob gas lower or higher than the priority fee
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
lambda fork: [
pytest.param([1], id="single_blob"),
pytest.param([fork.max_blobs_per_tx()], id="max_blobs"),
],
)
@pytest.mark.parametrize(
"tx_access_list",
[[], [AccessList(address=100, storage_keys=[100, 200])]],
ids=["no_access_list", "access_list"],
)
@pytest.mark.parametrize("tx_max_fee_per_gas", [7, 14])
@pytest.mark.parametrize("tx_max_priority_fee_per_gas", [0, 7])
@pytest.mark.parametrize("tx_value", [0, 1])
@pytest.mark.parametrize(
"tx_calldata",
[b"", b"\x00", b"\x01"],
ids=["no_calldata", "single_zero_calldata", "single_one_calldata"],
)
@pytest.mark.parametrize("tx_max_fee_per_blob_gas_multiplier", [1, 100, 10000])
@pytest.mark.parametrize("sender_initial_balance", [0])
@pytest.mark.valid_from("Cancun")
def test_sufficient_balance_blob_tx_pre_fund_tx(
blockchain_test: BlockchainTestFiller,
total_account_minimum_balance: int,
sender: EOA,
env: Environment,
pre: Alloc,
txs: List[Transaction],
header_verify: Optional[Header],
):
"""
Check that transaction is accepted when user can exactly afford the blob gas specified (and
max_fee_per_gas would be enough for current block) because a funding transaction is
prepended in the same block.
- Transactions with max fee equal or higher than current block base fee
- Transactions with and without priority fee
- Transactions with and without value
- Transactions with and without calldata
- Transactions with max fee per blob gas lower or higher than the priority fee
"""
pre_funding_sender = pre.fund_eoa(amount=(21_000 * 100) + total_account_minimum_balance)
txs = [
Transaction(
sender=pre_funding_sender,
to=sender,
value=total_account_minimum_balance,
gas_limit=21_000,
)
] + txs
blockchain_test(
pre=pre,
post={},
blocks=[
Block(
txs=txs,
header_verify=header_verify,
)
],
genesis_environment=env,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
lambda fork: [
pytest.param([1], id="single_blob"),
pytest.param([fork.max_blobs_per_tx()], id="max_blobs"),
],
)
@pytest.mark.parametrize(
"tx_access_list",
[[], [AccessList(address=100, storage_keys=[100, 200])]],
ids=["no_access_list", "access_list"],
)
@pytest.mark.parametrize("tx_max_fee_per_gas", [7, 14])
@pytest.mark.parametrize("tx_max_priority_fee_per_gas", [0, 7])
@pytest.mark.parametrize("tx_value", [0, 1])
@pytest.mark.parametrize(
"tx_calldata",
[b"", b"\x01"],
ids=["no_calldata", "single_non_zero_byte_calldata"],
)
@pytest.mark.parametrize("tx_max_fee_per_blob_gas_multiplier", [1, 100])
@pytest.mark.parametrize(
"tx_gas", [500_000], ids=[""]
) # Increase gas to account for contract code
@pytest.mark.parametrize(
"destination_account_balance", [100], ids=["100_wei_mid_execution"]
) # Amount sent by the contract to the sender mid execution
@pytest.mark.parametrize(
"destination_account_code",
[
Op.SSTORE(0, Op.BALANCE(Op.ORIGIN))
+ Op.CALL(Op.GAS, Op.ORIGIN, Op.SUB(Op.SELFBALANCE, Op.CALLVALUE), 0, 0, 0, 0)
+ Op.SSTORE(1, Op.BALANCE(Op.ORIGIN))
],
ids=[""],
) # Amount sent by the contract to the sender mid execution
@pytest.mark.valid_from("Cancun")
def test_blob_gas_subtraction_tx(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
sender_initial_balance: int,
txs: List[Transaction],
destination_account: Address,
destination_account_balance: int,
total_account_transactions_fee: int,
):
"""
Check that the blob gas fee for a transaction is subtracted from the sender balance before the
transaction is executed.
- Transactions with max fee equal or higher than current block base fee
- Transactions with and without value
- Transactions with and without calldata
- Transactions with max fee per blob gas lower or higher than the priority fee
- Transactions where an externally owned account sends funds to the sender mid execution
"""
assert len(txs) == 1
post = {
destination_account: Account(
storage={
0: sender_initial_balance - total_account_transactions_fee,
1: sender_initial_balance
- total_account_transactions_fee
+ destination_account_balance,
}
)
}
state_test(
pre=pre,
post=post,
tx=txs[0],
env=state_env,
)
@pytest.mark.parametrize_by_fork(
"blobs_per_tx",
SpecHelpers.all_valid_blob_combinations,
)
@pytest.mark.parametrize("account_balance_modifier", [-1], ids=["exact_balance_minus_1"])
@pytest.mark.parametrize("tx_error", [TransactionException.INSUFFICIENT_ACCOUNT_FUNDS], ids=[""])
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_insufficient_balance_blob_tx_combinations(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
env: Environment,
block: Block,
):
"""
Reject all valid blob transaction combinations in a block, but block is invalid.
- The amount of blobs is correct but the user cannot afford the
transaction total cost
"""
blockchain_test(
pre=pre,
post={},
blocks=[block],
genesis_environment=env,
)
def generate_invalid_tx_blob_count_tests(
fork: Fork,
) -> List:
"""Return a list of tests for invalid blob transactions due to invalid blob counts."""
return [
pytest.param(
[0],
TransactionException.TYPE_3_TX_ZERO_BLOBS,
id="too_few_blobs",
),
pytest.param(
[fork.max_blobs_per_tx() + 1],
[
TransactionException.TYPE_3_TX_MAX_BLOB_GAS_ALLOWANCE_EXCEEDED,
TransactionException.TYPE_3_TX_BLOB_COUNT_EXCEEDED,
],
id="too_many_blobs",
),
]
@pytest.mark.parametrize_by_fork(
"blobs_per_tx,tx_error",
generate_invalid_tx_blob_count_tests,
)
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_invalid_tx_blob_count(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
header_verify: Optional[Header],
rlp_modifier: Optional[Header],
):
"""
Reject blocks that include blob transactions with invalid blob counts.
- `blob count == 0` in type 3 transaction
- `blob count > MAX_BLOBS_PER_BLOCK` in type 3 transaction
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
blockchain_test_header_verify=header_verify,
blockchain_test_rlp_modifier=rlp_modifier,
)
@pytest.mark.parametrize(
"blob_hashes_per_tx",
[
[[Hash(1)]],
[[Hash(x) for x in range(2)]],
[add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG) + [Hash(2)]],
[[Hash(1)] + add_kzg_version([Hash(2)], Spec.BLOB_COMMITMENT_VERSION_KZG)],
],
ids=[
"single_blob",
"multiple_blobs",
"multiple_blobs_single_bad_hash_1",
"multiple_blobs_single_bad_hash_2",
],
)
@pytest.mark.parametrize(
"tx_error", [TransactionException.TYPE_3_TX_INVALID_BLOB_VERSIONED_HASH], ids=[""]
)
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_invalid_blob_hash_versioning_single_tx(
state_test: StateTestFiller,
state_env: Environment,
pre: Alloc,
txs: List[Transaction],
header_verify: Optional[Header],
rlp_modifier: Optional[Header],
):
"""
Reject blob transactions with invalid blob hash version.
- Transaction with single blob with invalid version
- Transaction with multiple blobs all with invalid version
- Transaction with multiple blobs either with invalid version
"""
assert len(txs) == 1
state_test(
pre=pre,
post={},
tx=txs[0],
env=state_env,
blockchain_test_header_verify=header_verify,
blockchain_test_rlp_modifier=rlp_modifier,
)
@pytest.mark.parametrize(
"blob_hashes_per_tx",
[
[
add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG),
[Hash(2)],
],
[
add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG),
[Hash(x) for x in range(1, 3)],
],
[
add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG),
[Hash(2)] + add_kzg_version([Hash(3)], Spec.BLOB_COMMITMENT_VERSION_KZG),
],
[
add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG),
add_kzg_version([Hash(2)], Spec.BLOB_COMMITMENT_VERSION_KZG),
[Hash(3)],
],
],
ids=[
"single_blob",
"multiple_blobs",
"multiple_blobs_single_bad_hash_1",
"multiple_blobs_single_bad_hash_2",
],
)
@pytest.mark.parametrize(
"tx_error", [TransactionException.TYPE_3_TX_INVALID_BLOB_VERSIONED_HASH], ids=[""]
)
@pytest.mark.exception_test
@pytest.mark.valid_from("Cancun")
def test_invalid_blob_hash_versioning_multiple_txs(
blockchain_test: BlockchainTestFiller,
pre: Alloc,