bridge: properly handle VAA submissions

This commit is contained in:
Hendrik Hofstadt 2020-08-24 12:56:19 +02:00
parent 206eca5ac5
commit 042ff4a14b
5 changed files with 21 additions and 4 deletions

View File

@ -100,6 +100,9 @@ pub enum Error {
/// Cannot wrap a solana native asset
#[error("CannotWrapNative")]
CannotWrapNative,
/// VAA for this transfer has already been submitted
#[error("VAAAlreadySubmitted")]
VAAAlreadySubmitted,
}
impl From<Error> for ProgramError {

View File

@ -38,6 +38,7 @@ impl PrintProgramError for Error {
Error::SameChainTransfer => info!("Error: SameChainTransfer"),
Error::VAATooLong => info!("Error: VAATooLong"),
Error::CannotWrapNative => info!("Error: CannotWrapNative"),
Error::VAAAlreadySubmitted => info!("Error: VAAAlreadySubmitted"),
}
}
}

View File

@ -22,10 +22,12 @@ use crate::vaa::{VAABody, VAA};
pub const CHAIN_ID_SOLANA: u8 = 1;
/// maximum number of guardians
pub const MAX_LEN_GUARDIAN_KEYS: usize = 20;
/// maximum size of a posted VAA
pub const MAX_VAA_SIZE: usize = 1000;
/// size of a foreign address in bytes
const FOREIGN_ADDRESS_SIZE: usize = 32;
/// length-prefixed serialized validator payment approval data
/// serialized VAA data
pub type VAAData = Vec<u8>;
/// X and Y point of P for guardians
pub type GuardianKey = [u8; 64];

View File

@ -654,9 +654,19 @@ impl Bridge {
if !proposal.matches_vaa(b) {
return Err(Error::VAAProposalMismatch.into());
}
if proposal.vaa_time != 0 {
return Err(Error::VAAAlreadySubmitted.into());
}
if vaa_data.len() > MAX_VAA_SIZE {
return Err(Error::VAATooLong.into());
}
// Set vaa
proposal.vaa;
for i in 0..vaa_data.len() {
proposal.vaa[i] = vaa_data[i]
}
// Stop byte
proposal.vaa[vaa_data.len()] = 0xff;
proposal.vaa_time = vaa.timestamp;
Ok(())

View File

@ -11,7 +11,7 @@ use solana_sdk::{account_info::AccountInfo, program_error::ProgramError, pubkey:
use zerocopy::AsBytes;
use crate::error::Error;
use crate::instruction::{ForeignAddress, VAAData, MAX_LEN_GUARDIAN_KEYS};
use crate::instruction::{ForeignAddress, VAAData, MAX_LEN_GUARDIAN_KEYS, MAX_VAA_SIZE};
use crate::vaa::BodyTransfer;
/// fee rate as a ratio
@ -65,7 +65,8 @@ pub struct TransferOutProposal {
/// nonce of the transfer
pub nonce: u32,
/// vaa to unlock the tokens on the foreign chain
pub vaa: [u8; 1000],
/// it is +1 byte long to make space for the termination byte
pub vaa: [u8; MAX_VAA_SIZE + 1],
/// time the vaa was submitted
pub vaa_time: u32,