Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit a365ded

Browse files
committed
Test recursion for all priority IXs
1 parent f6d803a commit a365ded

File tree

10 files changed

+171
-136
lines changed

10 files changed

+171
-136
lines changed

program/src/entrypoint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use pinocchio::{
33
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
44
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
55
};
6+
use pinocchio_pubkey::pubkey;
67

78
use crate::processor::*;
89

@@ -34,6 +35,7 @@ pub fn process_instruction(
3435
accounts: &[AccountInfo],
3536
instruction_data: &[u8],
3637
) -> ProgramResult {
38+
3739
let (discriminator, instruction_data) = instruction_data
3840
.split_first()
3941
.ok_or(ProgramError::InvalidInstructionData)?;

program/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
33
#![no_std]
44

5+
#![feature(split_at_checked)]
6+
57
mod entrypoint;
68
mod processor;

program/src/processor/batch.rs

Lines changed: 13 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,8 @@
1-
use core::mem::size_of;
21
use pinocchio::{
3-
account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult,
2+
account_info::AccountInfo, program_error::ProgramError, ProgramResult
43
};
54

6-
use crate::processor::{
7-
process_close_account, process_initialize_account3, process_initialize_mint,
8-
process_initialize_mint2, process_mint_to, process_transfer,
9-
};
10-
11-
macro_rules! map_accounts {
12-
// For 1 account
13-
($accounts:expr, $instruction_data:expr, 1) => {{
14-
let (account_idx, rest) = $instruction_data
15-
.split_first()
16-
.ok_or(ProgramError::InvalidInstructionData)?;
17-
*$instruction_data = rest;
18-
let batch_accounts = [$accounts[*account_idx as usize].clone()];
19-
batch_accounts
20-
}};
21-
22-
// For 2 accounts
23-
($accounts:expr, $instruction_data:expr, 2) => {{
24-
let (account_indices, rest) = $instruction_data.split_at(2);
25-
*$instruction_data = rest;
26-
let batch_accounts = [
27-
$accounts[account_indices[0] as usize].clone(),
28-
$accounts[account_indices[1] as usize].clone(),
29-
];
30-
batch_accounts
31-
}};
32-
33-
// For 3 accounts
34-
($accounts:expr, $instruction_data:expr, 3) => {{
35-
let (account_indices, rest) = $instruction_data.split_at(3);
36-
*$instruction_data = rest;
37-
let batch_accounts = [
38-
$accounts[account_indices[0] as usize].clone(),
39-
$accounts[account_indices[1] as usize].clone(),
40-
$accounts[account_indices[2] as usize].clone(),
41-
];
42-
batch_accounts
43-
}};
44-
}
5+
use crate::entrypoint::process_instruction;
456

467
#[inline(always)]
478
pub fn process_batch(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
@@ -50,77 +11,18 @@ pub fn process_batch(accounts: &[AccountInfo], instruction_data: &[u8]) -> Progr
5011
.split_first()
5112
.ok_or(ProgramError::InvalidInstructionData)?;
5213

53-
let mut discriminator;
14+
let mut lengths: &[u8];
15+
let mut accounts = accounts;
16+
let mut current_accounts: &[AccountInfo];
17+
let mut current_instruction_data: &[u8];
18+
5419
for _ in 0..*counter {
55-
(discriminator, instruction_data) = instruction_data
56-
.split_first()
20+
(lengths, instruction_data) = instruction_data
21+
.split_at_checked(2)
5722
.ok_or(ProgramError::InvalidInstructionData)?;
58-
match discriminator {
59-
// 0 - InitializeMint
60-
0 => {
61-
#[cfg(feature = "logging")]
62-
pinocchio::msg!("Batch Instruction: InitializeMint");
63-
64-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 2);
65-
process_initialize_mint(&batch_accounts, instruction_data, true)?;
66-
if instruction_data[size_of::<(u8, Pubkey)>()] == 0 {
67-
instruction_data = &instruction_data[size_of::<(u8, Pubkey, u8)>()..];
68-
} else {
69-
instruction_data = &instruction_data[size_of::<(u8, Pubkey, u8, Pubkey)>()..];
70-
}
71-
}
72-
// 3 - Transfer
73-
3 => {
74-
#[cfg(feature = "logging")]
75-
pinocchio::msg!("Batch Instruction: Transfer");
76-
77-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 3);
78-
process_transfer(&batch_accounts, instruction_data)?;
79-
instruction_data = &instruction_data[size_of::<u64>()..];
80-
}
81-
// 7 - MintTo
82-
7 => {
83-
#[cfg(feature = "logging")]
84-
pinocchio::msg!("Batch Instruction: MintTo");
85-
86-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 3);
87-
process_mint_to(&batch_accounts, instruction_data)?;
88-
instruction_data = &instruction_data[size_of::<u64>()..];
89-
}
90-
// 9 - CloseAccount
91-
9 => {
92-
#[cfg(feature = "logging")]
93-
pinocchio::msg!("Batch Instruction: CloseAccount");
94-
95-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 2);
96-
process_close_account(&batch_accounts)?;
97-
}
98-
// 18 - InitializeAccount3
99-
18 => {
100-
#[cfg(feature = "logging")]
101-
pinocchio::msg!("Batch Instruction: InitializeAccount3");
102-
103-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 3);
104-
process_initialize_account3(&batch_accounts, instruction_data)?;
105-
instruction_data = &instruction_data[size_of::<Pubkey>()..];
106-
}
107-
// 20 - InitializeMint2
108-
20 => {
109-
#[cfg(feature = "logging")]
110-
pinocchio::msg!("Instruction: InitializeMint2");
111-
112-
let batch_accounts = map_accounts!(accounts, &mut instruction_data, 1);
113-
process_initialize_mint2(&batch_accounts, instruction_data)?;
114-
if instruction_data[size_of::<(u8, Pubkey)>()] == 0 {
115-
instruction_data = &instruction_data[size_of::<(u8, Pubkey, u8)>()..];
116-
} else {
117-
instruction_data = &instruction_data[size_of::<(u8, Pubkey, u8, Pubkey)>()..];
118-
}
119-
}
120-
_ => {
121-
return Err(ProgramError::InvalidInstructionData);
122-
}
123-
}
23+
(current_accounts, accounts) = accounts.split_at_checked(lengths[0].into()).ok_or(ProgramError::InvalidInstructionData)?;
24+
(current_instruction_data, instruction_data) = instruction_data.split_at_checked(lengths[1].into()).ok_or(ProgramError::InvalidInstructionData)?;
25+
process_instruction(&token_interface::program::ID, current_accounts, current_instruction_data)?;
12426
}
12527
Ok(())
126-
}
28+
}

program/src/processor/get_account_data_size.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::check_account_owner;
1010

1111
#[inline(always)]
1212
pub fn process_get_account_data_size(accounts: &[AccountInfo]) -> ProgramResult {
13-
let [mint_info, _remaning @ ..] = accounts else {
13+
let [mint_info, _remaining @ ..] = accounts else {
1414
return Err(ProgramError::NotEnoughAccountKeys);
1515
};
1616

program/src/processor/revoke.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::validate_owner;
88

99
#[inline(always)]
1010
pub fn process_revoke(accounts: &[AccountInfo], _instruction_data: &[u8]) -> ProgramResult {
11-
let [source_account_info, owner_info, remaning @ ..] = accounts else {
11+
let [source_account_info, owner_info, remaining @ ..] = accounts else {
1212
return Err(ProgramError::NotEnoughAccountKeys);
1313
};
1414

@@ -19,7 +19,7 @@ pub fn process_revoke(accounts: &[AccountInfo], _instruction_data: &[u8]) -> Pro
1919
return Err(TokenError::AccountFrozen.into());
2020
}
2121

22-
validate_owner(&source_account.owner, owner_info, remaning)?;
22+
validate_owner(&source_account.owner, owner_info, remaining)?;
2323

2424
source_account.clear_delegate();
2525
source_account.set_delegated_amount(0);

program/src/processor/set_authority.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
2222

2323
// Validates the accounts.
2424

25-
let [account_info, authority_info, remaning @ ..] = accounts else {
25+
let [account_info, authority_info, remaining @ ..] = accounts else {
2626
return Err(ProgramError::NotEnoughAccountKeys);
2727
};
2828

@@ -35,7 +35,7 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
3535

3636
match authority_type {
3737
AuthorityType::AccountOwner => {
38-
validate_owner(&account.owner, authority_info, remaning)?;
38+
validate_owner(&account.owner, authority_info, remaining)?;
3939

4040
if let Some(authority) = new_authority {
4141
account.owner = *authority;
@@ -52,7 +52,7 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
5252
}
5353
AuthorityType::CloseAccount => {
5454
let authority = account.close_authority().unwrap_or(&account.owner);
55-
validate_owner(authority, authority_info, remaning)?;
55+
validate_owner(authority, authority_info, remaining)?;
5656

5757
if let Some(authority) = new_authority {
5858
account.set_close_authority(authority);
@@ -73,7 +73,7 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
7373
// mint_authority.
7474
let mint_authority = mint.mint_authority().ok_or(TokenError::FixedSupply)?;
7575

76-
validate_owner(mint_authority, authority_info, remaning)?;
76+
validate_owner(mint_authority, authority_info, remaining)?;
7777

7878
if let Some(authority) = new_authority {
7979
mint.set_mint_authority(authority);
@@ -88,7 +88,7 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8])
8888
.freeze_authority()
8989
.ok_or(TokenError::MintCannotFreeze)?;
9090

91-
validate_owner(freeze_authority, authority_info, remaning)?;
91+
validate_owner(freeze_authority, authority_info, remaining)?;
9292

9393
if let Some(authority) = new_authority {
9494
mint.set_freeze_authority(authority);

program/src/processor/shared/approve.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn process_approve(
1717

1818
let (source_account_info, expected_mint_info, delegate_info, owner_info, remaining) =
1919
if let Some(expected_decimals) = expected_decimals {
20-
let [source_account_info, expected_mint_info, delegate_info, owner_info, remaning @ ..] =
20+
let [source_account_info, expected_mint_info, delegate_info, owner_info, remaining @ ..] =
2121
accounts
2222
else {
2323
return Err(ProgramError::NotEnoughAccountKeys);
@@ -28,18 +28,18 @@ pub fn process_approve(
2828
Some((expected_mint_info, expected_decimals)),
2929
delegate_info,
3030
owner_info,
31-
remaning,
31+
remaining,
3232
)
3333
} else {
34-
let [source_account_info, delegate_info, owner_info, remaning @ ..] = accounts else {
34+
let [source_account_info, delegate_info, owner_info, remaining @ ..] = accounts else {
3535
return Err(ProgramError::NotEnoughAccountKeys);
3636
};
3737
(
3838
source_account_info,
3939
None,
4040
delegate_info,
4141
owner_info,
42-
remaning,
42+
remaining,
4343
)
4444
};
4545

program/src/processor/shared/initialize_account.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ pub fn process_initialize_account(
2424
) -> ProgramResult {
2525
// Accounts expected depend on whether we have the `rent_sysvar` account or not.
2626

27-
let (new_account_info, mint_info, owner, remaning) = if let Some(owner) = owner {
28-
let [new_account_info, mint_info, remaning @ ..] = accounts else {
27+
let (new_account_info, mint_info, owner, remaining) = if let Some(owner) = owner {
28+
let [new_account_info, mint_info, remaining @ ..] = accounts else {
2929
return Err(ProgramError::NotEnoughAccountKeys);
3030
};
31-
(new_account_info, mint_info, owner, remaning)
31+
(new_account_info, mint_info, owner, remaining)
3232
} else {
33-
let [new_account_info, mint_info, owner_info, remaning @ ..] = accounts else {
33+
let [new_account_info, mint_info, owner_info, remaining @ ..] = accounts else {
3434
return Err(ProgramError::NotEnoughAccountKeys);
3535
};
36-
(new_account_info, mint_info, owner_info.key(), remaning)
36+
(new_account_info, mint_info, owner_info.key(), remaining)
3737
};
3838

3939
// Check rent-exempt status of the token account.
4040

4141
let new_account_info_data_len = new_account_info.data_len();
4242

4343
let minimum_balance = if rent_sysvar_account {
44-
let rent_sysvar_info = remaning.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
44+
let rent_sysvar_info = remaining.first().ok_or(ProgramError::NotEnoughAccountKeys)?;
4545
let rent = unsafe { Rent::from_bytes(rent_sysvar_info.borrow_data_unchecked()) };
4646
rent.minimum_balance(new_account_info_data_len)
4747
} else {

program/src/processor/shared/transfer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ pub fn process_transfer(
2020
expected_mint_info,
2121
destination_account_info,
2222
authority_info,
23-
remaning,
23+
remaining,
2424
) = if let Some(decimals) = expected_decimals {
25-
let [source_account_info, mint_info, destination_account_info, authority_info, remaning @ ..] =
25+
let [source_account_info, mint_info, destination_account_info, authority_info, remaining @ ..] =
2626
accounts
2727
else {
2828
return Err(ProgramError::NotEnoughAccountKeys);
@@ -32,10 +32,10 @@ pub fn process_transfer(
3232
Some((mint_info, decimals)),
3333
destination_account_info,
3434
authority_info,
35-
remaning,
35+
remaining,
3636
)
3737
} else {
38-
let [source_account_info, destination_account_info, authority_info, remaning @ ..] =
38+
let [source_account_info, destination_account_info, authority_info, remaining @ ..] =
3939
accounts
4040
else {
4141
return Err(ProgramError::NotEnoughAccountKeys);
@@ -45,7 +45,7 @@ pub fn process_transfer(
4545
None,
4646
destination_account_info,
4747
authority_info,
48-
remaning,
48+
remaining,
4949
)
5050
};
5151

@@ -95,7 +95,7 @@ pub fn process_transfer(
9595
// Validates the authority (delegate or owner).
9696

9797
if source_account.delegate() == Some(authority_info.key()) {
98-
validate_owner(authority_info.key(), authority_info, remaning)?;
98+
validate_owner(authority_info.key(), authority_info, remaining)?;
9999

100100
let delegated_amount = source_account
101101
.delegated_amount()
@@ -110,7 +110,7 @@ pub fn process_transfer(
110110
}
111111
}
112112
} else {
113-
validate_owner(&source_account.owner, authority_info, remaning)?;
113+
validate_owner(&source_account.owner, authority_info, remaining)?;
114114
}
115115

116116
if self_transfer || amount == 0 {

0 commit comments

Comments
 (0)