ethereum.forks.arrow_glacier.state_trackerethereum.forks.gray_glacier.state_tracker
State Tracking for Block Execution.
Track state changes on top of a read-only PreState. At block end,
accumulated diffs feed into
PreState.compute_state_root_and_trie_changes().
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Replace the mutable State class with lightweight state trackers that
record diffs. BlockState accumulates committed transaction
changes across a block. TransactionState tracks in-flight changes
within a single transaction and supports copy-on-write rollback.
BlockState ¶
Accumulate committed transaction-level changes across a block.
Read chain: block writes -> pre_state.
storage_clears records addresses whose storage was wiped by
a pre-EIP-6780 SELFDESTRUCT earlier in the block, so later
reads must not fall back to pre_state storage.
| 39 | @final |
|---|
| 40 | @dataclass |
|---|
class BlockState:
pre_state¶
| 52 | pre_state: PreState |
|---|
account_writes¶
| 53 | account_writes: Dict[Address, Optional[Account]] = field( |
|---|---|
| 54 | default_factory=dict |
| 55 | ) |
storage_writes¶
| 56 | storage_writes: Dict[Address, Dict[Bytes32, U256]] = field( |
|---|---|
| 57 | default_factory=dict |
| 58 | ) |
code_writes¶
| 59 | code_writes: Dict[Hash32, Bytes] = field(default_factory=dict) |
|---|
storage_clears¶
| 60 | storage_clears: Set[Address] = field(default_factory=set) |
|---|
TransactionState ¶
Track in-flight state changes within a single transaction.
Read chain: tx writes -> block writes -> pre_state.
| 63 | @final |
|---|
| 64 | @dataclass |
|---|
class TransactionState:
parent¶
| 72 | parent: BlockState |
|---|
account_writes¶
| 73 | account_writes: Dict[Address, Optional[Account]] = field( |
|---|---|
| 74 | default_factory=dict |
| 75 | ) |
storage_writes¶
| 76 | storage_writes: Dict[Address, Dict[Bytes32, U256]] = field( |
|---|---|
| 77 | default_factory=dict |
| 78 | ) |
code_writes¶
| 79 | code_writes: Dict[Hash32, Bytes] = field(default_factory=dict) |
|---|
created_accounts¶
| 80 | created_accounts: Set[Address] = field(default_factory=set) |
|---|
storage_clears¶
| 81 | storage_clears: Set[Address] = field(default_factory=set) |
|---|
transient_storage¶
| 82 | transient_storage: Dict[Tuple[Address, Bytes32], U256] = field( |
|---|---|
| 83 | default_factory=dict |
| 84 | ) |
get_account_optional ¶
Get the Account object at an address. Return None (rather than
EMPTY_ACCOUNT) if there is no account at the address.
Parameters
tx_state : The transaction state. address : Address to look up.
Returns
account : Optional[Account]
Account at address.
def get_account_optional(tx_state: TransactionState, address: Address) -> Optional[Account]:
| 90 | <snip> |
|---|---|
| 107 | if address in tx_state.account_writes: |
| 108 | return tx_state.account_writes[address] |
| 109 | if address in tx_state.parent.account_writes: |
| 110 | return tx_state.parent.account_writes[address] |
| 111 | return tx_state.parent.pre_state.get_account_optional(address) |
get_account ¶
Get the Account object at an address. Return EMPTY_ACCOUNT
if there is no account at the address.
Use get_account_optional() if you care about the difference
between a non-existent account and EMPTY_ACCOUNT.
Parameters
tx_state : The transaction state. address : Address to look up.
Returns
account : Account
Account at address.
def get_account(tx_state: TransactionState, address: Address) -> Account:
| 115 | <snip> |
|---|---|
| 135 | account = get_account_optional(tx_state, address) |
| 136 | if account is None: |
| 137 | return EMPTY_ACCOUNT |
| 138 | else: |
| 139 | return account |
get_code ¶
Get the bytecode for a given code hash.
Read chain: tx code_writes -> block code_writes -> pre_state.
Parameters
tx_state : The transaction state. code_hash : Hash of the code to look up.
Returns
code : Bytes
The bytecode.
def get_code(tx_state: TransactionState, code_hash: Hash32) -> Bytes:
| 143 | <snip> |
|---|---|
| 161 | if code_hash == EMPTY_CODE_HASH: |
| 162 | return b"" |
| 163 | if code_hash in tx_state.code_writes: |
| 164 | return tx_state.code_writes[code_hash] |
| 165 | if code_hash in tx_state.parent.code_writes: |
| 166 | return tx_state.parent.code_writes[code_hash] |
| 167 | return tx_state.parent.pre_state.get_code(code_hash) |
get_storage ¶
Get a value at a storage key on an account. Return U256(0) if
the storage key has not been set previously.
Parameters
tx_state : The transaction state. address : Address of the account. key : Key to look up.
Returns
value : U256
Value at the key.
def get_storage(tx_state: TransactionState, address: Address, key: Bytes32) -> U256:
| 173 | <snip> |
|---|---|
| 192 | if address in tx_state.storage_writes: |
| 193 | if key in tx_state.storage_writes[address]: |
| 194 | return tx_state.storage_writes[address][key] |
| 195 | if address in tx_state.storage_clears: |
| 196 | return U256(0) |
| 197 | if address in tx_state.parent.storage_writes: |
| 198 | if key in tx_state.parent.storage_writes[address]: |
| 199 | return tx_state.parent.storage_writes[address][key] |
| 200 | if address in tx_state.parent.storage_clears: |
| 201 | return U256(0) |
| 202 | return tx_state.parent.pre_state.get_storage(address, key) |
get_storage_original ¶
Get the original value in a storage slot i.e. the value before the
current transaction began. Read from block-level writes, then
pre_state. Return U256(0) for accounts created in the current
transaction.
Parameters
tx_state : The transaction state. address : Address of the account to read the value from. key : Key of the storage slot.
def get_storage_original(tx_state: TransactionState, address: Address, key: Bytes32) -> U256:
| 208 | <snip> |
|---|---|
| 224 | if address in tx_state.created_accounts: |
| 225 | return U256(0) |
| 226 | if address in tx_state.parent.storage_writes: |
| 227 | if key in tx_state.parent.storage_writes[address]: |
| 228 | return tx_state.parent.storage_writes[address][key] |
| 229 | if address in tx_state.parent.storage_clears: |
| 230 | return U256(0) |
| 231 | return tx_state.parent.pre_state.get_storage(address, key) |
get_transient_storage ¶
Get a value at a storage key on an account from transient storage.
Return U256(0) if the storage key has not been set previously.
Parameters
tx_state : The transaction state. address : Address of the account. key : Key to look up.
Returns
value : U256
Value at the key.
def get_transient_storage(tx_state: TransactionState, address: Address, key: Bytes32) -> U256:
| 237 | <snip> |
|---|---|
| 256 | return tx_state.transient_storage.get((address, key), U256(0)) |
account_exists ¶
Check if an account exists in the state trie.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be checked.
Returns
account_exists : bool
True if account exists in the state trie, False otherwise.
def account_exists(tx_state: TransactionState, address: Address) -> bool:
| 260 | <snip> |
|---|---|
| 276 | return get_account_optional(tx_state, address) is not None |
account_has_code_or_nonce ¶
Check if an account has non-zero nonce or non-empty code.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be checked.
Returns
has_code_or_nonce : bool
True if the account has non-zero nonce or non-empty code,
False otherwise.
def account_has_code_or_nonce(tx_state: TransactionState, address: Address) -> bool:
| 282 | <snip> |
|---|---|
| 299 | account = get_account(tx_state, address) |
| 300 | return account.nonce != Uint(0) or account.code_hash != EMPTY_CODE_HASH |
account_has_storage ¶
Check if an account has storage.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be checked.
Returns
has_storage : bool
True if the account has storage, False otherwise.
def account_has_storage(tx_state: TransactionState, address: Address) -> bool:
| 304 | <snip> |
|---|---|
| 320 | if tx_state.storage_writes.get(address): |
| 321 | return True |
| 322 | if address in tx_state.storage_clears: |
| 323 | return False |
| 324 | if tx_state.parent.storage_writes.get(address): |
| 325 | return True |
| 326 | if address in tx_state.parent.storage_clears: |
| 327 | return False |
| 328 | return tx_state.parent.pre_state.account_has_storage(address) |
account_exists_and_is_empty ¶
Check if an account exists and has zero nonce, empty code and zero balance.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be checked.
Returns
exists_and_is_empty : bool
True if an account exists and has zero nonce, empty code and
zero balance, False otherwise.
def account_exists_and_is_empty(tx_state: TransactionState, address: Address) -> bool:
| 334 | <snip> |
|---|---|
| 352 | account = get_account_optional(tx_state, address) |
| 353 | return ( |
| 354 | account is not None |
| 355 | and account.nonce == Uint(0) |
| 356 | and account.code_hash == EMPTY_CODE_HASH |
| 357 | and account.balance == 0 |
| 358 | ) |
is_account_alive ¶
Check whether an account is both in the state and non-empty.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be checked.
Returns
is_alive : bool
True if the account is alive.
def is_account_alive(tx_state: TransactionState, address: Address) -> bool:
| 362 | <snip> |
|---|---|
| 378 | account = get_account_optional(tx_state, address) |
| 379 | return account is not None and account != EMPTY_ACCOUNT |
set_account ¶
Set the Account object at an address. Setting to None
deletes the account (but not its storage, see
destroy_account()).
Parameters
tx_state : The transaction state. address : Address to set. account : Account to set at address.
def set_account(tx_state: TransactionState, address: Address, account: Optional[Account]) -> None:
| 387 | <snip> |
|---|---|
| 402 | tx_state.account_writes[address] = account |
set_storage ¶
Set a value at a storage key on an account.
Parameters
tx_state : The transaction state. address : Address of the account. key : Key to set. value : Value to set at the key.
def set_storage(tx_state: TransactionState, address: Address, key: Bytes32, value: U256) -> None:
| 411 | <snip> |
|---|---|
| 426 | assert get_account_optional(tx_state, address) is not None |
| 427 | if address not in tx_state.storage_writes: |
| 428 | tx_state.storage_writes[address] = {} |
| 429 | tx_state.storage_writes[address][key] = value |
destroy_account ¶
Completely remove the account at address and all of its storage.
This function is made available exclusively for the SELFDESTRUCT
opcode. It is expected that SELFDESTRUCT will be disabled in a
future hardfork and this function will be removed. Only supports same
transaction destruction.
Parameters
tx_state : The transaction state. address : Address of account to destroy.
def destroy_account(tx_state: TransactionState, address: Address) -> None:
| 433 | <snip> |
|---|---|
| 449 | destroy_storage(tx_state, address) |
| 450 | set_account(tx_state, address, None) |
destroy_storage ¶
Completely remove the storage at address.
Only supports same transaction destruction.
Parameters
tx_state : The transaction state. address : Address of account whose storage is to be deleted.
def destroy_storage(tx_state: TransactionState, address: Address) -> None:
| 454 | <snip> |
|---|---|
| 467 | if address in tx_state.storage_writes: |
| 468 | del tx_state.storage_writes[address] |
| 469 | tx_state.storage_clears.add(address) |
mark_account_created ¶
Mark an account as having been created in the current transaction.
This information is used by get_storage_original() to handle an
obscure edgecase, and to respect the constraints added to
SELFDESTRUCT by EIP-6780.
The marker is not removed even if the account creation reverts.
Since the account cannot have had code prior to its creation and
can't call get_storage_original(), this is harmless.
Parameters
tx_state : The transaction state. address : Address of the account that has been created.
def mark_account_created(tx_state: TransactionState, address: Address) -> None:
| 473 | <snip> |
|---|---|
| 491 | tx_state.created_accounts.add(address) |
set_transient_storage ¶
Set a value at a storage key on an account in transient storage.
Parameters
tx_state : The transaction state. address : Address of the account. key : Key to set. value : Value to set at the key.
modify_state ¶
Modify an Account in the state.
def modify_state(tx_state: TransactionState, address: Address, f: Callable[[Account], None]) -> None:
| 526 | <snip> |
|---|---|
| 529 | set_account(tx_state, address, modify(get_account(tx_state, address), f)) |
move_ether ¶
Move funds between accounts.
Parameters
tx_state : The transaction state. sender_address : Address of the sender. recipient_address : Address of the recipient. amount : The amount to transfer.
def move_ether(tx_state: TransactionState, sender_address: Address, recipient_address: Address, amount: U256) -> None:
| 538 | <snip> |
|---|---|
| 553 | |
| 554 | def reduce_sender_balance(sender: Account) -> None: |
| 555 | if sender.balance < amount: |
| 556 | raise AssertionError |
| 557 | sender.balance -= amount |
| 558 | |
| 559 | def increase_recipient_balance(recipient: Account) -> None: |
| 560 | recipient.balance += amount |
| 561 | |
| 562 | modify_state(tx_state, sender_address, reduce_sender_balance) |
| 563 | modify_state(tx_state, recipient_address, increase_recipient_balance) |
create_ether ¶
Add newly created ether to an account.
Parameters
tx_state : The transaction state. address : Address of the account to which ether is added. amount : The amount of ether to be added to the account of interest.
def create_ether(tx_state: TransactionState, address: Address, amount: U256) -> None:
| 569 | <snip> |
|---|---|
| 582 | |
| 583 | def increase_balance(account: Account) -> None: |
| 584 | account.balance += amount |
| 585 | |
| 586 | modify_state(tx_state, address, increase_balance) |
set_account_balance ¶
Set the balance of an account.
Parameters
tx_state : The transaction state. address : Address of the account whose balance needs to be set. amount : The amount that needs to be set in the balance.
def set_account_balance(tx_state: TransactionState, address: Address, amount: U256) -> None:
| 592 | <snip> |
|---|---|
| 605 | |
| 606 | def set_balance(account: Account) -> None: |
| 607 | account.balance = amount |
| 608 | |
| 609 | modify_state(tx_state, address, set_balance) |
increment_nonce ¶
Increment the nonce of an account.
Parameters
tx_state : The transaction state. address : Address of the account whose nonce needs to be incremented.
def increment_nonce(tx_state: TransactionState, address: Address) -> None:
| 613 | <snip> |
|---|---|
| 624 | |
| 625 | def increase_nonce(sender: Account) -> None: |
| 626 | sender.nonce += Uint(1) |
| 627 | |
| 628 | modify_state(tx_state, address, increase_nonce) |
set_code ¶
Set Account code.
Parameters
tx_state : The transaction state. address : Address of the account whose code needs to be updated. code : The bytecode that needs to be set.
def set_code(tx_state: TransactionState, address: Address, code: Bytes) -> None:
| 634 | <snip> |
|---|---|
| 647 | code_hash = keccak256(code) |
| 648 | if code_hash != EMPTY_CODE_HASH: |
| 649 | tx_state.code_writes[code_hash] = code |
| 650 | |
| 651 | def write_code_hash(sender: Account) -> None: |
| 652 | sender.code_hash = code_hash |
| 653 | |
| 654 | modify_state(tx_state, address, write_code_hash) |
copy_tx_state ¶
Create a snapshot of the transaction state for rollback.
Deep-copy writes and transient storage. The parent reference and
created_accounts are shared (not rolled back).
Parameters
tx_state : The transaction state to snapshot.
Returns
snapshot : TransactionState
A copy of the transaction state.
def copy_tx_state(tx_state: TransactionState) -> TransactionState:
| 661 | <snip> |
|---|---|
| 678 | return TransactionState( |
| 679 | parent=tx_state.parent, |
| 680 | account_writes=dict(tx_state.account_writes), |
| 681 | storage_writes={ |
| 682 | addr: dict(slots) |
| 683 | for addr, slots in tx_state.storage_writes.items() |
| 684 | }, |
| 685 | code_writes=dict(tx_state.code_writes), |
| 686 | created_accounts=tx_state.created_accounts, |
| 687 | storage_clears=set(tx_state.storage_clears), |
| 688 | transient_storage=dict(tx_state.transient_storage), |
| 689 | ) |
restore_tx_state ¶
Restore transaction state from a snapshot (rollback on failure).
Parameters
tx_state : The transaction state to restore. snapshot : The snapshot to restore from.
def restore_tx_state(tx_state: TransactionState, snapshot: TransactionState) -> None:
| 695 | <snip> |
|---|---|
| 706 | tx_state.account_writes = snapshot.account_writes |
| 707 | tx_state.storage_writes = snapshot.storage_writes |
| 708 | tx_state.code_writes = snapshot.code_writes |
| 709 | tx_state.storage_clears = snapshot.storage_clears |
| 710 | tx_state.transient_storage = snapshot.transient_storage |
incorporate_tx_into_block ¶
Merge transaction writes into the block state and clear for reuse.
Parameters
tx_state : The transaction state to commit.
def incorporate_tx_into_block(tx_state: TransactionState) -> None:
| 717 | <snip> |
|---|---|
| 726 | block = tx_state.parent |
| 727 | |
| 728 | for address, account in tx_state.account_writes.items(): |
| 729 | block.account_writes[address] = account |
| 730 | |
| 731 | for address in tx_state.storage_clears: |
| 732 | block.storage_clears.add(address) |
| 733 | block.storage_writes.pop(address, None) |
| 734 | |
| 735 | for address, slots in tx_state.storage_writes.items(): |
| 736 | if address not in block.storage_writes: |
| 737 | block.storage_writes[address] = {} |
| 738 | block.storage_writes[address].update(slots) |
| 739 | |
| 740 | block.code_writes.update(tx_state.code_writes) |
| 741 | |
| 742 | tx_state.account_writes.clear() |
| 743 | tx_state.storage_writes.clear() |
| 744 | tx_state.code_writes.clear() |
| 745 | tx_state.created_accounts.clear() |
| 746 | tx_state.storage_clears.clear() |
| 747 | tx_state.transient_storage.clear() |
extract_block_diff ¶
Extract account, storage, and code diff from the block state.
Parameters
block_state : The block state.
Returns
diff : BlockDiff
Account, storage, and code changes accumulated during block execution.
destroy_touched_empty_accounts ¶
Destroy all touched accounts that are empty.
Parameters
tx_state : The transaction state. touched_accounts : All the accounts that have been touched in the current transaction.
def destroy_touched_empty_accounts(tx_state: TransactionState, touched_accounts: Set[Address]) -> None:
| 776 | <snip> |
|---|---|
| 787 | for address in touched_accounts: |
| 788 | if account_exists_and_is_empty(tx_state, address): |
| 789 | destroy_account(tx_state, address) |
touch_account ¶
Initialize an account to state.
Parameters
tx_state : The transaction state. address : Address of the account that needs to be initialized.
def touch_account(tx_state: TransactionState, address: Address) -> None:
| 793 | <snip> |
|---|---|
| 804 | if not account_exists(tx_state, address): |
| 805 | set_account(tx_state, address, EMPTY_ACCOUNT) |