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