Skip to content

Commit 3980457

Browse files
authored
add min gas limit to base_relayer program (#89)
1 parent ee15a14 commit 3980457

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

solana/programs/base_relayer/src/internal/gas_config.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::state::Cfg;
44

55
#[derive(Debug, Clone, PartialEq, Eq, InitSpace, AnchorSerialize, AnchorDeserialize)]
66
pub struct GasConfig {
7+
/// Minimum gas limit per cross-chain message
8+
pub min_gas_limit_per_message: u64,
79
/// Maximum gas limit per cross-chain message
810
pub max_gas_limit_per_message: u64,
911
/// Scaling factor for gas cost calculations
@@ -26,6 +28,10 @@ pub fn check_and_pay_for_gas<'info>(
2628
}
2729

2830
fn check_gas_limit(gas_limit: u64, cfg: &Cfg) -> Result<()> {
31+
require!(
32+
gas_limit >= cfg.gas_config.min_gas_limit_per_message,
33+
GasConfigError::GasLimitTooLow
34+
);
2935
require!(
3036
gas_limit <= cfg.gas_config.max_gas_limit_per_message,
3137
GasConfigError::GasLimitExceeded
@@ -66,6 +72,8 @@ fn pay_for_gas<'info>(
6672

6773
#[error_code]
6874
pub enum GasConfigError {
75+
#[msg("Gas limit too low")]
76+
GasLimitTooLow,
6977
#[msg("Gas limit exceeded")]
7078
GasLimitExceeded,
7179
}
@@ -100,15 +108,14 @@ mod tests {
100108

101109
#[test]
102110
fn check_gas_limit_allows_equal_limit() {
103-
let mut cfg = Cfg {
111+
let cfg = Cfg {
104112
guardian: Pubkey::new_unique(),
105113
eip1559: new_eip(),
106114
gas_config: GasConfig::test_new(TEST_GAS_FEE_RECEIVER),
107115
nonce: 0,
108116
};
109-
cfg.gas_config.max_gas_limit_per_message = 100;
110117

111-
let res = super::check_gas_limit(100, &cfg);
118+
let res = super::check_gas_limit(cfg.gas_config.max_gas_limit_per_message, &cfg);
112119
assert!(res.is_ok());
113120
}
114121

@@ -175,7 +182,7 @@ mod tests {
175182
}
176183
.to_account_metas(None);
177184

178-
let gas_limit = 123u64;
185+
let gas_limit = 123_000u64;
179186
let ix = Instruction {
180187
program_id: crate::ID,
181188
accounts,
@@ -194,7 +201,7 @@ mod tests {
194201
svm.send_transaction(tx).unwrap();
195202

196203
let final_receiver_balance = svm.get_account(&TEST_GAS_FEE_RECEIVER).unwrap().lamports;
197-
assert_eq!(final_receiver_balance - initial_receiver_balance, 246);
204+
assert_eq!(final_receiver_balance - initial_receiver_balance, 246_000);
198205
}
199206

200207
#[test]
@@ -253,7 +260,7 @@ mod tests {
253260
// Advance clock by one window so refresh_base_fee applies 100 -> 50
254261
mock_clock(&mut svm, start_time + 1);
255262

256-
let gas_limit = 1_000u64;
263+
let gas_limit = 100_000u64;
257264
let outgoing_message = Pubkey::new_unique();
258265
let message_to_relay = Keypair::new();
259266
let accounts = accounts::PayForRelay {
@@ -284,7 +291,7 @@ mod tests {
284291

285292
let final_receiver_balance = svm.get_account(&TEST_GAS_FEE_RECEIVER).unwrap().lamports;
286293
// base_fee 1 * gas_limit 1000 = 1_000
287-
assert_eq!(final_receiver_balance - initial_receiver_balance, 1_000);
294+
assert_eq!(final_receiver_balance - initial_receiver_balance, 100_000);
288295

289296
// Validate EIP-1559 state was updated for the new window and usage accounted
290297
let updated = fetch_cfg(&svm, &cfg_pda);

solana/programs/base_relayer/src/test_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl Eip1559Config {
3232
impl GasConfig {
3333
pub fn test_new(gas_fee_receiver: Pubkey) -> Self {
3434
Self {
35+
min_gas_limit_per_message: 100_000,
3536
max_gas_limit_per_message: 100_000_000,
3637
gas_cost_scaler: 1_000_000,
3738
gas_cost_scaler_dp: 10u64.pow(6),

0 commit comments

Comments
 (0)