25
25
from eth .vm .base import VM
26
26
27
27
from .blocks import FrontierBlock
28
+ from .constants import MAX_REFUND_QUOTIENT
28
29
from .state import FrontierState
29
30
from .headers import (
30
31
create_frontier_header_from_parent ,
34
35
from .validation import validate_frontier_transaction_against_header
35
36
36
37
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 :
40
40
# Reusable for other forks
41
41
# This skips setting the state root (set to 0 instead). The logic for making a state root
42
42
# 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,
47
47
in computation .get_log_entries ()
48
48
]
49
49
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
-
60
50
receipt = Receipt (
61
51
state_root = ZERO_HASH32 ,
62
- gas_used = gas_used ,
52
+ gas_used = new_cumulative_gas_used ,
63
53
logs = logs ,
64
54
)
65
55
@@ -103,14 +93,35 @@ def add_receipt_to_header(self,
103
93
state_root = self .state .make_state_root (),
104
94
)
105
95
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
107
115
def make_receipt (
116
+ cls ,
108
117
base_header : BlockHeaderAPI ,
109
118
transaction : SignedTransactionAPI ,
110
119
computation : ComputationAPI ,
111
120
state : StateAPI ) -> ReceiptAPI :
112
121
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 )
114
125
115
126
return receipt_without_state_root .copy (
116
127
state_root = state .make_state_root ()
0 commit comments