Skip to content

Commit b48bb44

Browse files
committed
refactor: transfer2 extract_tree_accounts
1 parent 81a7c5c commit b48bb44

File tree

4 files changed

+15
-33
lines changed

4 files changed

+15
-33
lines changed

programs/compressed-token/program/src/transfer2/accounts.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anchor_compressed_token::ErrorCode;
22
use anchor_lang::solana_program::program_error::ProgramError;
33
use light_account_checks::packed_accounts::ProgramPackedAccounts;
4-
use light_ctoken_types::instructions::transfer2::ZCompressedTokenInstructionDataTransfer2;
4+
use light_sdk_types::ACCOUNT_COMPRESSION_PROGRAM_ID;
55
use pinocchio::{account_info::AccountInfo, pubkey::Pubkey};
66
use spl_pod::solana_msg::msg;
77

@@ -90,17 +90,16 @@ impl<'info> Transfer2Accounts<'info> {
9090
pub fn cpi_accounts(
9191
&self,
9292
all_accounts: &'info [AccountInfo],
93-
inputs: &ZCompressedTokenInstructionDataTransfer2,
9493
packed_accounts: &'info ProgramPackedAccounts<'info, AccountInfo>,
9594
) -> Result<(&'info [AccountInfo], Vec<&'info Pubkey>), ProgramError> {
9695
// Extract tree accounts using highest index approach
97-
let (tree_accounts, tree_accounts_count) = extract_tree_accounts(inputs, packed_accounts)?;
96+
let tree_accounts = extract_tree_accounts(packed_accounts);
9897

9998
// Calculate static accounts count after skipping index 0 (system accounts only)
10099
let static_accounts_count = self.static_accounts_count()?;
101100

102101
// Include static CPI accounts + tree accounts based on highest tree index
103-
let cpi_accounts_end = 1 + static_accounts_count + tree_accounts_count;
102+
let cpi_accounts_end = 1 + static_accounts_count + tree_accounts.len();
104103
if all_accounts.len() < cpi_accounts_end {
105104
msg!(
106105
"Accounts len {} < expected cpi accounts len {}",
@@ -118,29 +117,13 @@ impl<'info> Transfer2Accounts<'info> {
118117
// TODO: unit test.
119118
/// Extract tree accounts by finding the highest tree index and using it as closing offset
120119
pub fn extract_tree_accounts<'info>(
121-
inputs: &ZCompressedTokenInstructionDataTransfer2,
122120
packed_accounts: &'info ProgramPackedAccounts<'info, AccountInfo>,
123-
) -> Result<(Vec<&'info Pubkey>, usize), ProgramError> {
124-
// Find highest tree index from input and output data to determine tree accounts range
125-
let mut highest_tree_index = 0u8;
126-
for input_data in inputs.in_token_data.iter() {
127-
highest_tree_index =
128-
highest_tree_index.max(input_data.merkle_context.merkle_tree_pubkey_index);
129-
highest_tree_index = highest_tree_index.max(input_data.merkle_context.queue_pubkey_index);
130-
}
131-
for output_data in inputs.out_token_data.iter() {
132-
highest_tree_index = highest_tree_index.max(output_data.merkle_tree);
133-
}
134-
135-
// Tree accounts span from index 0 to highest_tree_index in remaining accounts
136-
let tree_accounts_count = highest_tree_index + 1;
137-
// Extract tree account pubkeys from the determined range
138-
// Note: Don't switch to ArrayVec it results in weird memory access with non deterministic values.
139-
let mut tree_accounts = Vec::with_capacity(tree_accounts_count.into());
140-
for i in 0..tree_accounts_count {
141-
let account_key = packed_accounts.get_u8(i, "tree account")?.key();
142-
tree_accounts.push(account_key);
121+
) -> Vec<&'info Pubkey> {
122+
let mut tree_accounts = Vec::with_capacity(8);
123+
for account_info in packed_accounts.accounts {
124+
if account_info.is_owned_by(&ACCOUNT_COMPRESSION_PROGRAM_ID) {
125+
tree_accounts.push(account_info.key());
126+
}
143127
}
144-
145-
Ok((tree_accounts, tree_accounts_count.into()))
128+
tree_accounts
146129
}

programs/compressed-token/program/src/transfer2/native_compression/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn process_token_compression(
6464
solana_pubkey::Pubkey::new_from_array(*source_or_recipient.key())
6565
);
6666
msg!("Invalid token program ID {:?}", unsafe {
67-
source_or_recipient.owner()
67+
solana_pubkey::Pubkey::from(*source_or_recipient.owner())
6868
});
6969
return Err(ProgramError::InvalidInstructionData);
7070
}

programs/compressed-token/program/src/transfer2/processor.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,8 @@ pub fn process_transfer2(
161161
bench_sbf_end!("t_sum_check");
162162
if let Some(system_accounts) = validated_accounts.system.as_ref() {
163163
// Get CPI accounts slice and tree accounts for light-system-program invocation
164-
let (cpi_accounts, tree_pubkeys) = validated_accounts.cpi_accounts(
165-
accounts,
166-
&inputs,
167-
&validated_accounts.packed_accounts,
168-
)?;
164+
let (cpi_accounts, tree_pubkeys) =
165+
validated_accounts.cpi_accounts(accounts, &validated_accounts.packed_accounts)?;
169166
// Debug prints keep for now.
170167
{
171168
let _solana_tree_accounts = tree_pubkeys

programs/system/src/processor/create_inputs_cpi_data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub fn create_inputs_cpi_data<'a, 'info, T: InstructionData<'a>>(
5454
if current_mt_index != merkle_context.merkle_tree_pubkey_index || is_first_iter {
5555
is_first_iter = false;
5656
current_mt_index = merkle_context.merkle_tree_pubkey_index;
57+
solana_msg::msg!("current_mt_index {}", current_mt_index);
58+
solana_msg::msg!("accounts {}", accounts.len());
5759
current_hashed_mt = match &accounts[current_mt_index as usize] {
5860
AcpAccount::BatchedStateTree(tree) => {
5961
context.set_network_fee(

0 commit comments

Comments
 (0)