Skip to content

Commit

Permalink
removed unwrap_err
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jun 29, 2024
1 parent 54ecc33 commit 266351c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![cfg(feature = "test-sbf")]

use account_compression::errors::AccountCompressionErrorCode;
use account_compression::{
self, utils::constants::GROUP_AUTHORITY_SEED, GroupAuthority, RegisteredProgram, ID,
};
use anchor_lang::{system_program, InstructionData};
use light_test_utils::rpc::errors::assert_rpc_error;
use light_test_utils::rpc::rpc_connection::RpcConnection;
use light_test_utils::rpc::test_rpc::ProgramTestRpcConnection;
use light_test_utils::{airdrop_lamports, test_env::SYSTEM_PROGRAM_ID_TEST_KEYPAIR};
Expand Down Expand Up @@ -194,5 +196,11 @@ async fn test_create_and_update_group() {
&vec![&context.get_payer(), &other_program_keypair],
latest_blockhash,
);
context.process_transaction(transaction).await.unwrap_err();
let result = context.process_transaction(transaction).await;
assert_rpc_error(
result,
0,
AccountCompressionErrorCode::InvalidAuthority.into(),
)
.unwrap();
}
80 changes: 51 additions & 29 deletions test-programs/account-compression-test/tests/merkle_tree_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use account_compression::{
};
use anchor_lang::{error::ErrorCode, system_program, InstructionData, ToAccountMetas};
use light_concurrent_merkle_tree::{
event::MerkleTreeEvent, zero_copy::ConcurrentMerkleTreeZeroCopyMut,
errors::ConcurrentMerkleTreeError, event::MerkleTreeEvent,
zero_copy::ConcurrentMerkleTreeZeroCopyMut, ConcurrentMerkleTree26,
};
use light_hash_set::HashSetError;
use light_hasher::{zero_bytes::poseidon::ZERO_BYTES, Hasher, Poseidon};
Expand Down Expand Up @@ -219,7 +220,6 @@ async fn test_full_nullifier_queue(
merkle_tree_config: &StateMerkleTreeConfig,
queue_config: &NullifierQueueConfig,
) {
println!("SEQUENCE THRESHOLD: {}", queue_config.sequence_threshold);
let mut program_test = ProgramTest::default();
program_test.add_program("account_compression", ID, None);
program_test.add_program(
Expand Down Expand Up @@ -1089,7 +1089,7 @@ async fn test_nullify_leaves(

let other_merkle_tree_keypair = Keypair::new();
let invalid_nullifier_queue_keypair = Keypair::new();
let invalid_nullifier_queue_pubkey = nullifier_queue_keypair.pubkey();
let invalid_nullifier_queue_pubkey = invalid_nullifier_queue_keypair.pubkey();
functional_1_initialize_state_merkle_tree_and_nullifier_queue(
&mut context,
&payer_pubkey,
Expand Down Expand Up @@ -1149,6 +1149,9 @@ async fn test_nullify_leaves(
let element_index = reference_merkle_tree
.get_leaf_index(&elements[0].1)
.unwrap() as u64;
let element_one_index = reference_merkle_tree
.get_leaf_index(&elements[1].1)
.unwrap() as u64;
nullify(
&mut context,
&merkle_tree_pubkey,
Expand Down Expand Up @@ -1180,7 +1183,7 @@ async fn test_nullify_leaves(
.unwrap();
index as u16
};
nullify(
let result = nullify(
&mut context,
&merkle_tree_pubkey,
&nullifier_queue_pubkey,
Expand All @@ -1191,13 +1194,16 @@ async fn test_nullify_leaves(
valid_leaf_queue_index,
invalid_element_index,
)
.await
.unwrap_err();
.await;
assert_rpc_error(
result, 0, 10008, // Invalid proof
)
.unwrap();

// 3. nullify with invalid leaf queue index
let valid_element_index = 1;
let invalid_leaf_queue_index = 0;
nullify(
let result = nullify(
&mut context,
&merkle_tree_pubkey,
&nullifier_queue_pubkey,
Expand All @@ -1208,25 +1214,30 @@ async fn test_nullify_leaves(
invalid_leaf_queue_index,
valid_element_index,
)
.await
.unwrap_err();
.await;
assert_rpc_error(result, 0, AccountCompressionErrorCode::LeafNotFound.into()).unwrap();

// 4. nullify with invalid change log index
let invalid_changelog_index = 0;
nullify(
let result = nullify(
&mut context,
&merkle_tree_pubkey,
&nullifier_queue_pubkey,
queue_config,
&mut reference_merkle_tree,
&elements[1].1,
invalid_changelog_index,
leaf_queue_index as u16,
element_index,
valid_leaf_queue_index as u16,
element_one_index,
)
.await
.unwrap_err();

.await;
// returns LeafNotFound why?
assert_rpc_error(
result,
0,
ConcurrentMerkleTreeError::CannotUpdateLeaf.into(),
)
.unwrap();
// 5. nullify other leaf
nullify(
&mut context,
Expand All @@ -1244,19 +1255,24 @@ async fn test_nullify_leaves(

// 6. nullify leaf with nullifier queue that is not associated with the
// merkle tree
nullify(
let result = nullify(
&mut context,
&merkle_tree_pubkey,
&invalid_nullifier_queue_pubkey,
queue_config,
&mut reference_merkle_tree,
&elements[0].1,
2,
0,
valid_leaf_queue_index as u16,
element_index,
)
.await
.unwrap_err();
.await;
assert_rpc_error(
result,
0,
AccountCompressionErrorCode::MerkleTreeAndQueueNotAssociated.into(),
)
.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -1330,16 +1346,21 @@ async fn fail_3_insert_same_elements_into_nullifier_queue<R: RpcConnection>(
) {
let payer = context.get_payer().insecure_clone();

insert_into_single_nullifier_queue(
let result = insert_into_single_nullifier_queue(
&elements,
&payer,
&payer,
nullifier_queue_pubkey,
merkle_tree_pubkey,
context,
)
.await
.unwrap_err();
.await;
assert_rpc_error(
result,
0,
HashSetError::ElementAlreadyExists.into(), // Invalid proof
)
.unwrap();
}

async fn fail_4_insert_with_invalid_signer<R: RpcConnection>(
Expand All @@ -1352,16 +1373,21 @@ async fn fail_4_insert_with_invalid_signer<R: RpcConnection>(
airdrop_lamports(rpc, &invalid_signer.pubkey(), 1_000_000_000)
.await
.unwrap();
insert_into_single_nullifier_queue(
let result = insert_into_single_nullifier_queue(
&elements,
&invalid_signer,
&invalid_signer,
nullifier_queue_pubkey,
merkle_tree_pubkey,
rpc,
)
.await
.unwrap_err();
.await;
assert_rpc_error(
result,
0,
AccountCompressionErrorCode::InvalidAuthority.into(),
)
.unwrap();
}

async fn functional_5_test_insert_into_nullifier_queue<R: RpcConnection>(
Expand Down Expand Up @@ -1872,10 +1898,6 @@ pub async fn nullify<R: RpcConnection>(
array_element.sequence_number(),
Some(merkle_tree.sequence_number() + nullifier_queue_config.sequence_threshold as usize)
);
println!(
"ARRAY ELEMENT SEQ NUM: {}",
array_element.sequence_number().unwrap()
);
let event = event.unwrap().0;
match event {
MerkleTreeEvent::V1(_) => panic!("Expected V2 event"),
Expand Down
2 changes: 1 addition & 1 deletion test-programs/compressed-token-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ pub async fn failing_compress_decompress<const INDEXED_ARRAY_SIZE: usize, R: Rpc
&[&context_payer, payer],
)
.await;
println!("error_code {:?}", error_code);
println!("result: {:?}", result);
assert_custom_error_or_program_error(Err(result.unwrap_err()), error_code)
}

Expand Down

0 comments on commit 266351c

Please sign in to comment.