Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solana audit 2 #1333

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions target_chains/solana/programs/pyth-solana-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ pub mod pyth_solana_receiver {
);

let guardian_keys = &guardian_set.keys;
require_gte!(
vaa.signature_count(),
config.minimum_signatures,
ReceiverError::InsufficientGuardianSignatures
);

// Generate the same message hash (using keccak) that the Guardians used to generate their
// signatures. This message hash will be hashed again to produce the digest for
Expand Down Expand Up @@ -161,12 +166,6 @@ pub mod pyth_solana_receiver {
let treasury = &ctx.accounts.treasury;
let price_update_account = &mut ctx.accounts.price_update_account;

require_gte!(
vaa.signature_count(),
config.minimum_signatures,
ReceiverError::InsufficientGuardianSignatures
);

let vaa_components = VaaComponents {
verification_level: VerificationLevel::Partial {
num_signatures: vaa.signature_count(),
Expand Down Expand Up @@ -329,8 +328,7 @@ fn deserialize_guardian_set_checked(
wormhole: &Pubkey,
) -> Result<AccountVariant<GuardianSet>> {
let mut guardian_set_data: &[u8] = &account_info.try_borrow_data()?;
let guardian_set =
AccountVariant::<GuardianSet>::try_deserialize_unchecked(&mut guardian_set_data)?;
let guardian_set = AccountVariant::<GuardianSet>::try_deserialize(&mut guardian_set_data)?;

let expected_address = Pubkey::find_program_address(
&[
Expand Down Expand Up @@ -371,19 +369,22 @@ fn post_price_update_from_vaa<'info>(
vaa_payload: &[u8],
price_update: &MerklePriceUpdate,
) -> Result<()> {
let amount_to_pay = if treasury.lamports() == 0 {
Rent::get()?
.minimum_balance(0)
.max(config.single_update_fee_in_lamports)
} else {
config.single_update_fee_in_lamports
}; // First person to use the treasury account has to pay rent
if payer.lamports()
< Rent::get()?
.minimum_balance(0)
.saturating_add(config.single_update_fee_in_lamports)
.minimum_balance(payer.data_len())
.saturating_add(amount_to_pay)
{
return err!(ReceiverError::InsufficientFunds);
};

let transfer_instruction = system_instruction::transfer(
payer.key,
treasury.key,
config.single_update_fee_in_lamports,
);
let transfer_instruction = system_instruction::transfer(payer.key, treasury.key, amount_to_pay);
anchor_lang::solana_program::program::invoke(
&transfer_instruction,
&[payer.to_account_info(), treasury.to_account_info()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use {
get_config_address,
get_guardian_set_address,
get_treasury_address,
DEFAULT_TREASURY_ID,
SECONDARY_TREASURY_ID,
},
ID,
},
Expand Down Expand Up @@ -201,22 +199,6 @@ pub async fn setup_pyth_receiver(
.unwrap();
assert_eq!(config_account, initial_config);

program_simulator
.airdrop(
&get_treasury_address(DEFAULT_TREASURY_ID),
Rent::default().minimum_balance(0),
)
.await
.unwrap();

program_simulator
.airdrop(
&get_treasury_address(SECONDARY_TREASURY_ID),
Rent::default().minimum_balance(0),
)
.await
.unwrap();

ProgramTestFixtures {
program_simulator,
encoded_vaa_addresses,
Expand All @@ -234,8 +216,5 @@ pub async fn assert_treasury_balance(
.await
.unwrap();

assert_eq!(
treasury_balance,
expected_balance + Rent::default().minimum_balance(0)
);
assert_eq!(treasury_balance, expected_balance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use {
pubkey::Pubkey,
},
solana_sdk::{
rent::Rent,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -332,7 +333,12 @@ async fn test_post_price_update_from_vaa() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 1, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0),
DEFAULT_TREASURY_ID,
)
.await;

let mut price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down Expand Up @@ -382,7 +388,12 @@ async fn test_post_price_update_from_vaa() {
into_transaction_error(ReceiverError::InsufficientFunds)
);

assert_treasury_balance(&mut program_simulator, 1, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0),
DEFAULT_TREASURY_ID,
)
.await;

price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down Expand Up @@ -432,7 +443,7 @@ async fn test_post_price_update_from_vaa() {

assert_treasury_balance(
&mut program_simulator,
LAMPORTS_PER_SOL + 1,
Rent::default().minimum_balance(0) + LAMPORTS_PER_SOL,
DEFAULT_TREASURY_ID,
)
.await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use {
},
solana_program::pubkey::Pubkey,
solana_sdk::{
rent::Rent,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -78,7 +79,12 @@ async fn test_post_update() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 1, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0),
DEFAULT_TREASURY_ID,
)
.await;

let mut price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down Expand Up @@ -110,7 +116,12 @@ async fn test_post_update() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 2, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0) + 1,
DEFAULT_TREASURY_ID,
)
.await;

price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use {
},
serde_wormhole::RawMessage,
solana_sdk::{
rent::Rent,
signature::Keypair,
signer::Signer,
},
Expand Down Expand Up @@ -84,7 +85,12 @@ async fn test_post_update_atomic() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 1, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0),
DEFAULT_TREASURY_ID,
)
.await;

let mut price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down Expand Up @@ -119,7 +125,12 @@ async fn test_post_update_atomic() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 2, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0) + 1,
DEFAULT_TREASURY_ID,
)
.await;
assert_treasury_balance(&mut program_simulator, 0, SECONDARY_TREASURY_ID).await;

price_update_account = program_simulator
Expand Down Expand Up @@ -155,8 +166,18 @@ async fn test_post_update_atomic() {
.await
.unwrap();

assert_treasury_balance(&mut program_simulator, 2, DEFAULT_TREASURY_ID).await;
assert_treasury_balance(&mut program_simulator, 1, SECONDARY_TREASURY_ID).await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0) + 1,
DEFAULT_TREASURY_ID,
)
.await;
assert_treasury_balance(
&mut program_simulator,
Rent::default().minimum_balance(0),
SECONDARY_TREASURY_ID,
)
.await;

price_update_account = program_simulator
.get_anchor_account_data::<PriceUpdateV1>(price_update_keypair.pubkey())
Expand Down
Loading