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
4 changes: 3 additions & 1 deletion program-libs/batched-merkle-tree/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub const STATE_BLOOM_FILTER_NUM_HASHES: u64 = 10;
pub const ADDRESS_BLOOM_FILTER_CAPACITY: u64 = 2_301_536;
pub const ADDRESS_BLOOM_FILTER_NUM_HASHES: u64 = 10;

pub const DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE: u64 = 20 * 1024 + 8;
#[deprecated(note = "Use DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2 instead")]
pub const DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V1: u64 = 20 * 1024 + 8;
pub const DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2: u64 = 14020;

pub const ADDRESS_TREE_INIT_ROOT_40: [u8; 32] = [
28, 65, 107, 255, 208, 234, 51, 3, 131, 95, 62, 130, 202, 177, 176, 26, 216, 81, 64, 184, 200,
Expand Down
10 changes: 5 additions & 5 deletions program-libs/batched-merkle-tree/src/initialize_state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use light_merkle_tree_metadata::{

use crate::{
constants::{
DEFAULT_BATCH_SIZE, DEFAULT_BATCH_STATE_TREE_HEIGHT, DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE,
DEFAULT_BATCH_SIZE, DEFAULT_BATCH_STATE_TREE_HEIGHT, DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
DEFAULT_ZKP_BATCH_SIZE,
},
errors::BatchedMerkleTreeError,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Default for InitStateTreeAccountsInstructionData {
index: 0,
program_owner: None,
forester: None,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
bloom_filter_num_iters: 3,
input_queue_batch_size: DEFAULT_BATCH_SIZE,
output_queue_batch_size: DEFAULT_BATCH_SIZE,
Expand Down Expand Up @@ -294,7 +294,7 @@ pub mod test_utils {
index: 0,
program_owner: None,
forester: None,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
bloom_filter_num_iters: 3,
input_queue_batch_size: TEST_DEFAULT_BATCH_SIZE,
output_queue_batch_size: TEST_DEFAULT_BATCH_SIZE,
Expand All @@ -314,7 +314,7 @@ pub mod test_utils {
index: 0,
program_owner: None,
forester: None,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
bloom_filter_num_iters: 3,
input_queue_batch_size: 500,
output_queue_batch_size: 500,
Expand All @@ -334,7 +334,7 @@ pub mod test_utils {
index: 0,
program_owner: None,
forester: None,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE,
additional_bytes: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
bloom_filter_num_iters: STATE_BLOOM_FILTER_NUM_HASHES,
input_queue_batch_size: 15000,
output_queue_batch_size: 15000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,64 @@ impl DerefMut for InsertIntoQueuesInstructionDataMut<'_> {
mod test {

use super::*;
#[test]
fn test_ix_data() {
{
let leaves_capacity: u8 = 20;
let nullifiers_capacity: u8 = 20;
let addresses_capacity: u8 = 0;
let num_output_trees: u8 = 10;
let num_input_trees: u8 = 10;
let num_address_trees: u8 = 0;
let size = InsertIntoQueuesInstructionDataMut::required_size_for_capacity(
leaves_capacity,
nullifiers_capacity,
addresses_capacity,
num_output_trees,
num_input_trees,
num_address_trees,
);
println!("size update 20 pdas {}", size);
assert_eq!(size, 3165, "size update 20 pdas");
}
{
let leaves_capacity: u8 = 20;
let nullifiers_capacity: u8 = 0;
let addresses_capacity: u8 = 20;
let num_output_trees: u8 = 10;
let num_input_trees: u8 = 0;
let num_address_trees: u8 = 1;
let size = InsertIntoQueuesInstructionDataMut::required_size_for_capacity(
leaves_capacity,
nullifiers_capacity,
addresses_capacity,
num_output_trees,
num_input_trees,
num_address_trees,
);
println!("size create 20 pdas {}", size);
assert_eq!(size, 2345, "size create 20 pdas");
}
{
let leaves_capacity: u8 = 30;
let nullifiers_capacity: u8 = 0;
let addresses_capacity: u8 = 0;
let num_output_trees: u8 = 10;
let num_input_trees: u8 = 0;
let num_address_trees: u8 = 0;
let size = InsertIntoQueuesInstructionDataMut::required_size_for_capacity(
leaves_capacity,
nullifiers_capacity,
addresses_capacity,
num_output_trees,
num_input_trees,
num_address_trees,
);
println!("size create 30 ctokens {}", size);
assert_eq!(size, 1955, "size create 30 ctokens");
}
}

#[test]
fn test_rnd_insert_into_queues_ix_data() {
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
Expand Down
2 changes: 1 addition & 1 deletion program-tests/compressed-token-test/tests/mint/failing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![cfg(feature = "test-sbf")]
#![cfg(feature = "test-sbf")]

use anchor_lang::prelude::borsh::BorshDeserialize;
use light_client::indexer::Indexer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![cfg(feature = "test-sbf")]
#![cfg(feature = "test-sbf")]

use anchor_lang::{InstructionData, ToAccountMetas};
use light_program_test::{program_test::LightProgramTest, Indexer, ProgramTestConfig, Rpc};
Expand Down
14 changes: 11 additions & 3 deletions program-tests/system-test/tests/test_re_init_cpi_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use anchor_lang::Discriminator;
use light_account_checks::account_info::test_account_info::pinocchio::get_account_info;
use light_batched_merkle_tree::constants::DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE;
use light_batched_merkle_tree::constants::DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2;
const DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V1: u64 = 20 * 1024 + 8;

use light_program_test::{
program_test::{LightProgramTest, TestRpc},
ProgramTestConfig,
Expand Down Expand Up @@ -68,7 +70,8 @@ async fn test_re_init_cpi_account() {
);
assert_eq!(
pre_account.data.len(),
DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE as usize
DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V1 as usize,
"Legacy account should have V1 size"
);

// Create reinit instruction
Expand All @@ -92,6 +95,11 @@ async fn test_re_init_cpi_account() {
&CPI_CONTEXT_ACCOUNT_2_DISCRIMINATOR,
"Account should have new discriminator after reinit"
);
assert_eq!(
post_account.data.len(),
DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2 as usize,
"Account should be resized to V2 size after reinit"
);

// Verify merkle tree is preserved
// Legacy layout: discriminator (8) + fee_payer (32) + merkle_tree (32)
Expand Down Expand Up @@ -122,7 +130,7 @@ async fn test_re_init_cpi_account() {

// Deserialize the account to verify vector capacities
let deserialized = deserialize_cpi_context_account(&account_info).unwrap();

assert_eq!(deserialized.remaining_capacity(), 6500);
// Verify vector capacities match CpiContextAccountInitParams defaults
assert_eq!(
deserialized.new_addresses.capacity(),
Expand Down
5 changes: 3 additions & 2 deletions programs/registry/src/protocol_config/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use aligned_sized::aligned_sized;
use anchor_lang::prelude::*;
use light_batched_merkle_tree::constants::DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2;

use crate::errors::RegistryError;

Expand Down Expand Up @@ -57,7 +58,7 @@ impl Default for ProtocolConfig {
active_phase_length: 1000,
report_work_phase_length: 100,
network_fee: 5000,
cpi_context_size: 20 * 1024 + 8,
cpi_context_size: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
finalize_counter_limit: 100,
place_holder: Pubkey::default(),
address_network_fee: 10000,
Expand All @@ -80,7 +81,7 @@ impl ProtocolConfig {
active_phase_length: 1000,
report_work_phase_length: 100,
network_fee: 5000,
cpi_context_size: 20 * 1024 + 8,
cpi_context_size: DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2,
finalize_counter_limit: 100,
place_holder: Pubkey::default(),
address_network_fee: 10000,
Expand Down
13 changes: 12 additions & 1 deletion programs/system/src/accounts/init_context_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use light_account_checks::{
checks::{check_owner, check_signer},
discriminator::Discriminator,
};
use light_batched_merkle_tree::merkle_tree::BatchedMerkleTreeAccount;
use light_batched_merkle_tree::{
constants::DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2, merkle_tree::BatchedMerkleTreeAccount,
};
use light_compressed_account::constants::{
ACCOUNT_COMPRESSION_PROGRAM_ID, STATE_MERKLE_TREE_ACCOUNT_DISCRIMINATOR,
};
Expand Down Expand Up @@ -71,12 +73,21 @@ pub fn reinit_cpi_context_account(accounts: &[AccountInfo]) -> Result<()> {
return Err(ProgramError::NotEnoughAccountKeys);
}
let cpi_context_account = &accounts[0];

// Check owner before realloc
check_owner(&crate::ID, cpi_context_account)?;

// Read associated_merkle_tree BEFORE resizing (in case resize truncates data)
let associated_merkle_tree = {
let data = cpi_context_account.try_borrow_data()?;
CpiContextAccount::deserialize(&mut &data[8..])
.map_err(|_| ProgramError::BorshIoError)?
.associated_merkle_tree
};

// Realloc account to new size (14020 bytes)
cpi_context_account.resize(DEFAULT_CPI_CONTEXT_ACCOUNT_SIZE_V2 as usize)?;

let params: CpiContextAccountInitParams =
CpiContextAccountInitParams::new(associated_merkle_tree);
cpi_context_account_new::<true>(cpi_context_account, params)?;
Expand Down
4 changes: 4 additions & 0 deletions programs/system/src/cpi_context/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl<'a> ZCpiContextAccount2<'a> {
self.output_data_len.get()
}

pub fn remaining_capacity(&self) -> usize {
self.remaining_data.len()
}

/// Calculate the byte offsets for output data in the serialized account
/// Returns (start_offset, end_offset) where:
/// - start_offset: byte position where total_output_data_len field begins
Expand Down
2 changes: 1 addition & 1 deletion sdk-tests/sdk-pinocchio-v1-test/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![cfg(feature = "test-sbf")]
#![cfg(feature = "test-sbf")]

use borsh::BorshSerialize;
use light_compressed_account::compressed_account::CompressedAccountWithMerkleContext;
Expand Down
2 changes: 1 addition & 1 deletion sdk-tests/sdk-token-test/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![cfg(feature = "test-sbf")]
#![cfg(feature = "test-sbf")]

use anchor_lang::{AccountDeserialize, InstructionData};
use anchor_spl::token::TokenAccount;
Expand Down
Loading