Skip to content

Commit ad97e47

Browse files
wip
1 parent 4a1635e commit ad97e47

File tree

10 files changed

+154
-59
lines changed

10 files changed

+154
-59
lines changed

js/stateless.js/src/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const localTestActiveStateTreeInfos = (): TreeInfo[] => {
167167
{
168168
tree: new PublicKey(batchMerkleTree),
169169
queue: new PublicKey(batchQueue),
170-
cpiContext: PublicKey.default,
170+
cpiContext: new PublicKey(batchCpiContext),
171171
treeType: TreeType.StateV2,
172172
nextTreeInfo: null,
173173
},
@@ -258,7 +258,8 @@ export const cpiContext2Pubkey = 'cpi2cdhkH5roePvcudTgUL8ppEBfTay1desGh8G8QxK';
258258
// V2 testing.
259259
export const batchMerkleTree = 'HLKs5NJ8FXkJg8BrzJt56adFYYuwg5etzDtBbQYTsixu'; // v2 merkle tree (includes nullifier queue)
260260
export const batchQueue = '6L7SzhYB3anwEQ9cphpJ1U7Scwj57bx2xueReg7R9cKU'; // v2 output queue
261-
261+
// export const batchCpiContext = '7Hp52chxaew8bW1ApR4fck2bh6Y8qA1pu3qwH6N9zaLj';
262+
export const batchCpiContext = 'HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R';
262263
export const confirmConfig: ConfirmOptions = {
263264
commitment: 'confirmed',
264265
preflightCommitment: 'confirmed',

js/stateless.js/src/utils/validation.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CompressedAccountWithMerkleContext,
55
bn,
66
} from '../state';
7+
import { featureFlags } from '../constants';
78

89
export const validateSufficientBalance = (balance: BN) => {
910
if (balance.lt(bn(0))) {
@@ -38,7 +39,15 @@ export const validateNumbersForProof = (
3839
`Invalid number of compressed accounts for proof: ${hashesLength}. Allowed numbers: ${[1, 2, 3, 4].join(', ')}`,
3940
);
4041
}
41-
validateNumbers(hashesLength, [1, 2, 3, 4], 'compressed accounts');
42+
if (!featureFlags.isV2()) {
43+
validateNumbers(hashesLength, [1, 2, 3, 4], 'compressed accounts');
44+
} else {
45+
validateNumbers(
46+
hashesLength,
47+
[1, 2, 3, 4, 8],
48+
'compressed accounts',
49+
);
50+
}
4251
validateNumbersForNonInclusionProof(newAddressesLength);
4352
} else {
4453
if (hashesLength > 0) {
@@ -51,14 +60,22 @@ export const validateNumbersForProof = (
5160

5261
/// Ensure that the amount if compressed accounts is allowed.
5362
export const validateNumbersForInclusionProof = (hashesLength: number) => {
54-
validateNumbers(hashesLength, [1, 2, 3, 4, 8], 'compressed accounts');
63+
if (!featureFlags.isV2()) {
64+
validateNumbers(hashesLength, [1, 2, 3, 4], 'compressed accounts');
65+
} else {
66+
validateNumbers(hashesLength, [1, 2, 3, 4, 8], 'compressed accounts');
67+
}
5568
};
5669

5770
/// Ensure that the amount if new addresses is allowed.
5871
export const validateNumbersForNonInclusionProof = (
5972
newAddressesLength: number,
6073
) => {
61-
validateNumbers(newAddressesLength, [1, 2], 'new addresses');
74+
if (!featureFlags.isV2()) {
75+
validateNumbers(newAddressesLength, [1, 2], 'new addresses');
76+
} else {
77+
validateNumbers(newAddressesLength, [1, 2, 3, 4], 'new addresses');
78+
}
6279
};
6380

6481
/// V1 circuit safeguards.

program-libs/account-checks/src/checks.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use solana_msg::msg;
2+
use solana_pubkey::Pubkey;
3+
14
use crate::{
25
discriminator::{Discriminator, DISCRIMINATOR_LEN},
36
error::AccountError,
@@ -130,6 +133,12 @@ pub fn check_owner<A: AccountInfoTrait>(
130133
account_info: &A,
131134
) -> Result<(), AccountError> {
132135
if !account_info.is_owned_by(owner) {
136+
msg!("account_info.pubkey(): {:?}", account_info.pubkey());
137+
msg!(
138+
"account_info.key(): {:?}",
139+
Pubkey::new_from_array(account_info.key())
140+
);
141+
msg!("owner: {}", Pubkey::new_from_array(*owner));
133142
return Err(AccountError::AccountOwnedByWrongProgram);
134143
}
135144
Ok(())

program-libs/zero-copy/src/vec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::{
77
#[cfg(feature = "std")]
88
use std::vec::Vec;
99

10+
// use pinocchio::{format, msg};
1011
use zerocopy::{little_endian::U32, Ref};
1112

1213
use crate::{add_padding, errors::ZeroCopyError, ZeroCopyTraits};

programs/system/src/cpi_context/process_cpi_context.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,43 @@ pub fn process_cpi_context<'a, 'info, T: InstructionData<'a>>(
5959
Some(cpi_context_account_info) => cpi_context_account_info,
6060
None => return Err(SystemProgramError::CpiContextAccountUndefined.into()),
6161
};
62+
msg!("deserializing cpi context account");
6263
let mut cpi_context_account = deserialize_cpi_context_account(cpi_context_account_info)?;
64+
msg!("cpi context account deserialized");
6365
// We only validate when executing with the cpi context.
6466
if !cpi_context.first_set_context && !cpi_context.set_context {
67+
msg!("validating cpi context associated with merkle tree");
6568
validate_cpi_context_associated_with_merkle_tree(
6669
&instruction_data,
6770
&cpi_context_account,
6871
remaining_accounts,
6972
)?;
73+
msg!("cpi context associated with merkle tree validated");
7074
}
7175
if cpi_context.set_context || cpi_context.first_set_context {
76+
msg!("setting cpi context");
7277
set_cpi_context(fee_payer, cpi_context_account_info, instruction_data)?;
78+
msg!("cpi context set");
7379
return Ok(None);
7480
} else {
7581
if cpi_context_account.is_empty() {
82+
msg!("cpi context is empty");
7683
return Err(SystemProgramError::CpiContextEmpty.into());
7784
}
85+
msg!("checking fee payer");
7886
if (*cpi_context_account.fee_payer).to_bytes() != fee_payer {
87+
msg!("fee payer mismatch");
7988
msg!(format!(" {:?} != {:?}", fee_payer, cpi_context_account.fee_payer).as_str());
8089
return Err(SystemProgramError::CpiContextFeePayerMismatch.into());
8190
}
8291
// Zero out the fee payer since the cpi context is being consumed in this instruction.
8392
*cpi_context_account.fee_payer = Pubkey::default().into();
8493
instruction_data.set_cpi_context(cpi_context_account)?;
94+
msg!("cpi context set final exit");
8595
return Ok(Some((1, instruction_data)));
8696
}
8797
}
98+
msg!("cpi context is none");
8899
Ok(Some((0, instruction_data)))
89100
}
90101
pub fn set_cpi_context<'a, 'info, T: InstructionData<'a>>(
@@ -106,20 +117,31 @@ pub fn set_cpi_context<'a, 'info, T: InstructionData<'a>>(
106117
// relay_fee
107118
// 2. Subsequent invocations check the proof and fee payer
108119

120+
msg!("set_cpi_context: deserializing cpi context account");
109121
let mut cpi_context_account =
110122
deserialize_cpi_context_account(cpi_context_account_info).map_err(ProgramError::from)?;
123+
msg!("set_cpi_context: cpi context account deserialized");
111124

112125
if instruction_data.cpi_context().unwrap().first_set_context {
126+
msg!("set_cpi_context: clearing cpi context");
113127
cpi_context_account.clear();
128+
msg!("set_cpi_context: setting fee payer");
114129
*cpi_context_account.fee_payer = fee_payer.into();
130+
msg!("set_cpi_context: storing instruction data 1");
115131
cpi_context_account.store_data(&instruction_data)?;
132+
msg!("set_cpi_context: instruction data stored 1");
116133
} else if *cpi_context_account.fee_payer == fee_payer && !cpi_context_account.is_empty() {
134+
msg!("set_cpi_context: storing instruction data 2");
117135
cpi_context_account.store_data(&instruction_data)?;
136+
msg!("set_cpi_context: instruction data stored 2");
118137
} else {
138+
msg!("set_cpi_context: fee payer mismatch");
119139
msg!(format!(" {:?} != {:?}", fee_payer, cpi_context_account.fee_payer).as_str());
120140
return Err(SystemProgramError::CpiContextFeePayerMismatch.into());
121141
}
122142

143+
msg!("set_cpi_context: DONE. exit");
144+
123145
Ok(())
124146
}
125147
/*

programs/system/src/cpi_context/state.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ impl<'a> ZCpiContextAccount<'a> {
7272
&'a mut self,
7373
instruction_data: &crate::context::WrappedInstructionData<'b, T>,
7474
) -> Result<(), light_zero_copy::errors::ZeroCopyError> {
75+
msg!("store_data: entry");
7576
let pre_address_len = self.new_addresses.len();
77+
msg!(&format!("pre_address_len: {}", pre_address_len));
78+
7679
// Store new addresses
7780
for address in instruction_data.new_addresses() {
81+
msg!("store_data: new address loop");
7882
let new_address = CpiContextNewAddressParamsAssignedPacked {
7983
owner: instruction_data.owner().to_bytes(), // Use instruction data owner
8084
seed: address.seed(),
@@ -90,13 +94,22 @@ impl<'a> ZCpiContextAccount<'a> {
9094
as u8
9195
+ pre_address_len as u8,
9296
};
93-
// msg!(format!("cpi context new address {:?}", new_address).as_str());
97+
msg!(&format!("cpi context new address {:?}", new_address));
98+
99+
msg!(&format!(
100+
"push: entry to len {} capacity {}",
101+
self.new_addresses.len(),
102+
self.new_addresses.capacity()
103+
));
104+
msg!(&format!("new_address: {:?}", new_address));
94105
self.new_addresses.push(new_address)?;
106+
msg!("store_data: new address pushed");
95107
}
96108

97109
// Store input accounts
98110
for input in instruction_data.input_accounts() {
99111
if input.skip() {
112+
msg!("store_data: input account skipped");
100113
continue;
101114
}
102115
let in_account = CpiContextInAccount {
@@ -137,6 +150,7 @@ impl<'a> ZCpiContextAccount<'a> {
137150
// Store output accounts
138151
for output in instruction_data.output_accounts() {
139152
if output.skip() {
153+
msg!("store_data: output account skipped");
140154
// TODO: check what skip does
141155
continue;
142156
}
@@ -149,9 +163,11 @@ impl<'a> ZCpiContextAccount<'a> {
149163
with_address: if output.address().is_some() { 1 } else { 0 },
150164
address: output.address().unwrap_or([0; 32]),
151165
};
166+
msg!("store_data: output account pushed");
152167
self.out_accounts.push(out_account)?;
153168
sol_log_compute_units();
154169
if let Some(data) = output.data() {
170+
msg!("store_data: output data");
155171
// 330 CU
156172
*self.output_data_len += 1;
157173
// TODO: add unchecked new at this will fail with MemoryNotZeroed
@@ -162,10 +178,11 @@ impl<'a> ZCpiContextAccount<'a> {
162178
new_data.as_mut_slice().copy_from_slice(&data.data);
163179
self.output_data.push(new_data);
164180
self.remaining_data = remaining_data;
181+
msg!("self.remaining_data: updated");
165182
}
166183
sol_log_compute_units();
167184
}
168-
185+
msg!("store_data: exit");
169186
Ok(())
170187
}
171188
}
@@ -178,38 +195,50 @@ impl Discriminator for ZCpiContextAccount<'_> {
178195
pub fn deserialize_cpi_context_account<'a>(
179196
account_info: &AccountInfo,
180197
) -> std::result::Result<ZCpiContextAccount<'a>, ZeroCopyError> {
198+
msg!("deserialize_cpi_context_account: checking owner");
181199
check_owner(&ID, account_info).map_err(|_| ZeroCopyError::IterFromOutOfBounds)?;
200+
msg!("deserialize_cpi_context_account: owner checked");
182201
let mut account_data = account_info
183202
.try_borrow_mut_data()
184203
.map_err(|_| ZeroCopyError::IterFromOutOfBounds)?;
204+
msg!("deserialize_cpi_context_account: account data borrowed");
185205
let data = unsafe { slice::from_raw_parts_mut(account_data.as_mut_ptr(), account_data.len()) };
206+
msg!("deserialize_cpi_context_account: data sliced");
186207
let (discriminator, data) = data.split_at_mut(8);
208+
msg!("deserialize_cpi_context_account: discriminator checked");
187209
if discriminator != &CPI_CONTEXT_ACCOUNT_DISCRIMINATOR {
188210
msg!("Invalid cpi context account discriminator.");
189211
return Err(ZeroCopyError::IterFromOutOfBounds);
190212
}
213+
msg!("deserialize_cpi_context_account: fee payer checked");
191214
let (fee_payer, data) =
192215
Ref::<&'a mut [u8], light_compressed_account::pubkey::Pubkey>::from_prefix(data)?;
193-
216+
msg!("deserialize_cpi_context_account: associated merkle tree checked");
194217
let (associated_merkle_tree, data) =
195218
Ref::<&'a mut [u8], light_compressed_account::pubkey::Pubkey>::from_prefix(data)?;
196-
219+
msg!("deserialize_cpi_context_account: new addresses checked");
197220
let (new_addresses, data) =
198221
ZeroCopyVecU8::<'a, CpiContextNewAddressParamsAssignedPacked>::from_bytes_at(data)?;
222+
msg!("deserialize_cpi_context_account: readonly addresses checked");
199223
let (readonly_addresses, data) =
200224
ZeroCopyVecU8::<'a, ZPackedReadOnlyAddress>::from_bytes_at(data)?;
225+
msg!("deserialize_cpi_context_account: readonly accounts checked");
201226
let (readonly_accounts, data) =
202227
ZeroCopyVecU8::<'a, ZPackedReadOnlyCompressedAccount>::from_bytes_at(data)?;
228+
msg!("deserialize_cpi_context_account: in accounts checked");
203229
let (in_accounts, data) = ZeroCopyVecU8::<'a, CpiContextInAccount>::from_bytes_at(data)?;
230+
msg!("deserialize_cpi_context_account: in accounts checked");
204231
let (out_accounts, data) = ZeroCopyVecU8::<'a, CpiContextOutAccount>::from_bytes_at(data)?;
232+
msg!("deserialize_cpi_context_account: out accounts checked");
205233
let (output_data_len, mut data) = Ref::<&'a mut [u8], U16>::from_prefix(data)?;
234+
msg!("deserialize_cpi_context_account: output data len checked");
206235
let mut output_data = Vec::with_capacity(output_data_len.get() as usize);
207236
for _ in 0..output_data_len.get() {
208237
let (output_data_slice, inner_data) = ZeroCopySliceMut::<U16, u8>::from_bytes_at(data)?;
209238
output_data.push(output_data_slice);
210239
data = inner_data;
211240
}
212-
241+
msg!("deserialize_cpi_context_account: cpi context account created..");
213242
Ok(ZCpiContextAccount {
214243
fee_payer,
215244
associated_merkle_tree,
@@ -291,7 +320,7 @@ pub fn cpi_context_account_new<'a>(
291320
data,
292321
)?;
293322
let (readonly_addresses, data) =
294-
ZeroCopyVecU8::<'a, ZPackedReadOnlyAddress>::new_at(params.readonly_accounts_len, data)?;
323+
ZeroCopyVecU8::<'a, ZPackedReadOnlyAddress>::new_at(params.readonly_addresses_len, data)?;
295324
let (readonly_accounts, data) = ZeroCopyVecU8::<'a, ZPackedReadOnlyCompressedAccount>::new_at(
296325
params.readonly_accounts_len,
297326
data,

programs/system/src/invoke_cpi/instruction_small.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
use light_account_checks::AccountIterator;
2-
use light_compressed_account::instruction_data::traits::AccountOptions;
3-
use pinocchio::account_info::AccountInfo;
4-
use pinocchio::msg;
5-
use pinocchio::pubkey::Pubkey;
61
use crate::{
72
accounts::{
83
account_checks::{
@@ -14,6 +9,11 @@ use crate::{
149
errors::SystemProgramError,
1510
Result,
1611
};
12+
use light_account_checks::AccountIterator;
13+
use light_compressed_account::instruction_data::traits::AccountOptions;
14+
use pinocchio::account_info::AccountInfo;
15+
use pinocchio::msg;
16+
use pinocchio::pubkey::Pubkey;
1717

1818
#[derive(PartialEq, Eq)]
1919
pub struct ExecutionAccounts<'info> {
@@ -75,9 +75,16 @@ impl<'info> InvokeCpiInstructionSmall<'info> {
7575
None
7676
};
7777

78+
msg!("trying cpi context account");
7879
let cpi_context_account = check_option_cpi_context_account(&mut accounts, account_options)?;
80+
msg!(&format!(
81+
"trying cpi context account {:?}",
82+
cpi_context_account.map(|a| a.key())
83+
));
7984
// msg!(&format!("!account_options.write_to_cpi_context: {:?}", !account_options.write_to_cpi_context));
85+
8086
let remaining_accounts = if !account_options.write_to_cpi_context {
87+
msg!("2222");
8188
accounts.remaining()?
8289
} else {
8390
&[]
@@ -90,7 +97,7 @@ impl<'info> InvokeCpiInstructionSmall<'info> {
9097
// .map(|a| Pubkey::try_from(a.key().as_ref()).unwrap())
9198
// .collect::<Vec<_>>()
9299
// )
93-
// );
100+
// );
94101
Ok((
95102
Self {
96103
fee_payer,

programs/system/src/invoke_cpi/processor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use light_compressed_account::instruction_data::traits::InstructionData;
2-
use pinocchio::{account_info::AccountInfo, pubkey::Pubkey};
2+
use pinocchio::{account_info::AccountInfo, msg, pubkey::Pubkey};
33

44
pub use crate::Result;
55
use crate::{
@@ -29,13 +29,15 @@ pub fn process_invoke_cpi<
2929
instruction_data: T,
3030
remaining_accounts: &'info [AccountInfo],
3131
) -> Result<()> {
32+
msg!("process_invoke_cpi");
3233
let instruction_data = WrappedInstructionData::new(instruction_data)?;
3334

3435
cpi_signer_checks::<T>(
3536
&invoking_program,
3637
accounts.get_authority().key(),
3738
&instruction_data,
3839
)?;
40+
msg!("cpi_signer_checks done");
3941

4042
let (cpi_context_inputs_len, instruction_data) = match process_cpi_context(
4143
instruction_data,

0 commit comments

Comments
 (0)