Skip to content
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
56 changes: 19 additions & 37 deletions program/rust/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use {
check_valid_fresh_account,
get_rent,
pyth_assert,
send_lamports,
try_convert,
},
},
Expand All @@ -23,10 +22,7 @@ use {
program_error::ProgramError,
program_memory::sol_memset,
pubkey::Pubkey,
system_instruction::{
allocate,
assign,
},
system_instruction::create_account,
},
std::{
borrow::BorrowMut,
Expand Down Expand Up @@ -107,7 +103,9 @@ pub trait PythAccount: Pod {
load_account_as_mut::<Self>(account)
}

// Creates PDA accounts only when needed, and initializes it as one of the Pyth accounts
/// Creates PDA accounts only when needed, and initializes it as one of the Pyth accounts.
/// This PDA initialization assumes that the account has 0 lamports.
/// TO DO: Fix this once we can resize the program.
fn initialize_pda<'a>(
account: &AccountInfo<'a>,
funding_account: &AccountInfo<'a>,
Expand All @@ -118,52 +116,36 @@ pub trait PythAccount: Pod {
) -> Result<(), ProgramError> {
let target_rent = get_rent()?.minimum_balance(Self::MINIMUM_SIZE);

if account.lamports() < target_rent {
send_lamports(
if account.data_len() == 0 {
create(
funding_account,
account,
system_program,
target_rent - account.lamports(),
program_id,
Self::MINIMUM_SIZE,
target_rent,
seeds,
)?;
}

if account.data_len() == 0 {
allocate_data(account, system_program, Self::MINIMUM_SIZE, seeds)?;
assign_owner(account, program_id, system_program, seeds)?;
Self::initialize(account, version)?;
}

Ok(())
}
}

/// Given an already empty `AccountInfo`, allocate the data field to the given size. This make no
/// assumptions about owner.
fn allocate_data<'a>(
account: &AccountInfo<'a>,
fn create<'a>(
from: &AccountInfo<'a>,
to: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
space: usize,
seeds: &[&[u8]],
) -> Result<(), ProgramError> {
let allocate_instruction = allocate(account.key, try_convert(space)?);
invoke_signed(
&allocate_instruction,
&[account.clone(), system_program.clone()],
&[seeds],
)?;
Ok(())
}

/// Given a newly created `AccountInfo`, assign the owner to the given program id.
fn assign_owner<'a>(
account: &AccountInfo<'a>,
owner: &Pubkey,
system_program: &AccountInfo<'a>,
space: usize,
lamports: u64,
seeds: &[&[u8]],
) -> Result<(), ProgramError> {
let assign_instruction = assign(account.key, owner);
let create_instruction = create_account(from.key, to.key, lamports, try_convert(space)?, owner);
invoke_signed(
&assign_instruction,
&[account.clone(), system_program.clone()],
&create_instruction,
&[from.clone(), to.clone(), system_program.clone()],
&[seeds],
)?;
Ok(())
Expand Down
11 changes: 0 additions & 11 deletions program/rust/src/tests/test_upd_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ async fn test_upd_permissions() {
let mut data_curation_authority = Pubkey::new_unique();
let mut security_authority = Pubkey::new_unique();

// Airdrop some lamports to check whether someone can DDOS the account
let permissions_pubkey = sim.get_permissions_pubkey();
sim.airdrop(&permissions_pubkey, Rent::default().minimum_balance(0))
.await
.unwrap();
let permission_account = sim.get_account(permissions_pubkey).await.unwrap();
assert_eq!(
permission_account.lamports,
Rent::default().minimum_balance(0)
);

// Should fail because payer is not the authority
assert_eq!(
sim.upd_permissions(
Expand Down
16 changes: 0 additions & 16 deletions program/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ use {
solana_program::{
account_info::AccountInfo,
bpf_loader_upgradeable,
program::invoke,
program_error::ProgramError,
pubkey::Pubkey,
system_instruction::transfer,
sysvar::rent::Rent,
},
std::cell::Ref,
Expand Down Expand Up @@ -263,20 +261,6 @@ pub fn check_is_upgrade_authority_for_program(
Ok(())
}

pub fn send_lamports<'a>(
from: &AccountInfo<'a>,
to: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
amount: u64,
) -> Result<(), ProgramError> {
let transfer_instruction = transfer(from.key, to.key, amount);
invoke(
&transfer_instruction,
&[from.clone(), to.clone(), system_program.clone()],
)?;
Ok(())
}

#[cfg(not(test))]
pub fn get_rent() -> Result<Rent, ProgramError> {
use solana_program::sysvar::Sysvar;
Expand Down