From eb91e1a7eee2c98c3b576809d120ce2664312081 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Thu, 9 Jan 2025 19:29:13 +0000 Subject: [PATCH] format --- .../programs/token-escrow/tests/test.rs | 57 +++++++--- .../token-escrow/tests/test_compressed_pda.rs | 100 ++++++++++-------- forester/src/batch_processor/common.rs | 6 +- forester/tests/batched_address_test.rs | 14 ++- forester/tests/batched_state_test.rs | 8 +- forester/tests/e2e_test.rs | 9 +- forester/tests/test_utils.rs | 33 +++--- sdk-libs/client/src/rpc/solana_rpc.rs | 5 +- 8 files changed, 136 insertions(+), 96 deletions(-) diff --git a/examples/token-escrow/programs/token-escrow/tests/test.rs b/examples/token-escrow/programs/token-escrow/tests/test.rs index b6caecc03..7ff502fa2 100644 --- a/examples/token-escrow/programs/token-escrow/tests/test.rs +++ b/examples/token-escrow/programs/token-escrow/tests/test.rs @@ -10,12 +10,20 @@ // - create escrow pda and just prove that utxo exists -> read utxo from compressed token account // release compressed tokens +use light_client::indexer::Indexer; use light_hasher::Poseidon; -use light_program_test::test_env::{setup_test_programs_with_accounts, EnvAccounts}; +use light_program_test::{ + indexer::{TestIndexer, TestIndexerExtensions}, + test_env::{setup_test_programs_with_accounts, EnvAccounts}, +}; use light_prover_client::gnark::helpers::{ProofType, ProverConfig}; use light_system_program::sdk::{compressed_account::MerkleContext, event::PublicTransactionEvent}; use light_test_utils::{ airdrop_lamports, assert_rpc_error, + conversions::{ + program_to_sdk_public_transaction_event, sdk_to_program_compressed_account, + sdk_to_program_compressed_proof, sdk_to_program_token_data, + }, spl::{create_mint_helper, mint_tokens_helper}, FeeConfig, RpcConnection, RpcError, TransactionParams, }; @@ -24,9 +32,6 @@ use solana_sdk::{ instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer, transaction::Transaction, }; -use light_client::indexer::Indexer; -use light_program_test::indexer::{TestIndexer, TestIndexerExtensions}; -use light_test_utils::conversions::{program_to_sdk_public_transaction_event, sdk_to_program_compressed_account, sdk_to_program_compressed_proof, sdk_to_program_token_data}; use token_escrow::{ escrow_with_compressed_pda::sdk::get_token_owner_pda, escrow_with_pda::sdk::{ @@ -283,7 +288,10 @@ pub async fn perform_escrow + TestIndexerExtensi create_escrow_instruction(create_ix_inputs, *escrow_amount) } -pub async fn perform_escrow_with_event + TestIndexerExtensions>( +pub async fn perform_escrow_with_event< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( rpc: &mut R, test_indexer: &mut I, env: &EnvAccounts, @@ -313,7 +321,10 @@ pub async fn perform_escrow_with_event + TestInd .await? .unwrap(); let slot = rpc.get_slot().await.unwrap(); - test_indexer.add_compressed_accounts_with_token_data(slot, &program_to_sdk_public_transaction_event(event.0)); + test_indexer.add_compressed_accounts_with_token_data( + slot, + &program_to_sdk_public_transaction_event(event.0), + ); Ok(()) } @@ -336,7 +347,7 @@ pub async fn perform_escrow_failing + TestIndexe rpc.process_transaction(transaction).await } -pub async fn assert_escrow + TestIndexerExtensions> ( +pub async fn assert_escrow + TestIndexerExtensions>( rpc: &mut R, test_indexer: &I, payer_pubkey: &Pubkey, @@ -355,8 +366,10 @@ pub async fn assert_escrow + TestIndexerExtensio assert_eq!(token_data_escrow.amount, escrow_amount); assert_eq!(token_data_escrow.owner, token_owner_pda); - let token_data_change_compressed_token_account = - test_indexer.get_token_compressed_accounts()[0].token_data.clone(); + let token_data_change_compressed_token_account = test_indexer.get_token_compressed_accounts() + [0] + .token_data + .clone(); assert_eq!( token_data_change_compressed_token_account.amount, amount - escrow_amount @@ -449,7 +462,10 @@ pub async fn perform_withdrawal + TestIndexerExt create_withdrawal_escrow_instruction(create_ix_inputs, *withdrawal_amount) } -pub async fn perform_withdrawal_with_event + TestIndexerExtensions>( +pub async fn perform_withdrawal_with_event< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( rpc: &mut R, test_indexer: &mut I, env: &EnvAccounts, @@ -476,11 +492,17 @@ pub async fn perform_withdrawal_with_event + Tes .await? .unwrap(); let slot = rpc.get_slot().await.unwrap(); - test_indexer.add_compressed_accounts_with_token_data(slot, &program_to_sdk_public_transaction_event(event.0)); + test_indexer.add_compressed_accounts_with_token_data( + slot, + &program_to_sdk_public_transaction_event(event.0), + ); Ok(()) } -pub async fn perform_withdrawal_failing + TestIndexerExtensions>( +pub async fn perform_withdrawal_failing< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( rpc: &mut R, test_indexer: &mut I, env: &EnvAccounts, @@ -522,10 +544,13 @@ pub fn assert_withdrawal + TestIndexerExtensions "Withdrawal compressed account doesn't exist or has incorrect amount {} expected amount", withdrawal_amount ); - let token_data_escrow_change = test_indexer.get_token_compressed_accounts().iter().any(|x| { - x.token_data.owner == token_owner_pda - && x.token_data.amount == escrow_amount - withdrawal_amount - }); + let token_data_escrow_change = test_indexer + .get_token_compressed_accounts() + .iter() + .any(|x| { + x.token_data.owner == token_owner_pda + && x.token_data.amount == escrow_amount - withdrawal_amount + }); assert!( token_data_escrow_change, "Escrow change compressed account doesn't exist or has incorrect amount {} expected amount", diff --git a/examples/token-escrow/programs/token-escrow/tests/test_compressed_pda.rs b/examples/token-escrow/programs/token-escrow/tests/test_compressed_pda.rs index 49b30267c..24cc35e96 100644 --- a/examples/token-escrow/programs/token-escrow/tests/test_compressed_pda.rs +++ b/examples/token-escrow/programs/token-escrow/tests/test_compressed_pda.rs @@ -14,30 +14,42 @@ // release compressed tokens use anchor_lang::AnchorDeserialize; +use light_client::{indexer::Indexer, rpc::merkle_tree::MerkleTreeExt}; use light_hasher::{Hasher, Poseidon}; -use light_program_test::indexer::{TestIndexer, TestIndexerExtensions}; -use light_program_test::test_env::{setup_test_programs_with_accounts, EnvAccounts}; +use light_program_test::{ + indexer::{TestIndexer, TestIndexerExtensions}, + test_env::{setup_test_programs_with_accounts, EnvAccounts}, +}; use light_prover_client::gnark::helpers::{ProverConfig, ProverMode}; -use light_system_program::sdk::address::{derive_address, derive_address_legacy}; -use light_system_program::sdk::compressed_account::MerkleContext; -use light_system_program::sdk::event::PublicTransactionEvent; -use light_system_program::NewAddressParams; -use light_test_utils::conversions::{ - program_to_sdk_public_transaction_event, sdk_to_program_compressed_account, - sdk_to_program_compressed_proof, sdk_to_program_token_data, +use light_system_program::{ + sdk::{ + address::{derive_address, derive_address_legacy}, + compressed_account::MerkleContext, + event::PublicTransactionEvent, + }, + NewAddressParams, +}; +use light_test_utils::{ + conversions::{ + program_to_sdk_public_transaction_event, sdk_to_program_compressed_account, + sdk_to_program_compressed_proof, sdk_to_program_token_data, + }, + spl::{create_mint_helper, mint_tokens_helper}, + FeeConfig, RpcConnection, RpcError, TransactionParams, +}; +use solana_sdk::{ + instruction::{Instruction, InstructionError}, + signature::Keypair, + signer::Signer, + transaction::Transaction, }; -use light_test_utils::spl::{create_mint_helper, mint_tokens_helper}; -use light_test_utils::{FeeConfig, RpcConnection, RpcError, TransactionParams}; -use solana_sdk::instruction::{Instruction, InstructionError}; -use solana_sdk::signature::Keypair; -use solana_sdk::{signer::Signer, transaction::Transaction}; -use light_client::indexer::Indexer; -use light_client::rpc::merkle_tree::MerkleTreeExt; -use token_escrow::escrow_with_compressed_pda::sdk::{ - create_escrow_instruction, create_withdrawal_instruction, get_token_owner_pda, - CreateCompressedPdaEscrowInstructionInputs, CreateCompressedPdaWithdrawalInstructionInputs, +use token_escrow::{ + escrow_with_compressed_pda::sdk::{ + create_escrow_instruction, create_withdrawal_instruction, get_token_owner_pda, + CreateCompressedPdaEscrowInstructionInputs, CreateCompressedPdaWithdrawalInstructionInputs, + }, + EscrowError, EscrowTimeLock, }; -use token_escrow::{EscrowError, EscrowTimeLock}; #[tokio::test] async fn test_escrow_with_compressed_pda() { @@ -45,7 +57,7 @@ async fn test_escrow_with_compressed_pda() { String::from("token_escrow"), token_escrow::ID, )])) - .await; + .await; let payer = rpc.get_payer().insecure_clone(); let test_indexer = TestIndexer::init_from_env( @@ -69,7 +81,7 @@ async fn test_escrow_with_compressed_pda() { vec![amount], vec![payer.pubkey()], ) - .await; + .await; let seed = [1u8; 32]; let escrow_amount = 100u64; @@ -84,8 +96,8 @@ async fn test_escrow_with_compressed_pda() { escrow_amount, seed, ) - .await - .unwrap(); + .await + .unwrap(); let current_slot = rpc.get_slot().await.unwrap(); let lockup_end = lock_up_time + current_slot; @@ -98,7 +110,7 @@ async fn test_escrow_with_compressed_pda() { &seed, &lockup_end, ) - .await; + .await; println!("withdrawal _----------------------------------------------------------------"); let withdrawal_amount = escrow_amount; @@ -112,7 +124,7 @@ async fn test_escrow_with_compressed_pda() { new_lock_up_time, withdrawal_amount, ) - .await; + .await; let instruction_error = InstructionError::Custom(EscrowError::EscrowLocked.into()); let transaction_error = @@ -130,8 +142,8 @@ async fn test_escrow_with_compressed_pda() { new_lock_up_time, withdrawal_amount, ) - .await - .unwrap(); + .await + .unwrap(); assert_withdrawal( &mut rpc, @@ -143,7 +155,7 @@ async fn test_escrow_with_compressed_pda() { &seed, new_lock_up_time, ) - .await; + .await; } pub async fn perform_escrow_failing( @@ -164,7 +176,7 @@ pub async fn perform_escrow_failing( lock_up_time, escrow_amount, ) - .await; + .await; let latest_blockhash = rpc.get_latest_blockhash().await.unwrap(); let transaction = Transaction::new_signed_with_payer( &[instruction], @@ -194,7 +206,7 @@ pub async fn perform_escrow_with_event( lock_up_time, escrow_amount, ) - .await; + .await; let event = rpc .create_and_send_transaction_with_event::( &[instruction], @@ -210,9 +222,10 @@ pub async fn perform_escrow_with_event( ) .await?; let slot = rpc.get_slot().await.unwrap(); - test_indexer.add_compressed_accounts_with_token_data(slot, &program_to_sdk_public_transaction_event( - event.unwrap().0, - )); + test_indexer.add_compressed_accounts_with_token_data( + slot, + &program_to_sdk_public_transaction_event(event.unwrap().0), + ); Ok(()) } @@ -246,9 +259,11 @@ async fn create_escrow_ix( let rpc_result = test_indexer .create_proof_for_compressed_accounts( Some(vec![input_compressed_account_hash]), - Some(vec![compressed_input_account_with_context - .merkle_context - .merkle_tree_pubkey]), + Some(vec![ + compressed_input_account_with_context + .merkle_context + .merkle_tree_pubkey, + ]), Some(&[address]), Some(vec![env.address_merkle_tree_pubkey]), context, @@ -376,7 +391,7 @@ pub async fn perform_withdrawal_with_event( new_lock_up_time, escrow_amount, ) - .await; + .await; let event = rpc .create_and_send_transaction_with_event::( &[instruction], @@ -386,9 +401,10 @@ pub async fn perform_withdrawal_with_event( ) .await?; let slot = rpc.get_slot().await.unwrap(); - test_indexer.add_compressed_accounts_with_token_data(slot, &program_to_sdk_public_transaction_event( - event.unwrap().0, - )); + test_indexer.add_compressed_accounts_with_token_data( + slot, + &program_to_sdk_public_transaction_event(event.unwrap().0), + ); Ok(()) } @@ -410,7 +426,7 @@ pub async fn perform_withdrawal_failing( new_lock_up_time, escrow_amount, ) - .await; + .await; let latest_blockhash = rpc.get_latest_blockhash().await.unwrap(); let transaction = Transaction::new_signed_with_payer( &[instruction], diff --git a/forester/src/batch_processor/common.rs b/forester/src/batch_processor/common.rs index 8cf9b9a00..3fe4e4b81 100644 --- a/forester/src/batch_processor/common.rs +++ b/forester/src/batch_processor/common.rs @@ -108,7 +108,7 @@ impl + IndexerType> BatchProcessor { }; Self::calculate_completion_from_tree(account.data.as_mut_slice()) - } + } async fn get_output_queue_completion(&self, rpc: &mut R) -> f64 { let mut account = match rpc.get_account(self.context.output_queue).await { @@ -117,7 +117,7 @@ impl + IndexerType> BatchProcessor { }; Self::calculate_completion_from_queue(account.data.as_mut_slice()) - } + } fn calculate_completion_from_tree(data: &mut [u8]) -> f64 { let tree = match BatchedMerkleTreeAccount::state_tree_from_bytes_mut(data) { @@ -153,7 +153,7 @@ impl + IndexerType> BatchProcessor { let remaining = total - batch.get_num_inserted_zkps(); remaining as f64 / total as f64 - } + } async fn process_state_append(&self) -> Result { let mut rpc = self.context.rpc_pool.get_connection().await?; diff --git a/forester/tests/batched_address_test.rs b/forester/tests/batched_address_test.rs index 3ca0ec266..279db9c64 100644 --- a/forester/tests/batched_address_test.rs +++ b/forester/tests/batched_address_test.rs @@ -1,18 +1,17 @@ use std::{sync::Arc, time::Duration}; use forester::run_pipeline; -use forester_utils::{ - registry::{register_test_forester, update_test_forester}, -}; +use forester_utils::registry::{register_test_forester, update_test_forester}; use light_batched_merkle_tree::{ batch::BatchState, initialize_address_tree::InitAddressTreeAccountsInstructionData, merkle_tree::BatchedMerkleTreeAccount, }; use light_client::{ + indexer::AddressMerkleTreeAccounts, rpc::{solana_rpc::SolanaRpcUrl, RpcConnection, SolanaRpcConnection}, rpc_pool::SolanaRpcPool, }; -use light_program_test::test_env::EnvAccounts; +use light_program_test::{indexer::TestIndexer, test_env::EnvAccounts}; use light_prover_client::gnark::helpers::{LightValidatorConfig, ProverConfig, ProverMode}; use light_test_utils::{ create_address_test_program_sdk::perform_create_pda_with_event_rnd, e2e_test_env::E2ETestEnv, @@ -25,8 +24,7 @@ use tokio::{ time::{sleep, timeout}, }; use tracing::log::info; -use light_client::indexer::AddressMerkleTreeAccounts; -use light_program_test::indexer::TestIndexer; + use crate::test_utils::{forester_config, general_action_config, init, keypair_action_config}; mod test_utils; @@ -142,7 +140,7 @@ async fn test_address_batched() { println!("Creating new address batch tree..."); let merkle_tree_keypair = Keypair::new(); - env.indexer + env.indexer .add_address_merkle_tree( &mut env.rpc, &merkle_tree_keypair, @@ -150,7 +148,7 @@ async fn test_address_batched() { None, 2, ) - .await; + .await; env_accounts.batch_address_merkle_tree = merkle_tree_keypair.pubkey(); let address_trees: Vec = env diff --git a/forester/tests/batched_state_test.rs b/forester/tests/batched_state_test.rs index 74cdb57d6..f0da3c225 100644 --- a/forester/tests/batched_state_test.rs +++ b/forester/tests/batched_state_test.rs @@ -10,11 +10,9 @@ use light_client::{ rpc::{solana_rpc::SolanaRpcUrl, RpcConnection, SolanaRpcConnection}, rpc_pool::SolanaRpcPool, }; -use light_program_test::test_env::EnvAccounts; +use light_program_test::{indexer::TestIndexer, test_env::EnvAccounts}; use light_prover_client::gnark::helpers::LightValidatorConfig; -use light_test_utils::{ - e2e_test_env::{init_program_test_env, E2ETestEnv}, -}; +use light_test_utils::e2e_test_env::{init_program_test_env, E2ETestEnv}; use serial_test::serial; use solana_program::native_token::LAMPORTS_PER_SOL; use solana_sdk::{ @@ -25,7 +23,7 @@ use tokio::{ time::timeout, }; use tracing::log::info; -use light_program_test::indexer::TestIndexer; + use crate::test_utils::{forester_config, init}; mod test_utils; diff --git a/forester/tests/e2e_test.rs b/forester/tests/e2e_test.rs index 9fd58c40a..3c7e81c41 100644 --- a/forester/tests/e2e_test.rs +++ b/forester/tests/e2e_test.rs @@ -5,14 +5,13 @@ use account_compression::{ AddressMerkleTreeAccount, }; use forester::{queue_helpers::fetch_queue_item_data, run_pipeline, utils::get_protocol_config}; -use forester_utils::{ - registry::register_test_forester, -}; +use forester_utils::registry::register_test_forester; use light_client::{ + indexer::{AddressMerkleTreeAccounts, StateMerkleTreeAccounts}, rpc::{solana_rpc::SolanaRpcUrl, RpcConnection, RpcError, SolanaRpcConnection}, rpc_pool::SolanaRpcPool, }; -use light_program_test::test_env::EnvAccounts; +use light_program_test::{indexer::TestIndexer, test_env::EnvAccounts}; use light_prover_client::gnark::helpers::{ spawn_prover, LightValidatorConfig, ProverConfig, ProverMode, }; @@ -30,8 +29,6 @@ use tokio::{ sync::{mpsc, oneshot, Mutex}, time::{sleep, timeout}, }; -use light_client::indexer::{AddressMerkleTreeAccounts, StateMerkleTreeAccounts}; -use light_program_test::indexer::TestIndexer; mod test_utils; use test_utils::*; diff --git a/forester/tests/test_utils.rs b/forester/tests/test_utils.rs index b6f11106c..4cfbbc6ca 100644 --- a/forester/tests/test_utils.rs +++ b/forester/tests/test_utils.rs @@ -6,16 +6,15 @@ use forester::{ telemetry::setup_telemetry, ForesterConfig, }; -use light_client::rpc::RpcConnection; -use light_program_test::test_env::get_test_env_accounts; -use light_prover_client::gnark::helpers::{spawn_validator, LightValidatorConfig}; -use light_test_utils::{ - e2e_test_env::{GeneralActionConfig, KeypairActionConfig, User}, +use light_client::{ + indexer::{Indexer, IndexerError, NewAddressProofWithContext}, + rpc::RpcConnection, }; +use light_program_test::{indexer::TestIndexerExtensions, test_env::get_test_env_accounts}; +use light_prover_client::gnark::helpers::{spawn_validator, LightValidatorConfig}; +use light_test_utils::e2e_test_env::{GeneralActionConfig, KeypairActionConfig, User}; use solana_sdk::signature::{Keypair, Signer}; use tracing::debug; -use light_client::indexer::{Indexer, IndexerError, NewAddressProofWithContext}; -use light_program_test::indexer::TestIndexerExtensions; #[allow(dead_code)] pub async fn init(config: Option) { @@ -108,7 +107,10 @@ pub fn generate_pubkey_254() -> Pubkey { } #[allow(dead_code)] -pub async fn assert_new_address_proofs_for_photon_and_test_indexer + TestIndexerExtensions>( +pub async fn assert_new_address_proofs_for_photon_and_test_indexer< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( indexer: &mut I, trees: &[Pubkey], addresses: &[Pubkey], @@ -176,7 +178,10 @@ pub async fn assert_new_address_proofs_for_photon_and_test_indexer + TestIndexerExtensions>( +pub async fn assert_accounts_by_owner< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( indexer: &mut I, user: &User, photon_indexer: &PhotonIndexer, @@ -210,14 +215,16 @@ pub async fn assert_accounts_by_owner + TestInde } #[allow(dead_code)] -pub async fn assert_account_proofs_for_photon_and_test_indexer + TestIndexerExtensions>( +pub async fn assert_account_proofs_for_photon_and_test_indexer< + R: RpcConnection, + I: Indexer + TestIndexerExtensions, +>( indexer: &mut I, user_pubkey: &Pubkey, photon_indexer: &PhotonIndexer, ) { - let accs: Result, IndexerError> = indexer - .get_compressed_accounts_by_owner(user_pubkey) - .await; + let accs: Result, IndexerError> = + indexer.get_compressed_accounts_by_owner(user_pubkey).await; for account_hash in accs.unwrap() { let photon_result = photon_indexer .get_multiple_compressed_account_proofs(vec![account_hash.clone()]) diff --git a/sdk-libs/client/src/rpc/solana_rpc.rs b/sdk-libs/client/src/rpc/solana_rpc.rs index e2ec99a23..a98091a0b 100644 --- a/sdk-libs/client/src/rpc/solana_rpc.rs +++ b/sdk-libs/client/src/rpc/solana_rpc.rs @@ -27,10 +27,9 @@ use solana_transaction_status::{ use tokio::time::{sleep, Instant}; use crate::{ - rpc::{errors::RpcError, rpc_connection::RpcConnection}, + rpc::{errors::RpcError, merkle_tree::MerkleTreeExt, rpc_connection::RpcConnection}, transaction_params::TransactionParams, }; -use crate::rpc::merkle_tree::MerkleTreeExt; pub enum SolanaRpcUrl { Testnet, @@ -463,4 +462,4 @@ impl RpcConnection for SolanaRpcConnection { } } -impl MerkleTreeExt for SolanaRpcConnection {} \ No newline at end of file +impl MerkleTreeExt for SolanaRpcConnection {}