Skip to content

Commit b9eebe4

Browse files
authored
chore: add address tree height error (#1590)
1 parent c0b63e8 commit b9eebe4

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

program-libs/batched-merkle-tree/src/merkle_tree.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
287287
// Root for binary Merkle tree with all zero leaves.
288288
root_history.push(light_hasher::Poseidon::zero_bytes()[height as usize]);
289289
} else if tree_type == TreeType::BatchedAddress {
290+
// Sanity check since init value is hardcoded.
291+
#[cfg(not(test))]
292+
if height != 40 {
293+
return Err(MerkleTreeMetadataError::InvalidHeight.into());
294+
}
290295
// Initialized indexed Merkle tree root.
291296
// See https://github.com/Lightprotocol/light-protocol/blob/c143c24f95c901e2eac96bc2bd498719958192cf/program-libs/indexed-merkle-tree/src/reference.rs#L69
292297
root_history.push(ADDRESS_TREE_INIT_ROOT_40);
@@ -1130,7 +1135,7 @@ mod test {
11301135
root_history_len,
11311136
batch_size,
11321137
zkp_batch_size,
1133-
10,
1138+
40,
11341139
num_iter,
11351140
bloom_filter_capacity,
11361141
TreeType::BatchedAddress,
@@ -1569,7 +1574,7 @@ mod test {
15691574
let num_iter = 1;
15701575
let mut current_slot = 1;
15711576
let bloom_filter_capacity = 8000;
1572-
let height = 4;
1577+
let height = 40;
15731578
let mut account = BatchedMerkleTreeAccount::init(
15741579
&mut account_data,
15751580
&Pubkey::new_unique(),
@@ -1703,7 +1708,7 @@ mod test {
17031708
let root_history_len = 10;
17041709
let num_iter = 1;
17051710
let bloom_filter_capacity = 8000;
1706-
let height = 4;
1711+
let height = 40;
17071712
let pubkey = Pubkey::new_unique();
17081713
let mut account = BatchedMerkleTreeAccount::init(
17091714
&mut account_data,
@@ -1741,7 +1746,7 @@ mod test {
17411746
let root_history_len = 10;
17421747
let num_iter = 1;
17431748
let bloom_filter_capacity = 8000;
1744-
let height = 4;
1749+
let height = 40;
17451750
let pubkey = Pubkey::new_unique();
17461751
let associated_queue = Pubkey::new_unique();
17471752
let account = BatchedMerkleTreeAccount::init(

program-libs/batched-merkle-tree/tests/initialize_address_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn test_rnd_account_init() {
6969
rollover_threshold: Some(rng.gen_range(0..100)),
7070
close_threshold: None,
7171
root_history_capacity: rng.gen_range(1..1000),
72-
height: rng.gen_range(1..32),
72+
height: 40,
7373
};
7474

7575
let mt_account_size = get_merkle_tree_account_size(

program-libs/batched-merkle-tree/tests/rollover_address_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn test_rnd_rollover() {
187187
rollover_threshold: Some(rng.gen_range(0..100)),
188188
close_threshold: None,
189189
root_history_capacity: rng.gen_range(1..1000),
190-
height: rng.gen_range(1..32),
190+
height: 40,
191191
};
192192
if forester.is_some() {
193193
params.network_fee = None;
@@ -307,7 +307,7 @@ fn test_batched_tree_is_ready_for_rollover() {
307307
height,
308308
num_iter,
309309
bloom_filter_capacity,
310-
TreeType::BatchedAddress,
310+
TreeType::BatchedState,
311311
)
312312
.unwrap();
313313

@@ -318,7 +318,7 @@ fn test_batched_tree_is_ready_for_rollover() {
318318
);
319319

320320
let tree_capacity = 2u64.pow(height);
321-
let start_index = 2;
321+
let start_index = 0;
322322
let rollover_threshold =
323323
tree_capacity * metadata.rollover_metadata.rollover_threshold / 100 - start_index;
324324
// fill tree almost to the rollover threshold

program-libs/merkle-tree-metadata/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum MerkleTreeMetadataError {
1818
InvalidTreeType,
1919
#[error("Invalid Rollover Threshold.")]
2020
InvalidRolloverThreshold,
21+
#[error("Invalid Height.")]
22+
InvalidHeight,
2123
}
2224

2325
#[cfg(feature = "solana")]
@@ -32,6 +34,7 @@ impl From<MerkleTreeMetadataError> for u32 {
3234
MerkleTreeMetadataError::NotReadyForRollover => 14006,
3335
MerkleTreeMetadataError::InvalidTreeType => 14007,
3436
MerkleTreeMetadataError::InvalidRolloverThreshold => 14008,
37+
MerkleTreeMetadataError::InvalidHeight => 14009,
3538
}
3639
}
3740
}

programs/system/src/processor/cpi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn create_cpi_data_and_context<
3939
];
4040
let account_indices =
4141
Vec::<u8>::with_capacity((num_nullifiers + num_leaves + num_new_addresses) as usize);
42+
// Min (remaining accounts or num values) for there cannot be more trees than accounts or values.
4243
let bytes_size = InsertIntoQueuesInstructionDataMut::required_size_for_capacity(
4344
num_leaves,
4445
num_nullifiers,

0 commit comments

Comments
 (0)