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