Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
165 changes: 71 additions & 94 deletions controller/program/tests/tests/admin/add_lst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use inf1_test_utils::{
acc_bef_aft, any_lst_state_list, any_normal_pk, any_pool_state, assert_diffs_lst_state_list,
assert_jiminy_prog_err, find_pool_reserves_ata, find_protocol_fee_accumulator_ata,
fixtures_accounts_opt_cloned, keys_signer_writable_to_metas, lst_state_list_account, mock_mint,
mock_token_acc, pool_state_account, raw_mint, raw_token_acc, silence_mollusk_logs,
upsert_account, AnyPoolStateArgs, LstStateListChanges, LstStateListData,
NewPoolStateBoolsBuilder, PkAccountTup, PoolStateBools, ALL_FIXTURES, JITOSOL_MINT,
mock_token_acc, mollusk_exec_validate, pool_state_account, raw_mint, raw_token_acc,
silence_mollusk_logs, AccountMap, AnyPoolStateArgs, LstStateListChanges, LstStateListData,
NewPoolStateBoolsBuilder, PoolStateBools, ALL_FIXTURES, JITOSOL_MINT,
};

use jiminy_cpi::program_error::INVALID_ARGUMENT;
Expand Down Expand Up @@ -76,19 +76,19 @@ fn add_lst_ix(keys: &AddLstIxKeysOwned) -> Instruction {
}
}

fn add_lst_fixtures_accounts_opt(keys: &AddLstIxKeysOwned) -> Vec<PkAccountTup> {
fixtures_accounts_opt_cloned(keys.0.iter().copied()).collect()
fn add_lst_fixtures_accounts_opt(keys: &AddLstIxKeysOwned) -> AccountMap {
fixtures_accounts_opt_cloned(keys.0.iter().copied())
}

fn assert_correct_add(
bef: &[PkAccountTup],
aft: &[PkAccountTup],
bef: &AccountMap,
aft: &AccountMap,
mint: &[u8; 32],
token_program: &[u8; 32],
expected_sol_value_calculator: &[u8; 32],
) {
let lamports_bef: u128 = bef.iter().map(|(_, acc)| acc.lamports as u128).sum();
let lamports_aft: u128 = aft.iter().map(|(_, acc)| acc.lamports as u128).sum();
let lamports_bef: u128 = bef.values().map(|acc| acc.lamports as u128).sum();
let lamports_aft: u128 = aft.values().map(|acc| acc.lamports as u128).sum();
assert_eq!(lamports_bef, lamports_aft);

let (_, pool_reserves_bump) = find_pool_reserves_ata(token_program, mint);
Expand Down Expand Up @@ -146,34 +146,30 @@ fn add_lst_jitosol_fixture() {
let ix = add_lst_ix(&keys);
let mut accounts = add_lst_fixtures_accounts_opt(&keys);

upsert_account(
&mut accounts,
(
Pubkey::new_from_array(admin),
Account {
lamports: u64::MAX,
..Default::default()
},
),
accounts.insert(
Pubkey::new_from_array(admin),
Account {
lamports: u64::MAX,
..Default::default()
},
);

upsert_account(
&mut accounts,
(
Pubkey::new_from_array(PROTOCOL_FEE_ID),
Account {
..Default::default()
},
),
accounts.insert(
Pubkey::new_from_array(PROTOCOL_FEE_ID),
Account {
..Default::default()
},
);

let InstructionResult {
program_result,
resulting_accounts,
..
} = SVM.with(|svm| {
svm.process_and_validate_instruction(&ix, &accounts, &[Check::all_rent_exempt()])
});
let (
accounts,
InstructionResult {
program_result,
resulting_accounts,
..
},
) = SVM.with(|svm| mollusk_exec_validate(svm, &ix, &accounts, &[Check::all_rent_exempt()]));
let resulting_accounts: AccountMap = resulting_accounts.into_iter().collect();

assert_eq!(program_result, ProgramResult::Success);

Expand Down Expand Up @@ -205,7 +201,7 @@ fn add_lst_proptest(
mint: [u8; 32],
token_program: [u8; 32],
sol_value_calculator: [u8; 32],
additional_accounts: impl IntoIterator<Item = PkAccountTup>,
additional_accounts: impl IntoIterator<Item = (Pubkey, Account)>,
error_type: Option<TestErrorType>,
) -> TestCaseResult {
silence_mollusk_logs();
Expand All @@ -217,79 +213,60 @@ fn add_lst_proptest(
let ix = add_lst_ix(&keys);
let mut accounts = add_lst_fixtures_accounts_opt(&keys);

// Common upserts
upsert_account(
&mut accounts,
(
LST_STATE_LIST_ID.into(),
lst_state_list_account(lst_state_list),
),
);
upsert_account(
&mut accounts,
(POOL_STATE_ID.into(), pool_state_account(pool)),
// Common inserts
accounts.insert(
LST_STATE_LIST_ID.into(),
lst_state_list_account(lst_state_list),
);
upsert_account(
&mut accounts,
(
Pubkey::new_from_array(admin),
Account {
..Default::default()
},
),
accounts.insert(POOL_STATE_ID.into(), pool_state_account(pool));
accounts.insert(
Pubkey::new_from_array(admin),
Account {
..Default::default()
},
);
upsert_account(
&mut accounts,
(
Pubkey::new_from_array(payer),
Account {
lamports: u64::MAX,
..Default::default()
},
),
accounts.insert(
Pubkey::new_from_array(payer),
Account {
lamports: u64::MAX,
..Default::default()
},
);
upsert_account(
&mut accounts,
(
Pubkey::new_from_array(PROTOCOL_FEE_ID),
Account {
..Default::default()
},
),
accounts.insert(
Pubkey::new_from_array(PROTOCOL_FEE_ID),
Account {
..Default::default()
},
);

let (pool_reserves_addr, _) = find_pool_reserves_ata(&token_program, &mint);
let (protocol_fee_accumulator_addr, _) =
find_protocol_fee_accumulator_ata(&token_program, &mint);

upsert_account(
&mut accounts,
(
pool_reserves_addr,
mock_token_acc(raw_token_acc(mint, POOL_STATE_ID, 0)),
),
accounts.insert(
pool_reserves_addr,
mock_token_acc(raw_token_acc(mint, POOL_STATE_ID, 0)),
);

upsert_account(
&mut accounts,
(
protocol_fee_accumulator_addr,
mock_token_acc(raw_token_acc(mint, PROTOCOL_FEE_ID, 0)),
),
accounts.insert(
protocol_fee_accumulator_addr,
mock_token_acc(raw_token_acc(mint, PROTOCOL_FEE_ID, 0)),
);

// Additional test-specific upserts
additional_accounts
.into_iter()
.for_each(|account| upsert_account(&mut accounts, account));

let InstructionResult {
program_result,
resulting_accounts,
..
} = SVM.with(|svm| {
svm.process_and_validate_instruction(&ix, &accounts, &[Check::all_rent_exempt()])
});
// Additional test-specific inserts
for (pk, acc) in additional_accounts {
accounts.insert(pk, acc);
}

let (
accounts,
InstructionResult {
program_result,
resulting_accounts,
..
},
) = SVM.with(|svm| mollusk_exec_validate(svm, &ix, &accounts, &[Check::all_rent_exempt()]));
let resulting_accounts: AccountMap = resulting_accounts.into_iter().collect();

if let Some(error_type) = error_type {
match error_type {
Expand Down
29 changes: 16 additions & 13 deletions controller/program/tests/tests/admin/lst_input/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use inf1_ctl_jiminy::{
};
use inf1_test_utils::{
acc_bef_aft, any_lst_state, any_pool_state, assert_diffs_lst_state_list,
assert_jiminy_prog_err, dedup_accounts, distinct_idxs, idx_oob, list_sample_flat_map,
lst_state_list_account, mock_mint, mock_sys_acc, pool_state_account, raw_mint, AnyLstStateArgs,
AnyPoolStateArgs, Diff, LstStateArgs, LstStateData, LstStateListChanges, PkAccountTup,
assert_jiminy_prog_err, distinct_idxs, idx_oob, list_sample_flat_map, lst_state_list_account,
mock_mint, mock_sys_acc, mollusk_exec, pool_state_account, raw_mint, AccountMap,
AnyLstStateArgs, AnyPoolStateArgs, Diff, LstStateArgs, LstStateData, LstStateListChanges,
PoolStateBools,
};
use jiminy_cpi::program_error::ProgramError;
Expand All @@ -24,14 +24,19 @@ use crate::common::{MAX_LST_STATES, SVM};
pub fn set_lst_input_test(
expected_is_input_disabled: bool,
ix: &Instruction,
bef: &[PkAccountTup],
bef: &AccountMap,
expected_err: Option<impl Into<ProgramError>>,
) {
let InstructionResult {
program_result,
resulting_accounts: aft,
..
} = SVM.with(|svm| svm.process_instruction(ix, bef));
let (
_,
InstructionResult {
program_result,
resulting_accounts: aft_vec,
..
},
) = SVM.with(|svm| mollusk_exec(svm, ix, bef));

let aft: AccountMap = aft_vec.into_iter().collect();

let [list_bef, list_aft] = acc_bef_aft(&LST_STATE_LIST_ID.into(), bef, &aft).map(|a| {
LstStatePackedList::of_acc_data(&a.data)
Expand Down Expand Up @@ -87,7 +92,7 @@ pub fn set_lst_input_test_accs(
keys: SetLstInputIxKeysOwned,
pool: PoolState,
lst_state_list: Vec<LstState>,
) -> Vec<PkAccountTup> {
) -> AccountMap {
// dont care abt lamports, shouldnt affect anything
const LAMPORTS: u64 = 1_000_000_000;
let accs = NewSetLstInputIxAccsBuilder::start()
Expand All @@ -102,9 +107,7 @@ pub fn set_lst_input_test_accs(
))
.with_pool_state(pool_state_account(pool))
.build();
let mut res = keys.0.into_iter().map(Into::into).zip(accs.0).collect();
dedup_accounts(&mut res);
res
keys.0.into_iter().map(Into::into).zip(accs.0).collect()
}

pub type ToInpTup = (SetLstInputIxKeysOwned, usize, PoolState, Vec<LstStateData>);
Expand Down
20 changes: 10 additions & 10 deletions controller/program/tests/tests/admin/lst_input/disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use inf1_ctl_jiminy::{
};
use inf1_test_utils::{
any_pool_state, gen_pool_state, keys_signer_writable_to_metas, silence_mollusk_logs,
AnyPoolStateArgs, GenPoolStateArgs, LstStateData, PkAccountTup, PoolStateBools, PoolStatePks,
AccountMap, AnyPoolStateArgs, GenPoolStateArgs, LstStateData, PoolStateBools, PoolStatePks,
};
use jiminy_cpi::program_error::{ProgramError, INVALID_ARGUMENT, MISSING_REQUIRED_SIGNATURE};
use proptest::prelude::*;
Expand Down Expand Up @@ -43,7 +43,7 @@ fn disable_lst_input_ix(keys: DisableLstInputIxKeysOwned, idx: usize) -> Instruc

fn disable_lst_input_test(
ix: &Instruction,
bef: &[PkAccountTup],
bef: &AccountMap,
expected_err: Option<impl Into<ProgramError>>,
) {
set_lst_input_test(true, ix, bef, expected_err);
Expand Down Expand Up @@ -83,7 +83,7 @@ fn to_inp(
PoolState,
Vec<LstStateData>,
),
) -> (Instruction, Vec<PkAccountTup>) {
) -> (Instruction, AccountMap) {
(
disable_lst_input_ix(keys, idx),
set_lst_input_test_accs(
Expand All @@ -94,7 +94,7 @@ fn to_inp(
)
}

fn correct_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn correct_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
any_pool_state(AnyPoolStateArgs {
bools: PoolStateBools::normal(),
..Default::default()
Expand All @@ -113,7 +113,7 @@ proptest! {
}
}

fn unauthorized_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn unauthorized_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
any_pool_state(AnyPoolStateArgs {
bools: PoolStateBools::normal(),
..Default::default()
Expand All @@ -132,7 +132,7 @@ proptest! {
}
}

fn missing_sig_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn missing_sig_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
correct_strat().prop_map(|(mut ix, accs)| {
ix.accounts[SET_LST_INPUT_IX_ACCS_IDX_ADMIN].is_signer = false;
(ix, accs)
Expand All @@ -149,7 +149,7 @@ proptest! {
}
}

fn rebalancing_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn rebalancing_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
any_pool_state(AnyPoolStateArgs {
bools: PoolStateBools::normal().with_is_rebalancing(Some(Just(true).boxed())),
..Default::default()
Expand All @@ -172,7 +172,7 @@ proptest! {
}
}

fn pool_disabled_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn pool_disabled_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
any_pool_state(AnyPoolStateArgs {
bools: PoolStateBools::normal().with_is_disabled(Some(Just(true).boxed())),
..Default::default()
Expand All @@ -195,7 +195,7 @@ proptest! {
}
}

fn lst_idx_oob_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn lst_idx_oob_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
lst_idx_oob_to_inp_strat().prop_map(to_inp)
}

Expand All @@ -213,7 +213,7 @@ proptest! {
}
}

fn lst_idx_mismatch_strat() -> impl Strategy<Value = (Instruction, Vec<PkAccountTup>)> {
fn lst_idx_mismatch_strat() -> impl Strategy<Value = (Instruction, AccountMap)> {
lst_idx_mismatch_to_inp_strat().prop_map(to_inp)
}

Expand Down
Loading