Skip to content

Commit

Permalink
chore: document invoke/process, rename verify_state_proof.rs -> verif…
Browse files Browse the repository at this point in the history
…y_proof.rs, replace ok_or() with ok_or_else()
  • Loading branch information
ananas-block committed Jan 18, 2025
1 parent 4402869 commit 34e5e01
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 121 deletions.
4 changes: 2 additions & 2 deletions program-libs/batched-merkle-tree/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Batch {
self.leaf_index_could_exist_in_batch(leaf_index)?;
leaf_index
.checked_sub(self.start_index)
.ok_or(BatchedMerkleTreeError::LeafIndexNotInBatch)
.ok_or_else(|| BatchedMerkleTreeError::LeafIndexNotInBatch)
}

/// Stores the value in a value store,
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Batch {
let (before, after) = bloom_filter_stores.split_at_mut(bloom_filter_index);
let (bloom_filter, after) = after
.split_first_mut()
.ok_or(BatchedMerkleTreeError::InvalidIndex)?;
.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?;

// 2. Insert value into the bloom filter at bloom_filter_index.
BloomFilter::new(
Expand Down
24 changes: 13 additions & 11 deletions program-libs/batched-merkle-tree/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use light_merkle_tree_metadata::{
queue::QueueType,
};
use light_utils::{
account::{check_account_info, check_discriminator, set_discriminator, DISCRIMINATOR_LEN},
account::{check_account_info, set_discriminator, DISCRIMINATOR_LEN},
hashchain::create_hash_chain_from_array,
pubkey::Pubkey,
};
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let tree = Self::state_from_account_info(account_info)?;
Ok(*tree
.get_root_by_index(index)
.ok_or(BatchedMerkleTreeError::InvalidIndex)?)
.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?)
}

/// Checks address Merkle tree account and returns the root.
Expand All @@ -115,7 +115,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let tree = Self::address_from_account_info(account_info)?;
Ok(*tree
.get_root_by_index(index)
.ok_or(BatchedMerkleTreeError::InvalidIndex)?)
.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?)
}

/// Deserialize a batched state Merkle tree from account info.
Expand All @@ -140,6 +140,8 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
pub fn state_from_bytes(
account_data: &'a mut [u8],
) -> Result<BatchedMerkleTreeAccount<'a>, BatchedMerkleTreeError> {
use light_utils::account::check_discriminator;
check_discriminator::<Self>(&account_data[0..DISCRIMINATOR_LEN])?;
Self::from_bytes::<BATCHED_STATE_TREE_TYPE>(account_data)
}

Expand Down Expand Up @@ -185,8 +187,8 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
account_data: &'a mut [u8],
) -> Result<BatchedMerkleTreeAccount<'a>, BatchedMerkleTreeError> {
let account_data_len = account_data.len();
let (discriminator, account_data) = account_data.split_at_mut(DISCRIMINATOR_LEN);
check_discriminator::<Self>(discriminator)?;
// Discriminator is already checked in check_account_info.
let (_discriminator, account_data) = account_data.split_at_mut(DISCRIMINATOR_LEN);
let (metadata, account_data) =
Ref::<&'a mut [u8], BatchedMerkleTreeMetadata>::from_prefix(account_data)
.map_err(|e| BatchedMerkleTreeError::ZeroCopyCastError(e.to_string()))?;
Expand Down Expand Up @@ -351,7 +353,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let old_root = self
.root_history
.last()
.ok_or(BatchedMerkleTreeError::InvalidIndex)?;
.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?;
let mut start_index_bytes = [0u8; 32];
start_index_bytes[24..].copy_from_slice(&start_index.to_be_bytes());
create_hash_chain_from_array([
Expand Down Expand Up @@ -453,7 +455,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let old_root = self
.root_history
.last()
.ok_or(BatchedMerkleTreeError::InvalidIndex)?;
.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?;

if QUEUE_TYPE == QueueType::BatchedInput as u64 {
create_hash_chain_from_array([*old_root, new_root, leaves_hashchain])?
Expand Down Expand Up @@ -670,7 +672,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
// inclusion of a value which was in the batch that was just zeroed out.
self.zero_out_roots(
sequence_number,
root_index.ok_or(BatchedMerkleTreeError::InvalidIndex)?,
root_index.ok_or_else(|| BatchedMerkleTreeError::InvalidIndex)?,
);
}

Expand Down Expand Up @@ -752,7 +754,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let num_inserted_elements = self
.batches
.get(current_batch)
.ok_or(BatchedMerkleTreeError::InvalidBatchIndex)?
.ok_or_else(|| BatchedMerkleTreeError::InvalidBatchIndex)?
.get_num_inserted_elements();
// Keep for finegrained unit test
println!("current_batch: {}", current_batch);
Expand All @@ -770,7 +772,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let previous_full_batch = self
.batches
.get_mut(previous_full_batch_index)
.ok_or(BatchedMerkleTreeError::InvalidBatchIndex)?;
.ok_or_else(|| BatchedMerkleTreeError::InvalidBatchIndex)?;

let batch_is_inserted = previous_full_batch.get_state() == BatchState::Inserted;
let previous_batch_is_ready =
Expand All @@ -786,7 +788,7 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
let bloom_filter = self
.bloom_filter_stores
.get_mut(previous_full_batch_index)
.ok_or(BatchedMerkleTreeError::InvalidBatchIndex)?;
.ok_or_else(|| BatchedMerkleTreeError::InvalidBatchIndex)?;
bloom_filter.as_mut_slice().iter_mut().for_each(|x| *x = 0);
}
// 3.2 Mark bloom filter zeroed.
Expand Down
6 changes: 3 additions & 3 deletions program-libs/batched-merkle-tree/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a> BatchedQueueAccount<'a> {
let index = batch.get_value_index_in_batch(leaf_index)?;
let element = self.value_vecs[batch_index]
.get_mut(index as usize)
.ok_or(BatchedMerkleTreeError::InclusionProofByIndexFailed)?;
.ok_or_else(||BatchedMerkleTreeError::InclusionProofByIndexFailed)?;

if element == hash_chain_value {
return Ok(true);
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<'a> BatchedQueueAccount<'a> {
let index = batch.get_value_index_in_batch(leaf_index)?;
let element = self.value_vecs[batch_index]
.get_mut(index as usize)
.ok_or(BatchedMerkleTreeError::InclusionProofByIndexFailed)?;
.ok_or_else(||BatchedMerkleTreeError::InclusionProofByIndexFailed)?;

if element == hash_chain_value {
*element = [0u8; 32];
Expand Down Expand Up @@ -438,7 +438,7 @@ pub(crate) fn insert_into_current_batch(
let mut hashchain_store = hashchain_store.get_mut(batch_index);
let current_batch = batches
.get_mut(batch_index)
.ok_or(BatchedMerkleTreeError::InvalidBatchIndex)?;
.ok_or_else(||BatchedMerkleTreeError::InvalidBatchIndex)?;
// 1. Check that the current batch is ready.
// 1.1. If the current batch is inserted, clear the batch.
{
Expand Down
15 changes: 9 additions & 6 deletions programs/system/src/invoke/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub fn derive_new_addresses(
new_addresses: &mut Vec<[u8; 32]>,
) -> Result<()> {
let invoking_program_id_bytes = if let Some(invoking_program_id) = invoking_program_id {
invoking_program_id.to_bytes()
Some(invoking_program_id.to_bytes())
} else {
[0u8; 32]
None
};

new_address_params
Expand All @@ -49,16 +49,19 @@ pub fn derive_new_addresses(
)
.map_err(ProgramError::from)?,
BatchedMerkleTreeAccount::DISCRIMINATOR => {
if invoking_program_id.is_none() {
return err!(SystemProgramError::DeriveAddressError);
}
let invoking_program_id_bytes =
if let Some(bytes) = invoking_program_id_bytes.as_ref() {
Ok(bytes)
} else {
err!(SystemProgramError::DeriveAddressError)
}?;
derive_address(
&new_address_params.seed,
&remaining_accounts
[new_address_params.address_merkle_tree_account_index as usize]
.key()
.to_bytes(),
&invoking_program_id_bytes,
invoking_program_id_bytes,
)
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion programs/system/src/invoke/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub mod nullify_state;
pub mod processor;
pub mod sol_compression;
pub mod sum_check;
pub mod verify_proof;
pub mod verify_signer;
pub mod verify_state_proof;
1 change: 1 addition & 0 deletions programs/system/src/invoke/nullify_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn insert_nullifiers<
is_writable: true,
});
account_infos.push(account_info.clone());
// 1. Check invoking signer is eligible to write to the nullifier queue.
let (_, network_fee, _, _) = check_program_owner_state_merkle_tree::<true>(
&ctx.remaining_accounts[account.merkle_context.merkle_tree_pubkey_index as usize],
invoking_program,
Expand Down
Loading

0 comments on commit 34e5e01

Please sign in to comment.