From 4da1aaafeffdcaf690ad7edb474a879ec362cc5f Mon Sep 17 00:00:00 2001 From: ananas Date: Wed, 5 Nov 2025 16:39:49 +0000 Subject: [PATCH] test: add remaining cpi context account capacity assert to reinit cpi context account test --- .../batched-merkle-tree/src/constants.rs | 4 +- .../src/initialize_state_tree.rs | 10 ++-- .../instruction_data/insert_into_queues.rs | 58 +++++++++++++++++++ .../tests/mint/failing.rs | 2 +- .../tests/test_cpi_context_event.rs | 2 +- .../tests/test_re_init_cpi_account.rs | 14 ++++- .../registry/src/protocol_config/state.rs | 5 +- .../src/accounts/init_context_account.rs | 13 ++++- programs/system/src/cpi_context/state.rs | 4 ++ sdk-tests/sdk-pinocchio-v1-test/tests/test.rs | 2 +- sdk-tests/sdk-token-test/tests/test.rs | 2 +- 11 files changed, 100 insertions(+), 16 deletions(-) diff --git a/program-libs/batched-merkle-tree/src/constants.rs b/program-libs/batched-merkle-tree/src/constants.rs index 85fc5f86bf..85e13857f8 100644 --- a/program-libs/batched-merkle-tree/src/constants.rs +++ b/program-libs/batched-merkle-tree/src/constants.rs @@ -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, diff --git a/program-libs/batched-merkle-tree/src/initialize_state_tree.rs b/program-libs/batched-merkle-tree/src/initialize_state_tree.rs index b6863a553c..4c5bf0a5b0 100644 --- a/program-libs/batched-merkle-tree/src/initialize_state_tree.rs +++ b/program-libs/batched-merkle-tree/src/initialize_state_tree.rs @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/program-libs/compressed-account/src/instruction_data/insert_into_queues.rs b/program-libs/compressed-account/src/instruction_data/insert_into_queues.rs index 57206dbfef..077a21d58d 100644 --- a/program-libs/compressed-account/src/instruction_data/insert_into_queues.rs +++ b/program-libs/compressed-account/src/instruction_data/insert_into_queues.rs @@ -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}; diff --git a/program-tests/compressed-token-test/tests/mint/failing.rs b/program-tests/compressed-token-test/tests/mint/failing.rs index ac008b84b9..31ec33b0ae 100644 --- a/program-tests/compressed-token-test/tests/mint/failing.rs +++ b/program-tests/compressed-token-test/tests/mint/failing.rs @@ -1,4 +1,4 @@ -// #![cfg(feature = "test-sbf")] +#![cfg(feature = "test-sbf")] use anchor_lang::prelude::borsh::BorshDeserialize; use light_client::indexer::Indexer; diff --git a/program-tests/system-cpi-test/tests/test_cpi_context_event.rs b/program-tests/system-cpi-test/tests/test_cpi_context_event.rs index 391ca87256..5dcbc69457 100644 --- a/program-tests/system-cpi-test/tests/test_cpi_context_event.rs +++ b/program-tests/system-cpi-test/tests/test_cpi_context_event.rs @@ -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}; diff --git a/program-tests/system-test/tests/test_re_init_cpi_account.rs b/program-tests/system-test/tests/test_re_init_cpi_account.rs index 057a112dcf..8f18f3e767 100644 --- a/program-tests/system-test/tests/test_re_init_cpi_account.rs +++ b/program-tests/system-test/tests/test_re_init_cpi_account.rs @@ -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, @@ -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 @@ -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) @@ -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(), diff --git a/programs/registry/src/protocol_config/state.rs b/programs/registry/src/protocol_config/state.rs index 04ade39f1d..eaaa41b2b1 100644 --- a/programs/registry/src/protocol_config/state.rs +++ b/programs/registry/src/protocol_config/state.rs @@ -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; @@ -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, @@ -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, diff --git a/programs/system/src/accounts/init_context_account.rs b/programs/system/src/accounts/init_context_account.rs index 9529319dc6..82238c9f35 100644 --- a/programs/system/src/accounts/init_context_account.rs +++ b/programs/system/src/accounts/init_context_account.rs @@ -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, }; @@ -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::(cpi_context_account, params)?; diff --git a/programs/system/src/cpi_context/state.rs b/programs/system/src/cpi_context/state.rs index a5b6e4b031..d9acfa94f6 100644 --- a/programs/system/src/cpi_context/state.rs +++ b/programs/system/src/cpi_context/state.rs @@ -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 diff --git a/sdk-tests/sdk-pinocchio-v1-test/tests/test.rs b/sdk-tests/sdk-pinocchio-v1-test/tests/test.rs index e9ad1fd4ad..0ae7f5c029 100644 --- a/sdk-tests/sdk-pinocchio-v1-test/tests/test.rs +++ b/sdk-tests/sdk-pinocchio-v1-test/tests/test.rs @@ -1,4 +1,4 @@ -// #![cfg(feature = "test-sbf")] +#![cfg(feature = "test-sbf")] use borsh::BorshSerialize; use light_compressed_account::compressed_account::CompressedAccountWithMerkleContext; diff --git a/sdk-tests/sdk-token-test/tests/test.rs b/sdk-tests/sdk-token-test/tests/test.rs index bbef7d1816..2cd0d623cf 100644 --- a/sdk-tests/sdk-token-test/tests/test.rs +++ b/sdk-tests/sdk-token-test/tests/test.rs @@ -1,4 +1,4 @@ -// #![cfg(feature = "test-sbf")] +#![cfg(feature = "test-sbf")] use anchor_lang::{AccountDeserialize, InstructionData}; use anchor_spl::token::TokenAccount;