Skip to content

Commit 0495813

Browse files
authored
re-organize Computation methods (ethereum#1528)
1 parent 6922d9b commit 0495813

File tree

1 file changed

+72
-63
lines changed

1 file changed

+72
-63
lines changed

eth/vm/computation.py

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def is_origin_computation(self) -> bool:
149149
"""
150150
return self.msg.sender == self.transaction_context.origin
151151

152+
#
153+
# Error handling
154+
#
152155
@property
153156
def is_success(self) -> bool:
154157
"""
@@ -193,32 +196,6 @@ def should_erase_return_data(self) -> bool:
193196
"""
194197
return self.is_error and self._error.erases_return_data
195198

196-
#
197-
# Execution
198-
#
199-
def prepare_child_message(self,
200-
gas: int,
201-
to: Address,
202-
value: int,
203-
data: bytes,
204-
code: bytes,
205-
**kwargs: Any) -> Message:
206-
"""
207-
Helper method for creating a child computation.
208-
"""
209-
kwargs.setdefault('sender', self.msg.storage_address)
210-
211-
child_message = Message(
212-
gas=gas,
213-
to=to,
214-
value=value,
215-
data=data,
216-
code=code,
217-
depth=self.msg.depth + 1,
218-
**kwargs
219-
)
220-
return child_message
221-
222199
#
223200
# Memory Management
224201
#
@@ -272,6 +249,12 @@ def memory_read(self, start_position: int, size: int) -> bytes:
272249
"""
273250
return self._memory.read(start_position, size)
274251

252+
#
253+
# Gas Consumption
254+
#
255+
def get_gas_meter(self) -> GasMeter:
256+
return GasMeter(self.msg.gas)
257+
275258
def consume_gas(self, amount: int, reason: str) -> None:
276259
"""
277260
Consume ``amount`` of gas from the remaining gas.
@@ -291,6 +274,30 @@ def refund_gas(self, amount: int) -> None:
291274
"""
292275
return self._gas_meter.refund_gas(amount)
293276

277+
def get_gas_refund(self) -> int:
278+
if self.is_error:
279+
return 0
280+
else:
281+
return self._gas_meter.gas_refunded + sum(c.get_gas_refund() for c in self.children)
282+
283+
def get_gas_used(self) -> int:
284+
if self.should_burn_gas:
285+
return self.msg.gas
286+
else:
287+
return max(
288+
0,
289+
self.msg.gas - self._gas_meter.gas_remaining,
290+
)
291+
292+
def get_gas_remaining(self) -> int:
293+
if self.should_burn_gas:
294+
return 0
295+
else:
296+
return self._gas_meter.gas_remaining
297+
298+
#
299+
# Stack management
300+
#
294301
def stack_pop(self, num_items: int=1, type_hint: str=None) -> Any:
295302
# TODO: Needs to be replaced with
296303
# `Union[int, bytes, Tuple[Union[int, bytes], ...]]` if done properly
@@ -326,7 +333,7 @@ def stack_dup(self, position: int) -> None:
326333
return self._stack.dup(position)
327334

328335
#
329-
# Computed properties.
336+
# Computation result
330337
#
331338
@property
332339
def output(self) -> bytes:
@@ -349,6 +356,29 @@ def output(self, value: bytes) -> None:
349356
#
350357
# Runtime operations
351358
#
359+
def prepare_child_message(self,
360+
gas: int,
361+
to: Address,
362+
value: int,
363+
data: bytes,
364+
code: bytes,
365+
**kwargs: Any) -> Message:
366+
"""
367+
Helper method for creating a child computation.
368+
"""
369+
kwargs.setdefault('sender', self.msg.storage_address)
370+
371+
child_message = Message(
372+
gas=gas,
373+
to=to,
374+
value=value,
375+
data=data,
376+
code=code,
377+
depth=self.msg.depth + 1,
378+
**kwargs
379+
)
380+
return child_message
381+
352382
def apply_child_computation(self, child_msg: Message) -> 'BaseComputation':
353383
"""
354384
Apply the vm message ``child_msg`` as a child computation.
@@ -387,6 +417,9 @@ def add_child_computation(self, child_computation: 'BaseComputation') -> None:
387417
self.return_data = child_computation.output
388418
self.children.append(child_computation)
389419

420+
#
421+
# Account management
422+
#
390423
def register_account_for_deletion(self, beneficiary: Address) -> None:
391424
validate_canonical_address(beneficiary, title="Self destruct beneficiary address")
392425

@@ -397,20 +430,6 @@ def register_account_for_deletion(self, beneficiary: Address) -> None:
397430
)
398431
self.accounts_to_delete[self.msg.storage_address] = beneficiary
399432

400-
def add_log_entry(self, account: Address, topics: List[int], data: bytes) -> None:
401-
validate_canonical_address(account, title="Log entry address")
402-
for topic in topics:
403-
validate_uint256(topic, title="Log entry topic")
404-
validate_is_bytes(data, title="Log entry data")
405-
self._log_entries.append(
406-
(self.transaction_context.get_next_log_counter(), account, topics, data))
407-
408-
#
409-
# Getters
410-
#
411-
def get_gas_meter(self) -> GasMeter:
412-
return GasMeter(self.msg.gas)
413-
414433
def get_accounts_for_deletion(self) -> Tuple[Tuple[bytes, bytes], ...]:
415434
if self.is_error:
416435
return tuple()
@@ -420,6 +439,17 @@ def get_accounts_for_deletion(self) -> Tuple[Tuple[bytes, bytes], ...]:
420439
*(child.get_accounts_for_deletion() for child in self.children)
421440
)).items())
422441

442+
#
443+
# EVM logging
444+
#
445+
def add_log_entry(self, account: Address, topics: List[int], data: bytes) -> None:
446+
validate_canonical_address(account, title="Log entry address")
447+
for topic in topics:
448+
validate_uint256(topic, title="Log entry topic")
449+
validate_is_bytes(data, title="Log entry data")
450+
self._log_entries.append(
451+
(self.transaction_context.get_next_log_counter(), account, topics, data))
452+
423453
def _get_log_entries(self) -> List[Tuple[int, bytes, List[int], bytes]]:
424454
"""
425455
Return the log entries for this computation and its children.
@@ -438,27 +468,6 @@ def _get_log_entries(self) -> List[Tuple[int, bytes, List[int], bytes]]:
438468
def get_log_entries(self) -> Tuple[Tuple[bytes, List[int], bytes], ...]:
439469
return tuple(log[1:] for log in self._get_log_entries())
440470

441-
def get_gas_refund(self) -> int:
442-
if self.is_error:
443-
return 0
444-
else:
445-
return self._gas_meter.gas_refunded + sum(c.get_gas_refund() for c in self.children)
446-
447-
def get_gas_used(self) -> int:
448-
if self.should_burn_gas:
449-
return self.msg.gas
450-
else:
451-
return max(
452-
0,
453-
self.msg.gas - self._gas_meter.gas_remaining,
454-
)
455-
456-
def get_gas_remaining(self) -> int:
457-
if self.should_burn_gas:
458-
return 0
459-
else:
460-
return self._gas_meter.gas_remaining
461-
462471
#
463472
# Context Manager API
464473
#
@@ -516,7 +525,7 @@ def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None:
516525
self.msg.value,
517526
self.msg.depth,
518527
"y" if self.msg.is_static else "n",
519-
self.msg.gas - self._gas_meter.gas_remaining,
528+
self.get_gas_used(),
520529
self._gas_meter.gas_remaining,
521530
)
522531

0 commit comments

Comments
 (0)