Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Jan 9, 2025
1 parent 31f954c commit eb91e1a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 96 deletions.
57 changes: 41 additions & 16 deletions examples/token-escrow/programs/token-escrow/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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::{
Expand Down Expand Up @@ -283,7 +288,10 @@ pub async fn perform_escrow<R: RpcConnection, I: Indexer<R> + TestIndexerExtensi
create_escrow_instruction(create_ix_inputs, *escrow_amount)
}

pub async fn perform_escrow_with_event<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
pub async fn perform_escrow_with_event<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut I,
env: &EnvAccounts,
Expand Down Expand Up @@ -313,7 +321,10 @@ pub async fn perform_escrow_with_event<R: RpcConnection, I: Indexer<R> + 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(())
}

Expand All @@ -336,7 +347,7 @@ pub async fn perform_escrow_failing<R: RpcConnection, I: Indexer<R> + TestIndexe
rpc.process_transaction(transaction).await
}

pub async fn assert_escrow<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>> (
pub async fn assert_escrow<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
rpc: &mut R,
test_indexer: &I,
payer_pubkey: &Pubkey,
Expand All @@ -355,8 +366,10 @@ pub async fn assert_escrow<R: RpcConnection, I: Indexer<R> + 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
Expand Down Expand Up @@ -449,7 +462,10 @@ pub async fn perform_withdrawal<R: RpcConnection, I: Indexer<R> + TestIndexerExt
create_withdrawal_escrow_instruction(create_ix_inputs, *withdrawal_amount)
}

pub async fn perform_withdrawal_with_event<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
pub async fn perform_withdrawal_with_event<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut I,
env: &EnvAccounts,
Expand All @@ -476,11 +492,17 @@ pub async fn perform_withdrawal_with_event<R: RpcConnection, I: Indexer<R> + 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<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
pub async fn perform_withdrawal_failing<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut I,
env: &EnvAccounts,
Expand Down Expand Up @@ -522,10 +544,13 @@ pub fn assert_withdrawal<R: RpcConnection, I: Indexer<R> + 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,50 @@
// 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() {
let (mut rpc, env) = setup_test_programs_with_accounts(Some(vec![(
String::from("token_escrow"),
token_escrow::ID,
)]))
.await;
.await;
let payer = rpc.get_payer().insecure_clone();

let test_indexer = TestIndexer::init_from_env(
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -98,7 +110,7 @@ async fn test_escrow_with_compressed_pda() {
&seed,
&lockup_end,
)
.await;
.await;

println!("withdrawal _----------------------------------------------------------------");
let withdrawal_amount = escrow_amount;
Expand All @@ -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 =
Expand All @@ -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,
Expand All @@ -143,7 +155,7 @@ async fn test_escrow_with_compressed_pda() {
&seed,
new_lock_up_time,
)
.await;
.await;
}

pub async fn perform_escrow_failing<R: RpcConnection + MerkleTreeExt>(
Expand All @@ -164,7 +176,7 @@ pub async fn perform_escrow_failing<R: RpcConnection + MerkleTreeExt>(
lock_up_time,
escrow_amount,
)
.await;
.await;
let latest_blockhash = rpc.get_latest_blockhash().await.unwrap();
let transaction = Transaction::new_signed_with_payer(
&[instruction],
Expand Down Expand Up @@ -194,7 +206,7 @@ pub async fn perform_escrow_with_event<R: RpcConnection + MerkleTreeExt>(
lock_up_time,
escrow_amount,
)
.await;
.await;
let event = rpc
.create_and_send_transaction_with_event::<PublicTransactionEvent>(
&[instruction],
Expand All @@ -210,9 +222,10 @@ pub async fn perform_escrow_with_event<R: RpcConnection + MerkleTreeExt>(
)
.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(())
}

Expand Down Expand Up @@ -246,9 +259,11 @@ async fn create_escrow_ix<R: RpcConnection + MerkleTreeExt>(
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,
Expand Down Expand Up @@ -376,7 +391,7 @@ pub async fn perform_withdrawal_with_event<R: RpcConnection + MerkleTreeExt>(
new_lock_up_time,
escrow_amount,
)
.await;
.await;
let event = rpc
.create_and_send_transaction_with_event::<PublicTransactionEvent>(
&[instruction],
Expand All @@ -386,9 +401,10 @@ pub async fn perform_withdrawal_with_event<R: RpcConnection + MerkleTreeExt>(
)
.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(())
}

Expand All @@ -410,7 +426,7 @@ pub async fn perform_withdrawal_failing<R: RpcConnection + MerkleTreeExt>(
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],
Expand Down
6 changes: 3 additions & 3 deletions forester/src/batch_processor/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<R: RpcConnection, I: Indexer<R> + IndexerType<R>> BatchProcessor<R, I> {
};

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 {
Expand All @@ -117,7 +117,7 @@ impl<R: RpcConnection, I: Indexer<R> + IndexerType<R>> BatchProcessor<R, I> {
};

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) {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<R: RpcConnection, I: Indexer<R> + IndexerType<R>> BatchProcessor<R, I> {

let remaining = total - batch.get_num_inserted_zkps();
remaining as f64 / total as f64
}
}

async fn process_state_append(&self) -> Result<usize> {
let mut rpc = self.context.rpc_pool.get_connection().await?;
Expand Down
Loading

0 comments on commit eb91e1a

Please sign in to comment.