ethereum.forks.tangerine_whistle.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 | @dataclass |
|---|
class BlockState:
pre_state¶
| 51 | pre_state: PreState |
|---|
account_writes¶
| 52 | account_writes: Dict[Address, Optional[Account]] = field( |
|---|---|
| 53 | default_factory=dict |
| 54 | ) |
storage_writes¶
| 55 | storage_writes: Dict[Address, Dict[Bytes32, U256]] = field( |
|---|---|
| 56 | default_factory=dict |
| 57 | ) |
code_writes¶
| 58 | code_writes: Dict[Hash32, Bytes] = field(default_factory=dict) |
|---|
storage_clears¶
| 59 | 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.
| 62 | @dataclass |
|---|
class TransactionState:
parent¶
| 70 | parent: BlockState |
|---|
account_writes¶
| 71 | account_writes: Dict[Address, Optional[Account]] = field( |
|---|---|
| 72 | default_factory=dict |
| 73 | ) |
storage_writes¶
| 74 | storage_writes: Dict[Address, Dict[Bytes32, U256]] = field( |
|---|---|
| 75 | default_factory=dict |
| 76 | ) |
code_writes¶
| 77 | code_writes: Dict[Hash32, Bytes] = field(default_factory=dict) |
|---|
created_accounts¶
| 78 | created_accounts: Set[Address] = field(default_factory=set) |
|---|
storage_clears¶
| 79 | storage_clears: Set[Address] = field(default_factory=set) |
|---|
transient_storage¶
| 80 | transient_storage: Dict[Tuple[Address, Bytes32], U256] = field( |
|---|---|
| 81 | default_factory=dict |
| 82 | ) |
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]:
| 88 | """ |
|---|---|
| 89 | Get the ``Account`` object at an address. Return ``None`` (rather than |
| 90 | ``EMPTY_ACCOUNT``) if there is no account at the address. |
| 91 | |
| 92 | Parameters |
| 93 | ---------- |
| 94 | tx_state : |
| 95 | The transaction state. |
| 96 | address : |
| 97 | Address to look up. |
| 98 | |
| 99 | Returns |
| 100 | ------- |
| 101 | account : ``Optional[Account]`` |
| 102 | Account at address. |
| 103 | |
| 104 | """ |
| 105 | if address in tx_state.account_writes: |
| 106 | return tx_state.account_writes[address] |
| 107 | if address in tx_state.parent.account_writes: |
| 108 | return tx_state.parent.account_writes[address] |
| 109 | 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:
| 113 | """ |
|---|---|
| 114 | Get the ``Account`` object at an address. Return ``EMPTY_ACCOUNT`` |
| 115 | if there is no account at the address. |
| 116 | |
| 117 | Use ``get_account_optional()`` if you care about the difference |
| 118 | between a non-existent account and ``EMPTY_ACCOUNT``. |
| 119 | |
| 120 | Parameters |
| 121 | ---------- |
| 122 | tx_state : |
| 123 | The transaction state. |
| 124 | address : |
| 125 | Address to look up. |
| 126 | |
| 127 | Returns |
| 128 | ------- |
| 129 | account : ``Account`` |
| 130 | Account at address. |
| 131 | |
| 132 | """ |
| 133 | account = get_account_optional(tx_state, address) |
| 134 | if account is None: |
| 135 | return EMPTY_ACCOUNT |
| 136 | else: |
| 137 | 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:
| 141 | """ |
|---|---|
| 142 | Get the bytecode for a given code hash. |
| 143 | |
| 144 | Read chain: tx code_writes -> block code_writes -> pre_state. |
| 145 | |
| 146 | Parameters |
| 147 | ---------- |
| 148 | tx_state : |
| 149 | The transaction state. |
| 150 | code_hash : |
| 151 | Hash of the code to look up. |
| 152 | |
| 153 | Returns |
| 154 | ------- |
| 155 | code : ``Bytes`` |
| 156 | The bytecode. |
| 157 | |
| 158 | """ |
| 159 | if code_hash == EMPTY_CODE_HASH: |
| 160 | return b"" |
| 161 | if code_hash in tx_state.code_writes: |
| 162 | return tx_state.code_writes[code_hash] |
| 163 | if code_hash in tx_state.parent.code_writes: |
| 164 | return tx_state.parent.code_writes[code_hash] |
| 165 | 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:
| 171 | """ |
|---|---|
| 172 | Get a value at a storage key on an account. Return ``U256(0)`` if |
| 173 | the storage key has not been set previously. |
| 174 | |
| 175 | Parameters |
| 176 | ---------- |
| 177 | tx_state : |
| 178 | The transaction state. |
| 179 | address : |
| 180 | Address of the account. |
| 181 | key : |
| 182 | Key to look up. |
| 183 | |
| 184 | Returns |
| 185 | ------- |
| 186 | value : ``U256`` |
| 187 | Value at the key. |
| 188 | |
| 189 | """ |
| 190 | if address in tx_state.storage_writes: |
| 191 | if key in tx_state.storage_writes[address]: |
| 192 | return tx_state.storage_writes[address][key] |
| 193 | if address in tx_state.storage_clears: |
| 194 | return U256(0) |
| 195 | if address in tx_state.parent.storage_writes: |
| 196 | if key in tx_state.parent.storage_writes[address]: |
| 197 | return tx_state.parent.storage_writes[address][key] |
| 198 | if address in tx_state.parent.storage_clears: |
| 199 | return U256(0) |
| 200 | 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:
| 206 | """ |
|---|---|
| 207 | Get the original value in a storage slot i.e. the value before the |
| 208 | current transaction began. Read from block-level writes, then |
| 209 | pre_state. Return ``U256(0)`` for accounts created in the current |
| 210 | transaction. |
| 211 | |
| 212 | Parameters |
| 213 | ---------- |
| 214 | tx_state : |
| 215 | The transaction state. |
| 216 | address : |
| 217 | Address of the account to read the value from. |
| 218 | key : |
| 219 | Key of the storage slot. |
| 220 | |
| 221 | """ |
| 222 | if address in tx_state.created_accounts: |
| 223 | return U256(0) |
| 224 | if address in tx_state.parent.storage_writes: |
| 225 | if key in tx_state.parent.storage_writes[address]: |
| 226 | return tx_state.parent.storage_writes[address][key] |
| 227 | if address in tx_state.parent.storage_clears: |
| 228 | return U256(0) |
| 229 | 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:
| 235 | """ |
|---|---|
| 236 | Get a value at a storage key on an account from transient storage. |
| 237 | Return ``U256(0)`` if the storage key has not been set previously. |
| 238 | |
| 239 | Parameters |
| 240 | ---------- |
| 241 | tx_state : |
| 242 | The transaction state. |
| 243 | address : |
| 244 | Address of the account. |
| 245 | key : |
| 246 | Key to look up. |
| 247 | |
| 248 | Returns |
| 249 | ------- |
| 250 | value : ``U256`` |
| 251 | Value at the key. |
| 252 | |
| 253 | """ |
| 254 | 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:
| 258 | """ |
|---|---|
| 259 | Check if an account exists in the state trie. |
| 260 | |
| 261 | Parameters |
| 262 | ---------- |
| 263 | tx_state : |
| 264 | The transaction state. |
| 265 | address : |
| 266 | Address of the account that needs to be checked. |
| 267 | |
| 268 | Returns |
| 269 | ------- |
| 270 | account_exists : ``bool`` |
| 271 | True if account exists in the state trie, False otherwise. |
| 272 | |
| 273 | """ |
| 274 | 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:
| 280 | """ |
|---|---|
| 281 | Check if an account has non-zero nonce or non-empty code. |
| 282 | |
| 283 | Parameters |
| 284 | ---------- |
| 285 | tx_state : |
| 286 | The transaction state. |
| 287 | address : |
| 288 | Address of the account that needs to be checked. |
| 289 | |
| 290 | Returns |
| 291 | ------- |
| 292 | has_code_or_nonce : ``bool`` |
| 293 | True if the account has non-zero nonce or non-empty code, |
| 294 | False otherwise. |
| 295 | |
| 296 | """ |
| 297 | account = get_account(tx_state, address) |
| 298 | 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:
| 302 | """ |
|---|---|
| 303 | Check if an account has storage. |
| 304 | |
| 305 | Parameters |
| 306 | ---------- |
| 307 | tx_state : |
| 308 | The transaction state. |
| 309 | address : |
| 310 | Address of the account that needs to be checked. |
| 311 | |
| 312 | Returns |
| 313 | ------- |
| 314 | has_storage : ``bool`` |
| 315 | True if the account has storage, False otherwise. |
| 316 | |
| 317 | """ |
| 318 | if tx_state.storage_writes.get(address): |
| 319 | return True |
| 320 | if address in tx_state.storage_clears: |
| 321 | return False |
| 322 | if tx_state.parent.storage_writes.get(address): |
| 323 | return True |
| 324 | if address in tx_state.parent.storage_clears: |
| 325 | return False |
| 326 | 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:
| 332 | """ |
|---|---|
| 333 | Check if an account exists and has zero nonce, empty code and zero |
| 334 | balance. |
| 335 | |
| 336 | Parameters |
| 337 | ---------- |
| 338 | tx_state : |
| 339 | The transaction state. |
| 340 | address : |
| 341 | Address of the account that needs to be checked. |
| 342 | |
| 343 | Returns |
| 344 | ------- |
| 345 | exists_and_is_empty : ``bool`` |
| 346 | True if an account exists and has zero nonce, empty code and |
| 347 | zero balance, False otherwise. |
| 348 | |
| 349 | """ |
| 350 | account = get_account_optional(tx_state, address) |
| 351 | return ( |
| 352 | account is not None |
| 353 | and account.nonce == Uint(0) |
| 354 | and account.code_hash == EMPTY_CODE_HASH |
| 355 | and account.balance == 0 |
| 356 | ) |
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:
| 360 | """ |
|---|---|
| 361 | Check whether an account is both in the state and non-empty. |
| 362 | |
| 363 | Parameters |
| 364 | ---------- |
| 365 | tx_state : |
| 366 | The transaction state. |
| 367 | address : |
| 368 | Address of the account that needs to be checked. |
| 369 | |
| 370 | Returns |
| 371 | ------- |
| 372 | is_alive : ``bool`` |
| 373 | True if the account is alive. |
| 374 | |
| 375 | """ |
| 376 | account = get_account_optional(tx_state, address) |
| 377 | 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:
| 385 | """ |
|---|---|
| 386 | Set the ``Account`` object at an address. Setting to ``None`` |
| 387 | deletes the account (but not its storage, see |
| 388 | ``destroy_account()``). |
| 389 | |
| 390 | Parameters |
| 391 | ---------- |
| 392 | tx_state : |
| 393 | The transaction state. |
| 394 | address : |
| 395 | Address to set. |
| 396 | account : |
| 397 | Account to set at address. |
| 398 | |
| 399 | """ |
| 400 | 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:
| 409 | """ |
|---|---|
| 410 | Set a value at a storage key on an account. |
| 411 | |
| 412 | Parameters |
| 413 | ---------- |
| 414 | tx_state : |
| 415 | The transaction state. |
| 416 | address : |
| 417 | Address of the account. |
| 418 | key : |
| 419 | Key to set. |
| 420 | value : |
| 421 | Value to set at the key. |
| 422 | |
| 423 | """ |
| 424 | assert get_account_optional(tx_state, address) is not None |
| 425 | if address not in tx_state.storage_writes: |
| 426 | tx_state.storage_writes[address] = {} |
| 427 | 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:
| 431 | """ |
|---|---|
| 432 | Completely remove the account at ``address`` and all of its storage. |
| 433 | |
| 434 | This function is made available exclusively for the ``SELFDESTRUCT`` |
| 435 | opcode. It is expected that ``SELFDESTRUCT`` will be disabled in a |
| 436 | future hardfork and this function will be removed. Only supports same |
| 437 | transaction destruction. |
| 438 | |
| 439 | Parameters |
| 440 | ---------- |
| 441 | tx_state : |
| 442 | The transaction state. |
| 443 | address : |
| 444 | Address of account to destroy. |
| 445 | |
| 446 | """ |
| 447 | destroy_storage(tx_state, address) |
| 448 | 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:
| 452 | """ |
|---|---|
| 453 | Completely remove the storage at ``address``. |
| 454 | |
| 455 | Only supports same transaction destruction. |
| 456 | |
| 457 | Parameters |
| 458 | ---------- |
| 459 | tx_state : |
| 460 | The transaction state. |
| 461 | address : |
| 462 | Address of account whose storage is to be deleted. |
| 463 | |
| 464 | """ |
| 465 | if address in tx_state.storage_writes: |
| 466 | del tx_state.storage_writes[address] |
| 467 | 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:
| 471 | """ |
|---|---|
| 472 | Mark an account as having been created in the current transaction. |
| 473 | This information is used by ``get_storage_original()`` to handle an |
| 474 | obscure edgecase, and to respect the constraints added to |
| 475 | SELFDESTRUCT by EIP-6780. |
| 476 | |
| 477 | The marker is not removed even if the account creation reverts. |
| 478 | Since the account cannot have had code prior to its creation and |
| 479 | can't call ``get_storage_original()``, this is harmless. |
| 480 | |
| 481 | Parameters |
| 482 | ---------- |
| 483 | tx_state : |
| 484 | The transaction state. |
| 485 | address : |
| 486 | Address of the account that has been created. |
| 487 | |
| 488 | """ |
| 489 | 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.
def set_transient_storage(tx_state: TransactionState, address: Address, key: Bytes32, value: U256) -> None:
| 498 | """ |
|---|---|
| 499 | Set a value at a storage key on an account in transient storage. |
| 500 | |
| 501 | Parameters |
| 502 | ---------- |
| 503 | tx_state : |
| 504 | The transaction state. |
| 505 | address : |
| 506 | Address of the account. |
| 507 | key : |
| 508 | Key to set. |
| 509 | value : |
| 510 | Value to set at the key. |
| 511 | |
| 512 | """ |
| 513 | if value == U256(0): |
| 514 | tx_state.transient_storage.pop((address, key), None) |
| 515 | else: |
| 516 | tx_state.transient_storage[(address, key)] = value |
modify_state ¶
Modify an Account in the state.
def modify_state(tx_state: TransactionState, address: Address, f: Callable[[Account], None]) -> None:
| 524 | """ |
|---|---|
| 525 | Modify an ``Account`` in the state. |
| 526 | """ |
| 527 | 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:
| 536 | """ |
|---|---|
| 537 | Move funds between accounts. |
| 538 | |
| 539 | Parameters |
| 540 | ---------- |
| 541 | tx_state : |
| 542 | The transaction state. |
| 543 | sender_address : |
| 544 | Address of the sender. |
| 545 | recipient_address : |
| 546 | Address of the recipient. |
| 547 | amount : |
| 548 | The amount to transfer. |
| 549 | |
| 550 | """ |
| 551 | |
| 552 | def reduce_sender_balance(sender: Account) -> None: |
| 553 | if sender.balance < amount: |
| 554 | raise AssertionError |
| 555 | sender.balance -= amount |
| 556 | |
| 557 | def increase_recipient_balance(recipient: Account) -> None: |
| 558 | recipient.balance += amount |
| 559 | |
| 560 | modify_state(tx_state, sender_address, reduce_sender_balance) |
| 561 | 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:
| 567 | """ |
|---|---|
| 568 | Add newly created ether to an account. |
| 569 | |
| 570 | Parameters |
| 571 | ---------- |
| 572 | tx_state : |
| 573 | The transaction state. |
| 574 | address : |
| 575 | Address of the account to which ether is added. |
| 576 | amount : |
| 577 | The amount of ether to be added to the account of interest. |
| 578 | |
| 579 | """ |
| 580 | |
| 581 | def increase_balance(account: Account) -> None: |
| 582 | account.balance += amount |
| 583 | |
| 584 | 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:
| 590 | """ |
|---|---|
| 591 | Set the balance of an account. |
| 592 | |
| 593 | Parameters |
| 594 | ---------- |
| 595 | tx_state : |
| 596 | The transaction state. |
| 597 | address : |
| 598 | Address of the account whose balance needs to be set. |
| 599 | amount : |
| 600 | The amount that needs to be set in the balance. |
| 601 | |
| 602 | """ |
| 603 | |
| 604 | def set_balance(account: Account) -> None: |
| 605 | account.balance = amount |
| 606 | |
| 607 | 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:
| 611 | """ |
|---|---|
| 612 | Increment the nonce of an account. |
| 613 | |
| 614 | Parameters |
| 615 | ---------- |
| 616 | tx_state : |
| 617 | The transaction state. |
| 618 | address : |
| 619 | Address of the account whose nonce needs to be incremented. |
| 620 | |
| 621 | """ |
| 622 | |
| 623 | def increase_nonce(sender: Account) -> None: |
| 624 | sender.nonce += Uint(1) |
| 625 | |
| 626 | 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:
| 632 | """ |
|---|---|
| 633 | Set Account code. |
| 634 | |
| 635 | Parameters |
| 636 | ---------- |
| 637 | tx_state : |
| 638 | The transaction state. |
| 639 | address : |
| 640 | Address of the account whose code needs to be updated. |
| 641 | code : |
| 642 | The bytecode that needs to be set. |
| 643 | |
| 644 | """ |
| 645 | code_hash = keccak256(code) |
| 646 | if code_hash != EMPTY_CODE_HASH: |
| 647 | tx_state.code_writes[code_hash] = code |
| 648 | |
| 649 | def write_code_hash(sender: Account) -> None: |
| 650 | sender.code_hash = code_hash |
| 651 | |
| 652 | 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:
| 659 | """ |
|---|---|
| 660 | Create a snapshot of the transaction state for rollback. |
| 661 | |
| 662 | Deep-copy writes and transient storage. The parent reference and |
| 663 | ``created_accounts`` are shared (not rolled back). |
| 664 | |
| 665 | Parameters |
| 666 | ---------- |
| 667 | tx_state : |
| 668 | The transaction state to snapshot. |
| 669 | |
| 670 | Returns |
| 671 | ------- |
| 672 | snapshot : ``TransactionState`` |
| 673 | A copy of the transaction state. |
| 674 | |
| 675 | """ |
| 676 | return TransactionState( |
| 677 | parent=tx_state.parent, |
| 678 | account_writes=dict(tx_state.account_writes), |
| 679 | storage_writes={ |
| 680 | addr: dict(slots) |
| 681 | for addr, slots in tx_state.storage_writes.items() |
| 682 | }, |
| 683 | code_writes=dict(tx_state.code_writes), |
| 684 | created_accounts=tx_state.created_accounts, |
| 685 | storage_clears=set(tx_state.storage_clears), |
| 686 | transient_storage=dict(tx_state.transient_storage), |
| 687 | ) |
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:
| 693 | """ |
|---|---|
| 694 | Restore transaction state from a snapshot (rollback on failure). |
| 695 | |
| 696 | Parameters |
| 697 | ---------- |
| 698 | tx_state : |
| 699 | The transaction state to restore. |
| 700 | snapshot : |
| 701 | The snapshot to restore from. |
| 702 | |
| 703 | """ |
| 704 | tx_state.account_writes = snapshot.account_writes |
| 705 | tx_state.storage_writes = snapshot.storage_writes |
| 706 | tx_state.code_writes = snapshot.code_writes |
| 707 | tx_state.storage_clears = snapshot.storage_clears |
| 708 | 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:
| 715 | """ |
|---|---|
| 716 | Merge transaction writes into the block state and clear for reuse. |
| 717 | |
| 718 | Parameters |
| 719 | ---------- |
| 720 | tx_state : |
| 721 | The transaction state to commit. |
| 722 | |
| 723 | """ |
| 724 | block = tx_state.parent |
| 725 | |
| 726 | for address, account in tx_state.account_writes.items(): |
| 727 | block.account_writes[address] = account |
| 728 | |
| 729 | for address in tx_state.storage_clears: |
| 730 | block.storage_clears.add(address) |
| 731 | block.storage_writes.pop(address, None) |
| 732 | |
| 733 | for address, slots in tx_state.storage_writes.items(): |
| 734 | if address not in block.storage_writes: |
| 735 | block.storage_writes[address] = {} |
| 736 | block.storage_writes[address].update(slots) |
| 737 | |
| 738 | block.code_writes.update(tx_state.code_writes) |
| 739 | |
| 740 | tx_state.account_writes.clear() |
| 741 | tx_state.storage_writes.clear() |
| 742 | tx_state.code_writes.clear() |
| 743 | tx_state.created_accounts.clear() |
| 744 | tx_state.storage_clears.clear() |
| 745 | 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.
def extract_block_diff(block_state: BlockState) -> BlockDiff:
| 749 | """ |
|---|---|
| 750 | Extract account, storage, and code diff from the block state. |
| 751 | |
| 752 | Parameters |
| 753 | ---------- |
| 754 | block_state : |
| 755 | The block state. |
| 756 | |
| 757 | Returns |
| 758 | ------- |
| 759 | diff : `BlockDiff` |
| 760 | Account, storage, and code changes accumulated during block execution. |
| 761 | |
| 762 | """ |
| 763 | return BlockDiff( |
| 764 | account_changes=block_state.account_writes, |
| 765 | storage_changes=block_state.storage_writes, |
| 766 | code_changes=block_state.code_writes, |
| 767 | storage_clears=block_state.storage_clears, |
| 768 | ) |
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:
| 774 | """ |
|---|---|
| 775 | Destroy all touched accounts that are empty. |
| 776 | |
| 777 | Parameters |
| 778 | ---------- |
| 779 | tx_state : |
| 780 | The transaction state. |
| 781 | touched_accounts : |
| 782 | All the accounts that have been touched in the current transaction. |
| 783 | |
| 784 | """ |
| 785 | for address in touched_accounts: |
| 786 | if account_exists_and_is_empty(tx_state, address): |
| 787 | 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:
| 791 | """ |
|---|---|
| 792 | Initialize an account to state. |
| 793 | |
| 794 | Parameters |
| 795 | ---------- |
| 796 | tx_state : |
| 797 | The transaction state. |
| 798 | address : |
| 799 | Address of the account that needs to be initialized. |
| 800 | |
| 801 | """ |
| 802 | if not account_exists(tx_state, address): |
| 803 | set_account(tx_state, address, EMPTY_ACCOUNT) |