diff --git a/solana/bridge/program/src/api/governance.rs b/solana/bridge/program/src/api/governance.rs index 5195dc917..fb8abe91f 100644 --- a/solana/bridge/program/src/api/governance.rs +++ b/solana/bridge/program/src/api/governance.rs @@ -16,6 +16,12 @@ use crate::{ GuardianSet, GuardianSetDerivationData, }, + error::Error::{ + InvalidFeeRecipient, + InvalidGovernanceKey, + InvalidGovernanceWithdrawal, + InvalidGuardianSetUpgrade, + }, types::{ GovernancePayloadGuardianSetChange, GovernancePayloadSetMessageFee, @@ -26,12 +32,6 @@ use crate::{ ClaimableVAA, DeserializePayload, }, - Error::{ - InvalidFeeRecipient, - InvalidGovernanceKey, - InvalidGovernanceWithdrawal, - InvalidGuardianSetUpgrade, - }, CHAIN_ID_SOLANA, }; diff --git a/solana/bridge/program/src/api/initialize.rs b/solana/bridge/program/src/api/initialize.rs index 3bc9ad09c..38ea55e3c 100644 --- a/solana/bridge/program/src/api/initialize.rs +++ b/solana/bridge/program/src/api/initialize.rs @@ -5,8 +5,8 @@ use crate::{ GuardianSet, GuardianSetDerivationData, }, + error::Error::TooManyGuardians, types::*, - Error::TooManyGuardians, MAX_LEN_GUARDIAN_KEYS, }; use solana_program::sysvar::clock::Clock; diff --git a/solana/bridge/program/src/api/post_message.rs b/solana/bridge/program/src/api/post_message.rs index e2e855af5..eeeccc5c7 100644 --- a/solana/bridge/program/src/api/post_message.rs +++ b/solana/bridge/program/src/api/post_message.rs @@ -7,14 +7,13 @@ use crate::{ Sequence, SequenceDerivationData, }, - Error::{ + error::Error::{ InsufficientFees, MathOverflow, }, CHAIN_ID_SOLANA, }; use solana_program::{ - msg, pubkey::Pubkey, sysvar::clock::Clock, }; diff --git a/solana/bridge/program/src/api/post_vaa.rs b/solana/bridge/program/src/api/post_vaa.rs index feaa6bbc6..f162f12e1 100644 --- a/solana/bridge/program/src/api/post_vaa.rs +++ b/solana/bridge/program/src/api/post_vaa.rs @@ -19,8 +19,11 @@ use crate::{ SignatureSet, SignatureSetDerivationData, }, - Error, - Error::GuardianSetMismatch, + error::Error::{ + GuardianSetMismatch, + PostVAAConsensusFailed, + PostVAAGuardianSetExpired, + }, CHAIN_ID_SOLANA, }; use byteorder::{ @@ -29,7 +32,6 @@ use byteorder::{ }; use sha3::Digest; use solana_program::{ - msg, program_error::ProgramError, pubkey::Pubkey, }; @@ -155,7 +157,7 @@ pub fn post_vaa(ctx: &ExecutionContext, accs: &mut PostVAA, vaa: PostVAAData) -> }; if signature_count < required_consensus_count { - return Err(Error::PostVAAConsensusFailed.into()); + return Err(PostVAAConsensusFailed.into()); } // If the VAA originates from another chain we need to create the account and populate all fields @@ -186,7 +188,7 @@ fn check_active<'r>( if guardian_set.expiration_time != 0 && (guardian_set.expiration_time as i64) < clock.unix_timestamp { - return Err(Error::PostVAAGuardianSetExpired.into()); + return Err(PostVAAGuardianSetExpired.into()); } Ok(()) } diff --git a/solana/bridge/program/src/api/verify_signature.rs b/solana/bridge/program/src/api/verify_signature.rs index e153ab49c..dce663f2d 100644 --- a/solana/bridge/program/src/api/verify_signature.rs +++ b/solana/bridge/program/src/api/verify_signature.rs @@ -7,7 +7,7 @@ use crate::{ SignatureSet, SignatureSetDerivationData, }, - Error::{ + error::Error::{ GuardianSetMismatch, InstructionAtWrongIndex, InvalidHash, diff --git a/solana/bridge/program/src/error.rs b/solana/bridge/program/src/error.rs new file mode 100644 index 000000000..7e17bde5c --- /dev/null +++ b/solana/bridge/program/src/error.rs @@ -0,0 +1,35 @@ +//! Define application level errors that can be returned by the various instruction handlers that +//! make up the wormhole bridge. + +use crate::trace; +use solitaire::SolitaireError; + +#[derive(Debug)] +pub enum Error { + GuardianSetMismatch, + InstructionAtWrongIndex, + InsufficientFees, + InvalidFeeRecipient, + InvalidGovernanceAction, + InvalidGovernanceChain, + InvalidGovernanceKey, + InvalidGovernanceModule, + InvalidGovernanceWithdrawal, + InvalidGuardianSetUpgrade, + InvalidHash, + InvalidSecpInstruction, + MathOverflow, + PostVAAConsensusFailed, + PostVAAGuardianSetExpired, + TooManyGuardians, + VAAAlreadyExecuted, +} + +/// Errors thrown by the program will bubble up to the solitaire wrapper, which needs a way to +/// translate these errors into something Solitaire can log and handle. +impl From for SolitaireError { + fn from(e: Error) -> SolitaireError { + trace!("ProgramError: {:?}", e); + SolitaireError::Custom(e as u64) + } +} diff --git a/solana/bridge/program/src/lib.rs b/solana/bridge/program/src/lib.rs index c402a0b47..d06b37309 100644 --- a/solana/bridge/program/src/lib.rs +++ b/solana/bridge/program/src/lib.rs @@ -2,10 +2,9 @@ #![allow(non_upper_case_globals)] #![allow(incomplete_features)] -use solana_program::msg; - pub mod accounts; pub mod api; +pub mod error; pub mod types; pub mod vaa; @@ -50,39 +49,8 @@ pub use vaa::{ }; const MAX_LEN_GUARDIAN_KEYS: usize = 19; - const CHAIN_ID_SOLANA: u16 = 1; -#[derive(Debug)] -enum Error { - GuardianSetMismatch, - InstructionAtWrongIndex, - InsufficientFees, - InvalidFeeRecipient, - InvalidGovernanceAction, - InvalidGovernanceChain, - InvalidGovernanceKey, - InvalidGovernanceModule, - InvalidGovernanceWithdrawal, - InvalidGuardianSetUpgrade, - InvalidHash, - InvalidSecpInstruction, - MathOverflow, - PostVAAConsensusFailed, - PostVAAGuardianSetExpired, - TooManyGuardians, - VAAAlreadyExecuted, -} - -/// Translate from program specific errors to Solitaire framework errors. Log the error on the way -/// out of the program for debugging. -impl From for SolitaireError { - fn from(e: Error) -> SolitaireError { - msg!("ProgramError: {:?}", e); - SolitaireError::Custom(e as u64) - } -} - solitaire! { Initialize(InitializeData) => initialize, PostMessage(PostMessageData) => post_message, diff --git a/solana/bridge/program/src/types.rs b/solana/bridge/program/src/types.rs index 1a30439c4..a08adce23 100644 --- a/solana/bridge/program/src/types.rs +++ b/solana/bridge/program/src/types.rs @@ -21,7 +21,6 @@ use solitaire::{ AccountOwner, Owned, }, - trace, SolitaireError, }; use std::{ @@ -309,7 +308,6 @@ pub struct GovernancePayloadSetMessageFee { impl SerializePayload for GovernancePayloadSetMessageFee { fn serialize(&self, v: &mut W) -> std::result::Result<(), SolitaireError> { - use byteorder::WriteBytesExt; let mut fee_data = [0u8; 32]; self.fee.to_big_endian(&mut fee_data); v.write(&fee_data[..])?; @@ -359,11 +357,10 @@ pub struct GovernancePayloadTransferFees { impl SerializePayload for GovernancePayloadTransferFees { fn serialize(&self, v: &mut W) -> std::result::Result<(), SolitaireError> { - use byteorder::WriteBytesExt; let mut amount_data = [0u8; 32]; self.amount.to_big_endian(&mut amount_data); v.write(&amount_data)?; - v.write(&self.to); + v.write(&self.to)?; Ok(()) } } diff --git a/solana/bridge/program/src/vaa.rs b/solana/bridge/program/src/vaa.rs index 693c3f93e..931657fe0 100644 --- a/solana/bridge/program/src/vaa.rs +++ b/solana/bridge/program/src/vaa.rs @@ -3,13 +3,13 @@ use crate::{ Claim, ClaimDerivationData, }, - types::PostedMessage, - Error::{ + error::Error::{ InvalidGovernanceAction, InvalidGovernanceChain, InvalidGovernanceModule, VAAAlreadyExecuted, }, + types::PostedMessage, Result, CHAIN_ID_SOLANA, };