Skip to content

Commit db0a31b

Browse files
committed
fix: cpi tests
1 parent 6c8fb10 commit db0a31b

File tree

6 files changed

+185
-22
lines changed

6 files changed

+185
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program-tests/create-address-test-program/src/lib.rs

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use anchor_lang::{
77
solana_program::{instruction::Instruction, pubkey::Pubkey},
88
InstructionData,
99
};
10-
use light_sdk::{cpi::CpiSigner, derive_light_cpi_signer};
10+
use light_sdk::{
11+
cpi::{CpiAccountsSmall, CpiSigner},
12+
derive_light_cpi_signer,
13+
error::LightSdkError,
14+
};
15+
use light_sdk_types::{CompressionCpiAccountIndexSmall, PROGRAM_ACCOUNTS_LEN};
1116
use light_system_program::utils::get_registered_program_pda;
1217
pub mod create_pda;
1318
pub use create_pda::*;
@@ -17,8 +22,8 @@ use light_compressed_account::instruction_data::{
1722
use light_sdk::{
1823
constants::LIGHT_SYSTEM_PROGRAM_ID,
1924
cpi::{
20-
get_account_metas_from_config, invoke_light_system_program, to_account_metas_small,
21-
CpiAccountsConfig, CpiInstructionConfig,
25+
get_account_metas_from_config, invoke_light_system_program, CpiAccountsConfig,
26+
CpiInstructionConfig,
2227
},
2328
};
2429

@@ -59,6 +64,7 @@ pub mod system_cpi_test {
5964
config: CpiAccountsConfig,
6065
small_ix: bool,
6166
inputs: Vec<u8>,
67+
write_cpi_contex: bool,
6268
) -> Result<()> {
6369
let fee_payer = ctx.accounts.signer.to_account_info();
6470

@@ -68,8 +74,28 @@ pub mod system_cpi_test {
6874
CpiAccountsSmall::new_with_config(&fee_payer, ctx.remaining_accounts, config);
6975
let account_infos = cpi_accounts.to_account_infos();
7076

71-
let account_metas = to_account_metas_small(cpi_accounts)
72-
.map_err(|_| ErrorCode::AccountNotEnoughKeys)?;
77+
let account_metas = if !write_cpi_contex {
78+
to_account_metas_small(cpi_accounts).map_err(|_| ErrorCode::AccountNotEnoughKeys)?
79+
} else {
80+
let mut account_metas = vec![];
81+
account_metas.push(AccountMeta {
82+
pubkey: *cpi_accounts.fee_payer().key,
83+
is_signer: true,
84+
is_writable: true,
85+
});
86+
account_metas.push(AccountMeta {
87+
pubkey: *ctx.remaining_accounts[1].key,
88+
is_signer: true,
89+
is_writable: false,
90+
});
91+
let account = &ctx.remaining_accounts[2];
92+
account_metas.push(AccountMeta {
93+
pubkey: *account.key,
94+
is_signer: false,
95+
is_writable: true,
96+
});
97+
account_metas
98+
};
7399
(account_infos, account_metas)
74100
} else {
75101
use light_sdk::cpi::CpiAccounts;
@@ -211,11 +237,13 @@ pub fn create_invoke_read_only_account_info_instruction(
211237
config: CpiAccountsConfig,
212238
small: bool,
213239
remaining_accounts: Vec<AccountMeta>,
240+
write_cpi_contex: bool,
214241
) -> Instruction {
215242
let ix_data = crate::instruction::InvokeWithReadOnly {
216243
small_ix: small,
217244
inputs,
218245
config,
246+
write_cpi_contex,
219247
}
220248
.data();
221249
let accounts = crate::accounts::InvokeCpiReadOnly { signer };
@@ -225,3 +253,91 @@ pub fn create_invoke_read_only_account_info_instruction(
225253
data: ix_data,
226254
}
227255
}
256+
// Manual impl for failing tests
257+
pub fn to_account_metas_small(
258+
cpi_accounts: CpiAccountsSmall<'_, '_>,
259+
) -> light_sdk::error::Result<Vec<AccountMeta>> {
260+
// TODO: do a version with a const array instead of vector.
261+
let mut account_metas =
262+
Vec::with_capacity(1 + cpi_accounts.account_infos().len() - PROGRAM_ACCOUNTS_LEN);
263+
264+
account_metas.push(AccountMeta {
265+
pubkey: *cpi_accounts.fee_payer().key,
266+
is_signer: true,
267+
is_writable: true,
268+
});
269+
account_metas.push(AccountMeta {
270+
pubkey: *cpi_accounts.authority()?.key,
271+
is_signer: true,
272+
is_writable: false,
273+
});
274+
275+
account_metas.push(AccountMeta {
276+
pubkey: *cpi_accounts.registered_program_pda()?.key,
277+
is_signer: false,
278+
is_writable: false,
279+
});
280+
account_metas.push(AccountMeta {
281+
pubkey: *cpi_accounts.account_compression_authority()?.key,
282+
is_signer: false,
283+
is_writable: false,
284+
});
285+
286+
account_metas.push(AccountMeta {
287+
pubkey: *cpi_accounts.account_compression_program()?.key,
288+
is_signer: false,
289+
is_writable: false,
290+
});
291+
292+
account_metas.push(AccountMeta {
293+
pubkey: *cpi_accounts.system_program()?.key,
294+
is_signer: false,
295+
is_writable: false,
296+
});
297+
298+
let accounts = cpi_accounts.account_infos();
299+
let mut index = CompressionCpiAccountIndexSmall::SolPoolPda as usize;
300+
301+
if cpi_accounts.config().sol_pool_pda {
302+
let account = cpi_accounts.get_account_info(index)?;
303+
account_metas.push(AccountMeta {
304+
pubkey: *account.key,
305+
is_signer: false,
306+
is_writable: true,
307+
});
308+
index += 1;
309+
}
310+
311+
if cpi_accounts.config().sol_compression_recipient {
312+
let account = cpi_accounts.get_account_info(index)?;
313+
account_metas.push(AccountMeta {
314+
pubkey: *account.key,
315+
is_signer: false,
316+
is_writable: true,
317+
});
318+
index += 1;
319+
}
320+
if cpi_accounts.config().cpi_context {
321+
let account = cpi_accounts.get_account_info(index)?;
322+
account_metas.push(AccountMeta {
323+
pubkey: *account.key,
324+
is_signer: false,
325+
is_writable: true,
326+
});
327+
index += 1;
328+
}
329+
assert_eq!(cpi_accounts.system_accounts_end_offset(), index);
330+
331+
let tree_accounts = accounts
332+
.get(index..)
333+
.ok_or(LightSdkError::CpiAccountsIndexOutOfBounds(index))?;
334+
tree_accounts.iter().for_each(|acc| {
335+
account_metas.push(AccountMeta {
336+
pubkey: *acc.key,
337+
is_signer: false,
338+
is_writable: true,
339+
});
340+
});
341+
342+
Ok(account_metas)
343+
}

program-tests/system-cpi-test/tests/test.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "test-sbf")]
1+
//#![cfg(feature = "test-sbf")]
22

33
use account_compression::errors::AccountCompressionErrorCode;
44
use anchor_lang::{AnchorDeserialize, AnchorSerialize};
@@ -1258,6 +1258,15 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
12581258
)
12591259
.await
12601260
.unwrap();
1261+
println!(
1262+
"approve accounts: {:?}",
1263+
test_indexer
1264+
.get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None)
1265+
.await
1266+
.unwrap()
1267+
.value
1268+
.items
1269+
);
12611270
let compressed_token_data = test_indexer
12621271
.get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None)
12631272
.await
@@ -1287,6 +1296,7 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
12871296
.filter(|x| x.token.delegate.is_some())
12881297
.collect::<Vec<_>>()[0]
12891298
.clone();
1299+
println!("compressed_token_data {:?}", compressed_token_data);
12901300
perform_with_input_accounts(
12911301
&mut test_indexer,
12921302
&mut rpc,
@@ -1299,6 +1309,15 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
12991309
)
13001310
.await
13011311
.unwrap();
1312+
println!(
1313+
"revoke accounts {:?}",
1314+
test_indexer
1315+
.get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None)
1316+
.await
1317+
.unwrap()
1318+
.value
1319+
.items
1320+
);
13021321
let compressed_token_data = test_indexer
13031322
.get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None)
13041323
.await
@@ -1935,6 +1954,8 @@ pub async fn perform_with_input_accounts<R: Rpc, I: Indexer + TestIndexerExtensi
19351954
.merkle_tree_pubkey,
19361955
);
19371956
}
1957+
// CPi context order changed, since it is always 2 accounts in this case it is reversed.
1958+
hashes.reverse();
19381959
let merkle_tree_pubkey = compressed_account.merkle_context.merkle_tree_pubkey;
19391960
let nullifier_pubkey = compressed_account.merkle_context.queue_pubkey;
19401961
let cpi_context = match mode {

program-tests/system-cpi-v2-test/tests/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "test-sbf")]
1+
//#![cfg(feature = "test-sbf")]
22

33
use std::collections::HashMap;
44

program-tests/system-cpi-v2-test/tests/invoke_cpi_with_read_only.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "test-sbf")]
1+
// #![cfg(feature = "test-sbf")]
22

33
mod event;
44

@@ -2149,14 +2149,14 @@ async fn cpi_context_with_read_only() {
21492149
address_queue_pubkey: address_queue.into(),
21502150
address_merkle_tree_pubkey: address_tree.into(),
21512151
address_merkle_tree_root_index: rpc_result.value.get_address_root_indices()[0],
2152-
assigned_account_index: Some(0),
2152+
assigned_account_index: Some(2),
21532153
};
21542154
let new_address_params1 = NewAddressParamsAssigned {
21552155
seed: seed1,
21562156
address_queue_pubkey: address_queue.into(),
21572157
address_merkle_tree_pubkey: address_tree.into(),
21582158
address_merkle_tree_root_index: rpc_result.value.get_address_root_indices()[1],
2159-
assigned_account_index: Some(1),
2159+
assigned_account_index: Some(0),
21602160
};
21612161
let owner_account1 = Pubkey::new_unique();
21622162
// Insert into cpi context.
@@ -2294,6 +2294,12 @@ async fn cpi_context_with_read_only() {
22942294
output_account_balance[0].compressed_account.address,
22952295
Some(address)
22962296
);
2297+
println!("{:?}", address1);
2298+
let account = test_indexer
2299+
.get_compressed_account(address1, None)
2300+
.await
2301+
.unwrap();
2302+
assert_eq!(account.value.owner, owner_account1);
22972303
let output_account_balance =
22982304
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&owner_account1);
22992305
assert_eq!(
@@ -2445,14 +2451,14 @@ async fn cpi_context_with_account_info() {
24452451
address_queue_pubkey: address_queue.into(),
24462452
address_merkle_tree_pubkey: address_tree.into(),
24472453
address_merkle_tree_root_index: rpc_result.value.get_address_root_indices()[0],
2448-
assigned_account_index: Some(0),
2454+
assigned_account_index: Some(2),
24492455
};
24502456
let new_address_params1 = NewAddressParamsAssigned {
24512457
seed: seed1,
24522458
address_queue_pubkey: address_queue.into(),
24532459
address_merkle_tree_pubkey: address_tree.into(),
24542460
address_merkle_tree_root_index: rpc_result.value.get_address_root_indices()[1],
2455-
assigned_account_index: Some(2),
2461+
assigned_account_index: Some(0),
24562462
};
24572463
let owner_account1 = Pubkey::new_unique();
24582464
// Insert into cpi context.
@@ -2614,12 +2620,12 @@ async fn cpi_context_with_account_info() {
26142620
});
26152621
assert_eq!(output_account_balance.len(), 4);
26162622
assert_eq!(
2617-
output_account_balance[0].compressed_account.address,
2623+
output_account_balance[2].compressed_account.address,
26182624
Some(address)
26192625
);
26202626
assert_eq!(output_account_balance[1].compressed_account.address, None);
26212627
assert_eq!(
2622-
output_account_balance[2].compressed_account.address,
2628+
output_account_balance[0].compressed_account.address,
26232629
Some(address1)
26242630
);
26252631
assert_eq!(output_account_balance[3].compressed_account.address, None);
@@ -3131,17 +3137,37 @@ pub mod local_sdk {
31313137
onchain_config.cpi_context = config.cpi_context.is_some();
31323138
onchain_config.sol_pool_pda = config.sol_pool_pda.is_some();
31333139
onchain_config.sol_compression_recipient = config.sol_compression_recipient.is_some();
3134-
let remaining_accounts =
3135-
[get_light_system_account_metas(config), remaining_accounts].concat();
3136-
println!("remaining_accounts {:?}", remaining_accounts);
3140+
let write_into_cpi_context = if let Some(cpi_context) = cpi_context.as_ref() {
3141+
if small_ix {
3142+
cpi_context.first_set_context || cpi_context.set_context
3143+
} else {
3144+
false
3145+
}
3146+
} else {
3147+
false
3148+
};
3149+
let remaining_accounts = if write_into_cpi_context {
3150+
vec![
3151+
AccountMeta::new_readonly(
3152+
SystemAccountPubkeys::default().light_sytem_program,
3153+
false,
3154+
),
3155+
AccountMeta::new(Pubkey::new_from_array(LIGHT_CPI_SIGNER.cpi_signer), false),
3156+
AccountMeta::new(config.cpi_context.unwrap(), false),
3157+
]
3158+
} else {
3159+
[get_light_system_account_metas(config), remaining_accounts].concat()
3160+
};
31373161

31383162
let instruction = create_invoke_read_only_account_info_instruction(
31393163
payer.pubkey(),
31403164
[instruction_discriminator.to_vec(), ix_data].concat(),
31413165
onchain_config,
31423166
small_ix,
31433167
remaining_accounts,
3168+
write_into_cpi_context,
31443169
);
3170+
println!("instruction {:?}", instruction);
31453171
let res = rpc
31463172
.create_and_send_transaction_with_batched_event(
31473173
&[instruction],
@@ -3324,17 +3350,17 @@ pub mod local_sdk {
33243350

33253351
pub fn get_light_system_account_metas(config: SystemAccountMetaConfig) -> Vec<AccountMeta> {
33263352
let cpi_signer = Pubkey::new_from_array(LIGHT_CPI_SIGNER.cpi_signer);
3327-
println!("cpi signer {:?}", cpi_signer);
3353+
33283354
let default_pubkeys = SystemAccountPubkeys::default();
33293355
let mut vec = if config.small_ix {
33303356
// Accounts without noop and self program.
33313357
let vec = vec![
33323358
AccountMeta::new_readonly(default_pubkeys.light_sytem_program, false),
3333-
AccountMeta::new_readonly(default_pubkeys.account_compression_program, false),
3334-
AccountMeta::new_readonly(default_pubkeys.system_program, false),
33353359
AccountMeta::new_readonly(cpi_signer, false),
33363360
AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false),
33373361
AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false),
3362+
AccountMeta::new_readonly(default_pubkeys.account_compression_program, false),
3363+
AccountMeta::new_readonly(default_pubkeys.system_program, false),
33383364
];
33393365
vec
33403366
} else {

sdk-libs/sdk/src/instruction/system_accounts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ impl Default for SystemAccountPubkeys {
6161
)
6262
.0,
6363
noop_program: Pubkey::from(NOOP_PROGRAM_ID),
64-
// TODO: add correct pubkey
65-
sol_pool_pda: Pubkey::default(),
64+
sol_pool_pda: Pubkey::find_program_address(&[b"sol_pool_pda"], &Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID)).0,
6665
}
6766
}
6867
}

0 commit comments

Comments
 (0)