Skip to content

Commit 2b82874

Browse files
committed
chore(solana): factorize all bridge errors
1 parent 8dfd181 commit 2b82874

33 files changed

Lines changed: 261 additions & 359 deletions

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

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

33
use crate::base_to_solana::ProveBuffer;
4+
use crate::BridgeError;
45

56
/// Append chunk of serialized `Message` data to the `ProveBuffer`.
67
#[derive(Accounts)]
@@ -11,7 +12,7 @@ pub struct AppendToProveBufferData<'info> {
1112
/// Prove buffer account to append data to
1213
#[account(
1314
mut,
14-
has_one = owner @ AppendToProveBufferError::Unauthorized,
15+
has_one = owner @ BridgeError::BufferUnauthorizedAppend,
1516
)]
1617
pub prove_buffer: Account<'info, ProveBuffer>,
1718
}
@@ -25,12 +26,6 @@ pub fn append_to_prove_buffer_data_handler(
2526
Ok(())
2627
}
2728

28-
#[error_code]
29-
pub enum AppendToProveBufferError {
30-
#[msg("Only the owner can modify this prove buffer")]
31-
Unauthorized,
32-
}
33-
3429
#[cfg(test)]
3530
mod tests {
3631
use super::*;

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

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

33
use crate::base_to_solana::ProveBuffer;
4+
use crate::BridgeError;
45

56
/// Append chunk of MMR proof nodes to the `ProveBuffer`.
67
#[derive(Accounts)]
@@ -11,7 +12,7 @@ pub struct AppendToProveBufferProof<'info> {
1112
/// Prove buffer account to append proof nodes to
1213
#[account(
1314
mut,
14-
has_one = owner @ AppendToProveBufferProofError::Unauthorized,
15+
has_one = owner @ BridgeError::BufferUnauthorizedAppend,
1516
)]
1617
pub prove_buffer: Account<'info, ProveBuffer>,
1718
}
@@ -25,12 +26,6 @@ pub fn append_to_prove_buffer_proof_handler(
2526
Ok(())
2627
}
2728

28-
#[error_code]
29-
pub enum AppendToProveBufferProofError {
30-
#[msg("Only the owner can modify this prove buffer")]
31-
Unauthorized,
32-
}
33-
3429
#[cfg(test)]
3530
mod tests {
3631
use super::*;

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

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

33
use crate::base_to_solana::ProveBuffer;
4+
use crate::BridgeError;
45

56
/// Accounts struct for closing a prove buffer account.
67
#[derive(Accounts)]
@@ -13,7 +14,7 @@ pub struct CloseProveBuffer<'info> {
1314
#[account(
1415
mut,
1516
close = owner,
16-
has_one = owner @ CloseProveBufferError::Unauthorized,
17+
has_one = owner @ BridgeError::BufferUnauthorizedClose,
1718
)]
1819
pub prove_buffer: Account<'info, ProveBuffer>,
1920
}
@@ -23,12 +24,6 @@ pub fn close_prove_buffer_handler(_ctx: Context<CloseProveBuffer>) -> Result<()>
2324
Ok(())
2425
}
2526

26-
#[error_code]
27-
pub enum CloseProveBufferError {
28-
#[msg("Only the owner can close this prove buffer")]
29-
Unauthorized,
30-
}
31-
3227
#[cfg(test)]
3328
mod tests {
3429
use super::*;

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
},
88
common::{bridge::Bridge, BRIDGE_SEED, DISCRIMINATOR_LEN},
99
};
10+
use crate::BridgeError;
1011

1112
/// Buffered variant of `prove_message` that reads data/proof from a `ProveBuffer` and closes it.
1213
#[derive(Accounts)]
@@ -41,7 +42,7 @@ pub struct ProveMessageBuffered<'info> {
4142
#[account(
4243
mut,
4344
close = owner,
44-
has_one = owner @ ProveMessageBufferedError::Unauthorized,
45+
has_one = owner @ BridgeError::BufferUnauthorizedClose,
4546
)]
4647
pub prove_buffer: Account<'info, ProveBuffer>,
4748

@@ -57,15 +58,15 @@ pub fn prove_message_buffered_handler(
5758
// Pause
5859
require!(
5960
!ctx.accounts.bridge.paused,
60-
ProveMessageBufferedError::BridgePaused
61+
BridgeError::BridgePaused
6162
);
6263

6364
// Verify hash
6465
let data = &ctx.accounts.prove_buffer.data;
6566
let computed_hash = hash_message(&nonce.to_be_bytes(), &sender, data);
6667
require!(
6768
message_hash == computed_hash,
68-
ProveMessageBufferedError::InvalidMessageHash
69+
BridgeError::InvalidMessageHash
6970
);
7071

7172
// Verify proof
@@ -96,16 +97,6 @@ fn hash_message(nonce: &[u8], sender: &[u8; 20], data: &[u8]) -> [u8; 32] {
9697
keccak::hash(&data_to_hash).0
9798
}
9899

99-
#[error_code]
100-
pub enum ProveMessageBufferedError {
101-
#[msg("Invalid message hash")]
102-
InvalidMessageHash,
103-
#[msg("Only the owner can close this prove buffer")]
104-
Unauthorized,
105-
#[msg("Bridge is currently paused")]
106-
BridgePaused,
107-
}
108-
109100
#[cfg(test)]
110101
mod tests {
111102
use super::*;

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

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

33
use crate::common::{bridge::Bridge, BRIDGE_SEED};
4+
use crate::BridgeError;
45
use crate::{
56
base_to_solana::{
67
constants::INCOMING_MESSAGE_SEED,
@@ -60,13 +61,13 @@ pub fn prove_message_handler(
6061
message_hash: [u8; 32],
6162
) -> Result<()> {
6263
// Check if bridge is paused
63-
require!(!ctx.accounts.bridge.paused, ProveMessageError::BridgePaused);
64+
require!(!ctx.accounts.bridge.paused, BridgeError::BridgePaused);
6465

6566
// Verify that the provided message hash matches the computed hash
6667
let computed_hash = hash_message(&nonce.to_be_bytes(), &sender, &data);
6768
require!(
6869
message_hash == computed_hash,
69-
ProveMessageError::InvalidMessageHash
70+
BridgeError::InvalidMessageHash
7071
);
7172

7273
// Verify the MMR proof to ensure the message was included on the source chain
@@ -100,11 +101,3 @@ fn hash_message(nonce: &[u8], sender: &[u8; 20], data: &[u8]) -> [u8; 32] {
100101

101102
keccak::hash(&data_to_hash).0
102103
}
103-
104-
#[error_code]
105-
pub enum ProveMessageError {
106-
#[msg("Invalid message hash")]
107-
InvalidMessageHash,
108-
#[msg("Bridge is currently paused")]
109-
BridgePaused,
110-
}

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
base_to_solana::{constants::OUTPUT_ROOT_SEED, state::OutputRoot},
88
common::{bridge::Bridge, BRIDGE_SEED, DISCRIMINATOR_LEN},
99
};
10+
use crate::BridgeError;
1011

1112
/// Accounts struct for the `register_output_root` instruction that stores Base MMR roots
1213
/// on Solana for cross-chain message verification. This instruction allows a trusted oracle to
@@ -61,7 +62,7 @@ pub fn register_output_root_handler(
6162
// Check if bridge is paused
6263
require!(
6364
!ctx.accounts.bridge.paused,
64-
RegisterOutputRootError::BridgePaused
65+
BridgeError::BridgePaused
6566
);
6667

6768
// Build message hash for signatures
@@ -80,7 +81,7 @@ pub fn register_output_root_handler(
8081

8182
require!(
8283
base_approved_count as u8 >= ctx.accounts.bridge.base_oracle_config.threshold,
83-
RegisterOutputRootError::InsufficientBaseSignatures
84+
BridgeError::InsufficientBaseSignatures
8485
);
8586

8687
if ctx.accounts.bridge.partner_oracle_config.required_threshold > 0 {
@@ -100,7 +101,7 @@ pub fn register_output_root_handler(
100101
let partner_approved_count = partner_config.count_approvals(&unique_signers);
101102
require!(
102103
partner_approved_count as u8 >= partner_oracle_config.required_threshold,
103-
RegisterOutputRootError::InsufficientPartnerSignatures
104+
BridgeError::InsufficientPartnerSignatures
104105
);
105106
}
106107

@@ -113,7 +114,7 @@ pub fn register_output_root_handler(
113114
.protocol_config
114115
.block_interval_requirement
115116
== 0,
116-
RegisterOutputRootError::IncorrectBlockNumber
117+
BridgeError::IncorrectBlockNumber
117118
);
118119

119120
ctx.accounts.root.root = output_root;
@@ -123,18 +124,6 @@ pub fn register_output_root_handler(
123124
Ok(())
124125
}
125126

126-
#[error_code]
127-
pub enum RegisterOutputRootError {
128-
#[msg("IncorrectBlockNumber")]
129-
IncorrectBlockNumber,
130-
#[msg("BridgePaused")]
131-
BridgePaused,
132-
#[msg("Insufficient base oracle signatures to meet threshold")]
133-
InsufficientBaseSignatures,
134-
#[msg("Insufficient partner oracle signatures to meet threshold")]
135-
InsufficientPartnerSignatures,
136-
}
137-
138127
#[cfg(test)]
139128
mod tests {
140129
use super::*;

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::base_to_solana::{
77
constants::BRIDGE_CPI_AUTHORITY_SEED, state::IncomingMessage, Message, Transfer,
88
};
99
use crate::common::{bridge::Bridge, BRIDGE_SEED};
10+
use crate::BridgeError;
1011

1112
/// Accounts struct for the relay message instruction that executes cross-chain messages from Base to Solana.
1213
/// This instruction processes incoming messages that contain either pure instruction calls or token transfers
@@ -30,11 +31,11 @@ pub fn relay_message_handler<'a, 'info>(
3031
ctx: Context<'a, '_, 'info, 'info, RelayMessage<'info>>,
3132
) -> Result<()> {
3233
// Check if bridge is paused
33-
require!(!ctx.accounts.bridge.paused, RelayMessageError::BridgePaused);
34+
require!(!ctx.accounts.bridge.paused, BridgeError::BridgePaused);
3435

3536
require!(
3637
!ctx.accounts.message.executed,
37-
RelayMessageError::AlreadyExecuted
38+
BridgeError::AlreadyExecuted
3839
);
3940

4041
let message = ctx.accounts.message.message.clone();
@@ -81,11 +82,3 @@ pub fn relay_message_handler<'a, 'info>(
8182

8283
Ok(())
8384
}
84-
85-
#[error_code]
86-
pub enum RelayMessageError {
87-
#[msg("Message already executed")]
88-
AlreadyExecuted,
89-
#[msg("Bridge is currently paused")]
90-
BridgePaused,
91-
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anchor_lang::{
44
};
55

66
use crate::{common::SOL_VAULT_SEED, ID};
7+
use crate::BridgeError;
78

89
/// Instruction data for finalizing a native SOL transfer from Base to Solana.
910
///
@@ -35,7 +36,7 @@ impl FinalizeBridgeSol {
3536
let system_program_info = Program::<System>::try_from(next_account_info(&mut iter)?)?;
3637

3738
// Verify the recipient matches the instruction data
38-
require_keys_eq!(to_info.key(), self.to, FinalizeBridgeSolError::IncorrectTo);
39+
require_keys_eq!(to_info.key(), self.to, BridgeError::IncorrectTo);
3940

4041
// Verify the SOL vault PDA is correct
4142
let sol_vault_seeds = &[SOL_VAULT_SEED, self.remote_token.as_ref()];
@@ -44,7 +45,7 @@ impl FinalizeBridgeSol {
4445
require_keys_eq!(
4546
sol_vault_info.key(),
4647
sol_vault_pda,
47-
FinalizeBridgeSolError::IncorrectSolVault
48+
BridgeError::IncorrectSolVault
4849
);
4950

5051
// Transfer SOL from the SOL vault to the recipient
@@ -64,11 +65,3 @@ impl FinalizeBridgeSol {
6465
system_program::transfer(cpi_ctx, self.amount)
6566
}
6667
}
67-
68-
#[error_code]
69-
pub enum FinalizeBridgeSolError {
70-
#[msg("Incorrect to")]
71-
IncorrectTo,
72-
#[msg("Incorrect sol vault")]
73-
IncorrectSolVault,
74-
}

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anchor_lang::prelude::*;
22
use anchor_spl::token_interface::{self, Mint, TokenAccount, TokenInterface, TransferChecked};
33

44
use crate::{common::TOKEN_VAULT_SEED, ID};
5+
use crate::BridgeError;
56

67
/// Instruction data for finalizing a bridged SPL token transfer from Base to Solana.
78
///
@@ -44,14 +45,14 @@ impl FinalizeBridgeSpl {
4445
require_keys_eq!(
4546
mint.key(),
4647
self.local_token,
47-
FinalizeBridgeSplError::MintDoesNotMatchLocalToken
48+
BridgeError::MintDoesNotMatchLocalToken
4849
);
4950

5051
// Check that the token account is correct given the to address
5152
require_keys_eq!(
5253
to_token_account.key(),
5354
self.to,
54-
FinalizeBridgeSplError::TokenAccountDoesNotMatchTo
55+
BridgeError::TokenAccountDoesNotMatchTo
5556
);
5657

5758
// Check that the token vault is the expected PDA
@@ -67,7 +68,7 @@ impl FinalizeBridgeSpl {
6768
require_keys_eq!(
6869
token_vault.key(),
6970
token_vault_pda,
70-
FinalizeBridgeSplError::IncorrectTokenVault
71+
BridgeError::IncorrectTokenVault
7172
);
7273

7374
let seeds: &[&[&[u8]]] = &[&[
@@ -93,13 +94,3 @@ impl FinalizeBridgeSpl {
9394
Ok(())
9495
}
9596
}
96-
97-
#[error_code]
98-
pub enum FinalizeBridgeSplError {
99-
#[msg("Mint does not match local token")]
100-
MintDoesNotMatchLocalToken,
101-
#[msg("Token account does not match to address")]
102-
TokenAccountDoesNotMatchTo,
103-
#[msg("Incorrect token vault")]
104-
IncorrectTokenVault,
105-
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
common::{PartialTokenMetadata, WRAPPED_TOKEN_SEED},
99
ID,
1010
};
11+
use crate::BridgeError;
1112

1213
/// Instruction data for finalizing a wrapped token transfer from Base to Solana.
1314
///
@@ -51,14 +52,14 @@ impl FinalizeBridgeWrappedToken {
5152
require_keys_eq!(
5253
mint.key(),
5354
self.local_token,
54-
FinalizeBridgeWrappedTokenError::MintDoesNotMatchLocalToken
55+
BridgeError::MintDoesNotMatchLocalToken
5556
);
5657

5758
// Check that the to is correct given the to address
5859
require_keys_eq!(
5960
to_token_account.key(),
6061
self.to,
61-
FinalizeBridgeWrappedTokenError::TokenAccountDoesNotMatchTo,
62+
BridgeError::TokenAccountDoesNotMatchTo,
6263
);
6364

6465
// Get the partial token metadata
@@ -96,11 +97,3 @@ impl FinalizeBridgeWrappedToken {
9697
Ok(())
9798
}
9899
}
99-
100-
#[error_code]
101-
pub enum FinalizeBridgeWrappedTokenError {
102-
#[msg("Token account does not match to address")]
103-
TokenAccountDoesNotMatchTo,
104-
#[msg("Mint does not match local token")]
105-
MintDoesNotMatchLocalToken,
106-
}

0 commit comments

Comments
 (0)