ethereum.homestead.vm.interpreterethereum.dao_fork.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:
Items containing execution specific objects.~ethereum.homestead.vm.Evm~ethereum.dao_fork.vm.Evm
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.homestead.vm.Evm` |
146 | evm: :py:class:`~ethereum.dao_fork.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 as error: |
167 | rollback_transaction(state) |
168 | evm.gas_left = Uint(0) |
169 | evm.error = error |
170 | else: |
171 | set_code(state, message.current_target, contract_code) |
172 | commit_transaction(state) |
173 | else: |
174 | rollback_transaction(state) |
175 | 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.homestead.vm.Evm~ethereum.dao_fork.vm.Evm
def process_message(message: Message) -> Evm:
179 | """ |
---|---|
180 | Move ether and execute the relevant code. |
181 |
|
182 | Parameters |
183 | ---------- |
184 | message : |
185 | Transaction specific items. |
186 |
|
187 | Returns |
188 | ------- |
189 | evm: :py:class:`~ethereum.homestead.vm.Evm` |
189 | evm: :py:class:`~ethereum.dao_fork.vm.Evm` |
190 | Items containing execution specific objects |
191 | """ |
192 | state = message.block_env.state |
193 | if message.depth > STACK_DEPTH_LIMIT: |
194 | raise StackDepthLimitError("Stack depth limit reached") |
195 | |
196 | # take snapshot of state before processing the message |
197 | begin_transaction(state) |
198 | |
199 | touch_account(state, message.current_target) |
200 | |
201 | if message.should_transfer_value and message.value != 0: |
202 | move_ether( |
203 | state, message.caller, message.current_target, message.value |
204 | ) |
205 | |
206 | evm = execute_code(message) |
207 | if evm.error: |
208 | # revert state to the last saved checkpoint |
209 | # since the message call resulted in an error |
210 | rollback_transaction(state) |
211 | else: |
212 | commit_transaction(state) |
213 | 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:
217 | """ |
---|---|
218 | Executes bytecode present in the `message`. |
219 |
|
220 | Parameters |
221 | ---------- |
222 | message : |
223 | Transaction specific items. |
224 |
|
225 | Returns |
226 | ------- |
227 | evm: `ethereum.vm.EVM` |
228 | Items containing execution specific objects |
229 | """ |
230 | code = message.code |
231 | valid_jump_destinations = get_valid_jump_destinations(code) |
232 | |
233 | evm = Evm( |
234 | pc=Uint(0), |
235 | stack=[], |
236 | memory=bytearray(), |
237 | code=code, |
238 | gas_left=message.gas, |
239 | valid_jump_destinations=valid_jump_destinations, |
240 | logs=(), |
241 | refund_counter=0, |
242 | running=True, |
243 | message=message, |
244 | output=b"", |
245 | accounts_to_delete=set(), |
246 | error=None, |
247 | ) |
248 | try: |
249 | if evm.message.code_address in PRE_COMPILED_CONTRACTS: |
250 | evm_trace(evm, PrecompileStart(evm.message.code_address)) |
251 | PRE_COMPILED_CONTRACTS[evm.message.code_address](evm) |
252 | evm_trace(evm, PrecompileEnd()) |
253 | return evm |
254 |
|
255 | while evm.running and evm.pc < ulen(evm.code): |
256 | try: |
257 | op = Ops(evm.code[evm.pc]) |
258 | except ValueError: |
259 | raise InvalidOpcode(evm.code[evm.pc]) |
260 |
|
261 | evm_trace(evm, OpStart(op)) |
262 | op_implementation[op](evm) |
263 | evm_trace(evm, OpEnd()) |
264 |
|
265 | evm_trace(evm, EvmStop(Ops.STOP)) |
266 |
|
267 | except ExceptionalHalt as error: |
268 | evm_trace(evm, OpException(error)) |
269 | evm.gas_left = Uint(0) |
270 | evm.error = error |
271 | return evm |