ethereum.frontier.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) |
---|
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.
62 | @dataclass |
---|
class MessageCallOutput:
gas_left
76 | gas_left: Uint |
---|
refund_counter
77 | refund_counter: U256 |
---|
logs
78 | logs: Tuple[Log, ...] |
---|
accounts_to_delete
79 | accounts_to_delete: Set[Address] |
---|
error
80 | 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:
84 | """ |
---|---|
85 | If `message.current` is empty then it creates a smart contract |
86 | else it executes a call from the `message.caller` to the `message.target`. |
87 |
|
88 | Parameters |
89 | ---------- |
90 | message : |
91 | Transaction specific items. |
92 |
|
93 | Returns |
94 | ------- |
95 | output : `MessageCallOutput` |
96 | Output of the message call |
97 | """ |
98 | block_env = message.block_env |
99 | refund_counter = U256(0) |
100 | if message.target == Bytes0(b""): |
101 | is_collision = account_has_code_or_nonce( |
102 | block_env.state, message.current_target |
103 | ) or account_has_storage(block_env.state, message.current_target) |
104 | if is_collision: |
105 | return MessageCallOutput( |
106 | Uint(0), U256(0), tuple(), set(), AddressCollision() |
107 | ) |
108 | else: |
109 | evm = process_create_message(message) |
110 | else: |
111 | evm = process_message(message) |
112 | |
113 | if evm.error: |
114 | logs: Tuple[Log, ...] = () |
115 | accounts_to_delete = set() |
116 | else: |
117 | logs = evm.logs |
118 | accounts_to_delete = evm.accounts_to_delete |
119 | refund_counter += U256(evm.refund_counter) |
120 | |
121 | tx_end = TransactionEnd( |
122 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
123 | ) |
124 | evm_trace(evm, tx_end) |
125 | |
126 | return MessageCallOutput( |
127 | gas_left=evm.gas_left, |
128 | refund_counter=refund_counter, |
129 | logs=logs, |
130 | accounts_to_delete=accounts_to_delete, |
131 | error=evm.error, |
132 | ) |
process_create_message
Executes a call to create a smart contract.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:~ethereum.frontier.vm.Evm
Items containing execution specific objects.
def process_create_message(message: Message) -> Evm:
136 | """ |
---|---|
137 | Executes a call to create a smart contract. |
138 |
|
139 | Parameters |
140 | ---------- |
141 | message : |
142 | Transaction specific items. |
143 |
|
144 | Returns |
145 | ------- |
146 | evm: :py:class:`~ethereum.frontier.vm.Evm` |
147 | Items containing execution specific objects. |
148 | """ |
149 | state = message.block_env.state |
150 | # take snapshot of state before processing the message |
151 | begin_transaction(state) |
152 | |
153 | # If the address where the account is being created has storage, it is |
154 | # destroyed. This can only happen in the following highly unlikely |
155 | # circumstances: |
156 | # * The address created by two `CREATE` calls collide. |
157 | # * The first `CREATE` left empty code. |
158 | destroy_storage(state, message.current_target) |
159 | |
160 | evm = process_message(message) |
161 | if not evm.error: |
162 | contract_code = evm.output |
163 | contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT |
164 | try: |
165 | charge_gas(evm, contract_code_gas) |
166 | except ExceptionalHalt: |
167 | evm.output = b"" |
168 | else: |
169 | set_code(state, message.current_target, contract_code) |
170 | commit_transaction(state) |
171 | else: |
172 | rollback_transaction(state) |
173 | return evm |
process_message
Move ether and execute the relevant code.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:~ethereum.frontier.vm.Evm
Items containing execution specific objects
def process_message(message: Message) -> Evm:
177 | """ |
---|---|
178 | Move ether and execute the relevant code. |
179 |
|
180 | Parameters |
181 | ---------- |
182 | message : |
183 | Transaction specific items. |
184 |
|
185 | Returns |
186 | ------- |
187 | evm: :py:class:`~ethereum.frontier.vm.Evm` |
188 | Items containing execution specific objects |
189 | """ |
190 | state = message.block_env.state |
191 | if message.depth > STACK_DEPTH_LIMIT: |
192 | raise StackDepthLimitError("Stack depth limit reached") |
193 | |
194 | # take snapshot of state before processing the message |
195 | begin_transaction(state) |
196 | |
197 | touch_account(state, message.current_target) |
198 | |
199 | if message.value != 0: |
200 | move_ether( |
201 | state, message.caller, message.current_target, message.value |
202 | ) |
203 | |
204 | evm = execute_code(message) |
205 | if evm.error: |
206 | # revert state to the last saved checkpoint |
207 | # since the message call resulted in an error |
208 | rollback_transaction(state) |
209 | else: |
210 | commit_transaction(state) |
211 | 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:
215 | """ |
---|---|
216 | Executes bytecode present in the `message`. |
217 |
|
218 | Parameters |
219 | ---------- |
220 | message : |
221 | Transaction specific items. |
222 |
|
223 | Returns |
224 | ------- |
225 | evm: `ethereum.vm.EVM` |
226 | Items containing execution specific objects |
227 | """ |
228 | code = message.code |
229 | valid_jump_destinations = get_valid_jump_destinations(code) |
230 | |
231 | evm = Evm( |
232 | pc=Uint(0), |
233 | stack=[], |
234 | memory=bytearray(), |
235 | code=code, |
236 | gas_left=message.gas, |
237 | valid_jump_destinations=valid_jump_destinations, |
238 | logs=(), |
239 | refund_counter=0, |
240 | running=True, |
241 | message=message, |
242 | output=b"", |
243 | accounts_to_delete=set(), |
244 | error=None, |
245 | ) |
246 | try: |
247 | if evm.message.code_address in PRE_COMPILED_CONTRACTS: |
248 | evm_trace(evm, PrecompileStart(evm.message.code_address)) |
249 | PRE_COMPILED_CONTRACTS[evm.message.code_address](evm) |
250 | evm_trace(evm, PrecompileEnd()) |
251 | return evm |
252 |
|
253 | while evm.running and evm.pc < ulen(evm.code): |
254 | try: |
255 | op = Ops(evm.code[evm.pc]) |
256 | except ValueError: |
257 | raise InvalidOpcode(evm.code[evm.pc]) |
258 |
|
259 | evm_trace(evm, OpStart(op)) |
260 | op_implementation[op](evm) |
261 | evm_trace(evm, OpEnd()) |
262 |
|
263 | evm_trace(evm, EvmStop(Ops.STOP)) |
264 |
|
265 | except ExceptionalHalt as error: |
266 | evm_trace(evm, OpException(error)) |
267 | evm.gas_left = Uint(0) |
268 | evm.error = error |
269 | return evm |