Skip to content

Commit 2be399c

Browse files
souradeep-dasSamWilsn
authored andcommitted
add missing exceptions to prague
1 parent 8d2827b commit 2be399c

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

src/ethereum/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ class InvalidSignatureError(InvalidTransaction):
3939
"""
4040
Thrown when a transaction has an invalid signature.
4141
"""
42+
43+
class InsufficientBalanceError(InvalidTransaction):
44+
"""
45+
Thrown when a transaction cannot be executed due to insufficient sender funds.
46+
"""
47+
48+
class NonceMismatchError(InvalidTransaction):
49+
"""
50+
Thrown when a transaction's nonce does not match the expected nonce for the sender.
51+
"""

src/ethereum/prague/exceptions.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,52 @@ class TransactionTypeError(InvalidTransaction):
2222
def __init__(self, transaction_type: int):
2323
super().__init__(f"unknown transaction type `{transaction_type}`")
2424
self.transaction_type = transaction_type
25+
26+
class TransactionTypeContractCreationError(InvalidTransaction):
27+
"""
28+
Transaction type is not allowed for contract creation.
29+
"""
30+
31+
transaction_type: Final[int]
32+
"""
33+
The type byte of the transaction that caused the error.
34+
"""
35+
36+
def __init__(self, transaction_type: int):
37+
super().__init__(f"transaction type `{transaction_type}` not allowed for contract creation")
38+
self.transaction_type = transaction_type
39+
40+
class BlobGasLimitExceededError(InvalidTransaction):
41+
"""
42+
The blob gas limit for the transaction exceeds the maximum allowed.
43+
"""
44+
45+
class InsufficientMaxFeePerBlobGasError(InvalidTransaction):
46+
"""
47+
The maximum fee per blob gas is insufficient for the transaction.
48+
"""
49+
50+
class InsufficientMaxFeePerGasError(InvalidTransaction):
51+
"""
52+
The maximum fee per gas is insufficient for the transaction.
53+
"""
54+
55+
class InvalidBlobVersionedHashError(InvalidTransaction):
56+
"""
57+
The versioned hash of the blob is invalid.
58+
"""
59+
60+
class NoBlobDataError(InvalidTransaction):
61+
"""
62+
The transaction does not contain any blob data.
63+
"""
64+
65+
class PriorityFeeGreaterThanMaxFeeError(InvalidTransaction):
66+
"""
67+
The priority fee is greater than the maximum fee per gas.
68+
"""
69+
70+
class EmptyAuthorizationListError(InvalidTransaction):
71+
"""
72+
The authorization list in the transaction is empty.
73+
"""

src/ethereum/prague/fork.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@
2424
EthereumException,
2525
InvalidBlock,
2626
InvalidSenderError,
27+
InsufficientBalanceError,
28+
NonceMismatchError
2729
)
2830

2931
from . import vm
3032
from .blocks import Block, Header, Log, Receipt, Withdrawal, encode_receipt
3133
from .bloom import logs_bloom
34+
from .exceptions import (
35+
TransactionTypeContractCreationError,
36+
BlobGasLimitExceededError,
37+
InsufficientMaxFeePerBlobGasError,
38+
InsufficientMaxFeePerGasError,
39+
InvalidBlobVersionedHashError,
40+
NoBlobDataError,
41+
PriorityFeeGreaterThanMaxFeeError,
42+
EmptyAuthorizationListError
43+
)
3244
from .fork_types import Account, Address, Authorization, VersionedHash
3345
from .requests import (
3446
CONSOLIDATION_REQUEST_TYPE,
@@ -409,7 +421,7 @@ def check_transaction(
409421

410422
tx_blob_gas_used = calculate_total_blob_gas(tx)
411423
if tx_blob_gas_used > blob_gas_available:
412-
raise InvalidBlock
424+
raise BlobGasLimitExceededError("blob gas limit exceeded")
413425

414426
sender_address = recover_sender(block_env.chain_id, tx)
415427
sender_account = get_account(block_env.state, sender_address)
@@ -418,9 +430,9 @@ def check_transaction(
418430
tx, (FeeMarketTransaction, BlobTransaction, SetCodeTransaction)
419431
):
420432
if tx.max_fee_per_gas < tx.max_priority_fee_per_gas:
421-
raise InvalidBlock
433+
raise PriorityFeeGreaterThanMaxFeeError("priority fee greater than max fee")
422434
if tx.max_fee_per_gas < block_env.base_fee_per_gas:
423-
raise InvalidBlock
435+
raise InsufficientMaxFeePerGasError("insufficient max fee per gas")
424436

425437
priority_fee_per_gas = min(
426438
tx.max_priority_fee_per_gas,
@@ -436,14 +448,14 @@ def check_transaction(
436448

437449
if isinstance(tx, BlobTransaction):
438450
if len(tx.blob_versioned_hashes) == 0:
439-
raise InvalidBlock
451+
raise NoBlobDataError("no blob data in transaction")
440452
for blob_versioned_hash in tx.blob_versioned_hashes:
441453
if blob_versioned_hash[0:1] != VERSIONED_HASH_VERSION_KZG:
442-
raise InvalidBlock
454+
raise InvalidBlobVersionedHashError("invalid blob versioned hash")
443455

444456
blob_gas_price = calculate_blob_gas_price(block_env.excess_blob_gas)
445457
if Uint(tx.max_fee_per_blob_gas) < blob_gas_price:
446-
raise InvalidBlock
458+
raise InsufficientMaxFeePerBlobGasError("insufficient max fee per blob gas")
447459

448460
max_gas_fee += Uint(calculate_total_blob_gas(tx)) * Uint(
449461
tx.max_fee_per_blob_gas
@@ -454,16 +466,19 @@ def check_transaction(
454466

455467
if isinstance(tx, (BlobTransaction, SetCodeTransaction)):
456468
if not isinstance(tx.to, Address):
457-
raise InvalidBlock
469+
raise TransactionTypeContractCreationError(tx[0])
458470

459471
if isinstance(tx, SetCodeTransaction):
460472
if not any(tx.authorizations):
461-
raise InvalidBlock
473+
raise EmptyAuthorizationListError("empty authorization list")
462474

463475
if sender_account.nonce != tx.nonce:
464-
raise InvalidBlock
476+
if sender_account.nonce > tx.nonce:
477+
raise NonceMismatchError("nonce too low")
478+
else:
479+
raise NonceMismatchError("nonce too high")
465480
if Uint(sender_account.balance) < max_gas_fee + Uint(tx.value):
466-
raise InvalidBlock
481+
raise InsufficientBalanceError("insufficient sender balance")
467482
if sender_account.code and not is_valid_delegation(sender_account.code):
468483
raise InvalidSenderError("not EOA")
469484

0 commit comments

Comments
 (0)