Skip to content

Commit 75e1bd3

Browse files
committed
chore(solana): factorize all relayer errors
1 parent 2b82874 commit 75e1bd3

20 files changed

Lines changed: 61 additions & 84 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use anchor_lang::prelude::*;
2+
3+
#[error_code]
4+
pub enum RelayerError {
5+
// Initialization (6000-6099)
6+
#[msg("Only the upgrade authority can initialize the relayer")]
7+
UnauthorizedInitialization = 6000,
8+
9+
#[msg("Incorrect relayer program")]
10+
IncorrectRelayerProgram,
11+
12+
// Configuration (6100-6199)
13+
#[msg("Unauthorized to update configuration")]
14+
UnauthorizedConfigUpdate = 6100,
15+
16+
// Gas Validation (6200-6299)
17+
#[msg("Gas limit too low")]
18+
GasLimitTooLow = 6200,
19+
20+
#[msg("Gas limit exceeded")]
21+
GasLimitExceeded,
22+
23+
// Payment (6300-6399)
24+
#[msg("Incorrect gas fee receiver")]
25+
IncorrectGasFeeReceiver = 6300,
26+
}

solana/programs/base_relayer/src/instructions/config/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anchor_lang::prelude::*;
22

3-
use crate::{constants::CFG_SEED, state::Cfg};
3+
use crate::{constants::CFG_SEED, state::Cfg, RelayerError};
44

55
/// Accounts struct for configuration setter instructions
66
/// Only the guardian can update these parameters
@@ -9,7 +9,7 @@ pub struct SetConfig<'info> {
99
/// The bridge account containing configuration
1010
#[account(
1111
mut,
12-
has_one = guardian @ ConfigError::UnauthorizedConfigUpdate,
12+
has_one = guardian @ RelayerError::UnauthorizedConfigUpdate,
1313
seeds = [CFG_SEED],
1414
bump
1515
)]
@@ -19,13 +19,6 @@ pub struct SetConfig<'info> {
1919
pub guardian: Signer<'info>,
2020
}
2121

22-
/// Error codes for configuration updates
23-
#[error_code]
24-
pub enum ConfigError {
25-
#[msg("Unauthorized to update configuration")]
26-
UnauthorizedConfigUpdate = 6000,
27-
}
28-
2922
pub mod set_eip1559_config;
3023
pub mod set_gas_config;
3124
pub mod set_guardian;

solana/programs/base_relayer/src/instructions/initialize.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
constants::{CFG_SEED, DISCRIMINATOR_LEN},
55
internal::{Eip1559, Eip1559Config, GasConfig},
66
program::BaseRelayer as BaseRelayerProgram,
7-
Cfg,
7+
Cfg, RelayerError,
88
};
99

1010
/// Accounts for the initialize instruction that sets up the base relayer's initial state.
@@ -33,15 +33,15 @@ pub struct Initialize<'info> {
3333
/// Validates that the signer is indeed the upgrade authority.
3434
#[account(
3535
constraint = program_data.upgrade_authority_address == Some(upgrade_authority.key())
36-
@ InitializeError::UnauthorizedInitialization
36+
@ RelayerError::UnauthorizedInitialization
3737
)]
3838
pub program_data: Account<'info, ProgramData>,
3939

4040
/// The base_relayer program itself.
4141
/// Validates that program_data is the correct ProgramData account for this program.
4242
#[account(
4343
constraint = program.programdata_address()? == Some(program_data.key())
44-
@ InitializeError::IncorrectRelayerProgram
44+
@ RelayerError::IncorrectRelayerProgram
4545
)]
4646
pub program: Program<'info, BaseRelayerProgram>,
4747

@@ -74,20 +74,11 @@ pub fn initialize_handler(
7474
Ok(())
7575
}
7676

77-
/// Error codes for initialization
78-
#[error_code]
79-
pub enum InitializeError {
80-
#[msg("Only the upgrade authority can initialize the relayer")]
81-
UnauthorizedInitialization = 8000,
82-
#[msg("Incorrect relayer program")]
83-
IncorrectRelayerProgram = 8001,
84-
}
85-
8677
#[cfg(test)]
8778
mod tests {
8879
use super::*;
8980
use anchor_lang::{
90-
solana_program::{bpf_loader_upgradeable, instruction::Instruction, system_program},
81+
solana_program::{instruction::Instruction, system_program},
9182
InstructionData,
9283
};
9384
use solana_message::Message;
@@ -217,7 +208,7 @@ mod tests {
217208
// Verify the error message contains "UnauthorizedInitialization"
218209
let error_string = format!("{:?}", result.unwrap_err());
219210
assert!(
220-
error_string.contains("UnauthorizedInitialization") || error_string.contains("8000"), // Error code for UnauthorizedInitialization
211+
error_string.contains("UnauthorizedInitialization") || error_string.contains("6000"), // Error code for UnauthorizedInitialization
221212
"Expected UnauthorizedInitialization error, got: {}",
222213
error_string
223214
);

solana/programs/base_relayer/src/instructions/pay_for_relay.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
constants::{CFG_SEED, DISCRIMINATOR_LEN, MTR_SEED},
55
internal::check_and_pay_for_gas,
66
state::{Cfg, MessageToRelay},
7+
RelayerError,
78
};
89

910
#[derive(Accounts)]
@@ -22,7 +23,7 @@ pub struct PayForRelay<'info> {
2223

2324
/// The account that receives payment for the gas costs of bridging SOL to Base.
2425
/// CHECK: This account is validated to be the same as cfg.gas_config.gas_fee_receiver
25-
#[account(mut, address = cfg.gas_config.gas_fee_receiver @ PayForRelayError::IncorrectGasFeeReceiver)]
26+
#[account(mut, address = cfg.gas_config.gas_fee_receiver @ RelayerError::IncorrectGasFeeReceiver)]
2627
pub gas_fee_receiver: AccountInfo<'info>,
2728

2829
#[account(init, payer = payer, seeds = [MTR_SEED, mtr_salt.as_ref()], bump, space = DISCRIMINATOR_LEN + MessageToRelay::INIT_SPACE)]
@@ -57,12 +58,6 @@ pub fn pay_for_relay_handler(
5758
Ok(())
5859
}
5960

60-
#[error_code]
61-
pub enum PayForRelayError {
62-
#[msg("Incorrect gas fee receiver")]
63-
IncorrectGasFeeReceiver,
64-
}
65-
6661
#[cfg(test)]
6762
mod tests {
6863
use super::*;

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anchor_lang::prelude::*;
22

3-
use crate::state::Cfg;
3+
use crate::{state::Cfg, RelayerError};
44

55
#[derive(Debug, Clone, PartialEq, Eq, InitSpace, AnchorSerialize, AnchorDeserialize)]
66
pub struct GasConfig {
@@ -30,11 +30,11 @@ pub fn check_and_pay_for_gas<'info>(
3030
fn check_gas_limit(gas_limit: u64, cfg: &Cfg) -> Result<()> {
3131
require!(
3232
gas_limit >= cfg.gas_config.min_gas_limit_per_message,
33-
GasConfigError::GasLimitTooLow
33+
RelayerError::GasLimitTooLow
3434
);
3535
require!(
3636
gas_limit <= cfg.gas_config.max_gas_limit_per_message,
37-
GasConfigError::GasLimitExceeded
37+
RelayerError::GasLimitExceeded
3838
);
3939

4040
Ok(())
@@ -70,14 +70,6 @@ fn pay_for_gas<'info>(
7070
Ok(())
7171
}
7272

73-
#[error_code]
74-
pub enum GasConfigError {
75-
#[msg("Gas limit too low")]
76-
GasLimitTooLow,
77-
#[msg("Gas limit exceeded")]
78-
GasLimitExceeded,
79-
}
80-
8173
#[cfg(test)]
8274
mod tests {
8375
use super::*;

solana/programs/base_relayer/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
use anchor_lang::prelude::*;
44

55
mod constants;
6+
mod errors;
67
mod instructions;
78
mod internal;
89
mod state;
910

11+
pub use errors::*;
1012
use instructions::*;
1113
use internal::*;
1214
use state::*;

solana/programs/bridge/src/base_to_solana/instructions/buffered/prove_message_buffered.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use anchor_lang::{prelude::*, solana_program::keccak};
22

3+
use crate::BridgeError;
34
use crate::{
45
base_to_solana::{
56
constants::INCOMING_MESSAGE_SEED, internal::mmr, state::IncomingMessage, Message,
67
OutputRoot, ProveBuffer,
78
},
89
common::{bridge::Bridge, BRIDGE_SEED, DISCRIMINATOR_LEN},
910
};
10-
use crate::BridgeError;
1111

1212
/// Buffered variant of `prove_message` that reads data/proof from a `ProveBuffer` and closes it.
1313
#[derive(Accounts)]
@@ -56,10 +56,7 @@ pub fn prove_message_buffered_handler(
5656
message_hash: [u8; 32],
5757
) -> Result<()> {
5858
// Pause
59-
require!(
60-
!ctx.accounts.bridge.paused,
61-
BridgeError::BridgePaused
62-
);
59+
require!(!ctx.accounts.bridge.paused, BridgeError::BridgePaused);
6360

6461
// Verify hash
6562
let data = &ctx.accounts.prove_buffer.data;

solana/programs/bridge/src/base_to_solana/instructions/register_output_root.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use anchor_lang::prelude::*;
33
use crate::base_to_solana::constants::{PARTNER_PROGRAM_ID, PARTNER_SIGNERS_ACCOUNT_SEED};
44
use crate::base_to_solana::state::Signers;
55
use crate::base_to_solana::{compute_output_root_message_hash, recover_unique_evm_addresses};
6+
use crate::BridgeError;
67
use crate::{
78
base_to_solana::{constants::OUTPUT_ROOT_SEED, state::OutputRoot},
89
common::{bridge::Bridge, BRIDGE_SEED, DISCRIMINATOR_LEN},
910
};
10-
use crate::BridgeError;
1111

1212
/// Accounts struct for the `register_output_root` instruction that stores Base MMR roots
1313
/// on Solana for cross-chain message verification. This instruction allows a trusted oracle to
@@ -60,10 +60,7 @@ pub fn register_output_root_handler(
6060
signatures: Vec<[u8; 65]>,
6161
) -> Result<()> {
6262
// Check if bridge is paused
63-
require!(
64-
!ctx.accounts.bridge.paused,
65-
BridgeError::BridgePaused
66-
);
63+
require!(!ctx.accounts.bridge.paused, BridgeError::BridgePaused);
6764

6865
// Build message hash for signatures
6966
let message_hash =

solana/programs/bridge/src/base_to_solana/instructions/relay_message.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ pub fn relay_message_handler<'a, 'info>(
3333
// Check if bridge is paused
3434
require!(!ctx.accounts.bridge.paused, BridgeError::BridgePaused);
3535

36-
require!(
37-
!ctx.accounts.message.executed,
38-
BridgeError::AlreadyExecuted
39-
);
36+
require!(!ctx.accounts.message.executed, BridgeError::AlreadyExecuted);
4037

4138
let message = ctx.accounts.message.message.clone();
4239
let (transfer, ixs) = match message {

solana/programs/bridge/src/base_to_solana/instructions/token/finalize_sol_transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use anchor_lang::{
33
system_program::{self, Transfer},
44
};
55

6-
use crate::{common::SOL_VAULT_SEED, ID};
76
use crate::BridgeError;
7+
use crate::{common::SOL_VAULT_SEED, ID};
88

99
/// Instruction data for finalizing a native SOL transfer from Base to Solana.
1010
///

0 commit comments

Comments
 (0)