Skip to content

Commit

Permalink
Update mollusk git reference
Browse files Browse the repository at this point in the history
  • Loading branch information
febo committed Jan 14, 2025
1 parent 1910910 commit 4c45f35
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 73 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions program/tests/assert_instruction_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod setup;
use {
mollusk_svm::{result::Check, Mollusk},
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account::{Account as SolanaAccount, ReadableAccount},
program_pack::Pack,
pubkey::Pubkey,
},
Expand All @@ -29,7 +29,7 @@ fn initialize_mint() {
let mint_account = {
let space = Mint::LEN;
let lamports = mollusk.sysvars.rent.minimum_balance(space);
AccountSharedData::new(lamports, space, &id())
SolanaAccount::new(lamports, space, &id())
};

mollusk.process_and_validate_instruction(
Expand Down Expand Up @@ -61,15 +61,15 @@ fn initialize_account() {
let token_account = {
let space = Account::LEN;
let lamports = mollusk.sysvars.rent.minimum_balance(space);
AccountSharedData::new(lamports, space, &id())
SolanaAccount::new(lamports, space, &id())
};

mollusk.process_and_validate_instruction(
&instruction::initialize_account(&id(), &account, &mint, &owner).unwrap(),
&[
(account, token_account),
(mint, mint_account),
(owner, AccountSharedData::default()),
(owner, SolanaAccount::default()),
mollusk.sysvars.keyed_account_for_rent_sysvar(),
],
&[
Expand Down Expand Up @@ -99,7 +99,7 @@ fn mint_to() {
&[
(mint, mint_account),
(account, token_account),
(owner, AccountSharedData::default()),
(owner, SolanaAccount::default()),
],
&[
Check::success(),
Expand Down Expand Up @@ -133,7 +133,7 @@ fn transfer() {
&[
(source, source_token_account),
(destination, destination_token_account),
(owner, AccountSharedData::default()),
(owner, SolanaAccount::default()),
],
&[
Check::success(),
Expand Down Expand Up @@ -165,7 +165,7 @@ fn burn() {
&[
(mint, mint_account),
(account, token_account),
(owner, AccountSharedData::default()),
(owner, SolanaAccount::default()),
],
&[
Check::success(),
Expand Down Expand Up @@ -194,7 +194,7 @@ fn close_account() {
&[
(mint, mint_account),
(account, token_account),
(owner, AccountSharedData::default()),
(owner, SolanaAccount::default()),
],
&[Check::success(), Check::account(&account).closed().build()],
);
Expand Down
102 changes: 60 additions & 42 deletions program/tests/close_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod setup;
use {
mollusk_svm::{result::Check, Mollusk},
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account::{Account as SolanaAccount, ReadableAccount},
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
Expand All @@ -24,44 +24,53 @@ fn success_init_after_close_account() {
let destination = Pubkey::new_unique();
let decimals = 9;

let owner_account = AccountSharedData::new(1_000_000_000, 0, &system_program::id());
let owner_account = SolanaAccount::new(1_000_000_000, 0, &system_program::id());
let mint_account = setup::setup_mint_account(None, None, 0, decimals);
let token_account = setup::setup_token_account(&mint, &owner, 0);

let expected_destination_lamports = token_account.lamports();

mollusk.process_and_validate_instruction_chain(
&[
instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
.unwrap(),
system_instruction::create_account(
&owner,
&account,
1_000_000_000,
Account::LEN as u64,
&spl_token::id(),
(
&instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
.unwrap(),
&[Check::success()],
),
(
&system_instruction::create_account(
&owner,
&account,
1_000_000_000,
Account::LEN as u64,
&spl_token::id(),
),
&[Check::success()],
),
(
&instruction::initialize_account(&spl_token::id(), &account, &mint, &owner)
.unwrap(),
&[
Check::success(),
// Account successfully re-initialized.
Check::account(&account)
.data(setup::setup_token_account(&mint, &owner, 0).data())
.owner(&spl_token::id())
.build(),
// The destination should have the lamports from the closed account.
Check::account(&destination)
.lamports(expected_destination_lamports)
.build(),
],
),
instruction::initialize_account(&spl_token::id(), &account, &mint, &owner).unwrap(),
],
&[
(mint, mint_account),
(account, token_account),
(owner, owner_account),
(destination, AccountSharedData::default()),
(destination, SolanaAccount::default()),
mollusk.sysvars.keyed_account_for_rent_sysvar(),
],
&[
Check::success(),
// Account successfully re-initialized.
Check::account(&account)
.data(setup::setup_token_account(&mint, &owner, 0).data())
.owner(&spl_token::id())
.build(),
// The destination should have the lamports from the closed account.
Check::account(&destination)
.lamports(expected_destination_lamports)
.build(),
],
);
}

Expand All @@ -75,37 +84,46 @@ fn fail_init_after_close_account() {
let destination = Pubkey::new_unique();
let decimals = 9;

let owner_account = AccountSharedData::new(1_000_000_000, 0, &system_program::id());
let owner_account = SolanaAccount::new(1_000_000_000, 0, &system_program::id());
let mint_account = setup::setup_mint_account(None, None, 0, decimals);
let token_account = setup::setup_token_account(&mint, &owner, 0);

let expected_destination_lamports = token_account.lamports();

mollusk.process_and_validate_instruction_chain(
&[
instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
.unwrap(),
system_instruction::transfer(&owner, &account, 1_000_000_000),
instruction::initialize_account(&spl_token::id(), &account, &mint, &owner).unwrap(),
(
&instruction::close_account(&spl_token::id(), &account, &destination, &owner, &[])
.unwrap(),
&[Check::success()],
),
(
&system_instruction::transfer(&owner, &account, 1_000_000_000),
&[Check::success()],
),
(
&instruction::initialize_account(&spl_token::id(), &account, &mint, &owner)
.unwrap(),
&[
Check::err(ProgramError::InvalidAccountData),
// Account not re-initialized.
Check::account(&account)
.lamports(1_000_000_000)
.owner(&system_program::id())
.build(),
// The destination should have the lamports from the closed account.
Check::account(&destination)
.lamports(expected_destination_lamports)
.build(),
],
),
],
&[
(mint, mint_account),
(account, token_account),
(owner, owner_account),
(destination, AccountSharedData::default()),
(destination, SolanaAccount::default()),
mollusk.sysvars.keyed_account_for_rent_sysvar(),
],
&[
Check::err(ProgramError::InvalidAccountData),
// Account not re-initialized.
Check::account(&account)
.lamports(1_000_000_000)
.owner(&system_program::id())
.build(),
// The destination should have the lamports from the closed account.
Check::account(&destination)
.lamports(expected_destination_lamports)
.build(),
],
);
}
15 changes: 5 additions & 10 deletions program/tests/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
},
serial_test::serial,
solana_sdk::{
account::{create_account_for_test, Account as SolanaAccount, AccountSharedData},
account::{create_account_for_test, Account as SolanaAccount},
account_info::{AccountInfo, IntoAccountInfo},
entrypoint::ProgramResult,
instruction::Instruction,
Expand Down Expand Up @@ -54,12 +54,7 @@ fn do_process_instruction(
.accounts
.iter()
.zip(&accounts)
.map(|(account_meta, account)| {
(
account_meta.pubkey,
AccountSharedData::from((*account).clone()),
)
})
.map(|(account_meta, account)| (account_meta.pubkey, (*account).clone()))
.for_each(|(pubkey, account)| {
instruction_accounts.push((pubkey, account));
});
Expand All @@ -70,7 +65,7 @@ fn do_process_instruction(

// Update accounts after the instruction is processed.
for (original, (_, updated)) in accounts.iter_mut().zip(result.resulting_accounts.iter()) {
let account = SolanaAccount::from(updated.clone());
let account = updated.clone();
original.data = account.data;
original.lamports = account.lamports;
original.owner = account.owner;
Expand Down Expand Up @@ -101,7 +96,7 @@ fn do_process_instruction_dups(
executable: account_info.executable,
rent_epoch: account_info.rent_epoch,
};
dedup_accounts.push((*account_info.key, AccountSharedData::from(account)));
dedup_accounts.push((*account_info.key, account));
cached_accounts.insert(account_info.key, account_info);
}
});
Expand All @@ -114,7 +109,7 @@ fn do_process_instruction_dups(
.resulting_accounts
.iter()
.for_each(|(pubkey, account)| {
let account = SolanaAccount::from(account.clone());
let account = account.clone();
let account_info = cached_accounts.get(pubkey).unwrap();
if account.data.is_empty() {
// When the account is closed, the tests expect the data to
Expand Down
17 changes: 7 additions & 10 deletions program/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

use {
solana_sdk::{
account::{Account as SolanaAccount, AccountSharedData},
program_pack::Pack,
pubkey::Pubkey,
rent::Rent,
account::Account as SolanaAccount, program_pack::Pack, pubkey::Pubkey, rent::Rent,
},
spl_token::state::{Account, AccountState, Mint},
};
Expand All @@ -15,7 +12,7 @@ pub fn setup_mint_account(
freeze_authority: Option<&Pubkey>,
supply: u64,
decimals: u8,
) -> AccountSharedData {
) -> SolanaAccount {
let data = {
let mut data = vec![0; Mint::LEN];
let state = Mint {
Expand All @@ -32,15 +29,15 @@ pub fn setup_mint_account(
let space = data.len();
let lamports = Rent::default().minimum_balance(space);

AccountSharedData::from(SolanaAccount {
SolanaAccount {
lamports,
data,
owner: spl_token::id(),
..Default::default()
})
}
}

pub fn setup_token_account(mint: &Pubkey, owner: &Pubkey, amount: u64) -> AccountSharedData {
pub fn setup_token_account(mint: &Pubkey, owner: &Pubkey, amount: u64) -> SolanaAccount {
let data = {
let mut data = vec![0; Account::LEN];
let state = Account {
Expand All @@ -60,10 +57,10 @@ pub fn setup_token_account(mint: &Pubkey, owner: &Pubkey, amount: u64) -> Accoun
let space = data.len();
let lamports = Rent::default().minimum_balance(space);

AccountSharedData::from(SolanaAccount {
SolanaAccount {
lamports,
data,
owner: spl_token::id(),
..Default::default()
})
}
}

0 comments on commit 4c45f35

Please sign in to comment.