Skip to content

Commit 0fd6a16

Browse files
authored
Make PDA init simpler (#317)
1 parent 90ba997 commit 0fd6a16

File tree

3 files changed

+19
-64
lines changed

3 files changed

+19
-64
lines changed

program/rust/src/accounts.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use {
99
check_valid_fresh_account,
1010
get_rent,
1111
pyth_assert,
12-
send_lamports,
1312
try_convert,
1413
},
1514
},
@@ -23,10 +22,7 @@ use {
2322
program_error::ProgramError,
2423
program_memory::sol_memset,
2524
pubkey::Pubkey,
26-
system_instruction::{
27-
allocate,
28-
assign,
29-
},
25+
system_instruction::create_account,
3026
},
3127
std::{
3228
borrow::BorrowMut,
@@ -107,7 +103,9 @@ pub trait PythAccount: Pod {
107103
load_account_as_mut::<Self>(account)
108104
}
109105

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

121-
if account.lamports() < target_rent {
122-
send_lamports(
119+
if account.data_len() == 0 {
120+
create(
123121
funding_account,
124122
account,
125123
system_program,
126-
target_rent - account.lamports(),
124+
program_id,
125+
Self::MINIMUM_SIZE,
126+
target_rent,
127+
seeds,
127128
)?;
128-
}
129-
130-
if account.data_len() == 0 {
131-
allocate_data(account, system_program, Self::MINIMUM_SIZE, seeds)?;
132-
assign_owner(account, program_id, system_program, seeds)?;
133129
Self::initialize(account, version)?;
134130
}
131+
135132
Ok(())
136133
}
137134
}
138135

139-
/// Given an already empty `AccountInfo`, allocate the data field to the given size. This make no
140-
/// assumptions about owner.
141-
fn allocate_data<'a>(
142-
account: &AccountInfo<'a>,
136+
fn create<'a>(
137+
from: &AccountInfo<'a>,
138+
to: &AccountInfo<'a>,
143139
system_program: &AccountInfo<'a>,
144-
space: usize,
145-
seeds: &[&[u8]],
146-
) -> Result<(), ProgramError> {
147-
let allocate_instruction = allocate(account.key, try_convert(space)?);
148-
invoke_signed(
149-
&allocate_instruction,
150-
&[account.clone(), system_program.clone()],
151-
&[seeds],
152-
)?;
153-
Ok(())
154-
}
155-
156-
/// Given a newly created `AccountInfo`, assign the owner to the given program id.
157-
fn assign_owner<'a>(
158-
account: &AccountInfo<'a>,
159140
owner: &Pubkey,
160-
system_program: &AccountInfo<'a>,
141+
space: usize,
142+
lamports: u64,
161143
seeds: &[&[u8]],
162144
) -> Result<(), ProgramError> {
163-
let assign_instruction = assign(account.key, owner);
145+
let create_instruction = create_account(from.key, to.key, lamports, try_convert(space)?, owner);
164146
invoke_signed(
165-
&assign_instruction,
166-
&[account.clone(), system_program.clone()],
147+
&create_instruction,
148+
&[from.clone(), to.clone(), system_program.clone()],
167149
&[seeds],
168150
)?;
169151
Ok(())

program/rust/src/tests/test_upd_permissions.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ async fn test_upd_permissions() {
3636
let mut data_curation_authority = Pubkey::new_unique();
3737
let mut security_authority = Pubkey::new_unique();
3838

39-
// Airdrop some lamports to check whether someone can DDOS the account
40-
let permissions_pubkey = sim.get_permissions_pubkey();
41-
sim.airdrop(&permissions_pubkey, Rent::default().minimum_balance(0))
42-
.await
43-
.unwrap();
44-
let permission_account = sim.get_account(permissions_pubkey).await.unwrap();
45-
assert_eq!(
46-
permission_account.lamports,
47-
Rent::default().minimum_balance(0)
48-
);
49-
5039
// Should fail because payer is not the authority
5140
assert_eq!(
5241
sim.upd_permissions(

program/rust/src/utils.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ use {
2525
solana_program::{
2626
account_info::AccountInfo,
2727
bpf_loader_upgradeable,
28-
program::invoke,
2928
program_error::ProgramError,
3029
pubkey::Pubkey,
31-
system_instruction::transfer,
3230
sysvar::rent::Rent,
3331
},
3432
std::cell::Ref,
@@ -263,20 +261,6 @@ pub fn check_is_upgrade_authority_for_program(
263261
Ok(())
264262
}
265263

266-
pub fn send_lamports<'a>(
267-
from: &AccountInfo<'a>,
268-
to: &AccountInfo<'a>,
269-
system_program: &AccountInfo<'a>,
270-
amount: u64,
271-
) -> Result<(), ProgramError> {
272-
let transfer_instruction = transfer(from.key, to.key, amount);
273-
invoke(
274-
&transfer_instruction,
275-
&[from.clone(), to.clone(), system_program.clone()],
276-
)?;
277-
Ok(())
278-
}
279-
280264
#[cfg(not(test))]
281265
pub fn get_rent() -> Result<Rent, ProgramError> {
282266
use solana_program::sysvar::Sysvar;

0 commit comments

Comments
 (0)