Skip to content

Commit

Permalink
system-cpi-test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Jan 11, 2025
1 parent 74cadbd commit 385f845
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions program-tests/system-cpi-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ solana-sdk = { workspace = true }

[dev-dependencies]
solana-program-test = { workspace = true }
light-client = { workspace = true }
light-sdk = { workspace = true }
light-program-test = { workspace = true, features=["devenv"]}
light-test-utils = { workspace = true, features=["devenv"] }
reqwest = "0.11.26"
Expand Down
122 changes: 80 additions & 42 deletions program-tests/system-cpi-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
use account_compression::errors::AccountCompressionErrorCode;
use anchor_lang::{AnchorDeserialize, AnchorSerialize};
use light_batched_merkle_tree::initialize_state_tree::InitStateTreeAccountsInstructionData;
use light_compressed_token::{
process_transfer::InputTokenDataWithContext, token_data::AccountState,
};
use light_client::indexer::Indexer;
use light_compressed_token::process_transfer::InputTokenDataWithContext;
use light_hasher::{Hasher, Poseidon};
use light_program_test::{
indexer::{TestIndexer, TestIndexerExtensions},
test_batch_forester::{
create_batch_update_address_tree_instruction_data_with_proof, perform_batch_append,
},
test_env::{setup_test_programs_with_accounts, EnvAccounts},
};
use light_prover_client::gnark::helpers::{ProverConfig, ProverMode};
use light_registry::account_compression_cpi::sdk::create_batch_update_address_tree_instruction;
use light_sdk::token::{AccountState, TokenDataWithMerkleContext};
use light_system_program::{
errors::SystemProgramError,
sdk::{
Expand All @@ -30,11 +31,14 @@ use light_system_program::{
};
use light_test_utils::{
assert_rpc_error,
conversions::{
program_to_sdk_public_transaction_event,
sdk_to_program_compressed_account_with_merkle_context, sdk_to_program_compressed_proof,
},
e2e_test_env::init_program_test_env,
indexer::TestIndexer,
spl::{create_mint_helper, mint_tokens_helper},
system_program::transfer_compressed_sol_test,
Indexer, RpcConnection, RpcError, TokenDataWithContext,
RpcConnection, RpcError,
};
use light_utils::{hash_to_bn254_field_size_be, UtilsError};
use light_verifier::VerifierError;
Expand Down Expand Up @@ -198,25 +202,25 @@ async fn test_read_only_accounts() {
// account in batched state mt and value vec
let account_in_value_array = e2e_env
.indexer
.get_compressed_accounts_by_owner(&ID)
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| {
x.merkle_context.leaf_index == 101
&& x.merkle_context.merkle_tree_pubkey == env.batched_state_merkle_tree
})
.unwrap()
.clone();
.map(|x| sdk_to_program_compressed_account_with_merkle_context(x.clone()))
.unwrap();

let account_not_in_value_array_and_in_mt = e2e_env
.indexer
.get_compressed_accounts_by_owner(&ID)
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| {
x.merkle_context.leaf_index == 1
&& x.merkle_context.merkle_tree_pubkey == env.batched_state_merkle_tree
})
.unwrap()
.clone();
.map(|x| sdk_to_program_compressed_account_with_merkle_context(x.clone()))
.unwrap();

// 1. functional - 1 read only account proof by index, an create 1 new account
{
Expand Down Expand Up @@ -307,11 +311,11 @@ async fn test_read_only_accounts() {
let data = [4u8; 31];
let account_in_v1_tree = e2e_env
.indexer
.get_compressed_accounts_by_owner(&ID)
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| x.merkle_context.merkle_tree_pubkey == env.merkle_tree_pubkey)
.unwrap()
.clone();
.map(|x| sdk_to_program_compressed_account_with_merkle_context(x.clone()))
.unwrap();
let result = perform_create_pda_with_event(
&mut e2e_env.indexer,
&mut e2e_env.rpc,
Expand Down Expand Up @@ -586,14 +590,14 @@ async fn test_read_only_accounts() {
let data = [5u8; 31];
let input_account_in_mt = e2e_env
.indexer
.get_compressed_accounts_by_owner(&ID)
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| {
x.merkle_context.leaf_index == 2
&& x.merkle_context.merkle_tree_pubkey == env.batched_state_merkle_tree
})
.unwrap()
.clone();
.map(|x| sdk_to_program_compressed_account_with_merkle_context(x.clone()))
.unwrap();
perform_create_pda_with_event(
&mut e2e_env.indexer,
&mut e2e_env.rpc,
Expand All @@ -619,12 +623,13 @@ async fn test_read_only_accounts() {
for i in 31..34 {
let input_account_in_mt = e2e_env
.indexer
.get_compressed_accounts_by_owner(&ID)
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| {
x.merkle_context.leaf_index == i
&& x.merkle_context.merkle_tree_pubkey == env.batched_state_merkle_tree
})
.map(|x| sdk_to_program_compressed_account_with_merkle_context(x.clone()))
.unwrap()
.clone();
input_accounts.push(input_account_in_mt);
Expand Down Expand Up @@ -949,6 +954,8 @@ async fn only_test_create_pda() {
[0]
.compressed_account
.clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
// Failing 4 input account that is not owned by signer ----------------------------------------------
perform_with_input_accounts(
&mut test_indexer,
Expand All @@ -963,7 +970,10 @@ async fn only_test_create_pda() {
.await
.unwrap();
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
// Failing 5 provide cpi context but no cpi context account ----------------------------------------------
perform_with_input_accounts(
&mut test_indexer,
Expand Down Expand Up @@ -1053,7 +1063,10 @@ async fn only_test_create_pda() {
26, 211, 193, 195, 11, 219, 9, 155, 58, 172, 58, 200, 254, 75, 231, 106, 31, 168,
183, 76, 179, 113, 234, 101, 191, 99, 156, 98,
];
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
let keypair = Keypair::from_bytes(&CPI_SYSTEM_TEST_PROGRAM_ID_KEYPAIR).unwrap();
let result = transfer_compressed_sol_test(
&mut rpc,
Expand Down Expand Up @@ -1145,7 +1158,10 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
test_indexer.get_compressed_token_accounts_by_owner(&payer.pubkey())[0].clone();
// 1. Approve functional with cpi context
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
let compressed_token_data =
test_indexer.get_compressed_token_accounts_by_owner(&payer.pubkey())[0].clone();
perform_with_input_accounts(
Expand All @@ -1172,7 +1188,10 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
}
// 2. Revoke functional with cpi context
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
let compressed_token_data = test_indexer
.get_compressed_token_accounts_by_owner(&payer.pubkey())
.iter()
Expand All @@ -1198,7 +1217,10 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
}
// 3. Freeze functional with cpi context
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
let compressed_token_data =
test_indexer.get_compressed_token_accounts_by_owner(&payer.pubkey())[0].clone();
perform_with_input_accounts(
Expand All @@ -1221,7 +1243,10 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
}
// 4. Thaw functional with cpi context
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_account =
sdk_to_program_compressed_account_with_merkle_context(compressed_account);
let compressed_token_data =
test_indexer.get_compressed_token_accounts_by_owner(&payer.pubkey())[0].clone();
perform_with_input_accounts(
Expand All @@ -1243,15 +1268,16 @@ async fn test_approve_revoke_burn_freeze_thaw_with_cpi_context() {
}
// 5. Burn functional with cpi context
{
let compressed_account = test_indexer.get_compressed_accounts_by_owner(&ID)[0].clone();
let compressed_account =
test_indexer.get_compressed_accounts_with_merkle_context_by_owner(&ID)[0].clone();
let compressed_token_data =
test_indexer.get_compressed_token_accounts_by_owner(&payer.pubkey())[0].clone();
perform_with_input_accounts(
&mut test_indexer,
&mut rpc,
&payer,
None,
&compressed_account,
&sdk_to_program_compressed_account_with_merkle_context(compressed_account),
Some(compressed_token_data),
u32::MAX,
WithInputAccountsMode::Burn,
Expand Down Expand Up @@ -1456,8 +1482,11 @@ async fn test_create_pda_in_program_owned_merkle_trees() {
}

#[allow(clippy::too_many_arguments)]
pub async fn perform_create_pda_failing<R: RpcConnection>(
test_indexer: &mut TestIndexer<R>,
pub async fn perform_create_pda_failing<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
test_indexer: &mut I,
rpc: &mut R,
env: &EnvAccounts,
payer: &Keypair,
Expand Down Expand Up @@ -1492,8 +1521,11 @@ pub async fn perform_create_pda_failing<R: RpcConnection>(
}

#[allow(clippy::too_many_arguments)]
pub async fn perform_create_pda_with_event<R: RpcConnection>(
test_indexer: &mut TestIndexer<R>,
pub async fn perform_create_pda_with_event<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
test_indexer: &mut I,
rpc: &mut R,
env: &EnvAccounts,
payer: &Keypair,
Expand Down Expand Up @@ -1529,10 +1561,10 @@ pub async fn perform_create_pda_with_event<R: RpcConnection>(
}

#[allow(clippy::too_many_arguments)]
async fn perform_create_pda<R: RpcConnection>(
async fn perform_create_pda<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
env: &EnvAccounts,
seed: [u8; 32],
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
rpc: &mut R,
data: &[u8; 31],
payer_pubkey: Pubkey,
Expand Down Expand Up @@ -1709,7 +1741,7 @@ async fn perform_create_pda<R: RpcConnection>(
data: *data,
signer: &payer_pubkey,
output_compressed_account_merkle_tree_pubkey,
proof: &rpc_result.proof.unwrap(),
proof: &sdk_to_program_compressed_proof(rpc_result.proof.unwrap()),
new_address_params,
cpi_context_account: &env.cpi_context_account_pubkey,
owner_program,
Expand All @@ -1723,15 +1755,15 @@ async fn perform_create_pda<R: RpcConnection>(
create_pda_instruction(create_ix_inputs)
}

pub async fn assert_created_pda<R: RpcConnection>(
test_indexer: &mut TestIndexer<R>,
pub async fn assert_created_pda<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
seed: &[u8; 32],
data: &[u8; 31],
) {
let compressed_escrow_pda = test_indexer
.compressed_accounts
.get_compressed_accounts_with_merkle_context_by_owner(&ID)
.iter()
.find(|x| x.compressed_account.owner == ID)
.unwrap()
Expand Down Expand Up @@ -1768,13 +1800,16 @@ pub async fn assert_created_pda<R: RpcConnection>(
}

#[allow(clippy::too_many_arguments)]
pub async fn perform_with_input_accounts<R: RpcConnection>(
test_indexer: &mut TestIndexer<R>,
pub async fn perform_with_input_accounts<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
test_indexer: &mut I,
rpc: &mut R,
payer: &Keypair,
fee_payer: Option<&Keypair>,
compressed_account: &CompressedAccountWithMerkleContext,
token_account: Option<TokenDataWithContext>,
token_account: Option<TokenDataWithMerkleContext>,
expected_error_code: u32,
mode: WithInputAccountsMode,
) -> Result<(), RpcError> {
Expand Down Expand Up @@ -1816,7 +1851,7 @@ pub async fn perform_with_input_accounts<R: RpcConnection>(
_ => None,
};
let cpi_context_account_pubkey = test_indexer
.state_merkle_trees
.get_state_merkle_trees()
.iter()
.find(|x| x.accounts.merkle_tree == merkle_tree_pubkey)
.unwrap()
Expand Down Expand Up @@ -1870,7 +1905,7 @@ pub async fn perform_with_input_accounts<R: RpcConnection>(
input_nullifier_pubkey: &nullifier_pubkey,
cpi_context_account: &cpi_context_account_pubkey,
cpi_context,
proof: &rpc_result.proof,
proof: &sdk_to_program_compressed_proof(rpc_result.proof),
compressed_account: &PackedCompressedAccountWithMerkleContext {
compressed_account: compressed_account.compressed_account.clone(),
merkle_context: PackedMerkleContext {
Expand Down Expand Up @@ -1898,7 +1933,10 @@ pub async fn perform_with_input_accounts<R: RpcConnection>(
if expected_error_code == u32::MAX {
let result = result?.unwrap();
let slot: u64 = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &result.0);
test_indexer.add_compressed_accounts_with_token_data(
slot,
&program_to_sdk_public_transaction_event(result.0),
);
Ok(())
} else {
assert_rpc_error(result, 0, expected_error_code)
Expand Down

0 comments on commit 385f845

Please sign in to comment.