Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ thiserror = "^1.0.57"
solana-security-txt = { version = "1.1.1", optional = true }
solana-curve25519 = "2.2"
bincode = "1.3.3"
pinocchio-log = "0.5.0"
pinocchio = "0.9.2"
pinocchio-pubkey = "0.3.0"
pinocchio-system = "0.3.0"
Comment thread
snawaz marked this conversation as resolved.

[dev-dependencies]
base64 = "0.22.1"
Expand Down
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ pub enum DlpError {
NonceOutOfOrder = 12,
#[error("Computation overflow detected")]
Overflow = 13,
#[error("Too many seeds")]
TooManySeeds = 14,
}

impl From<DlpError> for ProgramError {
fn from(e: DlpError) -> Self {
ProgramError::Custom(e as u32)
}
}

impl From<DlpError> for pinocchio::program_error::ProgramError {
fn from(e: DlpError) -> Self {
pinocchio::program_error::ProgramError::Custom(e as u32)
}
}
90 changes: 85 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#![allow(unexpected_cfgs)] // silence clippy for target_os solana and other solana program custom features

use crate::processor::process_call_handler;
use pinocchio_log::log;
use solana_program::{
account_info::AccountInfo, declare_id, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey,
account_info::AccountInfo,
declare_id,
entrypoint::{self, ProgramResult},
msg,
program_error::ProgramError,
pubkey::Pubkey,
};

use discriminator::DlpDiscriminator;

pub mod args;
pub mod consts;
mod discriminator;
Expand All @@ -17,8 +24,12 @@ pub mod state;

declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh");

#[cfg(not(feature = "no-entrypoint"))]
solana_program::entrypoint!(process_instruction);
pub mod fast {
pinocchio_pubkey::declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh");
}

entrypoint::custom_heap_default!();
entrypoint::custom_panic_default!();

#[cfg(all(not(feature = "no-entrypoint"), feature = "solana-security-txt"))]
solana_security_txt::security_txt! {
Expand All @@ -30,7 +41,76 @@ solana_security_txt::security_txt! {
source_code: "https://github.com/magicblock-labs/delegation-program"
}

pub fn process_instruction(
/// # Safety
///
/// It's pretty close to the code generated by entrypoint!() macro, with one minor tweak to
/// support fallback branch.
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
const UNINIT: core::mem::MaybeUninit<pinocchio::account_info::AccountInfo> =
core::mem::MaybeUninit::<pinocchio::account_info::AccountInfo>::uninit();
let mut accounts = [UNINIT; { pinocchio::MAX_TX_ACCOUNTS }];

let (program_id, count, data) =
pinocchio::entrypoint::deserialize::<{ pinocchio::MAX_TX_ACCOUNTS }>(input, &mut accounts);
match fast_process_instruction(
program_id,
core::slice::from_raw_parts(accounts.as_ptr() as _, count),
data,
) {
Some(Ok(())) => pinocchio::SUCCESS,
Some(Err(error)) => error.into(),

// Fallback to the slow path that does not use pinocchio SDK.
None => slow_entrypoint(input),
}
}

/// # Safety
///
/// It's pretty close to the code generated by entrypoint!() macro, with one difference: the
/// function name is slow_entrypoint() as opposed to entrypoint() because this is a fallback
/// entrypoint (a slow one).
pub unsafe fn slow_entrypoint(input: *mut u8) -> u64 {
let (program_id, accounts, instruction_data) = unsafe { entrypoint::deserialize(input) };
match slow_process_instruction(program_id, &accounts, instruction_data) {
Ok(()) => entrypoint::SUCCESS,
Err(error) => error.into(),
}
}

pub fn fast_process_instruction(
program_id: &pinocchio::pubkey::Pubkey,
accounts: &[pinocchio::account_info::AccountInfo],
data: &[u8],
) -> Option<pinocchio::ProgramResult> {
if data.len() < 8 {
return Some(Err(
pinocchio::program_error::ProgramError::InvalidInstructionData,
));
}

let (discriminator_bytes, data) = data.split_at(8);

let discriminator = match DlpDiscriminator::try_from(discriminator_bytes[0]) {
Ok(discriminator) => discriminator,
Err(_) => {
log!("Failed to read and parse discriminator");
return Some(Err(
pinocchio::program_error::ProgramError::InvalidInstructionData,
));
}
};
Comment thread
snawaz marked this conversation as resolved.

match discriminator {
discriminator::DlpDiscriminator::Delegate => Some(processor::fast::process_delegate(
program_id, accounts, data,
)),
_ => None,
}
}

pub fn slow_process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
data: &[u8],
Expand Down
Loading