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