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

Commit 9230120

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 13699fb commit 9230120

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,31 @@ pub fn merkle_tree_prove_leaf(
6363
) -> Result<Box<ChangeLogEvent>> {
6464
merkle_tree_apply_fn!(header, tree_id, tree_bytes, prove_leaf, args)
6565
}
66+
67+
pub fn merkle_tree_initialize(
68+
header: &ConcurrentMerkleTreeHeader,
69+
tree_id: Pubkey,
70+
tree_bytes: &mut [u8],
71+
) -> Result<Box<ChangeLogEvent>> {
72+
merkle_tree_apply_fn_mut!(header, tree_id, tree_bytes, initialize,)
73+
}
74+
75+
pub fn tree_bytes_unititialized(tree_bytes: &[u8]) -> bool {
76+
tree_bytes.iter().all(|&x| x == 0)
77+
}
78+
79+
pub fn assert_tree_is_empty(
80+
header: &ConcurrentMerkleTreeHeader,
81+
tree_id: Pubkey,
82+
tree_bytes: &mut [u8],
83+
) -> Result<()> {
84+
// If the tree is batch initialized and not finalized yet, we can treat it as empty.
85+
// Before the tree is finalized, the tree_bytes will be all 0 as only the header will be
86+
// initialized at that point, so we may skip the deserialization.
87+
if header.get_is_batch_initialized() && tree_bytes_unititialized(tree_bytes) {
88+
return Ok(());
89+
}
90+
// check the tree is empty
91+
merkle_tree_apply_fn_mut!(header, tree_id, tree_bytes, prove_tree_is_empty,)?;
92+
Ok(())
93+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub mod spl_account_compression {
174174
let merkle_tree_size = merkle_tree_get_size(&header)?;
175175
let (tree_bytes, canopy_bytes) = rest.split_at_mut(merkle_tree_size);
176176
let id = ctx.accounts.merkle_tree.key();
177-
let change_log_event = merkle_tree_apply_fn_mut!(header, id, tree_bytes, initialize,)?;
177+
let change_log_event = merkle_tree_initialize(&header, id, tree_bytes)?;
178178
wrap_event(
179179
&AccountCompressionEvent::ChangeLog(*change_log_event),
180180
&ctx.accounts.noop,
@@ -264,7 +264,7 @@ pub mod spl_account_compression {
264264
let (tree_bytes, canopy_bytes) = rest.split_at_mut(merkle_tree_size);
265265
// ensure the tree is not initialized, the hacky way
266266
require!(
267-
tree_bytes.iter().all(|&x| x == 0),
267+
tree_bytes_unititialized(tree_bytes),
268268
AccountCompressionError::TreeAlreadyInitialized
269269
);
270270
set_canopy_leaf_nodes(
@@ -564,11 +564,8 @@ pub mod spl_account_compression {
564564
let merkle_tree_size = merkle_tree_get_size(&header)?;
565565
let (tree_bytes, canopy_bytes) = rest.split_at_mut(merkle_tree_size);
566566

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

573570
// Close merkle tree account
574571
// 1. Move lamports

0 commit comments

Comments
 (0)