Skip to content

Commit

Permalink
refactor: remove transfer to save contract size
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Aug 6, 2024
1 parent 38ada8a commit 6ff3f80
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 69 deletions.
23 changes: 4 additions & 19 deletions program/rust/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use {
},
solana_program::{
entrypoint::ProgramResult,
program::invoke,
pubkey::Pubkey,
system_instruction,
sysvar::slot_history::AccountInfo,
},
};
Expand Down Expand Up @@ -110,29 +108,16 @@ pub fn process_instruction(
}

fn reserve_new_price_feed_index<'a>(
funding_account: &AccountInfo<'a>,
permissions_account: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
) -> Result<u32, ProgramError> {
if permissions_account.data_len() < PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX {
let new_size = PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX;
let rent = Rent::get()?;
let new_minimum_balance = rent.minimum_balance(new_size);
let lamports_diff = new_minimum_balance.saturating_sub(permissions_account.lamports());
if lamports_diff > 0 {
invoke(
&system_instruction::transfer(
funding_account.key,
permissions_account.key,
lamports_diff,
),
&[
funding_account.clone(),
permissions_account.clone(),
system_program.clone(),
],
)?;
}
pyth_assert(
permissions_account.lamports() >= new_minimum_balance,
ProgramError::AccountNotRentExempt,
)?;

permissions_account.realloc(new_size, true)?;
let mut header = load_account_as_mut::<AccountHeader>(permissions_account)?;
Expand Down
17 changes: 5 additions & 12 deletions program/rust/src/processor/add_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use {
// account[1] product account [writable]
// account[2] new price account [writable]
// account[3] permissions account [writable]
// account[4] system program account []
pub fn add_price(
program_id: &Pubkey,
accounts: &[AccountInfo],
Expand All @@ -52,11 +51,10 @@ pub fn add_price(
)?;


let (funding_account, product_account, price_account, permissions_account, system_program) =
match accounts {
[x, y, z, p, q] => Ok((x, y, z, p, q)),
_ => Err(OracleError::InvalidNumberOfAccounts),
}?;
let (funding_account, product_account, price_account, permissions_account) = match accounts {
[x, y, z, p] => Ok((x, y, z, p)),
_ => Err(OracleError::InvalidNumberOfAccounts),
}?;

check_valid_funding_account(funding_account)?;
check_permissioned_funding_account(
Expand All @@ -74,10 +72,6 @@ pub fn add_price(
&cmd_args.header,
)?;
check_valid_writable_account(program_id, permissions_account)?;
pyth_assert(
solana_program::system_program::check_id(system_program.key),
OracleError::InvalidSystemAccount.into(),
)?;

let mut product_data =
load_checked::<ProductAccount>(product_account, cmd_args.header.version)?;
Expand All @@ -89,8 +83,7 @@ pub fn add_price(
price_data.product_account = *product_account.key;
price_data.next_price_account = product_data.first_price_account;
price_data.min_pub_ = PRICE_ACCOUNT_DEFAULT_MIN_PUB;
price_data.feed_index =
reserve_new_price_feed_index(funding_account, permissions_account, system_program)?;
price_data.feed_index = reserve_new_price_feed_index(permissions_account)?;
product_data.first_price_account = *price_account.key;

Ok(())
Expand Down
12 changes: 3 additions & 9 deletions program/rust/src/processor/init_price_feed_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use {
// account[0] funding account [signer writable]
// account[1] price account [writable]
// account[2] permissions account [writable]
// account[3] system program account []
pub fn init_price_feed_index(
program_id: &Pubkey,
accounts: &[AccountInfo],
Expand All @@ -41,8 +40,8 @@ pub fn init_price_feed_index(
ProgramError::InvalidArgument,
)?;

let (funding_account, price_account, permissions_account, system_program) = match accounts {
[x, y, p, z] => Ok((x, y, p, z)),
let (funding_account, price_account, permissions_account) = match accounts {
[x, y, p] => Ok((x, y, p)),
_ => Err(OracleError::InvalidNumberOfAccounts),
}?;

Expand All @@ -55,18 +54,13 @@ pub fn init_price_feed_index(
cmd,
)?;
check_valid_writable_account(program_id, permissions_account)?;
pyth_assert(
solana_program::system_program::check_id(system_program.key),
OracleError::InvalidSystemAccount.into(),
)?;

let mut price_account_data = load_checked::<PriceAccount>(price_account, cmd.version)?;
pyth_assert(
price_account_data.feed_index == 0,
OracleError::FeedIndexAlreadyInitialized.into(),
)?;
price_account_data.feed_index =
reserve_new_price_feed_index(funding_account, permissions_account, system_program)?;
price_account_data.feed_index = reserve_new_price_feed_index(permissions_account)?;

Ok(())
}
1 change: 0 additions & 1 deletion program/rust/src/tests/pyth_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ impl PythSimulator {
AccountMeta::new(product_keypair.pubkey(), true),
AccountMeta::new(price_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
AccountMeta::new(system_program::id(), false),
],
);

Expand Down
9 changes: 0 additions & 9 deletions program/rust/src/tests/test_add_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ fn test_add_price() {
let mut permissions_setup = AccountSetup::new_permission(&program_id);
let permissions_account = permissions_setup.as_account_info();

let mut system_program = AccountSetup::new_system_program();
let system_program_account = system_program.as_account_info();

{
let mut permissions_account_data =
PermissionAccount::initialize(&permissions_account, PC_VERSION).unwrap();
Expand Down Expand Up @@ -92,7 +89,6 @@ fn test_add_price() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
instruction_data_add_price,
)
Expand All @@ -116,7 +112,6 @@ fn test_add_price() {
product_account.clone(),
price_account_2.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
instruction_data_add_price,
)
Expand All @@ -142,7 +137,6 @@ fn test_add_price() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
permissions_account.clone(),
],
instruction_data_add_price
Expand All @@ -159,7 +153,6 @@ fn test_add_price() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
instruction_data_add_price
),
Expand All @@ -184,7 +177,6 @@ fn test_add_price() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
instruction_data_add_price
),
Expand All @@ -210,7 +202,6 @@ fn test_add_price() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
instruction_data_add_price
),
Expand Down
5 changes: 0 additions & 5 deletions program/rust/src/tests/test_permission_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ fn test_permission_migration() {
let mut price_account = price_setup.as_account_info();
PriceAccount::initialize(&price_account, PC_VERSION).unwrap();

let mut system_program = AccountSetup::new_system_program();
let system_program_account = system_program.as_account_info();


product_account.is_signer = false;
mapping_account.is_signer = false;
price_account.is_signer = false;
Expand Down Expand Up @@ -155,7 +151,6 @@ fn test_permission_migration() {
product_account.clone(),
price_account.clone(),
permissions_account.clone(),
system_program_account.clone(),
],
bytes_of::<AddPriceArgs>(&AddPriceArgs {
header: AddPrice.into(),
Expand Down
14 changes: 0 additions & 14 deletions program/rust/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,6 @@ impl AccountSetup {
}
}

pub fn new_system_program() -> Self {
let key = system_program::id();
let owner = system_program::id(); //?
let balance = Rent::minimum_balance(&Rent::default(), 0);
let data = [0u8; UPPER_BOUND_OF_ALL_ACCOUNT_SIZES];
AccountSetup {
key,
owner,
balance,
size: 0,
data,
}
}

pub fn as_account_info(&mut self) -> AccountInfo {
AccountInfo::new(
&self.key,
Expand Down

0 comments on commit 6ff3f80

Please sign in to comment.