Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jan 9, 2025
1 parent 2acc67c commit 382cdef
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 127 deletions.
6 changes: 1 addition & 5 deletions js/compressed-token/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,4 @@ const typesConfig = {
plugins: [dts()],
};

export default [
rolls('cjs', 'browser'),
rolls('cjs', 'node'),
typesConfig,
];
export default [rolls('cjs', 'browser'), rolls('cjs', 'node'), typesConfig];
6 changes: 1 addition & 5 deletions js/stateless.js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,4 @@ const typesConfig = {
plugins: [dts()],
};

export default [
rolls('cjs', 'browser'),
rolls('cjs', 'node'),
typesConfig,
];
export default [rolls('cjs', 'browser'), rolls('cjs', 'node'), typesConfig];
3 changes: 0 additions & 3 deletions program-libs/batched-merkle-tree/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub struct BatchedQueueMetadata {
pub next_index: u64,
}

// TODO: make discriminators anchor conistent
impl Discriminator for BatchedQueueAccount<'_> {
const DISCRIMINATOR: [u8; 8] = *b"queueacc";
}
Expand Down Expand Up @@ -529,11 +528,9 @@ pub fn output_queue_from_bytes(
),
BatchedMerkleTreeError,
> {
// let mut start_offset = BatchedQueueMetadata::LEN;
let (batches, account_data) = ZeroCopySliceMutU64::from_bytes_at(account_data)?;
let (value_vecs, account_data) =
ZeroCopyVecU64::from_bytes_at_multiple(num_value_stores, account_data)?;

let (bloom_filter_stores, account_data) =
ZeroCopySliceMutU64::from_bytes_at_multiple(num_stores, account_data)?;
let (hashchain_store, _) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ fn test_account_init() {
let merkle_tree_rent = 1_000_000_000;

let params = InitAddressTreeAccountsInstructionData::test_default();
println!("{:?}", params);
let mt_params = CreateTreeParams::from_address_ix_params(params, owner);
let ref_mt_account = BatchedMerkleTreeMetadata::new_address_tree(mt_params, merkle_tree_rent);
println!("ref_mt_account {:?}", ref_mt_account);
let account_data_len = mt_account_data.len();
println!("account_data_len {:?}", account_data_len);
init_batched_address_merkle_tree_account(
owner.into(),
params.clone(),
Expand All @@ -46,7 +42,6 @@ fn test_account_init() {
);
}

#[ignore = "debug later network_fee and rollover_threshold missmatch"]
#[test]
fn test_rnd_account_init() {
use rand::SeedableRng;
Expand All @@ -71,7 +66,7 @@ fn test_rnd_account_init() {
index: rng.gen_range(0..1000),
program_owner,
forester,
bloom_filter_num_iters: 2, //rng.gen_range(0..4),
bloom_filter_num_iters: rng.gen_range(0..4),
input_queue_batch_size: rng.gen_range(1..1000) * input_queue_zkp_batch_size,
input_queue_zkp_batch_size,
// 8 bits per byte, divisible by 8 for aligned memory
Expand All @@ -80,7 +75,7 @@ fn test_rnd_account_init() {
rollover_threshold: Some(rng.gen_range(0..100)),
close_threshold: None,
root_history_capacity: rng.gen_range(1..1000),
input_queue_num_batches: 2, // rng.gen_range(1..4),
input_queue_num_batches: rng.gen_range(1..4),
height: rng.gen_range(1..32),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn test_rnd_account_init() {
program_owner,
forester,
additional_bytes: rng.gen_range(0..1000),
bloom_filter_num_iters: 2, //rng.gen_range(0..4),
bloom_filter_num_iters: rng.gen_range(0..4),
input_queue_batch_size: rng.gen_range(1..1000) * input_queue_zkp_batch_size,
output_queue_batch_size: rng.gen_range(1..1000) * output_queue_zkp_batch_size,
input_queue_zkp_batch_size,
Expand All @@ -173,7 +173,7 @@ fn test_rnd_account_init() {
rollover_threshold: Some(rng.gen_range(0..100)),
close_threshold: None,
root_history_capacity: rng.gen_range(1..1000),
input_queue_num_batches: 2, //rng.gen_range(1..4),
input_queue_num_batches: rng.gen_range(1..4),
output_queue_num_batches: rng.gen_range(1..4),
height: rng.gen_range(1..32),
};
Expand Down Expand Up @@ -268,7 +268,6 @@ fn test_rnd_account_init() {
merkle_tree_rent + queue_rent + additional_bytes_rent,
mt_pubkey,
);
println!("queue_account_params: {:?}", queue_account_params);
let ref_output_queue_account = create_output_queue_account(queue_account_params);
assert_queue_zero_copy_inited(
output_queue_account_data.as_mut_slice(),
Expand Down
39 changes: 25 additions & 14 deletions program-libs/batched-merkle-tree/tests/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use serial_test::serial;
pub fn assert_nullifier_queue_insert(
pre_account: BatchedMerkleTreeMetadata,
pre_batches: ZeroCopySliceMutU64<Batch>,
pre_value_vecs: Vec<ZeroCopyVecU64<[u8; 32]>>,
pre_value_vecs: &mut Vec<ZeroCopyVecU64<[u8; 32]>>,
pre_roots: Vec<[u8; 32]>,
pre_hashchains: Vec<ZeroCopyVecU64<[u8; 32]>>,
merkle_tree_account: BatchedMerkleTreeAccount,
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn assert_nullifier_queue_insert(
pub fn assert_input_queue_insert(
mut pre_account: BatchedMerkleTreeMetadata,
mut pre_batches: ZeroCopySliceMutU64<Batch>,
mut pre_value_vecs: Vec<ZeroCopyVecU64<[u8; 32]>>,
pre_value_vecs: &mut Vec<ZeroCopyVecU64<[u8; 32]>>,
pre_roots: Vec<[u8; 32]>,
mut pre_hashchains: Vec<ZeroCopyVecU64<[u8; 32]>>,
mut merkle_tree_account: BatchedMerkleTreeAccount,
Expand Down Expand Up @@ -321,8 +321,22 @@ pub fn assert_output_queue_insert(
"BatchedQueueAccount changed."
);
assert_eq!(pre_hashchains, output_account.hashchain_store);
assert_eq!(pre_value_store, output_account.value_vecs);
for (i, (value_store, pre)) in output_account
.value_vecs
.iter()
.zip(pre_value_store.iter())
.enumerate()
{
for (j, (value, pre_value)) in value_store.iter().zip(pre.iter()).enumerate() {
assert_eq!(
*value, *pre_value,
"{} {} \n value store {:?}\n pre {:?}",
i, j, value_store, pre
);
}
}
assert_eq!(pre_batches, output_account.batches.to_vec());
assert_eq!(pre_value_store, output_account.value_vecs);
Ok(())
}

Expand Down Expand Up @@ -364,6 +378,7 @@ pub fn simulate_transaction(
let batch_start_index =
output_account.batches.get(batch_index).unwrap().start_index;
included = true;
println!("overwriting value: {:?}", value);
*value = [0u8; 32];
leaf_index = value_index as u64 + batch_start_index;
}
Expand Down Expand Up @@ -540,18 +555,14 @@ async fn test_simulate_transactions() {

let mut pre_mt_data = mt_account_data.clone();
let mut pre_account_bytes = output_queue_account_data.clone();
let mut pre_account_bytes2 = output_queue_account_data.clone();

let pre_output_account =
BatchedQueueAccount::output_queue_from_bytes_mut(&mut pre_account_bytes).unwrap();
let pre_output_metadata = pre_output_account.get_metadata().clone();
let pre_output_batches = pre_output_account.batches;
let pre_output_value_stores = pre_output_account.value_vecs;
let mut pre_output_value_stores = pre_output_account.value_vecs;
let pre_output_hashchains = pre_output_account.hashchain_store;
let pre_output_value_stores_2 =
BatchedQueueAccount::output_queue_from_bytes_mut(&mut pre_account_bytes2)
.unwrap()
.value_vecs;

let mut pre_mt_account_bytes = mt_account_data.clone();
let pre_merkle_tree_account =
BatchedMerkleTreeAccount::state_tree_from_bytes_mut(&mut pre_mt_account_bytes)
Expand Down Expand Up @@ -585,7 +596,7 @@ async fn test_simulate_transactions() {
assert_nullifier_queue_insert(
pre_mt_account,
pre_batches,
pre_output_value_stores,
&mut pre_output_value_stores, // mut to remove values proven by index
pre_roots,
pre_mt_hashchains,
merkle_tree_account,
Expand All @@ -602,7 +613,7 @@ async fn test_simulate_transactions() {
assert_output_queue_insert(
pre_output_metadata,
pre_output_batches.to_vec(),
pre_output_value_stores_2,
pre_output_value_stores,
pre_output_hashchains,
BatchedQueueAccount::output_queue_from_bytes_mut(
&mut output_queue_account_data.clone(), // clone so that data cannot be modified
Expand Down Expand Up @@ -999,7 +1010,7 @@ async fn test_e2e() {
assert_nullifier_queue_insert(
pre_account,
pre_batches,
vec![],
&mut vec![],
pre_roots,
pre_hashchains,
merkle_tree_account,
Expand Down Expand Up @@ -1668,7 +1679,7 @@ async fn test_fill_queues_completely() {
assert_nullifier_queue_insert(
pre_account,
pre_batches,
vec![],
&mut vec![],
pre_roots,
pre_hashchains,
merkle_tree_account,
Expand Down Expand Up @@ -1909,7 +1920,7 @@ async fn test_fill_address_tree_completely() {
assert_input_queue_insert(
pre_account,
pre_batches,
vec![],
&mut vec![],
pre_roots,
pre_hashchains,
merkle_tree_account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn test_rnd_rollover() {
rollover_threshold: Some(rng.gen_range(0..100)),
close_threshold: None,
root_history_capacity: rng.gen_range(1..1000),
input_queue_num_batches: 2, // rng.gen_range(1..4),
input_queue_num_batches: rng.gen_range(1..4),
height: rng.gen_range(1..32),
};
if forester.is_some() {
Expand Down
4 changes: 2 additions & 2 deletions program-libs/batched-merkle-tree/tests/rollover_state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ fn test_rnd_rollover() {
program_owner,
forester,
additional_bytes: rng.gen_range(0..1000),
bloom_filter_num_iters: 2, // rng.gen_range(0..4),
bloom_filter_num_iters: rng.gen_range(0..4),
input_queue_batch_size: rng.gen_range(1..1000) * input_queue_zkp_batch_size,
output_queue_batch_size: rng.gen_range(1..1000) * output_queue_zkp_batch_size,
input_queue_zkp_batch_size,
Expand All @@ -509,7 +509,7 @@ fn test_rnd_rollover() {
rollover_threshold: Some(rng.gen_range(0..100)),
close_threshold: None,
root_history_capacity: rng.gen_range(1..1000),
input_queue_num_batches: 2, // rng.gen_range(1..4),
input_queue_num_batches: rng.gen_range(1..4),
output_queue_num_batches: rng.gen_range(1..4),
height: rng.gen_range(1..32),
};
Expand Down
39 changes: 0 additions & 39 deletions program-libs/batched-merkle-tree/tests/zero_copy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{cell::RefCell, rc::Rc};

use bytemuck::{Pod, Zeroable};
use light_batched_merkle_tree::{
constants::ACCOUNT_COMPRESSION_PROGRAM_ID,
initialize_address_tree::{
Expand All @@ -12,46 +11,8 @@ use light_batched_merkle_tree::{
merkle_tree::{get_merkle_tree_account_size_default, BatchedMerkleTreeAccount},
queue::{get_output_queue_account_size_default, BatchedQueueAccount},
};
use light_hasher::Discriminator;
use light_utils::account::set_discriminator;
use solana_program::{account_info::AccountInfo, pubkey::Pubkey};

// /// Tests:
// /// 1. functional init
// /// 2. functional deserialize
// /// 3. failing deserialize invalid data
// #[test]
// fn test_bytes_to_struct() {
// #[repr(C)]
// #[derive(Debug, PartialEq, Copy, Clone, Pod, Zeroable)]
// pub struct MyStruct {
// pub data: u64,
// }
// impl Discriminator for MyStruct {
// const DISCRIMINATOR: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
// }
// let mut bytes = vec![0; 8 + std::mem::size_of::<MyStruct>()];
// let mut empty_bytes = vec![0; 8 + std::mem::size_of::<MyStruct>()];

// // Test 1 functional init.
// set_discriminator::<MyStruct>(&mut bytes).unwrap();
// let inited_struct =
// &mut WrappedPointerMut::<MyStruct>::from_bytes_with_discriminator(&mut bytes).unwrap();

// (*inited_struct).data = 1;

// assert_eq!(bytes[0..8], MyStruct::DISCRIMINATOR);
// assert_eq!(bytes[8..].to_vec(), vec![1, 0, 0, 0, 0, 0, 0, 0]);
// // Test 2 functional deserialize.
// let inited_struct =
// *WrappedPointerMut::<MyStruct>::from_bytes_with_discriminator(&mut bytes).unwrap();
// assert_eq!(inited_struct, MyStruct { data: 1 });
// // Test 3 failing deserialize invalid data.
// let inited_struct =
// *WrappedPointerMut::<MyStruct>::from_bytes_with_discriminator(&mut empty_bytes).unwrap();
// assert_ne!(inited_struct, MyStruct { data: 1 });
// }

#[derive(Debug, PartialEq, Clone)]
pub struct TestAccount {
pub key: Pubkey,
Expand Down
3 changes: 0 additions & 3 deletions program-libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ pub enum UtilsError {
InvalidAccountBalance,
#[error("Failed to borrow rent sysvar.")]
FailedBorrowRentSysvar,
#[error("Load account failed {0}.")]
LoadAccountFailed(String),
}

// NOTE(vadorovsky): Unfortunately, we need to do it by hand.
Expand All @@ -75,7 +73,6 @@ impl From<UtilsError> for u32 {
UtilsError::AlreadyInitialized => 12012,
UtilsError::InvalidAccountBalance => 12013,
UtilsError::FailedBorrowRentSysvar => 12014,
UtilsError::LoadAccountFailed(_) => 12015,
UtilsError::HasherError(e) => u32::from(e),
}
}
Expand Down
3 changes: 1 addition & 2 deletions program-libs/zero-copy/src/slice_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref};

use crate::{add_padding, errors::ZeroCopyError};

// pub type ZeroCopySliceMutU64<'a, T> = ZeroCopySliceMut<'a, usize, T>;
pub type ZeroCopySliceMutU32<'a, T> = ZeroCopySliceMut<'a, u32, T>;
pub type ZeroCopySliceMutU64<'a, T> = ZeroCopySliceMut<'a, u64, T>;
pub type ZeroCopySliceMutU32<'a, T> = ZeroCopySliceMut<'a, u32, T>;
pub type ZeroCopySliceMutU16<'a, T> = ZeroCopySliceMut<'a, u16, T>;
pub type ZeroCopySliceMutU8<'a, T> = ZeroCopySliceMut<'a, u8, T>;

Expand Down
2 changes: 1 addition & 1 deletion program-libs/zero-copy/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ where
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.data == other.data && self.len() == other.len()
self.as_slice() == other.as_slice()
}
}

Expand Down
Loading

0 comments on commit 382cdef

Please sign in to comment.