Skip to content

Commit

Permalink
reduced indexed changelog proof size to NET_TREE_HEIGHT without canopy
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jun 26, 2024
1 parent 63335aa commit 8077caf
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 400 deletions.
13 changes: 7 additions & 6 deletions forester/src/nullifier/address_queue_nullifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn empty_address_queue<T: Indexer, R: RpcConnection>(

loop {
let merkle_tree =
get_indexed_merkle_tree::<AddressMerkleTreeAccount, R, Poseidon, usize, 26>(
get_indexed_merkle_tree::<AddressMerkleTreeAccount, R, Poseidon, usize, 26, 16>(
rpc,
address_merkle_tree_pubkey,
)
Expand Down Expand Up @@ -97,11 +97,12 @@ pub async fn get_changelog_indices<R: RpcConnection>(
merkle_tree_pubkey: &Pubkey,
client: &mut R,
) -> Result<(usize, usize), ForesterError> {
let merkle_tree = get_indexed_merkle_tree::<AddressMerkleTreeAccount, R, Poseidon, usize, 26>(
client,
*merkle_tree_pubkey,
)
.await;
let merkle_tree =
get_indexed_merkle_tree::<AddressMerkleTreeAccount, R, Poseidon, usize, 26, 16>(
client,
*merkle_tree_pubkey,
)
.await;
let changelog_index = merkle_tree.changelog_index();
let indexed_changelog_index = merkle_tree.indexed_changelog_index();
Ok((changelog_index, indexed_changelog_index))
Expand Down
2 changes: 1 addition & 1 deletion merkle-tree/concurrent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ where
return Err(ConcurrentMerkleTreeError::CannotUpdateEmpty);
}

if self.canopy_depth > 0 && proof.len() != self.height {
if self.canopy_depth > 0 {
self.update_proof_from_canopy(leaf_index, proof)?;
}
if changelog_index != self.changelog_index() {
Expand Down
3 changes: 2 additions & 1 deletion merkle-tree/indexed/src/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use light_concurrent_merkle_tree::event::RawIndexedElement;

/// NET_HEIGHT = HEIGHT - CANOPY_DEPTH
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexedChangelogEntry<I, const NET_HEIGHT: usize>
where
Expand All @@ -8,7 +9,7 @@ where
/// Element that was a subject to the change.
pub element: RawIndexedElement<I>,
/// Merkle proof of that operation.
pub proof: [[u8; 32]; NET_HEIGHT], // TODO: add const generic HEIGHT - CANOPY_DEPTH
pub proof: [[u8; 32]; NET_HEIGHT],
/// Index of a changelog entry in `ConcurrentMerkleTree` corresponding to
/// the same operation.
pub changelog_index: usize,
Expand Down
34 changes: 18 additions & 16 deletions merkle-tree/indexed/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use num_traits::{CheckedAdd, CheckedSub, ToBytes, Unsigned};
use crate::{errors::IndexedMerkleTreeError, IndexedMerkleTree};

#[derive(Debug)]
pub struct IndexedMerkleTreeCopy<H, I, const HEIGHT: usize>(IndexedMerkleTree<H, I, HEIGHT>)
pub struct IndexedMerkleTreeCopy<H, I, const HEIGHT: usize, const NET_HEIGHT: usize>(
IndexedMerkleTree<H, I, HEIGHT, NET_HEIGHT>,
)
where
H: Hasher,
I: CheckedAdd
Expand All @@ -25,7 +27,8 @@ where
+ Unsigned,
usize: From<I>;

impl<H, I, const HEIGHT: usize> IndexedMerkleTreeCopy<H, I, HEIGHT>
impl<H, I, const HEIGHT: usize, const NET_HEIGHT: usize>
IndexedMerkleTreeCopy<H, I, HEIGHT, NET_HEIGHT>
where
H: Hasher,
I: CheckedAdd
Expand Down Expand Up @@ -53,7 +56,7 @@ where
let indexed_changelog_metadata: CyclicBoundedVecMetadata =
unsafe { read_value_at(bytes, &mut offset) };

let expected_size = IndexedMerkleTree::<H, I, HEIGHT>::size_in_account(
let expected_size = IndexedMerkleTree::<H, I, HEIGHT, NET_HEIGHT>::size_in_account(
merkle_tree.height,
merkle_tree.changelog.capacity(),
merkle_tree.roots.capacity(),
Expand All @@ -65,13 +68,8 @@ where
ConcurrentMerkleTreeError::BufferSize(expected_size, bytes.len()),
));
}
println!("pre offset {} ", offset);

let indexed_changelog =
unsafe { read_cyclic_bounded_vec_at(bytes, &mut offset, &indexed_changelog_metadata) };
println!("pre offset {} ", offset);
println!("meta data {:?}", indexed_changelog_metadata);
println!("bytes len {}", bytes.len());

Ok(Self(IndexedMerkleTree {
merkle_tree,
Expand All @@ -81,7 +79,8 @@ where
}
}

impl<H, I, const HEIGHT: usize> Deref for IndexedMerkleTreeCopy<H, I, HEIGHT>
impl<H, I, const HEIGHT: usize, const NET_HEIGHT: usize> Deref
for IndexedMerkleTreeCopy<H, I, HEIGHT, NET_HEIGHT>
where
H: Hasher,
I: CheckedAdd
Expand All @@ -95,7 +94,7 @@ where
+ Unsigned,
usize: From<I>,
{
type Target = IndexedMerkleTree<H, I, HEIGHT>;
type Target = IndexedMerkleTree<H, I, HEIGHT, NET_HEIGHT>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -120,8 +119,9 @@ mod test {
const CANOPY_DEPTH: usize,
const INDEXED_CHANGELOG_SIZE: usize,
const OPERATIONS: usize,
const NET_HEIGHT: usize,
>() {
let mut mt_1 = IndexedMerkleTree::<Poseidon, usize, HEIGHT>::new(
let mut mt_1 = IndexedMerkleTree::<Poseidon, usize, HEIGHT, NET_HEIGHT>::new(
HEIGHT,
CHANGELOG_SIZE,
ROOTS,
Expand All @@ -133,7 +133,7 @@ mod test {

let mut bytes = vec![
0u8;
IndexedMerkleTree::<Poseidon, usize, HEIGHT>::size_in_account(
IndexedMerkleTree::<Poseidon, usize, HEIGHT, NET_HEIGHT>::size_in_account(
HEIGHT,
CHANGELOG_SIZE,
ROOTS,
Expand All @@ -144,7 +144,7 @@ mod test {

{
let mut mt_2 =
IndexedMerkleTreeZeroCopyMut::<Poseidon, usize, HEIGHT>::from_bytes_zero_copy_init(
IndexedMerkleTreeZeroCopyMut::<Poseidon, usize, HEIGHT, NET_HEIGHT>::from_bytes_zero_copy_init(
&mut bytes,
HEIGHT,
CANOPY_DEPTH,
Expand All @@ -163,7 +163,7 @@ mod test {
for _ in 0..OPERATIONS {
// Reload the tree from bytes on each iteration.
let mut mt_2 =
IndexedMerkleTreeZeroCopyMut::<Poseidon, usize, HEIGHT>::from_bytes_zero_copy_mut(
IndexedMerkleTreeZeroCopyMut::<Poseidon, usize, HEIGHT,NET_HEIGHT>::from_bytes_zero_copy_mut(
&mut bytes,
)
.unwrap();
Expand All @@ -177,7 +177,8 @@ mod test {

// Read a copy of that Merkle tree.
let mt_2 =
IndexedMerkleTreeCopy::<Poseidon, usize, HEIGHT>::from_bytes_copy(&bytes).unwrap();
IndexedMerkleTreeCopy::<Poseidon, usize, HEIGHT, NET_HEIGHT>::from_bytes_copy(&bytes)
.unwrap();

assert_eq!(mt_1, *mt_2);
}
Expand All @@ -189,7 +190,7 @@ mod test {
const ROOTS: usize = 2400;
const CANOPY_DEPTH: usize = 10;
const INDEXED_CHANGELOG_SIZE: usize = 256;

const NET_HEIGHT: usize = 16;
const OPERATIONS: usize = 1024;

from_bytes_copy::<
Expand All @@ -199,6 +200,7 @@ mod test {
CANOPY_DEPTH,
INDEXED_CHANGELOG_SIZE,
OPERATIONS,
NET_HEIGHT,
>()
}
}
Loading

0 comments on commit 8077caf

Please sign in to comment.