Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: test indexer #1468

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7ff96cf
forester batch ops refactored
sergeytimoshin Jan 5, 2025
c0a5afb
refactor batch processing workflows
sergeytimoshin Jan 6, 2025
d1f89b2
switched the verification order of input and output queues to priorit…
sergeytimoshin Jan 7, 2025
bcc1fa6
prioritization of the queue processing
sergeytimoshin Jan 7, 2025
a9c9da2
refactor imports
sergeytimoshin Jan 7, 2025
291fb09
add conversion utilities and refactor indexer code
sergeytimoshin Jan 8, 2025
4aa292a
- spl with conversions
sergeytimoshin Jan 9, 2025
3251bf3
- removed `TokenDataWithMerkleContext` from the client/indexer module a
sergeytimoshin Jan 9, 2025
9e45eb2
refactor program-test/utils*
sergeytimoshin Jan 9, 2025
e1a91a0
Add `TestIndexerExtensions` methods and update type constraints
sergeytimoshin Jan 9, 2025
ea026b1
program-tests/* refactor to include TestIndexerExtensions
sergeytimoshin Jan 9, 2025
2407965
program-tests/* refactoring wip
sergeytimoshin Jan 9, 2025
c3953c6
program-tests/* refactoring wip
sergeytimoshin Jan 9, 2025
88ae0cc
format + imports
sergeytimoshin Jan 9, 2025
ca215d0
refactor examples/*
sergeytimoshin Jan 9, 2025
0516e3a
refactor forester/* tests
sergeytimoshin Jan 9, 2025
f66a79d
program-tests/system_program.rs: fix proof Some/None
sergeytimoshin Jan 9, 2025
e32fbc5
refactor dependencies in compressed-token-test
sergeytimoshin Jan 9, 2025
31f954c
remove old indexer files
sergeytimoshin Jan 9, 2025
eb91e1a
format
sergeytimoshin Jan 9, 2025
5ade84e
use `LeafIndexInfo`
sergeytimoshin Jan 9, 2025
2430045
token-escrow & sdk-test-program cargo.toml deps fixes
sergeytimoshin Jan 10, 2025
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
316 changes: 36 additions & 280 deletions Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ where
let event = rpc
.create_and_send_transaction_with_event(&[instruction], &payer.pubkey(), &[payer], None)
.await?;
test_indexer.add_compressed_accounts_with_token_data(&event.unwrap().0);
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &event.unwrap().0);
Ok(())
}

Expand Down Expand Up @@ -421,7 +422,8 @@ where
let event = rpc
.create_and_send_transaction_with_event(&[instruction], &payer.pubkey(), &[payer], None)
.await?;
test_indexer.add_compressed_accounts_with_token_data(&event.unwrap().0);
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &event.unwrap().0);
Ok(())
}

Expand Down
6 changes: 4 additions & 2 deletions examples/name-service/programs/name-service/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ where
let event = rpc
.create_and_send_transaction_with_event(&[instruction], &payer.pubkey(), &[payer], None)
.await?;
test_indexer.add_compressed_accounts_with_token_data(&event.unwrap().0);
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &event.unwrap().0);
Ok(())
}

Expand Down Expand Up @@ -435,7 +436,8 @@ where
let event = rpc
.create_and_send_transaction_with_event(&[instruction], &payer.pubkey(), &[payer], None)
.await?;
test_indexer.add_compressed_accounts_with_token_data(&event.unwrap().0);
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &event.unwrap().0);
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion examples/token-escrow/programs/token-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ light-compressed-token = { workspace = true }
light-system-program = { workspace = true }
account-compression = { workspace = true }
light-hasher = { workspace = true }
light-verifier = { workspace = true }
light-sdk = { workspace = true, features = ["legacy"] }

[target.'cfg(not(target_os = "solana"))'.dependencies]
solana-sdk = { workspace = true }

[dev-dependencies]
light-verifier = { workspace = true }
light-client = { workspace = true }
light-test-utils = { workspace = true, features = ["devenv"] }
light-program-test = { workspace = true, features = ["devenv"] }
tokio = { workspace = true }
Expand Down
109 changes: 72 additions & 37 deletions examples/token-escrow/programs/token-escrow/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
// - 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,
indexer::TestIndexer,
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, Indexer, RpcConnection, RpcError, TransactionParams,
FeeConfig, RpcConnection, RpcError, TransactionParams,
};
use light_verifier::VerifierError;
use solana_sdk::{
Expand Down Expand Up @@ -205,16 +212,16 @@ async fn test_escrow_pda() {
);
}

pub async fn perform_escrow<R: RpcConnection>(
pub async fn perform_escrow<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
rpc: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
escrow_amount: &u64,
lock_up_time: &u64,
) -> Instruction {
let input_compressed_token_account_data = test_indexer
.token_compressed_accounts
.get_token_compressed_accounts()
.iter()
.find(|x| {
println!("searching token account: {:?}", x.token_data);
Expand Down Expand Up @@ -253,7 +260,9 @@ pub async fn perform_escrow<R: RpcConnection>(
.await;

let create_ix_inputs = CreateEscrowInstructionInputs {
input_token_data: &[input_compressed_token_account_data.token_data.clone()],
input_token_data: &[sdk_to_program_token_data(
input_compressed_token_account_data.token_data.clone(),
)],
lock_up_time: *lock_up_time,
signer: &payer_pubkey,
input_merkle_context: &[MerkleContext {
Expand All @@ -270,16 +279,21 @@ pub async fn perform_escrow<R: RpcConnection>(
],
output_compressed_accounts: &Vec::new(),
root_indices: &rpc_result.root_indices,
proof: &Some(rpc_result.proof),
proof: &Some(sdk_to_program_compressed_proof(rpc_result.proof)),
mint: &input_compressed_token_account_data.token_data.mint,
input_compressed_accounts: &[compressed_input_account_with_context.compressed_account],
input_compressed_accounts: &[sdk_to_program_compressed_account(
compressed_input_account_with_context.compressed_account,
)],
};
create_escrow_instruction(create_ix_inputs, *escrow_amount)
}

pub async fn perform_escrow_with_event<R: RpcConnection>(
pub async fn perform_escrow_with_event<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
escrow_amount: &u64,
Expand Down Expand Up @@ -307,13 +321,16 @@ pub async fn perform_escrow_with_event<R: RpcConnection>(
.await?
.unwrap();
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &event.0);
test_indexer.add_compressed_accounts_with_token_data(
slot,
&program_to_sdk_public_transaction_event(event.0),
);
Ok(())
}

pub async fn perform_escrow_failing<R: RpcConnection>(
pub async fn perform_escrow_failing<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
rpc: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
escrow_amount: &u64,
Expand All @@ -330,17 +347,17 @@ pub async fn perform_escrow_failing<R: RpcConnection>(
rpc.process_transaction(transaction).await
}

pub async fn assert_escrow<R: RpcConnection>(
pub async fn assert_escrow<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
rpc: &mut R,
test_indexer: &TestIndexer<R>,
test_indexer: &I,
payer_pubkey: &Pubkey,
amount: u64,
escrow_amount: u64,
lock_up_time: &u64,
) {
let token_owner_pda = get_token_owner_pda(payer_pubkey).0;
let token_data_escrow = test_indexer
.token_compressed_accounts
.get_token_compressed_accounts()
.iter()
.find(|x| x.token_data.owner == token_owner_pda)
.unwrap()
Expand All @@ -349,8 +366,10 @@ pub async fn assert_escrow<R: RpcConnection>(
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.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 All @@ -369,9 +388,9 @@ pub async fn assert_escrow<R: RpcConnection>(
assert_eq!(timelock_account.slot, *lock_up_time + current_slot);
}

pub async fn perform_withdrawal<R: RpcConnection>(
pub async fn perform_withdrawal<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
context: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
withdrawal_amount: &u64,
Expand All @@ -380,7 +399,7 @@ pub async fn perform_withdrawal<R: RpcConnection>(
let payer_pubkey = payer.pubkey();
let token_owner_pda = get_token_owner_pda(&invalid_signer.unwrap_or(payer_pubkey)).0;
let escrow_token_data_with_context = test_indexer
.token_compressed_accounts
.get_token_compressed_accounts()
.iter()
.find(|x| {
x.token_data.owner == token_owner_pda && x.token_data.amount >= *withdrawal_amount
Expand Down Expand Up @@ -414,7 +433,9 @@ pub async fn perform_withdrawal<R: RpcConnection>(
.await;

let create_ix_inputs = CreateEscrowInstructionInputs {
input_token_data: &[escrow_token_data_with_context.token_data.clone()],
input_token_data: &[sdk_to_program_token_data(
escrow_token_data_with_context.token_data.clone(),
)],
lock_up_time: 0,
signer: &payer_pubkey,
input_merkle_context: &[MerkleContext {
Expand All @@ -431,17 +452,22 @@ pub async fn perform_withdrawal<R: RpcConnection>(
],
output_compressed_accounts: &Vec::new(),
root_indices: &rpc_result.root_indices,
proof: &Some(rpc_result.proof),
proof: &Some(sdk_to_program_compressed_proof(rpc_result.proof)),
mint: &escrow_token_data_with_context.token_data.mint,
input_compressed_accounts: &[compressed_input_account_with_context.compressed_account],
input_compressed_accounts: &[sdk_to_program_compressed_account(
compressed_input_account_with_context.compressed_account,
)],
};

create_withdrawal_escrow_instruction(create_ix_inputs, *withdrawal_amount)
}

pub async fn perform_withdrawal_with_event<R: RpcConnection>(
pub async fn perform_withdrawal_with_event<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
withdrawal_amount: &u64,
Expand All @@ -466,13 +492,19 @@ pub async fn perform_withdrawal_with_event<R: RpcConnection>(
.await?
.unwrap();
let slot = rpc.get_slot().await.unwrap();
test_indexer.add_compressed_accounts_with_token_data(slot, &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>(
pub async fn perform_withdrawal_failing<
R: RpcConnection,
I: Indexer<R> + TestIndexerExtensions<R>,
>(
rpc: &mut R,
test_indexer: &mut TestIndexer<R>,
test_indexer: &mut I,
env: &EnvAccounts,
payer: &Keypair,
withdrawal_amount: &u64,
Expand All @@ -495,15 +527,15 @@ pub async fn perform_withdrawal_failing<R: RpcConnection>(
);
rpc.process_transaction(transaction).await
}
pub fn assert_withdrawal<R: RpcConnection>(
test_indexer: &TestIndexer<R>,
pub fn assert_withdrawal<R: RpcConnection, I: Indexer<R> + TestIndexerExtensions<R>>(
test_indexer: &I,
payer_pubkey: &Pubkey,
withdrawal_amount: u64,
escrow_amount: u64,
) {
let token_owner_pda = get_token_owner_pda(payer_pubkey).0;
let token_data_withdrawal = test_indexer
.token_compressed_accounts
.get_token_compressed_accounts()
.iter()
.any(|x| x.token_data.owner == *payer_pubkey && x.token_data.amount == withdrawal_amount);

Expand All @@ -512,10 +544,13 @@ pub fn assert_withdrawal<R: RpcConnection>(
"Withdrawal compressed account doesn't exist or has incorrect amount {} expected amount",
withdrawal_amount
);
let token_data_escrow_change = test_indexer.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
Loading
Loading