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