@@ -40,6 +40,19 @@ impl Env {
40
40
} )
41
41
}
42
42
43
+ /// Calculates the maximum [EIP-4844] `data_fee` of the transaction.
44
+ ///
45
+ /// This is used for ensuring that the user has at least enough funds to pay the
46
+ /// `max_fee_per_blob_gas * total_blob_gas`, on top of regular gas costs.
47
+ ///
48
+ /// See EIP-4844:
49
+ /// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation>
50
+ pub fn calc_max_data_fee ( & self ) -> Option < U256 > {
51
+ self . tx . max_fee_per_blob_gas . map ( |max_fee_per_blob_gas| {
52
+ max_fee_per_blob_gas. saturating_mul ( U256 :: from ( self . tx . get_total_blob_gas ( ) ) )
53
+ } )
54
+ }
55
+
43
56
/// Validate the block environment.
44
57
#[ inline]
45
58
pub fn validate_block_env < SPEC : Spec > ( & self ) -> Result < ( ) , InvalidHeader > {
@@ -213,7 +226,8 @@ impl Env {
213
226
. ok_or ( InvalidTransaction :: OverflowPaymentInTransaction ) ?;
214
227
215
228
if SpecId :: enabled ( self . cfg . spec_id , SpecId :: CANCUN ) {
216
- let data_fee = self . calc_data_fee ( ) . expect ( "already checked" ) ;
229
+ // if the tx is not a blob tx, this will be None, so we add zero
230
+ let data_fee = self . calc_max_data_fee ( ) . unwrap_or_default ( ) ;
217
231
balance_check = balance_check
218
232
. checked_add ( U256 :: from ( data_fee) )
219
233
. ok_or ( InvalidTransaction :: OverflowPaymentInTransaction ) ?;
@@ -549,7 +563,7 @@ pub struct TxEnv {
549
563
}
550
564
551
565
impl TxEnv {
552
- /// See [EIP-4844] and [`Env::calc_data_fee `].
566
+ /// See [EIP-4844], [`Env::calc_data_fee`], and [`Env::calc_max_data_fee `].
553
567
///
554
568
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
555
569
#[ inline]
0 commit comments