ethereum.berlin.vm.interpreterethereum.london.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
65 | STACK_DEPTH_LIMIT = Uint(1024) |
---|
MAX_CODE_SIZE
66 | MAX_CODE_SIZE = 0x6000 |
---|
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. `touched_accounts`: Accounts that have been touched.
6. `error`: The error from the execution if any.
69 | @dataclass |
---|
class MessageCallOutput:
gas_left
84 | gas_left: Uint |
---|
refund_counter
85 | refund_counter: U256 |
---|
logs
86 | logs: Tuple[Log, ...] |
---|
accounts_to_delete
87 | accounts_to_delete: Set[Address] |
---|
touched_accounts
88 | touched_accounts: Set[Address] |
---|
error
89 | error: Optional[EthereumException] |
---|
process_message_call
If message.current
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:
93 | """ |
---|---|
94 | If `message.current` is empty then it creates a smart contract |
95 | else it executes a call from the `message.caller` to the `message.target`. |
96 |
|
97 | Parameters |
98 | ---------- |
99 | message : |
100 | Transaction specific items. |
101 |
|
102 | Returns |
103 | ------- |
104 | output : `MessageCallOutput` |
105 | Output of the message call |
106 | """ |
107 | block_env = message.block_env |
108 | refund_counter = U256(0) |
109 | if message.target == Bytes0(b""): |
110 | is_collision = account_has_code_or_nonce( |
111 | block_env.state, message.current_target |
112 | ) or account_has_storage(block_env.state, message.current_target) |
113 | if is_collision: |
114 | return MessageCallOutput( |
115 | Uint(0), U256(0), tuple(), set(), set(), AddressCollision() |
116 | ) |
117 | else: |
118 | evm = process_create_message(message) |
119 | else: |
120 | evm = process_message(message) |
121 | if account_exists_and_is_empty( |
122 | block_env.state, Address(message.target) |
123 | ): |
124 | evm.touched_accounts.add(Address(message.target)) |
125 | |
126 | if evm.error: |
127 | logs: Tuple[Log, ...] = () |
128 | accounts_to_delete = set() |
129 | touched_accounts = set() |
130 | else: |
131 | logs = evm.logs |
132 | accounts_to_delete = evm.accounts_to_delete |
133 | touched_accounts = evm.touched_accounts |
134 | refund_counter += U256(evm.refund_counter) |
135 | |
136 | tx_end = TransactionEnd( |
137 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
138 | ) |
139 | evm_trace(evm, tx_end) |
140 | |
141 | return MessageCallOutput( |
142 | gas_left=evm.gas_left, |
143 | refund_counter=refund_counter, |
144 | logs=logs, |
145 | accounts_to_delete=accounts_to_delete, |
146 | touched_accounts=touched_accounts, |
147 | error=evm.error, |
148 | ) |
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.berlin.vm.Evm~ethereum.london.vm.Evm
def process_create_message(message: Message) -> Evm:
152 | """ |
---|---|
153 | Executes a call to create a smart contract. |
154 |
|
155 | Parameters |
156 | ---------- |
157 | message : |
158 | Transaction specific items. |
159 |
|
160 | Returns |
161 | ------- |
161 | evm: :py:class:`~ethereum.berlin.vm.Evm` |
162 | evm: :py:class:`~ethereum.london.vm.Evm` |
163 | Items containing execution specific objects. |
164 | """ |
165 | state = message.block_env.state |
166 | # take snapshot of state before processing the message |
167 | begin_transaction(state) |
168 | |
169 | # If the address where the account is being created has storage, it is |
170 | # destroyed. This can only happen in the following highly unlikely |
171 | # circumstances: |
172 | # * The address created by a `CREATE` call collides with a subsequent |
173 | # `CREATE` or `CREATE2` call. |
174 | # * The first `CREATE` happened before Spurious Dragon and left empty |
175 | # code. |
176 | destroy_storage(state, message.current_target) |
177 | |
178 | # In the previously mentioned edge case the preexisting storage is ignored |
179 | # for gas refund purposes. In order to do this we must track created |
180 | # accounts. |
181 | mark_account_created(state, message.current_target) |
182 | |
183 | increment_nonce(state, message.current_target) |
184 | evm = process_message(message) |
185 | if not evm.error: |
186 | contract_code = evm.output |
187 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
188 | try: |
188 | charge_gas(evm, contract_code_gas) |
189 | if len(contract_code) > 0: |
190 | if contract_code[0] == 0xEF: |
191 | raise InvalidContractPrefix |
192 | charge_gas(evm, contract_code_gas) |
193 | if len(contract_code) > MAX_CODE_SIZE: |
194 | raise OutOfGasError |
195 | except ExceptionalHalt as error: |
196 | rollback_transaction(state) |
197 | evm.gas_left = Uint(0) |
198 | evm.output = b"" |
199 | evm.error = error |
200 | else: |
201 | set_code(state, message.current_target, contract_code) |
202 | commit_transaction(state) |
203 | else: |
204 | rollback_transaction(state) |
205 | 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.berlin.vm.Evm~ethereum.london.vm.Evm
def process_message(message: Message) -> Evm:
209 | """ |
---|---|
210 | Move ether and execute the relevant code. |
211 |
|
212 | Parameters |
213 | ---------- |
214 | message : |
215 | Transaction specific items. |
216 |
|
217 | Returns |
218 | ------- |
215 | evm: :py:class:`~ethereum.berlin.vm.Evm` |
219 | evm: :py:class:`~ethereum.london.vm.Evm` |
220 | Items containing execution specific objects |
221 | """ |
222 | state = message.block_env.state |
223 | if message.depth > STACK_DEPTH_LIMIT: |
224 | raise StackDepthLimitError("Stack depth limit reached") |
225 | |
226 | # take snapshot of state before processing the message |
227 | begin_transaction(state) |
228 | |
229 | touch_account(state, message.current_target) |
230 | |
231 | if message.should_transfer_value and message.value != 0: |
232 | move_ether( |
233 | state, message.caller, message.current_target, message.value |
234 | ) |
235 | |
236 | evm = execute_code(message) |
237 | if evm.error: |
238 | # revert state to the last saved checkpoint |
239 | # since the message call resulted in an error |
240 | rollback_transaction(state) |
241 | else: |
242 | commit_transaction(state) |
243 | return evm |
execute_code
Executes bytecode present in the message
.
Parameters
message : Transaction specific items.
Returns
evm: ethereum.vm.EVM
Items containing execution specific objects
def execute_code(message: Message) -> Evm:
247 | """ |
---|---|
248 | Executes bytecode present in the `message`. |
249 |
|
250 | Parameters |
251 | ---------- |
252 | message : |
253 | Transaction specific items. |
254 |
|
255 | Returns |
256 | ------- |
257 | evm: `ethereum.vm.EVM` |
258 | Items containing execution specific objects |
259 | """ |
260 | code = message.code |
261 | valid_jump_destinations = get_valid_jump_destinations(code) |
262 | |
263 | evm = Evm( |
264 | pc=Uint(0), |
265 | stack=[], |
266 | memory=bytearray(), |
267 | code=code, |
268 | gas_left=message.gas, |
269 | valid_jump_destinations=valid_jump_destinations, |
270 | logs=(), |
271 | refund_counter=0, |
272 | running=True, |
273 | message=message, |
274 | output=b"", |
275 | accounts_to_delete=set(), |
276 | touched_accounts=set(), |
277 | return_data=b"", |
278 | error=None, |
279 | accessed_addresses=message.accessed_addresses, |
280 | accessed_storage_keys=message.accessed_storage_keys, |
281 | ) |
282 | try: |
283 | if evm.message.code_address in PRE_COMPILED_CONTRACTS: |
284 | evm_trace(evm, PrecompileStart(evm.message.code_address)) |
285 | PRE_COMPILED_CONTRACTS[evm.message.code_address](evm) |
286 | evm_trace(evm, PrecompileEnd()) |
287 | return evm |
288 |
|
289 | while evm.running and evm.pc < ulen(evm.code): |
290 | try: |
291 | op = Ops(evm.code[evm.pc]) |
292 | except ValueError: |
293 | raise InvalidOpcode(evm.code[evm.pc]) |
294 |
|
295 | evm_trace(evm, OpStart(op)) |
296 | op_implementation[op](evm) |
297 | evm_trace(evm, OpEnd()) |
298 |
|
299 | evm_trace(evm, EvmStop(Ops.STOP)) |
300 |
|
301 | except ExceptionalHalt as error: |
302 | evm_trace(evm, OpException(error)) |
303 | evm.gas_left = Uint(0) |
304 | evm.output = b"" |
305 | evm.error = error |
306 | except Revert as error: |
307 | evm_trace(evm, OpException(error)) |
308 | evm.error = error |
309 | return evm |