Skip to content

Commit

Permalink
remove lamport transfer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Jan 6, 2024
1 parent d3ec732 commit be4f527
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 69 deletions.
14 changes: 2 additions & 12 deletions record/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use {
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
system_program,
},
std::mem::size_of,
};
Expand Down Expand Up @@ -62,9 +61,7 @@ pub enum RecordInstruction<'a> {
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The record account to reallocate
/// 1. `[signer, writable]` The payer account to fund reallocation
/// 2. `[]` System program for reallocation funding
/// 3. `[signer]` The account's owner
/// 1. `[signer]` The account's owner
Reallocate {
/// The length of the data to hold in the record account excluding meta
/// data
Expand Down Expand Up @@ -193,18 +190,11 @@ pub fn close_account(record_account: &Pubkey, signer: &Pubkey, receiver: &Pubkey
}

/// Create a `RecordInstruction::Reallocate` instruction
pub fn reallocate(
record_account: &Pubkey,
payer: &Pubkey,
signer: &Pubkey,
data_length: u64,
) -> Instruction {
pub fn reallocate(record_account: &Pubkey, signer: &Pubkey, data_length: u64) -> Instruction {
Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new(*record_account, false),
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(*signer, true),
],
data: RecordInstruction::Reallocate { data_length }.pack(),
Expand Down
23 changes: 0 additions & 23 deletions record/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use {
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program::invoke,
program_error::ProgramError,
program_pack::IsInitialized,
pubkey::Pubkey,
system_instruction,
sysvar::{rent::Rent, Sysvar},
},
spl_pod::bytemuck::{pod_from_bytes, pod_from_bytes_mut, pod_get_packed_len},
};
Expand Down Expand Up @@ -139,8 +136,6 @@ pub fn process_instruction(
RecordInstruction::Reallocate { data_length } => {
msg!("RecordInstruction::Reallocate");
let data_info = next_account_info(account_info_iter)?;
let payer_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;
let authority_info = next_account_info(account_info_iter)?;

{
Expand Down Expand Up @@ -178,24 +173,6 @@ pub fn process_instruction(
.unwrap(),
);
data_info.realloc(needed_account_length, false)?;

// if additional lamports needed to remain rent-exempt, transfer them
let current_lamport_reserve = data_info.lamports();
let rent = Rent::get()?;
let new_rent_exempt_reserve = rent.minimum_balance(needed_account_length);

let lamports_diff = new_rent_exempt_reserve.saturating_sub(current_lamport_reserve);
if lamports_diff > 0 {
invoke(
&system_instruction::transfer(payer_info.key, data_info.key, lamports_diff),
&[
payer_info.clone(),
data_info.clone(),
system_program_info.clone(),
],
)?;
}

Ok(())
}
}
Expand Down
89 changes: 55 additions & 34 deletions record/program/tests/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
instruction::{AccountMeta, Instruction, InstructionError},
pubkey::Pubkey,
rent::Rent,
system_instruction, system_program,
system_instruction,
},
solana_program_test::*,
solana_sdk::{
Expand Down Expand Up @@ -533,13 +533,19 @@ async fn reallocate_success() {
.checked_add(new_data_length as usize)
.unwrap();

let delta_account_data_length = new_data_length.saturating_sub(data.len() as u64);
let additional_lamports_needed =
Rent::default().minimum_balance(delta_account_data_length as usize);

let transaction = Transaction::new_signed_with_payer(
&[instruction::reallocate(
&account.pubkey(),
&context.payer.pubkey(),
&authority.pubkey(),
new_data_length,
)],
&[
instruction::reallocate(&account.pubkey(), &authority.pubkey(), new_data_length),
system_instruction::transfer(
&context.payer.pubkey(),
&account.pubkey(),
additional_lamports_needed,
),
],
Some(&context.payer.pubkey()),
&[&context.payer, &authority],
context.last_blockhash,
Expand All @@ -564,7 +570,6 @@ async fn reallocate_success() {
let transaction = Transaction::new_signed_with_payer(
&[instruction::reallocate(
&account.pubkey(),
&context.payer.pubkey(),
&authority.pubkey(),
old_data_length,
)],
Expand Down Expand Up @@ -598,22 +603,30 @@ async fn reallocate_fail_wrong_authority() {
initialize_storage_account(&mut context, &authority, &account, data).await;

let new_data_length = 16u64;
let delta_account_data_length = new_data_length.saturating_sub(data.len() as u64);
let additional_lamports_needed =
Rent::default().minimum_balance(delta_account_data_length as usize);

let wrong_authority = Keypair::new();
let transaction = Transaction::new_signed_with_payer(
&[Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new(account.pubkey(), false),
AccountMeta::new(context.payer.pubkey(), true),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new(wrong_authority.pubkey(), true),
],
data: instruction::RecordInstruction::Reallocate {
data_length: new_data_length,
}
.pack(),
}],
&[
Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new(account.pubkey(), false),
AccountMeta::new(wrong_authority.pubkey(), true),
],
data: instruction::RecordInstruction::Reallocate {
data_length: new_data_length,
}
.pack(),
},
system_instruction::transfer(
&context.payer.pubkey(),
&account.pubkey(),
additional_lamports_needed,
),
],
Some(&context.payer.pubkey()),
&[&context.payer, &wrong_authority],
context.last_blockhash,
Expand Down Expand Up @@ -643,21 +656,29 @@ async fn reallocate_fail_unsigned() {
initialize_storage_account(&mut context, &authority, &account, data).await;

let new_data_length = 16u64;
let delta_account_data_length = new_data_length.saturating_sub(data.len() as u64);
let additional_lamports_needed =
Rent::default().minimum_balance(delta_account_data_length as usize);

let transaction = Transaction::new_signed_with_payer(
&[Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new(account.pubkey(), false),
AccountMeta::new(context.payer.pubkey(), true),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new(authority.pubkey(), false),
],
data: instruction::RecordInstruction::Reallocate {
data_length: new_data_length,
}
.pack(),
}],
&[
Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new(account.pubkey(), false),
AccountMeta::new(authority.pubkey(), false),
],
data: instruction::RecordInstruction::Reallocate {
data_length: new_data_length,
}
.pack(),
},
system_instruction::transfer(
&context.payer.pubkey(),
&account.pubkey(),
additional_lamports_needed,
),
],
Some(&context.payer.pubkey()),
&[&context.payer],
context.last_blockhash,
Expand Down

0 comments on commit be4f527

Please sign in to comment.