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

Commit 14774fc

Browse files
committed
Tweaks
1 parent e7a59b6 commit 14774fc

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

program/src/entrypoint.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ default_panic_handler!();
2828
/// - `9`: `CloseAccount`
2929
/// - `18`: `InitializeAccount3`
3030
/// - `20`: `InitializeMint2`
31+
/// - `255`: `Batch`
3132
#[inline(always)]
3233
pub fn process_instruction(
3334
_program_id: &Pubkey,
3435
accounts: &[AccountInfo],
3536
instruction_data: &[u8],
3637
) -> ProgramResult {
37-
let (discriminator, instruction_data) = instruction_data
38-
.split_first()
39-
.ok_or(ProgramError::InvalidInstructionData)?;
38+
let [discriminator, instruction_data @ ..] = instruction_data else {
39+
return Err(ProgramError::InvalidInstructionData);
40+
};
4041

4142
match *discriminator {
4243
// 0 - InitializeMint

program/src/processor/batch.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ pub fn process_batch(mut accounts: &[AccountInfo], mut instruction_data: &[u8])
1313
loop {
1414
// Validates the instruction data and accounts offset.
1515

16-
match instruction_data.len() {
17-
0 => break,
18-
n if n < IX_HEADER_SIZE => {
19-
// The instruction data must have at least two bytes.
20-
return Err(ProgramError::InvalidInstructionData);
21-
}
22-
_ => (),
16+
if instruction_data.len() < IX_HEADER_SIZE {
17+
// The instruction data must have at least two bytes.
18+
return Err(ProgramError::InvalidInstructionData);
2319
}
2420

2521
// SAFETY: The instruction data is guaranteed to have at least two bytes.
@@ -42,6 +38,11 @@ pub fn process_batch(mut accounts: &[AccountInfo], mut instruction_data: &[u8])
4238
&instruction_data[IX_HEADER_SIZE..data_offset],
4339
)?;
4440

41+
if data_offset == instruction_data.len() {
42+
// The batch is complete.
43+
break;
44+
}
45+
4546
accounts = &accounts[expected_accounts..];
4647
instruction_data = &instruction_data[data_offset..];
4748
}

program/tests/batch.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,36 @@
22

33
mod setup;
44

5-
use std::{
6-
collections::{BTreeMap, HashMap},
7-
println,
8-
};
9-
10-
use pinocchio::instruction;
11-
use setup::{account, mint, TOKEN_PROGRAM_ID};
125
use solana_program_test::{tokio, ProgramTest};
136
use solana_sdk::{
147
instruction::{AccountMeta, Instruction},
158
program_error::ProgramError,
169
program_pack::Pack,
1710
pubkey::Pubkey,
1811
signature::{Keypair, Signer},
19-
system_instruction, system_program,
12+
system_instruction,
2013
transaction::Transaction,
2114
};
2215

2316
fn batch_instruction(instructions: Vec<Instruction>) -> Result<Instruction, ProgramError> {
24-
// Create a Vector of ordered, AccountMetas
17+
// Create a `Vec` of ordered `AccountMeta`s
2518
let mut accounts: Vec<AccountMeta> = vec![];
26-
// Start with the batch discriminator and a length byte
27-
19+
// Start with the batch discriminator
2820
let mut data: Vec<u8> = vec![0xff];
21+
2922
for instruction in instructions {
30-
// Error out on non-token IX
23+
// Error out on non-token IX.
3124
if instruction.program_id.ne(&spl_token::ID) {
3225
return Err(ProgramError::IncorrectProgramId);
3326
}
3427

35-
data.extend_from_slice(&[instruction.accounts.len() as u8]);
36-
data.extend_from_slice(&[instruction.data.len() as u8]);
28+
data.push(instruction.accounts.len() as u8);
29+
data.push(instruction.data.len() as u8);
30+
3731
data.extend_from_slice(&instruction.data);
3832
accounts.extend_from_slice(&instruction.accounts);
3933
}
34+
4035
Ok(Instruction {
4136
program_id: spl_token::ID,
4237
data,
@@ -102,7 +97,6 @@ async fn batch(token_program: Pubkey) {
10297
let owner_a = Keypair::new();
10398
let owner_b = Keypair::new();
10499
let owner_a_ta_a = Keypair::new();
105-
let owner_a_ta_b = Keypair::new();
106100
let owner_b_ta_a = Keypair::new();
107101

108102
let create_owner_a_ta_a = system_instruction::create_account(
@@ -126,13 +120,6 @@ async fn batch(token_program: Pubkey) {
126120
&owner_a.pubkey(),
127121
)
128122
.unwrap();
129-
let intialize_owner_a_ta_b = spl_token::instruction::initialize_account3(
130-
&token_program,
131-
&owner_a_ta_b.pubkey(),
132-
&mint_b.pubkey(),
133-
&owner_a.pubkey(),
134-
)
135-
.unwrap();
136123
let intialize_owner_b_ta_a = spl_token::instruction::initialize_account3(
137124
&token_program,
138125
&owner_b_ta_a.pubkey(),

0 commit comments

Comments
 (0)