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