Skip to content

Commit

Permalink
refactor: folder structure system program and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jan 30, 2025
1 parent fdfdf75 commit c4b1330
Show file tree
Hide file tree
Showing 97 changed files with 2,738 additions and 3,707 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use light_sdk::{
legacy::create_cpi_inputs_for_new_account, light_system_accounts, verify::verify, LightTraits,
};
use light_system_program::{
invoke::processor::CompressedProof,
processor::processor::CompressedProof,
sdk::{
address::derive_address_legacy,
compressed_account::{CompressedAccount, CompressedAccountData, PackedMerkleContext},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use light_compressed_token::process_transfer::{
TokenTransferOutputData,
};
use light_system_program::{
invoke::processor::CompressedProof,
processor::processor::CompressedProof,
sdk::{
address::{add_and_get_remaining_account_indices, pack_new_address_params},
compressed_account::{pack_merkle_context, CompressedAccount, MerkleContext},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use light_compressed_token::process_transfer::{
use light_hasher::{DataHasher, Poseidon};
use light_sdk::verify::verify;
use light_system_program::{
invoke::processor::CompressedProof,
processor::processor::CompressedProof,
sdk::{
compressed_account::{
CompressedAccount, CompressedAccountData, PackedCompressedAccountWithMerkleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use light_compressed_token::{
program::LightCompressedToken,
};
use light_sdk::{light_system_accounts, LightTraits};
use light_system_program::invoke::processor::CompressedProof;
use light_system_program::processor::processor::CompressedProof;

use crate::create_change_output_compressed_token_account;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use light_compressed_token::process_transfer::{
TokenTransferOutputData,
};
use light_system_program::{
invoke::processor::CompressedProof,
processor::processor::CompressedProof,
sdk::{
address::add_and_get_remaining_account_indices,
compressed_account::{CompressedAccount, MerkleContext},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use light_compressed_token::process_transfer::{
CompressedTokenInstructionDataTransfer, InputTokenDataWithContext,
PackedTokenTransferOutputData,
};
use light_system_program::invoke::processor::CompressedProof;
use light_system_program::processor::processor::CompressedProof;

use crate::{
create_change_output_compressed_token_account, EscrowCompressedTokensWithPda, EscrowError,
Expand Down
2 changes: 1 addition & 1 deletion examples/token-escrow/programs/token-escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_lang::{prelude::*, solana_program::pubkey::Pubkey};
use light_compressed_token::process_transfer::{
InputTokenDataWithContext, PackedTokenTransferOutputData,
};
use light_system_program::invoke::processor::CompressedProof;
use light_system_program::processor::processor::CompressedProof;
pub mod escrow_with_compressed_pda;
pub mod escrow_with_pda;

Expand Down
3 changes: 2 additions & 1 deletion program-libs/batched-merkle-tree/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ mod tests {
// to modify private Batch variables for assertions.
#[test]
fn test_get_num_inserted() {
let mut account_data = vec![0u8; 920];
let mut account_data = vec![0u8; 984];
let mut queue_metadata = QueueMetadata::default();
let associated_merkle_tree = Pubkey::new_unique();
queue_metadata.associated_merkle_tree = associated_merkle_tree;
Expand All @@ -911,6 +911,7 @@ mod tests {
zkp_batch_size,
num_iters,
bloom_filter_capacity,
Pubkey::new_unique(),
)
.unwrap();
// Tree height 4 -> capacity 16
Expand Down
2 changes: 1 addition & 1 deletion program-libs/batched-merkle-tree/src/batch_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn test_batch_size_validation() {
#[test]
fn test_output_queue_account_size() {
let metadata = BatchMetadata::new_output_queue(10, 2).unwrap();
let queue_size = 472 + (16 + 10 * 32) * 2 + (16 + 5 * 32) * 2;
let queue_size = 472 + (16 + 10 * 32) * 2 + (16 + 5 * 32) * 2 + 64;
assert_eq!(
metadata
.queue_account_size(QueueType::BatchedOutput as u64)
Expand Down
10 changes: 9 additions & 1 deletion program-libs/batched-merkle-tree/src/initialize_address_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ pub fn init_batched_address_merkle_tree_from_account_info(
let mt_data = &mut mt_account_info
.try_borrow_mut_data()
.map_err(|_| UtilsError::BorrowAccountDataFailed)?;
init_batched_address_merkle_tree_account(owner, params, mt_data, merkle_tree_rent)?;
init_batched_address_merkle_tree_account(
owner,
params,
mt_data,
merkle_tree_rent,
(*mt_account_info.key).into(),
)?;
Ok(())
}

Expand All @@ -123,6 +129,7 @@ pub fn init_batched_address_merkle_tree_account(
params: InitAddressTreeAccountsInstructionData,
mt_account_data: &mut [u8],
merkle_tree_rent: u64,
pubkey: Pubkey,
) -> Result<BatchedMerkleTreeAccount<'_>, BatchedMerkleTreeError> {
let height = params.height;

Expand Down Expand Up @@ -151,6 +158,7 @@ pub fn init_batched_address_merkle_tree_account(
};
BatchedMerkleTreeAccount::init(
mt_account_data,
&pubkey,
metadata,
params.root_history_capacity,
params.input_queue_batch_size,
Expand Down
12 changes: 11 additions & 1 deletion program-libs/batched-merkle-tree/src/initialize_state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use light_merkle_tree_metadata::{
rollover::{check_rollover_fee_sufficient, RolloverMetadata},
};
use light_utils::{
account::check_account_balance_is_rent_exempt, fee::compute_rollover_fee, pubkey::Pubkey,
account::check_account_balance_is_rent_exempt, fee::compute_rollover_fee,
hashv_to_bn254_field_size_be, pubkey::Pubkey,
};
use solana_program::{account_info::AccountInfo, msg};

Expand Down Expand Up @@ -204,6 +205,7 @@ pub fn init_batched_state_merkle_tree_accounts<'a>(
// Output queues have no bloom filter.
0,
0,
output_queue_pubkey,
)?;
}
let metadata = MerkleTreeMetadata {
Expand All @@ -228,6 +230,7 @@ pub fn init_batched_state_merkle_tree_accounts<'a>(
// to prove inclusion of this state for which we need a root from the tree account.
BatchedMerkleTreeAccount::init(
mt_account_data,
&mt_pubkey,
metadata,
params.root_history_capacity,
params.input_queue_batch_size,
Expand Down Expand Up @@ -379,6 +382,7 @@ pub struct CreateOutputQueueParams {
pub additional_bytes: u64,
pub rent: u64,
pub associated_merkle_tree: Pubkey,
pub queue_pubkey: Pubkey,
pub height: u32,
pub network_fee: u64,
}
Expand All @@ -389,6 +393,7 @@ impl CreateOutputQueueParams {
owner: Pubkey,
rent: u64,
associated_merkle_tree: Pubkey,
queue_pubkey: Pubkey,
) -> Self {
Self {
owner,
Expand All @@ -403,6 +408,7 @@ impl CreateOutputQueueParams {
associated_merkle_tree,
height: params.height,
network_fee: params.network_fee.unwrap_or_default(),
queue_pubkey,
}
}
}
Expand Down Expand Up @@ -439,5 +445,9 @@ pub fn create_output_queue_account(params: CreateOutputQueueParams) -> BatchedQu
metadata,
batch_metadata,
tree_capacity: 2u64.pow(params.height),
hashed_merkle_tree_pubkey: hashv_to_bn254_field_size_be(&[&params
.associated_merkle_tree
.to_bytes()]),
hashed_queue_pubkey: hashv_to_bn254_field_size_be(&[&params.queue_pubkey.to_bytes()]),
}
}
14 changes: 11 additions & 3 deletions program-libs/batched-merkle-tree/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use light_merkle_tree_metadata::{
use light_utils::{
account::{check_account_info, set_discriminator, DISCRIMINATOR_LEN},
hashchain::create_hash_chain_from_array,
hashv_to_bn254_field_size_be,
pubkey::Pubkey,
};
use light_verifier::{
Expand Down Expand Up @@ -213,6 +214,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
#[allow(clippy::too_many_arguments)]
pub fn init(
account_data: &'a mut [u8],
pubkey: &Pubkey,
metadata: MerkleTreeMetadata,
root_history_capacity: u32,
input_queue_batch_size: u64,
Expand All @@ -229,6 +231,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let (mut account_metadata, account_data) =
Ref::<&'a mut [u8], BatchedMerkleTreeMetadata>::from_prefix(account_data)
.map_err(ZeroCopyError::from)?;
account_metadata.hashed_pubkey = hashv_to_bn254_field_size_be(&[&pubkey.to_bytes()]);
account_metadata.metadata = metadata;
account_metadata.root_history_capacity = root_history_capacity;
account_metadata.height = height;
Expand Down Expand Up @@ -900,6 +903,7 @@ pub fn get_merkle_tree_account_size(
..Default::default()
},
capacity: 2u64.pow(height),
..Default::default()
};
mt_account.get_account_size().unwrap()
}
Expand Down Expand Up @@ -1006,15 +1010,17 @@ mod test {
/// 9. Batch 1 is inserted and Batch 0 is full and overlapping roots exist
#[test]
fn test_zero_out() {
let mut account_data = vec![0u8; 3128];
let mut account_data = vec![0u8; 3160];
let batch_size = 4;
let zkp_batch_size = 1;
let num_zkp_updates = batch_size / zkp_batch_size;
let root_history_len = 10;
let num_iter = 1;
let bloom_filter_capacity = 8000;
let pubkey = Pubkey::new_unique();
BatchedMerkleTreeAccount::init(
&mut account_data,
&pubkey,
MerkleTreeMetadata::default(),
root_history_len,
batch_size,
Expand Down Expand Up @@ -1377,7 +1383,7 @@ mod test {

#[test]
fn test_tree_is_full() {
let mut account_data = vec![0u8; 15672];
let mut account_data = vec![0u8; 15704];
let batch_size = 200;
let zkp_batch_size = 1;
let root_history_len = 10;
Expand All @@ -1387,6 +1393,7 @@ mod test {
let tree_capacity = 2u64.pow(height);
let account = BatchedMerkleTreeAccount::init(
&mut account_data,
&Pubkey::new_unique(),
MerkleTreeMetadata::default(),
root_history_len,
batch_size,
Expand Down Expand Up @@ -1415,7 +1422,7 @@ mod test {

#[test]
fn test_check_non_inclusion() {
let mut account_data = vec![0u8; 3192];
let mut account_data = vec![0u8; 3224];
let batch_size = 5;
let zkp_batch_size = 1;
let root_history_len = 10;
Expand All @@ -1424,6 +1431,7 @@ mod test {
let height = 4;
let mut account = BatchedMerkleTreeAccount::init(
&mut account_data,
&Pubkey::new_unique(),
MerkleTreeMetadata::default(),
root_history_len,
batch_size,
Expand Down
16 changes: 14 additions & 2 deletions program-libs/batched-merkle-tree/src/merkle_tree_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use light_merkle_tree_metadata::{
queue::QueueType,
rollover::RolloverMetadata,
};
use light_utils::{fee::compute_rollover_fee, pubkey::Pubkey};
use light_utils::{fee::compute_rollover_fee, hashv_to_bn254_field_size_be, pubkey::Pubkey};
use light_zero_copy::cyclic_vec::ZeroCopyCyclicVecU64;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

Expand Down Expand Up @@ -41,6 +41,7 @@ pub struct BatchedMerkleTreeMetadata {
pub root_history_capacity: u32,
pub capacity: u64,
pub queue_metadata: BatchMetadata,
pub hashed_pubkey: [u8; 32],
}

impl Default for BatchedMerkleTreeMetadata {
Expand All @@ -61,6 +62,7 @@ impl Default for BatchedMerkleTreeMetadata {
zkp_batch_size: 10,
..Default::default()
},
hashed_pubkey: [0u8; 32],
}
}
}
Expand Down Expand Up @@ -120,6 +122,7 @@ impl BatchedMerkleTreeMetadata {
root_history_capacity,
height,
num_iters,
tree_pubkey,
} = params;
Self {
metadata: MerkleTreeMetadata {
Expand Down Expand Up @@ -153,6 +156,7 @@ impl BatchedMerkleTreeMetadata {
)
.unwrap(),
capacity: 2u64.pow(height),
hashed_pubkey: hashv_to_bn254_field_size_be(&[&tree_pubkey.to_bytes()]),
}
}
}
Expand All @@ -171,9 +175,14 @@ pub struct CreateTreeParams {
pub root_history_capacity: u32,
pub height: u32,
pub num_iters: u64,
pub tree_pubkey: Pubkey,
}
impl CreateTreeParams {
pub fn from_state_ix_params(data: InitStateTreeAccountsInstructionData, owner: Pubkey) -> Self {
pub fn from_state_ix_params(
data: InitStateTreeAccountsInstructionData,
owner: Pubkey,
tree_pubkey: Pubkey,
) -> Self {
CreateTreeParams {
owner,
program_owner: data.program_owner,
Expand All @@ -187,12 +196,14 @@ impl CreateTreeParams {
root_history_capacity: data.root_history_capacity,
height: data.height,
num_iters: data.bloom_filter_num_iters,
tree_pubkey,
}
}

pub fn from_address_ix_params(
data: InitAddressTreeAccountsInstructionData,
owner: Pubkey,
tree_pubkey: Pubkey,
) -> Self {
CreateTreeParams {
owner,
Expand All @@ -207,6 +218,7 @@ impl CreateTreeParams {
root_history_capacity: data.root_history_capacity,
height: data.height,
num_iters: data.bloom_filter_num_iters,
tree_pubkey,
}
}
}
Loading

0 comments on commit c4b1330

Please sign in to comment.