Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit d241a11

Browse files
committed
refactored tree unitialized check on bytes directly, also moved tree initialization call in a wrapper as it started reporting a stack overflow
1 parent de9d4cf commit d241a11

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

account-compression/programs/account-compression/src/concurrent_tree_wrapper.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,24 @@ pub fn merkle_tree_append_leaf(
8686
) -> Result<Box<ChangeLogEvent>> {
8787
merkle_tree_apply_fn_mut!(header, tree_id, tree_bytes, append, *args)
8888
}
89+
90+
pub fn tree_bytes_unititialized(tree_bytes: &[u8]) -> bool {
91+
tree_bytes.iter().all(|&x| x == 0)
92+
}
93+
94+
#[inline(never)]
95+
pub fn assert_tree_is_empty(
96+
header: &ConcurrentMerkleTreeHeader,
97+
tree_id: Pubkey,
98+
tree_bytes: &mut [u8],
99+
) -> Result<()> {
100+
// If the tree is batch initialized and not finalized yet, we can treat it as empty.
101+
// Before the tree is finalized, the tree_bytes will be all 0 as only the header will be
102+
// initialized at that point, so we may skip the deserialization.
103+
if header.get_is_batch_initialized() && tree_bytes_unititialized(tree_bytes) {
104+
return Ok(());
105+
}
106+
// check the tree is empty
107+
merkle_tree_apply_fn_mut!(header, tree_id, tree_bytes, prove_tree_is_empty,)?;
108+
Ok(())
109+
}

account-compression/programs/account-compression/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub mod spl_account_compression {
267267
let (tree_bytes, canopy_bytes) = rest.split_at_mut(merkle_tree_size);
268268
// ensure the tree is not initialized, the hacky way
269269
require!(
270-
tree_bytes.iter().all(|&x| x == 0),
270+
tree_bytes_unititialized(tree_bytes),
271271
AccountCompressionError::TreeAlreadyInitialized
272272
);
273273
set_canopy_leaf_nodes(
@@ -567,11 +567,8 @@ pub mod spl_account_compression {
567567
let merkle_tree_size = merkle_tree_get_size(&header)?;
568568
let (tree_bytes, canopy_bytes) = rest.split_at_mut(merkle_tree_size);
569569

570-
// Check if the tree is either empty or is batch initialized and not finalized yet.
571-
if !header.get_is_batch_initialized() || !tree_bytes.iter().all(|&x| x == 0) {
572-
let id = ctx.accounts.merkle_tree.key();
573-
merkle_tree_apply_fn_mut!(header, id, tree_bytes, prove_tree_is_empty,)?;
574-
}
570+
let id = ctx.accounts.merkle_tree.key();
571+
assert_tree_is_empty(&header, id, tree_bytes)?;
575572

576573
// Close merkle tree account
577574
// 1. Move lamports

0 commit comments

Comments
 (0)