ethereum.byzantium.vm.interpreterethereum.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
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.
Returns
output : MessageCallOutput
Output of the message call
def process_message_call(message: Message) -> 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 | Returns |
101 | ------- |
102 | output : `MessageCallOutput` |
103 | Output of the message call |
104 | """ |
105 | block_env = message.block_env |
106 | refund_counter = U256(0) |
107 | if message.target == Bytes0(b""): |
108 | is_collision = account_has_code_or_nonce( |
109 | block_env.state, message.current_target |
110 | ) or account_has_storage(block_env.state, message.current_target) |
111 | if is_collision: |
112 | return MessageCallOutput( |
113 | Uint(0), U256(0), tuple(), set(), set(), AddressCollision() |
114 | ) |
115 | else: |
116 | evm = process_create_message(message) |
117 | else: |
118 | evm = process_message(message) |
119 | if account_exists_and_is_empty( |
120 | block_env.state, Address(message.target) |
121 | ): |
122 | evm.touched_accounts.add(Address(message.target)) |
123 | |
124 | if evm.error: |
125 | logs: Tuple[Log, ...] = () |
126 | accounts_to_delete = set() |
127 | touched_accounts = set() |
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.
Returns
evm: :py:class:
Items containing execution specific objects.~ethereum.byzantium.vm.Evm~ethereum.constantinople.vm.Evm
def process_create_message(message: Message) -> Evm:
150 | """ |
---|---|
151 | Executes a call to create a smart contract. |
152 |
|
153 | Parameters |
154 | ---------- |
155 | message : |
156 | Transaction specific items. |
157 |
|
158 | Returns |
159 | ------- |
160 | evm: :py:class:`~ethereum.byzantium.vm.Evm` |
160 | evm: :py:class:`~ethereum.constantinople.vm.Evm` |
161 | Items containing execution specific objects. |
162 | """ |
163 | state = message.block_env.state |
164 | # take snapshot of state before processing the message |
165 | begin_transaction(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 two `CREATE` calls collide. |
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(state, message.current_target) |
175 | |
176 | increment_nonce(state, message.current_target) |
177 | evm = process_message(message) |
178 | if not evm.error: |
179 | contract_code = evm.output |
180 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
181 | try: |
182 | charge_gas(evm, contract_code_gas) |
183 | if len(contract_code) > MAX_CODE_SIZE: |
184 | raise OutOfGasError |
185 | except ExceptionalHalt as error: |
186 | rollback_transaction(state) |
187 | evm.gas_left = Uint(0) |
188 | evm.output = b"" |
189 | evm.error = error |
190 | else: |
191 | set_code(state, message.current_target, contract_code) |
192 | commit_transaction(state) |
193 | else: |
194 | rollback_transaction(state) |
195 | return evm |
process_message
Move ether and execute the relevant code.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:
Items containing execution specific objects~ethereum.byzantium.vm.Evm~ethereum.constantinople.vm.Evm
def process_message(message: Message) -> Evm:
199 | """ |
---|---|
200 | Move ether and execute the relevant code. |
201 |
|
202 | Parameters |
203 | ---------- |
204 | message : |
205 | Transaction specific items. |
206 |
|
207 | Returns |
208 | ------- |
208 | evm: :py:class:`~ethereum.byzantium.vm.Evm` |
209 | evm: :py:class:`~ethereum.constantinople.vm.Evm` |
210 | Items containing execution specific objects |
211 | """ |
212 | state = message.block_env.state |
213 | if message.depth > STACK_DEPTH_LIMIT: |
214 | raise StackDepthLimitError("Stack depth limit reached") |
215 | |
216 | # take snapshot of state before processing the message |
217 | begin_transaction(state) |
218 | |
219 | touch_account(state, message.current_target) |
220 | |
221 | if message.should_transfer_value and message.value != 0: |
222 | move_ether( |
223 | state, message.caller, message.current_target, message.value |
224 | ) |
225 | |
226 | evm = execute_code(message) |
227 | if evm.error: |
228 | # revert state to the last saved checkpoint |
229 | # since the message call resulted in an error |
230 | rollback_transaction(state) |
231 | else: |
232 | commit_transaction(state) |
233 | return evm |
execute_code
Executes bytecode present in the message
.
Parameters
message : Transaction specific items.
Returns
evm: ethereum.vm.EVM
Items containing execution specific objects
def execute_code(message: Message) -> Evm:
237 | """ |
---|---|
238 | Executes bytecode present in the `message`. |
239 |
|
240 | Parameters |
241 | ---------- |
242 | message : |
243 | Transaction specific items. |
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 | valid_jump_destinations=valid_jump_destinations, |
260 | logs=(), |
261 | refund_counter=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 < 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.output = b"" |
293 | evm.error = error |
294 | except Revert as error: |
295 | evm_trace(evm, OpException(error)) |
296 | evm.error = error |
297 | return evm |