2525from eth .vm .base import VM
2626
2727from .blocks import FrontierBlock
28+ from .constants import MAX_REFUND_QUOTIENT
2829from .state import FrontierState
2930from .headers import (
3031 create_frontier_header_from_parent ,
3435from .validation import validate_frontier_transaction_against_header
3536
3637
37- def make_frontier_receipt (base_header : BlockHeaderAPI ,
38- transaction : SignedTransactionAPI ,
39- computation : ComputationAPI ) -> ReceiptAPI :
38+ def make_frontier_receipt (computation : ComputationAPI ,
39+ new_cumulative_gas_used : int ) -> ReceiptAPI :
4040 # Reusable for other forks
4141 # This skips setting the state root (set to 0 instead). The logic for making a state root
4242 # lives in the FrontierVM, so that state merkelization at each receipt is skipped at Byzantium+.
@@ -47,19 +47,9 @@ def make_frontier_receipt(base_header: BlockHeaderAPI,
4747 in computation .get_log_entries ()
4848 ]
4949
50- gas_remaining = computation .get_gas_remaining ()
51- gas_refund = computation .get_gas_refund ()
52- tx_gas_used = (
53- transaction .gas - gas_remaining
54- ) - min (
55- gas_refund ,
56- (transaction .gas - gas_remaining ) // 2 ,
57- )
58- gas_used = base_header .gas_used + tx_gas_used
59-
6050 receipt = Receipt (
6151 state_root = ZERO_HASH32 ,
62- gas_used = gas_used ,
52+ gas_used = new_cumulative_gas_used ,
6353 logs = logs ,
6454 )
6555
@@ -103,14 +93,35 @@ def add_receipt_to_header(self,
10393 state_root = self .state .make_state_root (),
10494 )
10595
106- @staticmethod
96+ @classmethod
97+ def calculate_net_gas_refund (cls , consumed_gas : int , gross_refund : int ) -> int :
98+ max_refund = consumed_gas // MAX_REFUND_QUOTIENT
99+ return min (max_refund , gross_refund )
100+
101+ @classmethod
102+ def finalize_gas_used (cls ,
103+ transaction : SignedTransactionAPI ,
104+ computation : ComputationAPI ) -> int :
105+
106+ gas_remaining = computation .get_gas_remaining ()
107+ consumed_gas = transaction .gas - gas_remaining
108+
109+ gross_refund = computation .get_gas_refund ()
110+ net_refund = cls .calculate_net_gas_refund (consumed_gas , gross_refund )
111+
112+ return consumed_gas - net_refund
113+
114+ @classmethod
107115 def make_receipt (
116+ cls ,
108117 base_header : BlockHeaderAPI ,
109118 transaction : SignedTransactionAPI ,
110119 computation : ComputationAPI ,
111120 state : StateAPI ) -> ReceiptAPI :
112121
113- receipt_without_state_root = make_frontier_receipt (base_header , transaction , computation )
122+ gas_used = base_header .gas_used + cls .finalize_gas_used (transaction , computation )
123+
124+ receipt_without_state_root = make_frontier_receipt (computation , gas_used )
114125
115126 return receipt_without_state_root .copy (
116127 state_root = state .make_state_root ()
0 commit comments