ethereum.spurious_dragon.vm.interpreterethereum.byzantium.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
63 | STACK_DEPTH_LIMIT = Uint(1024) |
---|
MAX_CODE_SIZE
64 | 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.
67 | @dataclass |
---|
class MessageCallOutput:
gas_left
82 | gas_left: Uint |
---|
refund_counter
83 | refund_counter: U256 |
---|
logs
84 | logs: Tuple[Log, ...] |
---|
accounts_to_delete
85 | accounts_to_delete: Set[Address] |
---|
touched_accounts
86 | touched_accounts: Set[Address] |
---|
error
87 | 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.
env : External items required for EVM execution.
Returns
output : MessageCallOutput
Output of the message call
def process_message_call(message: Message, env: Environment) -> MessageCallOutput:
93 | """ |
---|---|
94 | If `message.current` is empty then it creates a smart contract |
95 | else it executes a call from the `message.caller` to the `message.target`. |
96 |
|
97 | Parameters |
98 | ---------- |
99 | message : |
100 | Transaction specific items. |
101 |
|
102 | env : |
103 | External items required for EVM execution. |
104 |
|
105 | Returns |
106 | ------- |
107 | output : `MessageCallOutput` |
108 | Output of the message call |
109 | """ |
110 | if message.target == Bytes0(b""): |
111 | is_collision = account_has_code_or_nonce( |
112 | env.state, message.current_target |
113 | ) or account_has_storage(env.state, message.current_target) |
114 | if is_collision: |
115 | return MessageCallOutput( |
116 | Uint(0), U256(0), tuple(), set(), set(), AddressCollision() |
117 | ) |
118 | else: |
119 | evm = process_create_message(message, env) |
120 | else: |
121 | evm = process_message(message, env) |
122 | if account_exists_and_is_empty(env.state, Address(message.target)): |
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 | refund_counter = U256(0) |
130 | else: |
131 | logs = evm.logs |
132 | accounts_to_delete = evm.accounts_to_delete |
133 | touched_accounts = evm.touched_accounts |
134 | refund_counter = U256(evm.refund_counter) |
135 | |
136 | tx_end = TransactionEnd( |
137 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
138 | ) |
139 | evm_trace(evm, tx_end) |
140 | |
141 | return MessageCallOutput( |
142 | gas_left=evm.gas_left, |
143 | refund_counter=refund_counter, |
144 | logs=logs, |
145 | accounts_to_delete=accounts_to_delete, |
146 | touched_accounts=touched_accounts, |
147 | error=evm.error, |
148 | ) |
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.spurious_dragon.vm.Evm~ethereum.byzantium.vm.Evm
def process_create_message(message: Message, env: Environment) -> Evm:
152 | """ |
---|---|
153 | Executes a call to create a smart contract. |
154 |
|
155 | Parameters |
156 | ---------- |
157 | message : |
158 | Transaction specific items. |
159 | env : |
160 | External items required for EVM execution. |
161 |
|
162 | Returns |
163 | ------- |
163 | evm: :py:class:`~ethereum.spurious_dragon.vm.Evm` |
164 | evm: :py:class:`~ethereum.byzantium.vm.Evm` |
165 | Items containing execution specific objects. |
166 | """ |
167 | # take snapshot of state before processing the message |
168 | begin_transaction(env.state) |
169 | |
170 | # If the address where the account is being created has storage, it is |
171 | # destroyed. This can only happen in the following highly unlikely |
172 | # circumstances: |
173 | # * The address created by two `CREATE` calls collide. |
174 | # * The first `CREATE` happened before Spurious Dragon and left empty |
175 | # code. |
176 | destroy_storage(env.state, message.current_target) |
177 | |
178 | increment_nonce(env.state, message.current_target) |
179 | evm = process_message(message, env) |
180 | if not evm.error: |
181 | contract_code = evm.output |
182 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
183 | try: |
184 | charge_gas(evm, contract_code_gas) |
185 | if len(contract_code) > MAX_CODE_SIZE: |
186 | raise OutOfGasError |
187 | except ExceptionalHalt as error: |
188 | rollback_transaction(env.state) |
189 | evm.gas_left = Uint(0) |
190 | evm.output = b"" |
191 | evm.error = error |
192 | else: |
193 | set_code(env.state, message.current_target, contract_code) |
194 | commit_transaction(env.state) |
195 | else: |
196 | rollback_transaction(env.state) |
197 | 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.spurious_dragon.vm.Evm~ethereum.byzantium.vm.Evm
def process_message(message: Message, env: Environment) -> Evm:
201 | """ |
---|---|
202 | Executes a call to create a smart contract. |
203 |
|
204 | Parameters |
205 | ---------- |
206 | message : |
207 | Transaction specific items. |
208 | env : |
209 | External items required for EVM execution. |
210 |
|
211 | Returns |
212 | ------- |
211 | evm: :py:class:`~ethereum.spurious_dragon.vm.Evm` |
213 | evm: :py:class:`~ethereum.byzantium.vm.Evm` |
214 | Items containing execution specific objects |
215 | """ |
216 | if message.depth > STACK_DEPTH_LIMIT: |
217 | raise StackDepthLimitError("Stack depth limit reached") |
218 | |
219 | # take snapshot of state before processing the message |
220 | begin_transaction(env.state) |
221 | |
222 | touch_account(env.state, message.current_target) |
223 | |
224 | if message.should_transfer_value and message.value != 0: |
225 | move_ether( |
226 | env.state, message.caller, message.current_target, message.value |
227 | ) |
228 | |
229 | evm = execute_code(message, env) |
230 | if evm.error: |
231 | # revert state to the last saved checkpoint |
232 | # since the message call resulted in an error |
233 | rollback_transaction(env.state) |
234 | else: |
235 | commit_transaction(env.state) |
236 | 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:
240 | """ |
---|---|
241 | Executes bytecode present in the `message`. |
242 |
|
243 | Parameters |
244 | ---------- |
245 | message : |
246 | Transaction specific items. |
247 | env : |
248 | External items required for EVM execution. |
249 |
|
250 | Returns |
251 | ------- |
252 | evm: `ethereum.vm.EVM` |
253 | Items containing execution specific objects |
254 | """ |
255 | code = message.code |
256 | valid_jump_destinations = get_valid_jump_destinations(code) |
257 | |
258 | evm = Evm( |
259 | pc=Uint(0), |
260 | stack=[], |
261 | memory=bytearray(), |
262 | code=code, |
263 | gas_left=message.gas, |
264 | env=env, |
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 |