From 170cb25bf8e6622be7334df42f859529f70130d1 Mon Sep 17 00:00:00 2001 From: An-tol <88716253+An-tol@users.noreply.github.com> Date: Thu, 30 Jan 2025 21:25:48 +0300 Subject: [PATCH] Update minimum.rs Added check for zero payment. Improved documentation. Added additional tests. --- .../src/msg/gas_payment/policies/minimum.rs | 202 +++++++++++------- 1 file changed, 127 insertions(+), 75 deletions(-) diff --git a/rust/main/agents/relayer/src/msg/gas_payment/policies/minimum.rs b/rust/main/agents/relayer/src/msg/gas_payment/policies/minimum.rs index ea2c4d23d1..7819b3b3dd 100644 --- a/rust/main/agents/relayer/src/msg/gas_payment/policies/minimum.rs +++ b/rust/main/agents/relayer/src/msg/gas_payment/policies/minimum.rs @@ -1,20 +1,32 @@ use async_trait::async_trait; use derive_new::new; use eyre::Result; - use hyperlane_core::{ HyperlaneMessage, InterchainGasExpenditure, InterchainGasPayment, TxCostEstimate, U256, }; use crate::msg::gas_payment::GasPaymentPolicy; +/// Policy for checking the minimum gas payment. #[derive(Debug, new)] pub struct GasPaymentPolicyMinimum { + /// The minimum payment required to perform an operation. minimum_payment: U256, } #[async_trait] impl GasPaymentPolicy for GasPaymentPolicyMinimum { + /// Checks if the current gas payment meets the minimum requirements. + /// + /// # Arguments + /// - `_message`: The message to be sent. + /// - `current_payment`: The current gas payment. + /// - `_current_expenditure`: The current gas expenditure. + /// - `tx_cost_estimate`: The estimated transaction cost. + /// + /// # Returns + /// - `Ok(Some(gas_limit))` if the payment is sufficient. + /// - `Ok(None)` if the payment is insufficient. async fn message_meets_gas_payment_requirement( &self, _message: &HyperlaneMessage, @@ -22,6 +34,12 @@ impl GasPaymentPolicy for GasPaymentPolicyMinimum { _current_expenditure: &InterchainGasExpenditure, tx_cost_estimate: &TxCostEstimate, ) -> Result> { + // Early return if the payment is zero + if current_payment.payment.is_zero() { + return Ok(None); + } + + // Check if the payment meets the minimum requirement if current_payment.payment >= self.minimum_payment { Ok(Some(tx_cost_estimate.gas_limit)) } else { @@ -30,83 +48,117 @@ impl GasPaymentPolicy for GasPaymentPolicyMinimum { } } -#[tokio::test] -async fn test_gas_payment_policy_minimum() { +#[cfg(test)] +mod tests { + use super::*; use hyperlane_core::{HyperlaneMessage, H256}; - let min = U256::from(1000u32); - let policy = GasPaymentPolicyMinimum::new(min); - let message = HyperlaneMessage::default(); + #[tokio::test] + async fn test_gas_payment_policy_minimum() { + let min = U256::from(1000u32); + let policy = GasPaymentPolicyMinimum::new(min); // Using the automatically implemented new + let message = HyperlaneMessage::default(); - // If the payment is less than the minimum, returns false - let current_payment = InterchainGasPayment { - message_id: H256::zero(), - destination: message.destination, - payment: U256::from(999u32), - gas_amount: U256::zero(), - }; - // expenditure should make no difference - let current_expenditure = InterchainGasExpenditure { - message_id: H256::zero(), - gas_used: U256::from(1000000000u32), - tokens_used: U256::from(1000000000u32), - }; - assert_eq!( - policy - .message_meets_gas_payment_requirement( - &message, - ¤t_payment, - ¤t_expenditure, - &TxCostEstimate { - gas_limit: U256::from(100000u32), - gas_price: U256::from(100000u32).try_into().unwrap(), - l2_gas_limit: None, - }, - ) - .await - .unwrap(), - None - ); + // Test case: Payment is less than the minimum + let current_payment = InterchainGasPayment { + message_id: H256::zero(), + destination: message.destination, + payment: U256::from(999u32), + gas_amount: U256::zero(), + }; + assert_eq!( + policy + .message_meets_gas_payment_requirement( + &message, + ¤t_payment, + &InterchainGasExpenditure::default(), + &TxCostEstimate::default(), + ) + .await + .unwrap(), + None + ); - // If the payment is at least the minimum, returns false - let current_payment = InterchainGasPayment { - message_id: H256::zero(), - destination: message.destination, - payment: U256::from(1000u32), - gas_amount: U256::zero(), - }; - assert_eq!( - policy - .message_meets_gas_payment_requirement( - &message, - ¤t_payment, - ¤t_expenditure, - &TxCostEstimate { - gas_limit: U256::from(100000u32), - gas_price: U256::from(100001u32).try_into().unwrap(), - l2_gas_limit: None, - }, - ) - .await - .unwrap(), - Some(U256::from(100000u32)) - ); + // Test case: Payment is equal to the minimum + let current_payment = InterchainGasPayment { + payment: U256::from(1000u32), + ..current_payment + }; + assert_eq!( + policy + .message_meets_gas_payment_requirement( + &message, + ¤t_payment, + &InterchainGasExpenditure::default(), + &TxCostEstimate { + gas_limit: U256::from(100000u32), + ..Default::default() + }, + ) + .await + .unwrap(), + Some(U256::from(100000u32)) + ); - // Ensure that even if the l2_gas_limit isn't None, the gas_limit is what's returned - assert_eq!( - policy - .message_meets_gas_payment_requirement( - &message, - ¤t_payment, - ¤t_expenditure, - &TxCostEstimate { - gas_limit: U256::from(100000u32), - gas_price: U256::from(100001u32).try_into().unwrap(), - l2_gas_limit: Some(U256::from(22222u32)), - }, - ) - .await - .unwrap(), - Some(U256::from(100000u32)) - ); + // Test case: Payment is greater than the minimum + let current_payment = InterchainGasPayment { + payment: U256::from(1001u32), + ..current_payment + }; + assert_eq!( + policy + .message_meets_gas_payment_requirement( + &message, + ¤t_payment, + &InterchainGasExpenditure::default(), + &TxCostEstimate { + gas_limit: U256::from(100000u32), + ..Default::default() + }, + ) + .await + .unwrap(), + Some(U256::from(100000u32)) + ); + + // Test case: Payment is zero + let current_payment = InterchainGasPayment { + payment: U256::zero(), + ..current_payment + }; + assert_eq!( + policy + .message_meets_gas_payment_requirement( + &message, + ¤t_payment, + &InterchainGasExpenditure::default(), + &TxCostEstimate::default(), + ) + .await + .unwrap(), + None + ); + + // Test case: l2_gas_limit present but ignored + let current_payment = InterchainGasPayment { + payment: U256::from(1000u32), + ..current_payment + }; + assert_eq!( + policy + .message_meets_gas_payment_requirement( + &message, + ¤t_payment, + &InterchainGasExpenditure::default(), + &TxCostEstimate { + gas_limit: U256::from(100000u32), + l2_gas_limit: Some(U256::from(22222u32)), + ..Default::default() + }, + ) + .await + .unwrap(), + Some(U256::from(100000u32)) + ); + } }