Move errors to their own file.

Change-Id: If76fc6096f2788e13dad15ea088202159793fb0b
This commit is contained in:
Reisen 2021-07-12 09:39:03 +00:00
parent 35bdef1f8b
commit 18a6f429cb
9 changed files with 55 additions and 54 deletions

View File

@ -16,6 +16,12 @@ use crate::{
GuardianSet, GuardianSet,
GuardianSetDerivationData, GuardianSetDerivationData,
}, },
error::Error::{
InvalidFeeRecipient,
InvalidGovernanceKey,
InvalidGovernanceWithdrawal,
InvalidGuardianSetUpgrade,
},
types::{ types::{
GovernancePayloadGuardianSetChange, GovernancePayloadGuardianSetChange,
GovernancePayloadSetMessageFee, GovernancePayloadSetMessageFee,
@ -26,12 +32,6 @@ use crate::{
ClaimableVAA, ClaimableVAA,
DeserializePayload, DeserializePayload,
}, },
Error::{
InvalidFeeRecipient,
InvalidGovernanceKey,
InvalidGovernanceWithdrawal,
InvalidGuardianSetUpgrade,
},
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
}; };

View File

@ -5,8 +5,8 @@ use crate::{
GuardianSet, GuardianSet,
GuardianSetDerivationData, GuardianSetDerivationData,
}, },
error::Error::TooManyGuardians,
types::*, types::*,
Error::TooManyGuardians,
MAX_LEN_GUARDIAN_KEYS, MAX_LEN_GUARDIAN_KEYS,
}; };
use solana_program::sysvar::clock::Clock; use solana_program::sysvar::clock::Clock;

View File

@ -7,14 +7,13 @@ use crate::{
Sequence, Sequence,
SequenceDerivationData, SequenceDerivationData,
}, },
Error::{ error::Error::{
InsufficientFees, InsufficientFees,
MathOverflow, MathOverflow,
}, },
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
}; };
use solana_program::{ use solana_program::{
msg,
pubkey::Pubkey, pubkey::Pubkey,
sysvar::clock::Clock, sysvar::clock::Clock,
}; };

View File

@ -19,8 +19,11 @@ use crate::{
SignatureSet, SignatureSet,
SignatureSetDerivationData, SignatureSetDerivationData,
}, },
Error, error::Error::{
Error::GuardianSetMismatch, GuardianSetMismatch,
PostVAAConsensusFailed,
PostVAAGuardianSetExpired,
},
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
}; };
use byteorder::{ use byteorder::{
@ -29,7 +32,6 @@ use byteorder::{
}; };
use sha3::Digest; use sha3::Digest;
use solana_program::{ use solana_program::{
msg,
program_error::ProgramError, program_error::ProgramError,
pubkey::Pubkey, pubkey::Pubkey,
}; };
@ -155,7 +157,7 @@ pub fn post_vaa(ctx: &ExecutionContext, accs: &mut PostVAA, vaa: PostVAAData) ->
}; };
if signature_count < required_consensus_count { 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 // 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 if guardian_set.expiration_time != 0
&& (guardian_set.expiration_time as i64) < clock.unix_timestamp && (guardian_set.expiration_time as i64) < clock.unix_timestamp
{ {
return Err(Error::PostVAAGuardianSetExpired.into()); return Err(PostVAAGuardianSetExpired.into());
} }
Ok(()) Ok(())
} }

View File

@ -7,7 +7,7 @@ use crate::{
SignatureSet, SignatureSet,
SignatureSetDerivationData, SignatureSetDerivationData,
}, },
Error::{ error::Error::{
GuardianSetMismatch, GuardianSetMismatch,
InstructionAtWrongIndex, InstructionAtWrongIndex,
InvalidHash, InvalidHash,

View File

@ -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<Error> for SolitaireError {
fn from(e: Error) -> SolitaireError {
trace!("ProgramError: {:?}", e);
SolitaireError::Custom(e as u64)
}
}

View File

@ -2,10 +2,9 @@
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
#![allow(incomplete_features)] #![allow(incomplete_features)]
use solana_program::msg;
pub mod accounts; pub mod accounts;
pub mod api; pub mod api;
pub mod error;
pub mod types; pub mod types;
pub mod vaa; pub mod vaa;
@ -50,39 +49,8 @@ pub use vaa::{
}; };
const MAX_LEN_GUARDIAN_KEYS: usize = 19; const MAX_LEN_GUARDIAN_KEYS: usize = 19;
const CHAIN_ID_SOLANA: u16 = 1; 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<Error> for SolitaireError {
fn from(e: Error) -> SolitaireError {
msg!("ProgramError: {:?}", e);
SolitaireError::Custom(e as u64)
}
}
solitaire! { solitaire! {
Initialize(InitializeData) => initialize, Initialize(InitializeData) => initialize,
PostMessage(PostMessageData) => post_message, PostMessage(PostMessageData) => post_message,

View File

@ -21,7 +21,6 @@ use solitaire::{
AccountOwner, AccountOwner,
Owned, Owned,
}, },
trace,
SolitaireError, SolitaireError,
}; };
use std::{ use std::{
@ -309,7 +308,6 @@ pub struct GovernancePayloadSetMessageFee {
impl SerializePayload for GovernancePayloadSetMessageFee { impl SerializePayload for GovernancePayloadSetMessageFee {
fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> { fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> {
use byteorder::WriteBytesExt;
let mut fee_data = [0u8; 32]; let mut fee_data = [0u8; 32];
self.fee.to_big_endian(&mut fee_data); self.fee.to_big_endian(&mut fee_data);
v.write(&fee_data[..])?; v.write(&fee_data[..])?;
@ -359,11 +357,10 @@ pub struct GovernancePayloadTransferFees {
impl SerializePayload for GovernancePayloadTransferFees { impl SerializePayload for GovernancePayloadTransferFees {
fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> { fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> {
use byteorder::WriteBytesExt;
let mut amount_data = [0u8; 32]; let mut amount_data = [0u8; 32];
self.amount.to_big_endian(&mut amount_data); self.amount.to_big_endian(&mut amount_data);
v.write(&amount_data)?; v.write(&amount_data)?;
v.write(&self.to); v.write(&self.to)?;
Ok(()) Ok(())
} }
} }

View File

@ -3,13 +3,13 @@ use crate::{
Claim, Claim,
ClaimDerivationData, ClaimDerivationData,
}, },
types::PostedMessage, error::Error::{
Error::{
InvalidGovernanceAction, InvalidGovernanceAction,
InvalidGovernanceChain, InvalidGovernanceChain,
InvalidGovernanceModule, InvalidGovernanceModule,
VAAAlreadyExecuted, VAAAlreadyExecuted,
}, },
types::PostedMessage,
Result, Result,
CHAIN_ID_SOLANA, CHAIN_ID_SOLANA,
}; };