@@ -149,6 +149,9 @@ def is_origin_computation(self) -> bool:
149
149
"""
150
150
return self .msg .sender == self .transaction_context .origin
151
151
152
+ #
153
+ # Error handling
154
+ #
152
155
@property
153
156
def is_success (self ) -> bool :
154
157
"""
@@ -193,32 +196,6 @@ def should_erase_return_data(self) -> bool:
193
196
"""
194
197
return self .is_error and self ._error .erases_return_data
195
198
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
-
222
199
#
223
200
# Memory Management
224
201
#
@@ -272,6 +249,12 @@ def memory_read(self, start_position: int, size: int) -> bytes:
272
249
"""
273
250
return self ._memory .read (start_position , size )
274
251
252
+ #
253
+ # Gas Consumption
254
+ #
255
+ def get_gas_meter (self ) -> GasMeter :
256
+ return GasMeter (self .msg .gas )
257
+
275
258
def consume_gas (self , amount : int , reason : str ) -> None :
276
259
"""
277
260
Consume ``amount`` of gas from the remaining gas.
@@ -291,6 +274,30 @@ def refund_gas(self, amount: int) -> None:
291
274
"""
292
275
return self ._gas_meter .refund_gas (amount )
293
276
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
+ #
294
301
def stack_pop (self , num_items : int = 1 , type_hint : str = None ) -> Any :
295
302
# TODO: Needs to be replaced with
296
303
# `Union[int, bytes, Tuple[Union[int, bytes], ...]]` if done properly
@@ -326,7 +333,7 @@ def stack_dup(self, position: int) -> None:
326
333
return self ._stack .dup (position )
327
334
328
335
#
329
- # Computed properties.
336
+ # Computation result
330
337
#
331
338
@property
332
339
def output (self ) -> bytes :
@@ -349,6 +356,29 @@ def output(self, value: bytes) -> None:
349
356
#
350
357
# Runtime operations
351
358
#
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
+
352
382
def apply_child_computation (self , child_msg : Message ) -> 'BaseComputation' :
353
383
"""
354
384
Apply the vm message ``child_msg`` as a child computation.
@@ -387,6 +417,9 @@ def add_child_computation(self, child_computation: 'BaseComputation') -> None:
387
417
self .return_data = child_computation .output
388
418
self .children .append (child_computation )
389
419
420
+ #
421
+ # Account management
422
+ #
390
423
def register_account_for_deletion (self , beneficiary : Address ) -> None :
391
424
validate_canonical_address (beneficiary , title = "Self destruct beneficiary address" )
392
425
@@ -397,20 +430,6 @@ def register_account_for_deletion(self, beneficiary: Address) -> None:
397
430
)
398
431
self .accounts_to_delete [self .msg .storage_address ] = beneficiary
399
432
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
-
414
433
def get_accounts_for_deletion (self ) -> Tuple [Tuple [bytes , bytes ], ...]:
415
434
if self .is_error :
416
435
return tuple ()
@@ -420,6 +439,17 @@ def get_accounts_for_deletion(self) -> Tuple[Tuple[bytes, bytes], ...]:
420
439
* (child .get_accounts_for_deletion () for child in self .children )
421
440
)).items ())
422
441
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
+
423
453
def _get_log_entries (self ) -> List [Tuple [int , bytes , List [int ], bytes ]]:
424
454
"""
425
455
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]]:
438
468
def get_log_entries (self ) -> Tuple [Tuple [bytes , List [int ], bytes ], ...]:
439
469
return tuple (log [1 :] for log in self ._get_log_entries ())
440
470
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
-
462
471
#
463
472
# Context Manager API
464
473
#
@@ -516,7 +525,7 @@ def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None:
516
525
self .msg .value ,
517
526
self .msg .depth ,
518
527
"y" if self .msg .is_static else "n" ,
519
- self .msg . gas - self . _gas_meter . gas_remaining ,
528
+ self .get_gas_used () ,
520
529
self ._gas_meter .gas_remaining ,
521
530
)
522
531
0 commit comments