ethereum.forks.bogota.vm.instructions.environment
Ethereum Virtual Machine (EVM) Environmental Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM environment related instructions.
address ¶
Pushes the address of the current executing account to the stack.
Parameters
evm : The current EVM frame.
balance ¶
Pushes the balance of the given account onto the stack.
Parameters
evm : The current EVM frame.
def balance(evm: Evm) -> None:
| 59 | <snip> |
|---|---|
| 68 | # STACK |
| 69 | address = to_address_masked(pop(evm.stack)) |
| 70 | |
| 71 | # GAS |
| 72 | if address in evm.accessed_addresses: |
| 73 | charge_gas(evm, GasCosts.WARM_ACCESS) |
| 74 | else: |
| 75 | evm.accessed_addresses.add(address) |
| 76 | charge_gas(evm, GasCosts.COLD_ACCOUNT_ACCESS) |
| 77 | |
| 78 | # OPERATION |
| 79 | # Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0. |
| 80 | tx_state = evm.tx_env.state |
| 81 | balance = get_account(tx_state, address).balance |
| 82 | |
| 83 | push(evm.stack, balance) |
| 84 | |
| 85 | # PROGRAM COUNTER |
| 86 | evm.pc += Uint(1) |
origin ¶
Pushes the caller of the transaction's top-level call to the stack,
at every call depth. In a frame transaction this is the executing
frame's caller: FRAME_ENTRY_POINT in DEFAULT and VERIFY
frames, and the sender — possibly a contract — in SENDER frames.
Parameters
evm : The current EVM frame.
caller ¶
Pushes the address of the caller onto the stack.
Parameters
evm : The current EVM frame.
callvalue ¶
Push the value (in wei) sent with the call onto the stack.
Parameters
evm : The current EVM frame.
calldataload ¶
Push a word (32 bytes) of the input data belonging to the current environment onto the stack.
Parameters
evm : The current EVM frame.
def calldataload(evm: Evm) -> None:
| 162 | <snip> |
|---|---|
| 172 | # STACK |
| 173 | start_index = pop(evm.stack) |
| 174 | |
| 175 | # GAS |
| 176 | charge_gas(evm, GasCosts.OPCODE_CALLDATALOAD) |
| 177 | |
| 178 | # OPERATION |
| 179 | value = buffer_read(evm.call_data, start_index, U256(32)) |
| 180 | |
| 181 | push(evm.stack, U256.from_be_bytes(value)) |
| 182 | |
| 183 | # PROGRAM COUNTER |
| 184 | evm.pc += Uint(1) |
calldatasize ¶
Push the size of input data in current environment onto the stack.
Parameters
evm : The current EVM frame.
calldatacopy ¶
Copy a portion of the input data in current environment to memory.
This will also expand the memory, in case that the memory is insufficient to store the data.
Parameters
evm : The current EVM frame.
def calldatacopy(evm: Evm) -> None:
| 211 | <snip> |
|---|---|
| 223 | # STACK |
| 224 | memory_start_index = pop(evm.stack) |
| 225 | data_start_index = pop(evm.stack) |
| 226 | size = pop(evm.stack) |
| 227 | |
| 228 | # GAS |
| 229 | words = ceil32(Uint(size)) // Uint(32) |
| 230 | copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words) |
| 231 | extend_memory = calculate_gas_extend_memory( |
| 232 | evm.memory, [(memory_start_index, size)] |
| 233 | ) |
| 234 | charge_gas( |
| 235 | evm, |
| 236 | GasCosts.OPCODE_CALLDATACOPY_BASE + copy_gas_cost + extend_memory.cost, |
| 237 | ) |
| 238 | |
| 239 | # OPERATION |
| 240 | evm.memory += b"\x00" * extend_memory.expand_by |
| 241 | value = buffer_read(evm.call_data, data_start_index, size) |
| 242 | memory_write(evm.memory, memory_start_index, value) |
| 243 | |
| 244 | # PROGRAM COUNTER |
| 245 | evm.pc += Uint(1) |
codesize ¶
Push the size of code running in current environment onto the stack.
Parameters
evm : The current EVM frame.
codecopy ¶
Copy a portion of the code in current environment to memory.
This will also expand the memory, in case that the memory is insufficient to store the data.
Parameters
evm : The current EVM frame.
def codecopy(evm: Evm) -> None:
| 272 | <snip> |
|---|---|
| 284 | # STACK |
| 285 | memory_start_index = pop(evm.stack) |
| 286 | code_start_index = pop(evm.stack) |
| 287 | size = pop(evm.stack) |
| 288 | |
| 289 | # GAS |
| 290 | words = ceil32(Uint(size)) // Uint(32) |
| 291 | copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words) |
| 292 | extend_memory = calculate_gas_extend_memory( |
| 293 | evm.memory, [(memory_start_index, size)] |
| 294 | ) |
| 295 | charge_gas( |
| 296 | evm, |
| 297 | GasCosts.OPCODE_CODECOPY_BASE + copy_gas_cost + extend_memory.cost, |
| 298 | ) |
| 299 | |
| 300 | # OPERATION |
| 301 | evm.memory += b"\x00" * extend_memory.expand_by |
| 302 | value = buffer_read(evm.code, code_start_index, size) |
| 303 | memory_write(evm.memory, memory_start_index, value) |
| 304 | |
| 305 | # PROGRAM COUNTER |
| 306 | evm.pc += Uint(1) |
gasprice ¶
Push the gas price used in current environment onto the stack.
Parameters
evm : The current EVM frame.
extcodesize ¶
Push the code size of a given account onto the stack.
Parameters
evm : The current EVM frame.
def extcodesize(evm: Evm) -> None:
| 333 | <snip> |
|---|---|
| 342 | # STACK |
| 343 | address = to_address_masked(pop(evm.stack)) |
| 344 | |
| 345 | # GAS |
| 346 | if address in evm.accessed_addresses: |
| 347 | access_gas_cost = GasCosts.WARM_ACCESS |
| 348 | else: |
| 349 | evm.accessed_addresses.add(address) |
| 350 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 351 | access_gas_cost += GasCosts.WARM_ACCESS # Code reading cost (EIP-8038) |
| 352 | charge_gas(evm, access_gas_cost) |
| 353 | |
| 354 | # OPERATION |
| 355 | tx_state = evm.tx_env.state |
| 356 | code_hash = get_account(tx_state, address).code_hash |
| 357 | code = get_code(tx_state, code_hash) |
| 358 | |
| 359 | codesize = U256(len(code)) |
| 360 | push(evm.stack, codesize) |
| 361 | |
| 362 | # PROGRAM COUNTER |
| 363 | evm.pc += Uint(1) |
extcodecopy ¶
Copy a portion of an account's code to memory.
Parameters
evm : The current EVM frame.
def extcodecopy(evm: Evm) -> None:
| 367 | <snip> |
|---|---|
| 376 | # STACK |
| 377 | address = to_address_masked(pop(evm.stack)) |
| 378 | memory_start_index = pop(evm.stack) |
| 379 | code_start_index = pop(evm.stack) |
| 380 | size = pop(evm.stack) |
| 381 | |
| 382 | # GAS |
| 383 | words = ceil32(Uint(size)) // Uint(32) |
| 384 | copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words) |
| 385 | extend_memory = calculate_gas_extend_memory( |
| 386 | evm.memory, [(memory_start_index, size)] |
| 387 | ) |
| 388 | |
| 389 | if address in evm.accessed_addresses: |
| 390 | access_gas_cost = GasCosts.WARM_ACCESS |
| 391 | else: |
| 392 | evm.accessed_addresses.add(address) |
| 393 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 394 | access_gas_cost += GasCosts.WARM_ACCESS # Code reading cost (EIP-8038) |
| 395 | |
| 396 | total_gas_cost = access_gas_cost + copy_gas_cost + extend_memory.cost |
| 397 | |
| 398 | charge_gas(evm, total_gas_cost) |
| 399 | |
| 400 | # OPERATION |
| 401 | evm.memory += b"\x00" * extend_memory.expand_by |
| 402 | tx_state = evm.tx_env.state |
| 403 | code_hash = get_account(tx_state, address).code_hash |
| 404 | code = get_code(tx_state, code_hash) |
| 405 | |
| 406 | value = buffer_read(code, code_start_index, size) |
| 407 | memory_write(evm.memory, memory_start_index, value) |
| 408 | |
| 409 | # PROGRAM COUNTER |
| 410 | evm.pc += Uint(1) |
returndatasize ¶
Pushes the size of the return data buffer onto the stack.
Parameters
evm : The current EVM frame.
returndatacopy ¶
Copies data from the return data buffer to memory.
Parameters
evm : The current EVM frame.
def returndatacopy(evm: Evm) -> None:
| 437 | <snip> |
|---|---|
| 446 | # STACK |
| 447 | memory_start_index = pop(evm.stack) |
| 448 | return_data_start_position = pop(evm.stack) |
| 449 | size = pop(evm.stack) |
| 450 | |
| 451 | # GAS |
| 452 | words = ceil32(Uint(size)) // Uint(32) |
| 453 | copy_gas_cost = ExecutionGas( |
| 454 | GasCosts.OPCODE_RETURNDATACOPY_PER_WORD * words |
| 455 | ) |
| 456 | extend_memory = calculate_gas_extend_memory( |
| 457 | evm.memory, [(memory_start_index, size)] |
| 458 | ) |
| 459 | charge_gas( |
| 460 | evm, |
| 461 | GasCosts.OPCODE_RETURNDATACOPY_BASE |
| 462 | + copy_gas_cost |
| 463 | + extend_memory.cost, |
| 464 | ) |
| 465 | if Uint(return_data_start_position) + Uint(size) > ulen(evm.return_data): |
| 466 | raise OutOfBoundsRead |
| 467 | |
| 468 | evm.memory += b"\x00" * extend_memory.expand_by |
| 469 | value = evm.return_data[ |
| 470 | return_data_start_position : return_data_start_position + size |
| 471 | ] |
| 472 | memory_write(evm.memory, memory_start_index, value) |
| 473 | |
| 474 | # PROGRAM COUNTER |
| 475 | evm.pc += Uint(1) |
extcodehash ¶
Returns the keccak256 hash of a contract’s bytecode.
Parameters
evm : The current EVM frame.
def extcodehash(evm: Evm) -> None:
| 479 | <snip> |
|---|---|
| 488 | # STACK |
| 489 | address = to_address_masked(pop(evm.stack)) |
| 490 | |
| 491 | # GAS |
| 492 | if address in evm.accessed_addresses: |
| 493 | access_gas_cost = GasCosts.WARM_ACCESS |
| 494 | else: |
| 495 | evm.accessed_addresses.add(address) |
| 496 | access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS |
| 497 | |
| 498 | charge_gas(evm, access_gas_cost) |
| 499 | |
| 500 | # OPERATION |
| 501 | tx_state = evm.tx_env.state |
| 502 | account = get_account(tx_state, address) |
| 503 | |
| 504 | if account == EMPTY_ACCOUNT: |
| 505 | codehash = U256(0) |
| 506 | else: |
| 507 | codehash = U256.from_be_bytes(account.code_hash) |
| 508 | |
| 509 | push(evm.stack, codehash) |
| 510 | |
| 511 | # PROGRAM COUNTER |
| 512 | evm.pc += Uint(1) |
self_balance ¶
Pushes the balance of the current address to the stack.
Parameters
evm : The current EVM frame.
def self_balance(evm: Evm) -> None:
| 516 | <snip> |
|---|---|
| 525 | # STACK |
| 526 | pass |
| 527 | |
| 528 | # GAS |
| 529 | charge_gas(evm, GasCosts.OPCODE_SELFBALANCE) |
| 530 | |
| 531 | # OPERATION |
| 532 | # Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0. |
| 533 | balance = get_account(evm.tx_env.state, evm.current_target).balance |
| 534 | |
| 535 | push(evm.stack, balance) |
| 536 | |
| 537 | # PROGRAM COUNTER |
| 538 | evm.pc += Uint(1) |
base_fee ¶
Pushes the base fee of the current block on to the stack.
Parameters
evm : The current EVM frame.
blob_hash ¶
Pushes the versioned hash at a particular index on to the stack.
Parameters
evm : The current EVM frame.
def blob_hash(evm: Evm) -> None:
| 565 | <snip> |
|---|---|
| 574 | # STACK |
| 575 | index = pop(evm.stack) |
| 576 | |
| 577 | # GAS |
| 578 | charge_gas(evm, GasCosts.OPCODE_BLOBHASH) |
| 579 | |
| 580 | # OPERATION |
| 581 | if int(index) < len(evm.tx_env.blob_versioned_hashes): |
| 582 | blob_hash = evm.tx_env.blob_versioned_hashes[index] |
| 583 | else: |
| 584 | blob_hash = Bytes32(b"\x00" * 32) |
| 585 | push(evm.stack, U256.from_be_bytes(blob_hash)) |
| 586 | |
| 587 | # PROGRAM COUNTER |
| 588 | evm.pc += Uint(1) |
blob_base_fee ¶
Pushes the blob base fee on to the stack.
Parameters
evm : The current EVM frame.
def blob_base_fee(evm: Evm) -> None:
| 592 | <snip> |
|---|---|
| 601 | # STACK |
| 602 | pass |
| 603 | |
| 604 | # GAS |
| 605 | charge_gas(evm, GasCosts.OPCODE_BLOBBASEFEE) |
| 606 | |
| 607 | # OPERATION |
| 608 | blob_base_fee = calculate_blob_gas_price(evm.block_env.excess_blob_gas) |
| 609 | push(evm.stack, U256(blob_base_fee)) |
| 610 | |
| 611 | # PROGRAM COUNTER |
| 612 | evm.pc += Uint(1) |