ethereum.tangerine_whistle.vm.interpreterethereum.spurious_dragon.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 = Uint(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. `error`: The error from the execution if any.
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( |
106 | Uint(0), U256(0), tuple(), set(), AddressCollision() |
112 | Uint(0), U256(0), tuple(), set(), set(), AddressCollision() |
113 | ) |
114 | else: |
115 | evm = process_create_message(message, env) |
116 | else: |
111 | evm = process_message(message, env) |
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 | |
132 | tx_end = TransactionEnd( |
133 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
134 | ) |
135 | evm_trace(evm, tx_end) |
136 | |
137 | return MessageCallOutput( |
138 | gas_left=evm.gas_left, |
139 | refund_counter=refund_counter, |
140 | logs=logs, |
141 | accounts_to_delete=accounts_to_delete, |
142 | touched_accounts=touched_accounts, |
143 | error=evm.error, |
144 | ) |
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:
Items containing execution specific objects.~ethereum.tangerine_whistle.vm.Evm~ethereum.spurious_dragon.vm.Evm
def process_create_message(message: Message, env: Environment) -> Evm:
148 | """ |
---|---|
149 | Executes a call to create a smart contract. |
150 |
|
151 | Parameters |
152 | ---------- |
153 | message : |
154 | Transaction specific items. |
155 | env : |
156 | External items required for EVM execution. |
157 |
|
158 | Returns |
159 | ------- |
149 | evm: :py:class:`~ethereum.tangerine_whistle.vm.Evm` |
160 | evm: :py:class:`~ethereum.spurious_dragon.vm.Evm` |
161 | Items containing execution specific objects. |
162 | """ |
163 | # take snapshot of state before processing the message |
164 | begin_transaction(env.state) |
165 | |
166 | # If the address where the account is being created has storage, it is |
167 | # destroyed. This can only happen in the following highly unlikely |
168 | # circumstances: |
169 | # * The address created by two `CREATE` calls collide. |
159 | # * The first `CREATE` left empty code. |
170 | # * The first `CREATE` happened before Spurious Dragon and left empty |
171 | # code. |
172 | destroy_storage(env.state, message.current_target) |
173 | |
174 | increment_nonce(env.state, message.current_target) |
175 | evm = process_message(message, env) |
176 | if not evm.error: |
177 | contract_code = evm.output |
178 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
179 | try: |
167 | charge_gas(evm, contract_code_gas) |
180 | charge_gas(evm, contract_code_gas) |
181 | if len(contract_code) > MAX_CODE_SIZE: |
182 | raise OutOfGasError |
183 | except ExceptionalHalt as error: |
184 | rollback_transaction(env.state) |
185 | evm.gas_left = Uint(0) |
186 | evm.error = error |
187 | else: |
188 | set_code(env.state, message.current_target, contract_code) |
189 | commit_transaction(env.state) |
190 | else: |
191 | rollback_transaction(env.state) |
192 | 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:
Items containing execution specific objects~ethereum.tangerine_whistle.vm.Evm~ethereum.spurious_dragon.vm.Evm
def process_message(message: Message, env: Environment) -> Evm:
196 | """ |
---|---|
197 | Executes a call to create a smart contract. |
198 |
|
199 | Parameters |
200 | ---------- |
201 | message : |
202 | Transaction specific items. |
203 | env : |
204 | External items required for EVM execution. |
205 |
|
206 | Returns |
207 | ------- |
193 | evm: :py:class:`~ethereum.tangerine_whistle.vm.Evm` |
208 | evm: :py:class:`~ethereum.spurious_dragon.vm.Evm` |
209 | Items containing execution specific objects |
210 | """ |
211 | if message.depth > STACK_DEPTH_LIMIT: |
212 | raise StackDepthLimitError("Stack depth limit reached") |
213 | |
214 | # take snapshot of state before processing the message |
215 | begin_transaction(env.state) |
216 | |
217 | touch_account(env.state, message.current_target) |
218 | |
219 | if message.should_transfer_value and message.value != 0: |
220 | move_ether( |
221 | env.state, message.caller, message.current_target, message.value |
222 | ) |
223 | |
224 | evm = execute_code(message, env) |
225 | if evm.error: |
226 | # revert state to the last saved checkpoint |
227 | # since the message call resulted in an error |
228 | rollback_transaction(env.state) |
229 | else: |
230 | commit_transaction(env.state) |
231 | 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:
235 | """ |
---|---|
236 | Executes bytecode present in the `message`. |
237 |
|
238 | Parameters |
239 | ---------- |
240 | message : |
241 | Transaction specific items. |
242 | env : |
243 | External items required for EVM execution. |
244 |
|
245 | Returns |
246 | ------- |
247 | evm: `ethereum.vm.EVM` |
248 | Items containing execution specific objects |
249 | """ |
250 | code = message.code |
251 | valid_jump_destinations = get_valid_jump_destinations(code) |
252 | |
253 | evm = Evm( |
254 | pc=Uint(0), |
255 | stack=[], |
256 | memory=bytearray(), |
257 | code=code, |
258 | gas_left=message.gas, |
259 | env=env, |
260 | valid_jump_destinations=valid_jump_destinations, |
261 | logs=(), |
262 | refund_counter=0, |
263 | running=True, |
264 | message=message, |
265 | output=b"", |
266 | accounts_to_delete=set(), |
267 | touched_accounts=set(), |
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 < ulen(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.error = error |
293 | return evm |