浏览代码

refactor: remove duplicate gas limit check in process_event

Co-Authored-By: Jayant Krishnamurthy <jayant@dourolabs.xyz>
Devin AI 10 月之前
父节点
当前提交
2a2bb7a4d3

+ 3 - 0
apps/fortuna/config.sample.yaml

@@ -9,6 +9,9 @@ chains:
     # Increase the transaction gas limit by 10% each time the callback fails
     # defaults to 100 (i.e., don't change the gas limit) if not specified.
     backoff_gas_multiplier_pct: 110
+    # Maximum percentage that the gas limit can be multiplied by during backoff retries
+    # defaults to 500 (i.e., up to 5x the original gas limit) if not specified.
+    backoff_gas_multiplier_cap_pct: 500
     min_keeper_balance: 100000000000000000
 
     # Provider configuration

+ 5 - 0
apps/fortuna/src/api.rs

@@ -92,6 +92,9 @@ pub struct BlockchainState {
     /// The BlockStatus of the block that is considered to be confirmed on the blockchain.
     /// For eg., Finalized, Safe
     pub confirmed_block_status: BlockStatus,
+
+    /// The maximum percentage that the gas limit can be multiplied by during backoff retries
+    pub backoff_gas_multiplier_cap_pct: u64,
 }
 
 pub enum RestError {
@@ -212,6 +215,7 @@ mod test {
             provider_address: PROVIDER,
             reveal_delay_blocks: 1,
             confirmed_block_status: BlockStatus::Latest,
+            backoff_gas_multiplier_cap_pct: 500,
         };
 
         let metrics_registry = Arc::new(RwLock::new(Registry::default()));
@@ -225,6 +229,7 @@ mod test {
             provider_address: PROVIDER,
             reveal_delay_blocks: 2,
             confirmed_block_status: BlockStatus::Latest,
+            backoff_gas_multiplier_cap_pct: 500,
         };
 
         let mut chains = HashMap::new();

+ 1 - 0
apps/fortuna/src/command/run.rs

@@ -296,6 +296,7 @@ async fn setup_chain_state(
         provider_address: *provider,
         reveal_delay_blocks: chain_config.reveal_delay_blocks,
         confirmed_block_status: chain_config.confirmed_block_status,
+        backoff_gas_multiplier_cap_pct: chain_config.backoff_gas_multiplier_cap_pct,
     };
     Ok(state)
 }

+ 9 - 0
apps/fortuna/src/config.rs

@@ -174,6 +174,15 @@ pub struct EthereumConfig {
     /// The percentage multiplier to apply to the priority fee (100 = no change, e.g. 150 = 150% of base fee)
     #[serde(default = "default_priority_fee_multiplier_pct")]
     pub priority_fee_multiplier_pct: u64,
+
+    /// The maximum percentage that the gas limit can be multiplied by during backoff retries
+    /// For example, 500 means the gas limit can be increased up to 500% of the original estimate
+    #[serde(default = "default_backoff_gas_multiplier_cap_pct")]
+    pub backoff_gas_multiplier_cap_pct: u64,
+}
+
+fn default_backoff_gas_multiplier_cap_pct() -> u64 {
+    500  // Default to 500% (5x) maximum gas multiplier
 }
 
 fn default_backoff_gas_multiplier_pct() -> u64 {

+ 8 - 4
apps/fortuna/src/keeper.rs

@@ -248,6 +248,10 @@ pub async fn run_keeper_threads(
     let latest_safe_block = get_latest_safe_block(&chain_state).in_current_span().await;
     tracing::info!("latest safe block: {}", &latest_safe_block);
 
+    // Update BlockchainState with the gas multiplier cap from config
+    let mut chain_state = chain_state;
+    chain_state.backoff_gas_multiplier_cap_pct = chain_eth_config.backoff_gas_multiplier_cap_pct;
+
     let contract = Arc::new(
         InstrumentedSignablePythContract::from_config(
             &chain_eth_config,
@@ -434,8 +438,10 @@ pub async fn process_event_with_backoff(
                 multiplier,
                 e
             );
+            // Calculate new multiplier with backoff, capped by backoff_gas_multiplier_cap_pct
+            let new_multiplier = multiplier.saturating_mul(backoff_gas_multiplier_pct) / 100;
             current_multiplier.store(
-                multiplier.saturating_mul(backoff_gas_multiplier_pct) / 100,
+                std::cmp::min(new_multiplier, chain_state.backoff_gas_multiplier_cap_pct),
                 std::sync::atomic::Ordering::Relaxed,
             );
         },
@@ -537,10 +543,8 @@ pub async fn process_event(
         )));
     }
 
-    // Pad the gas estimate after checking it against the simulation gas limit, ensuring that
-    // the padded gas estimate doesn't exceed the maximum amount of gas we are willing to use.
+    // Pad the gas estimate after checking it against the simulation gas limit
     let gas_estimate = gas_estimate.saturating_mul(gas_estimate_multiplier_pct.into()) / 100;
-    let gas_estimate = gas_estimate.min((gas_limit * DEFAULT_GAS_ESTIMATE_MULTIPLIER_PCT) / 100);
 
     let contract_call = contract
         .reveal_with_callback(