ethereum.berlin.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
61 | STACK_DEPTH_LIMIT = Uint(1024) |
---|
MAX_CODE_SIZE
62 | 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.
65 | @dataclass |
---|
class MessageCallOutput:
gas_left
80 | gas_left: Uint |
---|
refund_counter
81 | refund_counter: U256 |
---|
logs
82 | logs: Tuple[Log, ...] |
---|
accounts_to_delete
83 | accounts_to_delete: Set[Address] |
---|
touched_accounts
84 | touched_accounts: Iterable[Address] |
---|
error
85 | error: Optional[Exception] |
---|
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:
91 | """ |
---|---|
92 | If `message.current` is empty then it creates a smart contract |
93 | else it executes a call from the `message.caller` to the `message.target`. |
94 |
|
95 | Parameters |
96 | ---------- |
97 | message : |
98 | Transaction specific items. |
99 |
|
100 | env : |
101 | External items required for EVM execution. |
102 |
|
103 | Returns |
104 | ------- |
105 | output : `MessageCallOutput` |
106 | Output of the message call |
107 | """ |
108 | if message.target == Bytes0(b""): |
109 | is_collision = account_has_code_or_nonce( |
110 | env.state, message.current_target |
111 | ) |
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, env) |
118 | else: |
119 | evm = process_message(message, env) |
120 | if account_exists_and_is_empty(env.state, Address(message.target)): |
121 | evm.touched_accounts.add(Address(message.target)) |
122 | |
123 | if evm.error: |
124 | logs: Tuple[Log, ...] = () |
125 | accounts_to_delete = set() |
126 | touched_accounts = set() |
127 | refund_counter = U256(0) |
128 | else: |
129 | logs = evm.logs |
130 | accounts_to_delete = evm.accounts_to_delete |
131 | touched_accounts = evm.touched_accounts |
132 | refund_counter = U256(evm.refund_counter) |
133 | |
134 | tx_end = TransactionEnd( |
135 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
136 | ) |
137 | evm_trace(evm, tx_end) |
138 | |
139 | return MessageCallOutput( |
140 | gas_left=evm.gas_left, |
141 | refund_counter=refund_counter, |
142 | logs=logs, |
143 | accounts_to_delete=accounts_to_delete, |
144 | touched_accounts=touched_accounts, |
145 | error=evm.error, |
146 | ) |
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.berlin.vm.Evm
Items containing execution specific objects.
def process_create_message(message: Message, env: Environment) -> Evm:
150 | """ |
---|---|
151 | Executes a call to create a smart contract. |
152 |
|
153 | Parameters |
154 | ---------- |
155 | message : |
156 | Transaction specific items. |
157 | env : |
158 | External items required for EVM execution. |
159 |
|
160 | Returns |
161 | ------- |
162 | evm: :py:class:`~ethereum.berlin.vm.Evm` |
163 | Items containing execution specific objects. |
164 | """ |
165 | # take snapshot of state before processing the message |
166 | begin_transaction(env.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(env.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(env.state, message.current_target) |
181 | |
182 | increment_nonce(env.state, message.current_target) |
183 | evm = process_message(message, env) |
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(env.state) |
193 | evm.gas_left = Uint(0) |
194 | evm.output = b"" |
195 | evm.error = error |
196 | else: |
197 | set_code(env.state, message.current_target, contract_code) |
198 | commit_transaction(env.state) |
199 | else: |
200 | rollback_transaction(env.state) |
201 | 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.berlin.vm.Evm
Items containing execution specific objects
def process_message(message: Message, env: Environment) -> Evm:
205 | """ |
---|---|
206 | Executes a call to create a smart contract. |
207 |
|
208 | Parameters |
209 | ---------- |
210 | message : |
211 | Transaction specific items. |
212 | env : |
213 | External items required for EVM execution. |
214 |
|
215 | Returns |
216 | ------- |
217 | evm: :py:class:`~ethereum.berlin.vm.Evm` |
218 | Items containing execution specific objects |
219 | """ |
220 | if message.depth > STACK_DEPTH_LIMIT: |
221 | raise StackDepthLimitError("Stack depth limit reached") |
222 | |
223 | # take snapshot of state before processing the message |
224 | begin_transaction(env.state) |
225 | |
226 | touch_account(env.state, message.current_target) |
227 | |
228 | if message.should_transfer_value and message.value != 0: |
229 | move_ether( |
230 | env.state, message.caller, message.current_target, message.value |
231 | ) |
232 | |
233 | evm = execute_code(message, env) |
234 | if evm.error: |
235 | # revert state to the last saved checkpoint |
236 | # since the message call resulted in an error |
237 | rollback_transaction(env.state) |
238 | else: |
239 | commit_transaction(env.state) |
240 | 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:
244 | """ |
---|---|
245 | Executes bytecode present in the `message`. |
246 |
|
247 | Parameters |
248 | ---------- |
249 | message : |
250 | Transaction specific items. |
251 | env : |
252 | External items required for EVM execution. |
253 |
|
254 | Returns |
255 | ------- |
256 | evm: `ethereum.vm.EVM` |
257 | Items containing execution specific objects |
258 | """ |
259 | code = message.code |
260 | valid_jump_destinations = get_valid_jump_destinations(code) |
261 | |
262 | evm = Evm( |
263 | pc=Uint(0), |
264 | stack=[], |
265 | memory=bytearray(), |
266 | code=code, |
267 | gas_left=message.gas, |
268 | env=env, |
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 |