Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/ethereum/forks/amsterdam/block_access_lists/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
track_nonce_change,
track_storage_read,
track_storage_write,
track_transaction_gas_used,
)

__all__ = [
Expand All @@ -53,5 +54,6 @@
"track_nonce_change",
"track_storage_read",
"track_storage_write",
"track_transaction_gas_used",
"validate_block_access_list_against_execution",
]
16 changes: 12 additions & 4 deletions src/ethereum/forks/amsterdam/block_access_lists/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"""

from dataclasses import dataclass, field
from typing import Dict, List, Set
from typing import Dict, List, Optional, Set

from ethereum_types.bytes import Bytes, Bytes32
from ethereum_types.numeric import U64, U256
from ethereum_types.numeric import U64, U256, Uint

from ..fork_types import Address
from .rlp_types import (
Expand Down Expand Up @@ -364,6 +364,7 @@ def add_touched_account(

def build_block_access_list(
builder: BlockAccessListBuilder,
gas_used_list: Optional[List[Uint]] = None,
) -> BlockAccessList:
"""
Build the final [`BlockAccessList`] from accumulated changes.
Expand All @@ -380,11 +381,14 @@ def build_block_access_list(
----------
builder :
The block access list builder containing all tracked changes.
gas_used_list :
Optional list of gas_used values for each transaction in the order
they appear in the block.

Returns
-------
block_access_list :
The final sorted and encoded block access list.
The final sorted and encoded block access list including gas_used.

[`BlockAccessList`]: ref:ethereum.forks.amsterdam.block_access_lists.rlp_types.BlockAccessList # noqa: E501

Expand Down Expand Up @@ -432,4 +436,8 @@ def build_block_access_list(

account_changes_list.sort(key=lambda x: x.address)

return BlockAccessList(account_changes=tuple(account_changes_list))
gas_used_tuple = tuple(gas_used_list) if gas_used_list else tuple()
return BlockAccessList(
account_changes=tuple(account_changes_list),
gas_used=gas_used_tuple,
)
7 changes: 4 additions & 3 deletions src/ethereum/forks/amsterdam/block_access_lists/rlp_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
address -> field -> block_access_index -> change.
"""

from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Tuple

from ethereum_types.bytes import Bytes, Bytes20, Bytes32
Expand Down Expand Up @@ -123,8 +123,9 @@ class AccountChanges:
class BlockAccessList:
"""
Block-Level Access List for EIP-7928.
Contains all addresses accessed during block execution.
RLP encoded as a list of AccountChanges.
Contains all addresses accessed during block execution and gas used per
transaction. RLP encoded as [account_changes, gas_used].
"""

account_changes: Tuple[AccountChanges, ...]
gas_used: Tuple[Uint, ...] = field(default_factory=tuple)
3 changes: 2 additions & 1 deletion src/ethereum/forks/amsterdam/block_access_lists/rlp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def rlp_encode_block_access_list(block_access_list: BlockAccessList) -> Bytes:
]
)

encoded = rlp.encode(cast(Extended, account_changes_list))
gas_used_list = [Uint(gas) for gas in block_access_list.gas_used]
encoded = rlp.encode(cast(Extended, [account_changes_list, gas_used_list]))
return Bytes(encoded)


Expand Down
26 changes: 26 additions & 0 deletions src/ethereum/forks/amsterdam/block_access_lists/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class StateChangeTracker:
Stack of snapshots for nested call frames to handle reverts properly.
"""

gas_used_list: List[Uint] = field(default_factory=list)
"""
List of gas_used values for each transaction in the order they appear
in the block.
"""


def set_block_access_index(
tracker: StateChangeTracker, block_access_index: Uint
Expand Down Expand Up @@ -666,3 +672,23 @@ def commit_call_frame(tracker: StateChangeTracker) -> None:
"""
if tracker.call_frame_snapshots:
tracker.call_frame_snapshots.pop()


def track_transaction_gas_used(
tracker: StateChangeTracker, gas_used: Uint
) -> None:
"""
Track the gas used by a transaction.

Records the gas_used value for each transaction in the order they
appear in the block. This is called after each transaction is processed.

Parameters
----------
tracker :
The state change tracker instance.
gas_used :
The gas used by the transaction.

"""
tracker.gas_used_list.append(gas_used)
8 changes: 7 additions & 1 deletion src/ethereum/forks/amsterdam/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
normalize_balance_changes,
set_block_access_index,
track_balance_change,
track_transaction_gas_used,
)
from .blocks import Block, Header, Log, Receipt, Withdrawal, encode_receipt
from .bloom import logs_bloom
Expand Down Expand Up @@ -809,7 +810,8 @@ def apply_body(
block_output=block_output,
)
block_output.block_access_list = build_block_access_list(
block_env.state.change_tracker.block_access_list_builder
block_env.state.change_tracker.block_access_list_builder,
block_env.state.change_tracker.gas_used_list,
)

return block_output
Expand Down Expand Up @@ -1033,6 +1035,10 @@ def process_transaction(
block_output.block_gas_used += tx_gas_used_after_refund
block_output.blob_gas_used += tx_blob_gas_used

track_transaction_gas_used(
block_env.state.change_tracker, tx_gas_used_after_refund
)

receipt = make_receipt(
tx, tx_output.error, block_output.block_gas_used, tx_output.logs
)
Expand Down
Loading