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