Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor includability check in forks #890

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/ethereum/arrow_glacier/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
base_fee_per_gas: Uint,
gas_available: Uint,
Expand All @@ -401,6 +402,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
base_fee_per_gas :
Expand All @@ -422,6 +425,18 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)

if isinstance(tx, FeeMarketTransaction):
gas_fee = tx.gas * tx.max_fee_per_gas
else:
gas_fee = tx.gas * tx.gas_price

ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -783,15 +798,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)

if isinstance(tx, FeeMarketTransaction):
max_gas_fee = tx.gas * tx.max_fee_per_gas
else:
max_gas_fee = tx.gas * tx.gas_price

ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= max_gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

effective_gas_fee = tx.gas * env.gas_price

gas = tx.gas - calculate_intrinsic_cost(tx)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/berlin/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
chain_id: U64,
Expand All @@ -323,6 +324,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -340,6 +343,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -680,9 +690,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/byzantium/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
chain_id: U64,
Expand All @@ -317,6 +318,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -334,6 +337,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -668,9 +678,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
14 changes: 11 additions & 3 deletions src/ethereum/constantinople/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
chain_id: U64,
Expand All @@ -317,6 +318,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -334,6 +337,14 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price

ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -668,9 +679,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/dao_fork/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
) -> Address:
Expand All @@ -327,6 +328,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -342,6 +345,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(tx)

Expand Down Expand Up @@ -673,9 +683,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/frontier/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
) -> Address:
Expand All @@ -308,6 +309,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -323,6 +326,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(tx)

Expand Down Expand Up @@ -654,9 +664,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
24 changes: 15 additions & 9 deletions src/ethereum/gray_glacier/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
base_fee_per_gas: Uint,
gas_available: Uint,
Expand All @@ -401,6 +402,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
base_fee_per_gas :
Expand All @@ -422,6 +425,18 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)

if isinstance(tx, FeeMarketTransaction):
gas_fee = tx.gas * tx.max_fee_per_gas
else:
gas_fee = tx.gas * tx.gas_price

ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -783,15 +798,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)

if isinstance(tx, FeeMarketTransaction):
max_gas_fee = tx.gas * tx.max_fee_per_gas
else:
max_gas_fee = tx.gas * tx.gas_price

ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= max_gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

effective_gas_fee = tx.gas * env.gas_price

gas = tx.gas - calculate_intrinsic_cost(tx)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/homestead/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
) -> Address:
Expand All @@ -310,6 +311,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -325,6 +328,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(tx)

Expand Down Expand Up @@ -656,9 +666,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
13 changes: 10 additions & 3 deletions src/ethereum/istanbul/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def validate_proof_of_work(header: Header) -> None:


def check_transaction(
env: vm.Environment,
tx: Transaction,
gas_available: Uint,
chain_id: U64,
Expand All @@ -317,6 +318,8 @@ def check_transaction(

Parameters
----------
env :
Environment for the Ethereum Virtual Machine.
tx :
The transaction.
gas_available :
Expand All @@ -334,6 +337,13 @@ def check_transaction(
InvalidBlock :
If the transaction is not includable.
"""
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

ensure(tx.gas <= gas_available, InvalidBlock)
sender_address = recover_sender(chain_id, tx)

Expand Down Expand Up @@ -669,9 +679,6 @@ def process_transaction(
sender = env.origin
sender_account = get_account(env.state, sender)
gas_fee = tx.gas * tx.gas_price
ensure(sender_account.nonce == tx.nonce, InvalidBlock)
ensure(sender_account.balance >= gas_fee + tx.value, InvalidBlock)
ensure(sender_account.code == bytearray(), InvalidBlock)

gas = tx.gas - calculate_intrinsic_cost(tx)
increment_nonce(env.state, sender)
Expand Down
Loading