-
Notifications
You must be signed in to change notification settings - Fork 650
/
computation.py
573 lines (486 loc) · 16.9 KB
/
computation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import itertools
from types import TracebackType
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Tuple,
Type,
Union,
cast,
)
from cached_property import cached_property
from eth_typing import (
Address,
)
from eth_utils import (
encode_hex,
get_extended_debug_logger,
)
from eth.abc import (
MemoryAPI,
StackAPI,
GasMeterAPI,
MessageAPI,
OpcodeAPI,
CodeStreamAPI,
ComputationAPI,
StateAPI,
TransactionContextAPI,
)
from eth.constants import (
GAS_MEMORY,
GAS_MEMORY_QUADRATIC_DENOMINATOR,
)
from eth.exceptions import (
Halt,
VMError,
)
from eth.typing import (
BytesOrView,
)
from eth._utils.datatypes import (
Configurable,
)
from eth._utils.numeric import (
ceil32,
)
from eth.validation import (
validate_canonical_address,
validate_is_bytes,
validate_uint256,
)
from eth.vm.code_stream import (
CodeStream,
)
from eth.vm.gas_meter import (
GasMeter,
)
from eth.vm.logic.invalid import (
InvalidOpcode,
)
from eth.vm.memory import (
Memory,
)
from eth.vm.message import (
Message,
)
from eth.vm.stack import (
Stack,
)
def NO_RESULT(computation: ComputationAPI) -> None:
"""
This is a special method intended for usage as the "no precompile found" result.
The type signature is designed to match the other precompiles.
"""
raise Exception("This method is never intended to be executed")
def memory_gas_cost(size_in_bytes: int) -> int:
size_in_words = ceil32(size_in_bytes) // 32
linear_cost = size_in_words * GAS_MEMORY
quadratic_cost = size_in_words ** 2 // GAS_MEMORY_QUADRATIC_DENOMINATOR
total_cost = linear_cost + quadratic_cost
return total_cost
class BaseComputation(Configurable, ComputationAPI):
"""
The base class for all execution computations.
.. note::
Each :class:`~eth.vm.computation.BaseComputation` class must be configured with:
``opcodes``: A mapping from the opcode integer value to the logic function for the opcode.
``_precompiles``: A mapping of contract address to the precompile function for execution
of precompiled contracts.
"""
state: StateAPI = None
msg: MessageAPI = None
transaction_context: TransactionContextAPI = None
_memory: MemoryAPI = None
_stack: StackAPI = None
_gas_meter: GasMeterAPI = None
code: CodeStreamAPI = None
children: List[ComputationAPI] = None
_output: bytes = b''
return_data: bytes = b''
_error: VMError = None
# TODO: use a NamedTuple for log entries
_log_entries: List[Tuple[int, Address, Tuple[int, ...], bytes]] = None
accounts_to_delete: Dict[Address, Address] = None
# VM configuration
opcodes: Dict[int, OpcodeAPI] = None
_precompiles: Dict[Address, Callable[[ComputationAPI], ComputationAPI]] = None
logger = get_extended_debug_logger('eth.vm.computation.Computation')
def __init__(self,
state: StateAPI,
message: MessageAPI,
transaction_context: TransactionContextAPI) -> None:
self.state = state
self.msg = message
self.transaction_context = transaction_context
self._memory = Memory()
self._stack = Stack()
self._gas_meter = self.get_gas_meter()
self.children = []
self.accounts_to_delete = {}
self._log_entries = []
code = message.code
self.code = CodeStream(code)
#
# Convenience
#
@property
def is_origin_computation(self) -> bool:
return self.msg.sender == self.transaction_context.origin
#
# Error handling
#
@property
def is_success(self) -> bool:
return self._error is None
@property
def is_error(self) -> bool:
return not self.is_success
@property
def error(self) -> VMError:
if self._error is not None:
return self._error
raise AttributeError("Computation does not have an error")
@error.setter
def error(self, value: VMError) -> None:
if self._error is not None:
raise AttributeError(f"Computation already has an error set: {self._error}")
self._error = value
def raise_if_error(self) -> None:
if self._error is not None:
raise self._error
@property
def should_burn_gas(self) -> bool:
return self.is_error and self._error.burns_gas
@property
def should_return_gas(self) -> bool:
return not self.should_burn_gas
@property
def should_erase_return_data(self) -> bool:
return self.is_error and self._error.erases_return_data
#
# Memory Management
#
def extend_memory(self, start_position: int, size: int) -> None:
validate_uint256(start_position, title="Memory start position")
validate_uint256(size, title="Memory size")
before_size = ceil32(len(self._memory))
after_size = ceil32(start_position + size)
before_cost = memory_gas_cost(before_size)
after_cost = memory_gas_cost(after_size)
if self.logger.show_debug2:
self.logger.debug2(
"MEMORY: size (%s -> %s) | cost (%s -> %s)",
before_size,
after_size,
before_cost,
after_cost,
)
if size:
if before_cost < after_cost:
gas_fee = after_cost - before_cost
self._gas_meter.consume_gas(
gas_fee,
reason=" ".join((
"Expanding memory",
str(before_size),
"->",
str(after_size),
))
)
self._memory.extend(start_position, size)
def memory_write(self, start_position: int, size: int, value: bytes) -> None:
return self._memory.write(start_position, size, value)
def memory_read(self, start_position: int, size: int) -> memoryview:
return self._memory.read(start_position, size)
def memory_read_bytes(self, start_position: int, size: int) -> bytes:
return self._memory.read_bytes(start_position, size)
#
# Gas Consumption
#
def get_gas_meter(self) -> GasMeterAPI:
return GasMeter(self.msg.gas)
def consume_gas(self, amount: int, reason: str) -> None:
return self._gas_meter.consume_gas(amount, reason)
def return_gas(self, amount: int) -> None:
return self._gas_meter.return_gas(amount)
def refund_gas(self, amount: int) -> None:
return self._gas_meter.refund_gas(amount)
def get_gas_refund(self) -> int:
if self.is_error:
return 0
else:
return self._gas_meter.gas_refunded + sum(c.get_gas_refund() for c in self.children)
def get_gas_used(self) -> int:
if self.should_burn_gas:
return self.msg.gas
else:
return max(
0,
self.msg.gas - self._gas_meter.gas_remaining,
)
def get_gas_remaining(self) -> int:
if self.should_burn_gas:
return 0
else:
return self._gas_meter.gas_remaining
#
# Stack management
#
def stack_swap(self, position: int) -> None:
return self._stack.swap(position)
def stack_dup(self, position: int) -> None:
return self._stack.dup(position)
# Stack manipulation is performance-sensitive code.
# Avoid method call overhead by proxying stack method directly to stack object
@cached_property
def stack_pop_ints(self) -> Callable[[int], Tuple[int, ...]]:
return self._stack.pop_ints
@cached_property
def stack_pop_bytes(self) -> Callable[[int], Tuple[bytes, ...]]:
return self._stack.pop_bytes
@cached_property
def stack_pop_any(self) -> Callable[[int], Tuple[Union[int, bytes], ...]]:
return self._stack.pop_any
@cached_property
def stack_pop1_int(self) -> Callable[[], int]:
return self._stack.pop1_int
@cached_property
def stack_pop1_bytes(self) -> Callable[[], bytes]:
return self._stack.pop1_bytes
@cached_property
def stack_pop1_any(self) -> Callable[[], Union[int, bytes]]:
return self._stack.pop1_any
@cached_property
def stack_push_int(self) -> Callable[[int], None]:
return self._stack.push_int
@cached_property
def stack_push_bytes(self) -> Callable[[bytes], None]:
return self._stack.push_bytes
#
# Computation result
#
@property
def output(self) -> bytes:
if self.should_erase_return_data:
return b''
else:
return self._output
@output.setter
def output(self, value: bytes) -> None:
validate_is_bytes(value)
self._output = value
#
# Runtime operations
#
def prepare_child_message(self,
gas: int,
to: Address,
value: int,
data: BytesOrView,
code: bytes,
**kwargs: Any) -> MessageAPI:
kwargs.setdefault('sender', self.msg.storage_address)
child_message = Message(
gas=gas,
to=to,
value=value,
data=data,
code=code,
depth=self.msg.depth + 1,
**kwargs
)
return child_message
def apply_child_computation(self, child_msg: MessageAPI) -> ComputationAPI:
child_computation = self.generate_child_computation(child_msg)
self.add_child_computation(child_computation)
return child_computation
def generate_child_computation(self, child_msg: MessageAPI) -> ComputationAPI:
if child_msg.is_create:
child_computation = self.apply_create_message(
self.state,
child_msg,
self.transaction_context,
)
else:
child_computation = self.apply_message(
self.state,
child_msg,
self.transaction_context,
)
return child_computation
def add_child_computation(self, child_computation: ComputationAPI) -> None:
if child_computation.is_error:
if child_computation.msg.is_create:
self.return_data = child_computation.output
elif child_computation.should_burn_gas:
self.return_data = b''
else:
self.return_data = child_computation.output
else:
if child_computation.msg.is_create:
self.return_data = b''
else:
self.return_data = child_computation.output
self.children.append(child_computation)
#
# Account management
#
def register_account_for_deletion(self, beneficiary: Address) -> None:
validate_canonical_address(beneficiary, title="Self destruct beneficiary address")
if self.msg.storage_address in self.accounts_to_delete:
raise ValueError(
"Invariant. Should be impossible for an account to be "
"registered for deletion multiple times"
)
self.accounts_to_delete[self.msg.storage_address] = beneficiary
def get_accounts_for_deletion(self) -> Tuple[Tuple[Address, Address], ...]:
if self.is_error:
return ()
else:
return tuple(dict(itertools.chain(
self.accounts_to_delete.items(),
*(child.get_accounts_for_deletion() for child in self.children)
)).items())
#
# EVM logging
#
def add_log_entry(self, account: Address, topics: Tuple[int, ...], data: bytes) -> None:
validate_canonical_address(account, title="Log entry address")
for topic in topics:
validate_uint256(topic, title="Log entry topic")
validate_is_bytes(data, title="Log entry data")
self._log_entries.append(
(self.transaction_context.get_next_log_counter(), account, topics, data))
def get_raw_log_entries(self) -> Tuple[Tuple[int, bytes, Tuple[int, ...], bytes], ...]:
if self.is_error:
return ()
else:
return tuple(sorted(itertools.chain(
self._log_entries,
*(child.get_raw_log_entries() for child in self.children)
)))
def get_log_entries(self) -> Tuple[Tuple[bytes, Tuple[int, ...], bytes], ...]:
return tuple(log[1:] for log in self.get_raw_log_entries())
#
# Context Manager API
#
def __enter__(self) -> ComputationAPI:
if self.logger.show_debug2:
self.logger.debug2(
(
"COMPUTATION STARTING: gas: %s | from: %s | to: %s | value: %s "
"| depth %s | static: %s"
),
self.msg.gas,
encode_hex(self.msg.sender),
encode_hex(self.msg.to),
self.msg.value,
self.msg.depth,
"y" if self.msg.is_static else "n",
)
return self
def __exit__(self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType]) -> Union[None, bool]:
if exc_value and isinstance(exc_value, VMError):
if self.logger.show_debug2:
self.logger.debug2(
(
"COMPUTATION ERROR: gas: %s | from: %s | to: %s | value: %s | "
"depth: %s | static: %s | error: %s"
),
self.msg.gas,
encode_hex(self.msg.sender),
encode_hex(self.msg.to),
self.msg.value,
self.msg.depth,
"y" if self.msg.is_static else "n",
exc_value,
)
self._error = exc_value
if self.should_burn_gas:
self.consume_gas(
self._gas_meter.gas_remaining,
reason=" ".join((
"Zeroing gas due to VM Exception:",
str(exc_value),
)),
)
# suppress VM exceptions
return True
elif exc_type is None and self.logger.show_debug2:
self.logger.debug2(
(
"COMPUTATION SUCCESS: from: %s | to: %s | value: %s | "
"depth: %s | static: %s | gas-used: %s | gas-remaining: %s"
),
encode_hex(self.msg.sender),
encode_hex(self.msg.to),
self.msg.value,
self.msg.depth,
"y" if self.msg.is_static else "n",
self.get_gas_used(),
self._gas_meter.gas_remaining,
)
return None
#
# State Transition
#
@classmethod
def apply_computation(cls,
state: StateAPI,
message: MessageAPI,
transaction_context: TransactionContextAPI) -> ComputationAPI:
with cls(state, message, transaction_context) as computation:
# Early exit on pre-compiles
precompile = computation.precompiles.get(message.code_address, NO_RESULT)
if precompile is not NO_RESULT:
precompile(computation)
return computation
show_debug2 = computation.logger.show_debug2
opcode_lookup = computation.opcodes
for opcode in computation.code:
try:
opcode_fn = opcode_lookup[opcode]
except KeyError:
opcode_fn = InvalidOpcode(opcode)
if show_debug2:
# We dig into some internals for debug logs
base_comp = cast(BaseComputation, computation)
computation.logger.debug2(
"OPCODE: 0x%x (%s) | pc: %s | stack: %s",
opcode,
opcode_fn.mnemonic,
max(0, computation.code.program_counter - 1),
base_comp._stack,
)
try:
opcode_fn(computation=computation)
except Halt:
break
return computation
#
# Opcode API
#
@property
def precompiles(self) -> Dict[Address, Callable[[ComputationAPI], Any]]:
if self._precompiles is None:
return {}
else:
return self._precompiles
@classmethod
def get_precompiles(cls) -> Dict[Address, Callable[[ComputationAPI], Any]]:
if cls._precompiles is None:
return {}
else:
return cls._precompiles
def get_opcode_fn(self, opcode: int) -> OpcodeAPI:
try:
return self.opcodes[opcode]
except KeyError:
return InvalidOpcode(opcode)