Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl ZNewAddressParamsAssignedPackedMut<'_> {
) {
self.seed = seed;
self.address_merkle_tree_root_index = address_merkle_tree_root_index;
self.address_queue_account_index = 0; // always 0 for v2 address trees.
self.address_queue_account_index = address_merkle_tree_account_index;
if let Some(assigned_account_index) = assigned_account_index {
self.assigned_account_index = assigned_account_index;
self.assigned_to_account = 1; // set to true
Expand Down
14 changes: 7 additions & 7 deletions program-libs/compressed-account/tests/zero_copy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn test_new_address_set_success_with_assigned_account() {
assert_eq!(z_new_address.assigned_account_index, 7);
assert_eq!(z_new_address.address_merkle_tree_account_index, 10);
assert_eq!(z_new_address.assigned_to_account, 1);
assert_eq!(z_new_address.address_queue_account_index, 0); // Invariant: always 0
assert_eq!(z_new_address.address_queue_account_index, 10);
}

#[test]
Expand Down Expand Up @@ -567,7 +567,7 @@ fn test_new_address_set_success_without_assigned_account() {
assert_eq!(z_new_address.assigned_account_index, 0);
assert_eq!(z_new_address.address_merkle_tree_account_index, 10);
assert_eq!(z_new_address.assigned_to_account, 0);
assert_eq!(z_new_address.address_queue_account_index, 0); // Invariant: always 0
assert_eq!(z_new_address.address_queue_account_index, 10);
}

#[test]
Expand All @@ -580,16 +580,16 @@ fn test_new_address_set_invariant_address_queue_account_index() {
NewAddressParamsAssignedPacked::zero_copy_at_mut(&mut data).unwrap();

// Execute multiple times with different inputs
// Invariant: address_queue_account_index == address_merkle_tree_account_index
// for v2 batched address trees (queue is embedded in the tree account).
z_new_address.set([1u8; 32], U16::new(100), Some(5), 10);
assert_eq!(z_new_address.address_queue_account_index, 0);
assert_eq!(z_new_address.address_queue_account_index, 10);

z_new_address.set([2u8; 32], U16::new(200), None, 20);
assert_eq!(z_new_address.address_queue_account_index, 0);
assert_eq!(z_new_address.address_queue_account_index, 20);

z_new_address.set([3u8; 32], U16::new(300), Some(50), 30);
assert_eq!(z_new_address.address_queue_account_index, 0);

// Assert invariant: address_queue_account_index always 0 for v2 address trees
assert_eq!(z_new_address.address_queue_account_index, 30);
}

// =============================================================================
Expand Down
14 changes: 13 additions & 1 deletion programs/system/src/processor/create_address_cpi_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ pub fn derive_new_addresses<'info, 'a, 'b: 'a, const ADDRESS_ASSIGNMENT: bool>(
Err(SystemProgramError::DeriveAddressError)
}?;

cpi_ix_data.addresses[i].tree_index = context.get_index_or_insert(
let tree_index = context.get_index_or_insert(
new_address_params.address_merkle_tree_account_index(),
remaining_accounts,
"V2 address tree",
)?;
cpi_ix_data.addresses[i].tree_index = tree_index;
cpi_ix_data.addresses[i].queue_index = tree_index;

context.set_address_fee(
tree.metadata.rollover_metadata.network_fee,
Expand All @@ -83,6 +85,16 @@ pub fn derive_new_addresses<'info, 'a, 'b: 'a, const ADDRESS_ASSIGNMENT: bool>(
tree.queue_batches.next_index,
);

// For batched address trees the queue is embedded in the tree
// account so both indices must point to the same account.
if new_address_params.address_queue_index()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we explicitly set it in line 74 and 75, does that make this check redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check addresses system program instruction data. The assignment is account compression program cpi instruction data. It's a safeguard in case that we are using the indices in other functions.

!= new_address_params.address_merkle_tree_account_index()
{
return Err(ProgramError::from(
SystemProgramError::AddressMerkleTreeAccountDiscriminatorMismatch,
));
}

(
derive_address(
&new_address_params.seed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3863,7 +3863,7 @@ async fn test_d9_edge_many_literals() {
#[tokio::test]
async fn test_d9_edge_mixed() {
use csdk_anchor_full_derived_test::d9_seeds::{
edge_cases::{AB, SEED_123, _UNDERSCORE_CONST},
edge_cases::{_UNDERSCORE_CONST, AB, SEED_123},
D9EdgeMixedParams,
};

Expand Down