ethereum.forks.bpo5.vm.interpreterethereum.forks.amsterdam.vm.interpreter
Ethereum Virtual Machine (EVM) Interpreter.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
A straightforward interpreter that executes EVM code.
STACK_DEPTH_LIMIT
| 74 | STACK_DEPTH_LIMIT = Uint(1024) |
|---|
MAX_CODE_SIZE
| 75 | MAX_CODE_SIZE = 0x6000 |
|---|
MAX_INIT_CODE_SIZE
| 76 | MAX_INIT_CODE_SIZE = 2 * MAX_CODE_SIZE |
|---|
MessageCallOutput
Output of a particular message call.
Contains the following:
1. `gas_left`: remaining gas after execution.
2. `refund_counter`: gas to refund after execution.
3. `logs`: list of `Log` generated during execution.
4. `accounts_to_delete`: Contracts which have self-destructed.
5. `error`: The error from the execution if any.
6. `return_data`: The output of the execution.
| 79 | @dataclass |
|---|
class MessageCallOutput:
gas_left
| 94 | gas_left: Uint |
|---|
refund_counter
| 95 | refund_counter: U256 |
|---|
logs
| 96 | logs: Tuple[Log, ...] |
|---|
accounts_to_delete
| 97 | accounts_to_delete: Set[Address] |
|---|
error
| 98 | error: Optional[EthereumException] |
|---|
return_data
| 99 | return_data: Bytes |
|---|
process_message_call
If message.target is empty then it creates a smart contract
else it executes a call from the message.caller to the message.target.
Parameters
message : Transaction specific items.
Returns
output : MessageCallOutput
Output of the message call
def process_message_call(message: Message) -> MessageCallOutput:
| 103 | """ |
|---|---|
| 104 | If `message.target` is empty then it creates a smart contract |
| 105 | else it executes a call from the `message.caller` to the `message.target`. |
| 106 | |
| 107 | Parameters |
| 108 | ---------- |
| 109 | message : |
| 110 | Transaction specific items. |
| 111 | |
| 112 | Returns |
| 113 | ------- |
| 114 | output : `MessageCallOutput` |
| 115 | Output of the message call |
| 116 | |
| 117 | """ |
| 118 | block_env = message.block_env |
| 119 | refund_counter = U256(0) |
| 120 | if message.target == Bytes0(b""): |
| 121 | is_collision = account_has_code_or_nonce( |
| 122 | block_env.state, message.current_target |
| 123 | ) or account_has_storage(block_env.state, message.current_target) |
| 124 | track_address(message.tx_env.state_changes, message.current_target) |
| 125 | if is_collision: |
| 126 | return MessageCallOutput( |
| 127 | Uint(0), |
| 128 | U256(0), |
| 129 | tuple(), |
| 130 | set(), |
| 131 | AddressCollision(), |
| 132 | Bytes(b""), |
| 133 | ) |
| 134 | else: |
| 135 | evm = process_create_message(message) |
| 136 | else: |
| 137 | if message.tx_env.authorizations != (): |
| 138 | refund_counter += set_delegation(message) |
| 139 | |
| 140 | delegated_address = get_delegated_code_address(message.code) |
| 141 | if delegated_address is not None: |
| 142 | message.disable_precompiles = True |
| 143 | message.accessed_addresses.add(delegated_address) |
| 144 | message.code = get_account(block_env.state, delegated_address).code |
| 134 | message.code_address = delegated_address |
| 145 | message.code_address = delegated_address |
| 146 | track_address(message.block_env.state_changes, delegated_address) |
| 147 | |
| 148 | evm = process_message(message) |
| 149 | |
| 150 | if evm.error: |
| 151 | logs: Tuple[Log, ...] = () |
| 152 | accounts_to_delete = set() |
| 153 | else: |
| 154 | logs = evm.logs |
| 155 | accounts_to_delete = evm.accounts_to_delete |
| 156 | refund_counter += U256(evm.refund_counter) |
| 157 | |
| 158 | tx_end = TransactionEnd( |
| 159 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
| 160 | ) |
| 161 | evm_trace(evm, tx_end) |
| 162 | |
| 163 | return MessageCallOutput( |
| 164 | gas_left=evm.gas_left, |
| 165 | refund_counter=refund_counter, |
| 166 | logs=logs, |
| 167 | accounts_to_delete=accounts_to_delete, |
| 168 | error=evm.error, |
| 169 | return_data=evm.output, |
| 170 | ) |
process_create_message
Executes a call to create a smart contract.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:
Items containing execution specific objects.~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm
def process_create_message(message: Message) -> Evm:
| 174 | """ |
|---|---|
| 175 | Executes a call to create a smart contract. |
| 176 | |
| 177 | Parameters |
| 178 | ---------- |
| 179 | message : |
| 180 | Transaction specific items. |
| 181 | |
| 182 | Returns |
| 183 | ------- |
| 172 | evm: :py:class:`~ethereum.forks.bpo5.vm.Evm` |
| 184 | evm: :py:class:`~ethereum.forks.amsterdam.vm.Evm` |
| 185 | Items containing execution specific objects. |
| 186 | |
| 187 | """ |
| 188 | state = message.block_env.state |
| 189 | transient_storage = message.tx_env.transient_storage |
| 190 | # take snapshot of state before processing the message |
| 191 | begin_transaction(state, transient_storage) |
| 192 | |
| 193 | # If the address where the account is being created has storage, it is |
| 194 | # destroyed. This can only happen in the following highly unlikely |
| 195 | # circumstances: |
| 196 | # * The address created by a `CREATE` call collides with a subsequent |
| 197 | # `CREATE` or `CREATE2` call. |
| 198 | # * The first `CREATE` happened before Spurious Dragon and left empty |
| 199 | # code. |
| 200 | destroy_storage(state, message.current_target) |
| 201 | |
| 202 | # In the previously mentioned edge case the preexisting storage is ignored |
| 203 | # for gas refund purposes. In order to do this we must track created |
| 204 | # accounts. This tracking is also needed to respect the constraints |
| 205 | # added to SELFDESTRUCT by EIP-6780. |
| 206 | mark_account_created(state, message.current_target) |
| 207 | |
| 208 | increment_nonce(state, message.current_target) |
| 209 | nonce_after = get_account(state, message.current_target).nonce |
| 210 | track_nonce_change( |
| 211 | message.state_changes, |
| 212 | message.current_target, |
| 213 | U64(nonce_after), |
| 214 | ) |
| 215 | |
| 216 | capture_pre_code(message.tx_env.state_changes, message.current_target, b"") |
| 217 | |
| 218 | evm = process_message(message) |
| 219 | if not evm.error: |
| 220 | contract_code = evm.output |
| 221 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
| 222 | try: |
| 223 | if len(contract_code) > 0: |
| 224 | if contract_code[0] == 0xEF: |
| 225 | raise InvalidContractPrefix |
| 226 | charge_gas(evm, contract_code_gas) |
| 227 | if len(contract_code) > MAX_CODE_SIZE: |
| 228 | raise OutOfGasError |
| 229 | except ExceptionalHalt as error: |
| 230 | rollback_transaction(state, transient_storage) |
| 231 | merge_on_failure(message.state_changes) |
| 232 | evm.gas_left = Uint(0) |
| 233 | evm.output = b"" |
| 234 | evm.error = error |
| 235 | else: |
| 236 | # Note: No need to capture pre code since it's always b"" here |
| 237 | set_code(state, message.current_target, contract_code) |
| 215 | commit_transaction(state, transient_storage) |
| 238 | if contract_code != b"": |
| 239 | track_code_change( |
| 240 | message.state_changes, |
| 241 | message.current_target, |
| 242 | contract_code, |
| 243 | ) |
| 244 | commit_transaction(state, transient_storage) |
| 245 | merge_on_success(message.state_changes) |
| 246 | else: |
| 217 | rollback_transaction(state, transient_storage) |
| 247 | rollback_transaction(state, transient_storage) |
| 248 | merge_on_failure(message.state_changes) |
| 249 | return evm |
process_message
Move ether and execute the relevant code.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:
Items containing execution specific objects~ethereum.forks.bpo5.vm.Evm~ethereum.forks.amsterdam.vm.Evm
def process_message(message: Message) -> Evm:
| 253 | """ |
|---|---|
| 254 | Move ether and execute the relevant code. |
| 255 | |
| 256 | Parameters |
| 257 | ---------- |
| 258 | message : |
| 259 | Transaction specific items. |
| 260 | |
| 261 | Returns |
| 262 | ------- |
| 232 | evm: :py:class:`~ethereum.forks.bpo5.vm.Evm` |
| 263 | evm: :py:class:`~ethereum.forks.amsterdam.vm.Evm` |
| 264 | Items containing execution specific objects |
| 265 | |
| 266 | """ |
| 267 | state = message.block_env.state |
| 268 | transient_storage = message.tx_env.transient_storage |
| 269 | if message.depth > STACK_DEPTH_LIMIT: |
| 270 | raise StackDepthLimitError("Stack depth limit reached") |
| 271 | |
| 240 | transient_storage = message.tx_env.transient_storage |
| 272 | code = message.code |
| 273 | valid_jump_destinations = get_valid_jump_destinations(code) |
| 274 | evm = Evm( |
| 275 | pc=Uint(0), |
| 276 | stack=[], |
| 277 | memory=bytearray(), |
| 278 | code=code, |
| 279 | gas_left=message.gas, |
| 280 | valid_jump_destinations=valid_jump_destinations, |
| 281 | logs=(), |
| 282 | refund_counter=0, |
| 283 | running=True, |
| 284 | message=message, |
| 285 | output=b"", |
| 286 | accounts_to_delete=set(), |
| 287 | return_data=b"", |
| 288 | error=None, |
| 289 | accessed_addresses=message.accessed_addresses, |
| 290 | accessed_storage_keys=message.accessed_storage_keys, |
| 291 | state_changes=message.state_changes, |
| 292 | ) |
| 293 | |
| 294 | # take snapshot of state before processing the message |
| 295 | begin_transaction(state, transient_storage) |
| 296 | |
| 297 | track_address(message.state_changes, message.current_target) |
| 298 | |
| 299 | if message.should_transfer_value and message.value != 0: |
| 266 | move_ether( |
| 300 | # Track value transfer |
| 301 | sender_balance = get_account(state, message.caller).balance |
| 302 | recipient_balance = get_account(state, message.current_target).balance |
| 303 | |
| 304 | track_address(message.state_changes, message.caller) |
| 305 | capture_pre_balance( |
| 306 | message.tx_env.state_changes, message.caller, sender_balance |
| 307 | ) |
| 308 | capture_pre_balance( |
| 309 | message.tx_env.state_changes, |
| 310 | message.current_target, |
| 311 | recipient_balance, |
| 312 | ) |
| 313 | |
| 314 | move_ether( |
| 315 | state, message.caller, message.current_target, message.value |
| 316 | ) |
| 317 | |
| 318 | sender_new_balance = get_account(state, message.caller).balance |
| 319 | recipient_new_balance = get_account( |
| 320 | state, message.current_target |
| 321 | ).balance |
| 322 | |
| 323 | track_balance_change( |
| 324 | message.state_changes, |
| 325 | message.caller, |
| 326 | U256(sender_new_balance), |
| 327 | ) |
| 328 | track_balance_change( |
| 329 | message.state_changes, |
| 330 | message.current_target, |
| 331 | U256(recipient_new_balance), |
| 332 | ) |
| 333 | |
| 334 | try: |
| 335 | if evm.message.code_address in PRE_COMPILED_CONTRACTS: |
| 336 | if not message.disable_precompiles: |
| 337 | evm_trace(evm, PrecompileStart(evm.message.code_address)) |
| 338 | PRE_COMPILED_CONTRACTS[evm.message.code_address](evm) |
| 339 | evm_trace(evm, PrecompileEnd()) |
| 340 | else: |
| 341 | while evm.running and evm.pc < ulen(evm.code): |
| 342 | try: |
| 343 | op = Ops(evm.code[evm.pc]) |
| 344 | except ValueError as e: |
| 345 | raise InvalidOpcode(evm.code[evm.pc]) from e |
| 346 | |
| 347 | evm_trace(evm, OpStart(op)) |
| 348 | op_implementation[op](evm) |
| 349 | evm_trace(evm, OpEnd()) |
| 350 | |
| 351 | evm_trace(evm, EvmStop(Ops.STOP)) |
| 352 | |
| 353 | except ExceptionalHalt as error: |
| 354 | evm_trace(evm, OpException(error)) |
| 355 | evm.gas_left = Uint(0) |
| 356 | evm.output = b"" |
| 357 | evm.error = error |
| 358 | except Revert as error: |
| 359 | evm_trace(evm, OpException(error)) |
| 360 | evm.error = error |
| 361 | |
| 362 | if evm.error: |
| 299 | # revert state to the last saved checkpoint |
| 300 | # since the message call resulted in an error |
| 301 | rollback_transaction(state, transient_storage) |
| 363 | rollback_transaction(state, transient_storage) |
| 364 | if not message.is_create: |
| 365 | merge_on_failure(evm.state_changes) |
| 366 | else: |
| 303 | commit_transaction(state, transient_storage) |
| 367 | commit_transaction(state, transient_storage) |
| 368 | if not message.is_create: |
| 369 | merge_on_success(evm.state_changes) |
| 370 | return evm |