From 627aab9703c47795247f8b6d21533520498ed025 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20Grze=C5=9Bkiewicz?= <lemures64@gmail.com>
Date: Tue, 25 Jun 2024 00:08:07 +0200
Subject: [PATCH] feat(eth-sender): separate gas calculations for blobs
 transactions (#2247)

This PR implements point (1) from

https://www.notion.so/matterlabs/Eth-sender-blob-fees-problem-b84e1715248944559a0a656a6c9da320

---------

Signed-off-by: tomg10 <lemures64@gmail.com>
---
 core/node/eth_sender/src/eth_fees_oracle.rs      |  6 +++---
 .../src/l1_gas_price/gas_adjuster/mod.rs         | 16 ++++++++++++++++
 core/node/fee_model/src/l1_gas_price/mod.rs      |  9 +++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/core/node/eth_sender/src/eth_fees_oracle.rs b/core/node/eth_sender/src/eth_fees_oracle.rs
index 431ef4c8856b..ba106d1d6b92 100644
--- a/core/node/eth_sender/src/eth_fees_oracle.rs
+++ b/core/node/eth_sender/src/eth_fees_oracle.rs
@@ -37,9 +37,9 @@ impl GasAdjusterFeesOracle {
         &self,
         previous_sent_tx: &Option<TxHistory>,
     ) -> Result<EthFees, EthSenderError> {
-        let base_fee_per_gas = self.gas_adjuster.get_base_fee(0);
-        let priority_fee_per_gas = self.gas_adjuster.get_priority_fee();
-        let blob_base_fee_per_gas = Some(self.gas_adjuster.get_blob_base_fee());
+        let base_fee_per_gas = self.gas_adjuster.get_blob_tx_base_fee();
+        let priority_fee_per_gas = self.gas_adjuster.get_blob_tx_priority_fee();
+        let blob_base_fee_per_gas = Some(self.gas_adjuster.get_blob_tx_blob_base_fee());
 
         if let Some(previous_sent_tx) = previous_sent_tx {
             // for blob transactions on re-sending need to double all gas prices
diff --git a/core/node/fee_model/src/l1_gas_price/gas_adjuster/mod.rs b/core/node/fee_model/src/l1_gas_price/gas_adjuster/mod.rs
index 9e553ba47bf2..34cbee9b09e5 100644
--- a/core/node/fee_model/src/l1_gas_price/gas_adjuster/mod.rs
+++ b/core/node/fee_model/src/l1_gas_price/gas_adjuster/mod.rs
@@ -334,6 +334,22 @@ impl L1TxParamsProvider for GasAdjuster {
     fn get_priority_fee(&self) -> u64 {
         self.config.default_priority_fee_per_gas
     }
+
+    // The idea is that when we finally decide to send blob tx, we want to offer gas fees high
+    // enough to "almost be certain" that the transaction gets included. To never have to double
+    // the gas prices as then we have very little control how much we pay in the end. This strategy
+    // works as no matter if we double or triple such price, we pay the same block base fees.
+    fn get_blob_tx_base_fee(&self) -> u64 {
+        self.base_fee_statistics.last_added_value() * 2
+    }
+
+    fn get_blob_tx_blob_base_fee(&self) -> u64 {
+        self.blob_base_fee_statistics.last_added_value().as_u64() * 2
+    }
+
+    fn get_blob_tx_priority_fee(&self) -> u64 {
+        self.get_priority_fee() * 2
+    }
 }
 
 /// Helper structure responsible for collecting the data about recent transactions,
diff --git a/core/node/fee_model/src/l1_gas_price/mod.rs b/core/node/fee_model/src/l1_gas_price/mod.rs
index 219dc2f9c38d..0dab2d921c40 100644
--- a/core/node/fee_model/src/l1_gas_price/mod.rs
+++ b/core/node/fee_model/src/l1_gas_price/mod.rs
@@ -27,4 +27,13 @@ pub trait L1TxParamsProvider: fmt::Debug + 'static + Send + Sync {
 
     /// Returns a lower bound for the `base_fee` value for the next L1 block.
     fn get_next_block_minimal_base_fee(&self) -> u64;
+
+    /// Returns the recommended `max_fee_per_gas` value (EIP1559) for blob transaction.
+    fn get_blob_tx_base_fee(&self) -> u64;
+
+    /// Returns the recommended `max_blob_fee_per_gas` value (EIP4844) for blob transaction.
+    fn get_blob_tx_blob_base_fee(&self) -> u64;
+
+    /// Returns the recommended `max_priority_fee_per_gas` value (EIP1559) for blob transaction.
+    fn get_blob_tx_priority_fee(&self) -> u64;
 }