Skip to content

Commit

Permalink
Use lazy entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
febo committed Oct 21, 2024
1 parent 693f836 commit 4435c76
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 76 deletions.
15 changes: 6 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cpi/pinocchio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"
test-sbf = []

[dependencies]
pinocchio = "0.4"
pinocchio-system = "0.1"
pinocchio = { version = "0.5", git = "https://github.com/febo/pinocchio.git", branch = "create-pda-unchecked" }
pinocchio-system = { version="0.1", git = "https://github.com/febo/pinocchio.git", branch = "create-pda-unchecked" }

[dev-dependencies]
solana-program-test = "2.0.3"
Expand Down
52 changes: 26 additions & 26 deletions cpi/pinocchio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@
#![deny(missing_docs)]

use pinocchio::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
instruction::{Account, AccountMeta, Instruction, Seed, Signer},
instruction::{Account, AccountMeta, Instruction},
lazy_entrypoint::InstructionContext,
program::invoke_signed_unchecked,
program_error::ProgramError,
pubkey::{create_program_address, Pubkey},
pubkey::create_program_address,
signer, ProgramResult,
};

pinocchio::entrypoint!(process_instruction);
// Since this is a single instruction program, we use the "lazy" variation
// of the entrypoint.
pinocchio::lazy_entrypoint!(process_instruction);

/// Amount of bytes of account data to allocate
pub const SIZE: usize = 42;

/// Instruction processor
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
// Account info to allocate abd for the program being invoked
let [allocated_info, _system_program_info] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
/// Instruction processor.
unsafe fn process_instruction(mut context: InstructionContext) -> ProgramResult {
// Account info to allocate and for the program being invoked. Here we are
// optimizing for CU, so we are not checking that the accounts are present
// ('unchecked' method will panic if the account is duplicated or UB if the
// account is missing).
let allocated_info = context.next_account_unchecked();
let _system_program_info = context.next_account_unchecked();

// Again, we are not checking that all accounts have been consumed (we assume
// that we got only the 2 accounts we expected).
let (instruction_data, program_id) = context.instruction_data_unchecked();

let expected_allocated_key =
create_program_address(&[b"You pass butter", &[instruction_data[0]]], program_id)?;
Expand All @@ -45,16 +48,13 @@ pub fn process_instruction(
data: &data,
};

unsafe {
invoke_signed_unchecked(
&instruction,
&[Account::from(allocated_info)],
&[Signer::from(&[
Seed::from(b"You pass butter"),
Seed::from(&[instruction_data[0]]),
])],
);
}
// Invoke the system program with the 'unchcked' function. This is safe since
// we know the accounts are not borrowed elsewhere.
invoke_signed_unchecked(
&instruction,
&[Account::from(&allocated_info)],
&[signer!(b"You pass butter", &[instruction_data[0]])],
);

Ok(())
}
2 changes: 1 addition & 1 deletion transfer-lamports/pinocchio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ no-entrypoint = []
test-sbf = []

[dependencies]
pinocchio = "0.4"
pinocchio = { version = "0.5", git = "https://github.com/febo/pinocchio.git", branch = "create-pda-unchecked" }

[dev-dependencies]
solana-program-test = "2.0.3"
Expand Down
26 changes: 18 additions & 8 deletions transfer-lamports/pinocchio/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
#![cfg(not(feature = "no-entrypoint"))]

use pinocchio::{account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, pubkey::Pubkey};
use pinocchio::{lazy_entrypoint::InstructionContext, ProgramResult};

pinocchio::entrypoint!(process_instruction);
// Since this is a single instruction program, we use the "lazy" variation
// of the entrypoint.
pinocchio::lazy_entrypoint!(process_instruction);

fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
crate::processor::process_instruction(program_id, accounts, instruction_data)
#[inline]
unsafe fn process_instruction(mut context: InstructionContext) -> ProgramResult {
// Account infos used in the transfer. Here we are optimizing for CU, so we
// are not checking that the accounts are present ('unchecked' method will panic
// if the account is duplicated or UB if the account is missing).
let source_info = context.next_account_unchecked();
let destination_info = context.next_account_unchecked();

// Transfer five lamports from the source to the destination using 'unchecked'
// borrowing. This is safe since we know the lamports are not borrowed elsewhere.
*source_info.borrow_mut_lamports_unchecked() -= 5; // withdraw five lamports
*destination_info.borrow_mut_lamports_unchecked() += 5; // deposit five lamports

Ok(())
}
1 change: 0 additions & 1 deletion transfer-lamports/pinocchio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
#![deny(missing_docs)]

mod entrypoint;
pub mod processor;
29 changes: 0 additions & 29 deletions transfer-lamports/pinocchio/src/processor.rs

This file was deleted.

0 comments on commit 4435c76

Please sign in to comment.