1
- use specification:: eip4844:: {
2
- BLOB_GASPRICE_UPDATE_FRACTION , MIN_BLOB_GASPRICE , TARGET_BLOB_GAS_PER_BLOCK ,
3
- } ;
1
+ use specification:: eip4844:: { self , MIN_BLOB_GASPRICE } ;
4
2
5
3
/// Structure holding block blob excess gas and it calculates blob fee
6
4
///
@@ -20,34 +18,62 @@ pub struct BlobExcessGasAndPrice {
20
18
21
19
impl BlobExcessGasAndPrice {
22
20
/// Creates a new instance by calculating the blob gas price with [`calc_blob_gasprice`].
23
- pub fn new ( excess_blob_gas : u64 ) -> Self {
24
- let blob_gasprice = calc_blob_gasprice ( excess_blob_gas) ;
21
+ pub fn new ( excess_blob_gas : u64 , is_prague : bool ) -> Self {
22
+ let blob_gasprice = calc_blob_gasprice ( excess_blob_gas, is_prague ) ;
25
23
Self {
26
24
excess_blob_gas,
27
25
blob_gasprice,
28
26
}
29
27
}
28
+
29
+ /// Calculate this block excess gas and price from the parent excess gas and gas used
30
+ /// and the target blob gas per block.
31
+ ///
32
+ /// This fields will be used to calculate `excess_blob_gas` with [`calc_excess_blob_gas`] func.
33
+ pub fn from_parent_and_target (
34
+ parent_excess_blob_gas : u64 ,
35
+ parent_blob_gas_used : u64 ,
36
+ parent_target_blob_gas_per_block : u64 ,
37
+ is_prague : bool ,
38
+ ) -> Self {
39
+ Self :: new (
40
+ calc_excess_blob_gas (
41
+ parent_excess_blob_gas,
42
+ parent_blob_gas_used,
43
+ parent_target_blob_gas_per_block,
44
+ ) ,
45
+ is_prague,
46
+ )
47
+ }
30
48
}
31
49
32
50
/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
33
51
///
34
52
/// See also [the EIP-4844 helpers]<https://eips.ethereum.org/EIPS/eip-4844#helpers>
35
53
/// (`calc_excess_blob_gas`).
36
54
#[ inline]
37
- pub fn calc_excess_blob_gas ( parent_excess_blob_gas : u64 , parent_blob_gas_used : u64 ) -> u64 {
38
- ( parent_excess_blob_gas + parent_blob_gas_used) . saturating_sub ( TARGET_BLOB_GAS_PER_BLOCK )
55
+ pub fn calc_excess_blob_gas (
56
+ parent_excess_blob_gas : u64 ,
57
+ parent_blob_gas_used : u64 ,
58
+ parent_target_blob_gas_per_block : u64 ,
59
+ ) -> u64 {
60
+ ( parent_excess_blob_gas + parent_blob_gas_used) . saturating_sub ( parent_target_blob_gas_per_block)
39
61
}
40
62
41
63
/// Calculates the blob gas price from the header's excess blob gas field.
42
64
///
43
65
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers)
44
66
/// (`get_blob_gasprice`).
45
67
#[ inline]
46
- pub fn calc_blob_gasprice ( excess_blob_gas : u64 ) -> u128 {
68
+ pub fn calc_blob_gasprice ( excess_blob_gas : u64 , is_prague : bool ) -> u128 {
47
69
fake_exponential (
48
70
MIN_BLOB_GASPRICE ,
49
71
excess_blob_gas,
50
- BLOB_GASPRICE_UPDATE_FRACTION ,
72
+ if is_prague {
73
+ eip4844:: BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
74
+ } else {
75
+ eip4844:: BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN
76
+ } ,
51
77
)
52
78
}
53
79
@@ -84,7 +110,10 @@ pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u128 {
84
110
#[ cfg( test) ]
85
111
mod tests {
86
112
use super :: * ;
87
- use specification:: eip4844:: GAS_PER_BLOB ;
113
+ use specification:: eip4844:: {
114
+ BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN , GAS_PER_BLOB ,
115
+ TARGET_BLOB_GAS_PER_BLOCK_CANCUN as TARGET_BLOB_GAS_PER_BLOCK ,
116
+ } ;
88
117
89
118
// https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L27
90
119
#[ test]
@@ -135,7 +164,11 @@ mod tests {
135
164
0 ,
136
165
) ,
137
166
] {
138
- let actual = calc_excess_blob_gas ( excess, blobs * GAS_PER_BLOB ) ;
167
+ let actual = calc_excess_blob_gas (
168
+ excess,
169
+ blobs * GAS_PER_BLOB ,
170
+ eip4844:: TARGET_BLOB_GAS_PER_BLOCK_CANCUN ,
171
+ ) ;
139
172
assert_eq ! ( actual, expected, "test: {t:?}" ) ;
140
173
}
141
174
}
@@ -148,18 +181,18 @@ mod tests {
148
181
( 2314057 , 1 ) ,
149
182
( 2314058 , 2 ) ,
150
183
( 10 * 1024 * 1024 , 23 ) ,
151
- // `calc_blob_gasprice` approximates `e ** (excess_blob_gas / BLOB_GASPRICE_UPDATE_FRACTION )` using Taylor expansion
184
+ // `calc_blob_gasprice` approximates `e ** (excess_blob_gas / BLOB_BASE_FEE_UPDATE_FRACTION )` using Taylor expansion
152
185
//
153
186
// to roughly find where boundaries will be hit:
154
- // 2 ** bits = e ** (excess_blob_gas / BLOB_GASPRICE_UPDATE_FRACTION )
155
- // excess_blob_gas = ln(2 ** bits) * BLOB_GASPRICE_UPDATE_FRACTION
187
+ // 2 ** bits = e ** (excess_blob_gas / BLOB_BASE_FEE_UPDATE_FRACTION )
188
+ // excess_blob_gas = ln(2 ** bits) * BLOB_BASE_FEE_UPDATE_FRACTION
156
189
( 148099578 , 18446739238971471609 ) , // output is just below the overflow
157
190
( 148099579 , 18446744762204311910 ) , // output is just after the overflow
158
191
( 161087488 , 902580055246494526580 ) ,
159
192
] ;
160
193
161
194
for & ( excess, expected) in blob_fee_vectors {
162
- let actual = calc_blob_gasprice ( excess) ;
195
+ let actual = calc_blob_gasprice ( excess, false ) ;
163
196
assert_eq ! ( actual, expected, "test: {excess}" ) ;
164
197
}
165
198
}
@@ -183,7 +216,7 @@ mod tests {
183
216
( 1 , 5 , 2 , 11 ) , // approximate 12.18
184
217
( 2 , 5 , 2 , 23 ) , // approximate 24.36
185
218
( 1 , 50000000 , 2225652 , 5709098764 ) ,
186
- ( 1 , 380928 , BLOB_GASPRICE_UPDATE_FRACTION , 1 ) ,
219
+ ( 1 , 380928 , BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN , 1 ) ,
187
220
] {
188
221
let actual = fake_exponential ( factor, numerator, denominator) ;
189
222
assert_eq ! ( actual, expected, "test: {t:?}" ) ;
0 commit comments