Skip to content
Closed
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
54 changes: 23 additions & 31 deletions programs/system/src/instructions/advance_nonce_account.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
ProgramResult,
};
use pinocchio::{account_info::AccountInfo, instruction::AccountMeta};

use crate::{FullInstructionData, InvokeParts};

/// Consumes a stored nonce, replacing it with a successor.
///
Expand All @@ -22,31 +19,26 @@ pub struct AdvanceNonceAccount<'a> {
pub authority: &'a AccountInfo,
}

impl AdvanceNonceAccount<'_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 3] = [
AccountMeta::writable(self.account.key()),
AccountMeta::readonly(self.recent_blockhashes_sysvar.key()),
AccountMeta::readonly_signer(self.authority.key()),
];

// instruction
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &[4],
};
const N_ACCOUNTS: usize = 3;
const DATA_LEN: usize = 1;

invoke_signed(
&instruction,
&[self.account, self.recent_blockhashes_sysvar, self.authority],
signers,
)
impl<'a> From<AdvanceNonceAccount<'a>>
for InvokeParts<'a, N_ACCOUNTS, FullInstructionData<DATA_LEN>>
{
fn from(value: AdvanceNonceAccount<'a>) -> Self {
InvokeParts {
program_id: crate::ID,
accounts: [
value.account,
value.recent_blockhashes_sysvar,
value.authority,
],
account_metas: [
AccountMeta::writable(value.account.key()),
AccountMeta::readonly(value.recent_blockhashes_sysvar.key()),
AccountMeta::readonly_signer(value.authority.key()),
],
instruction_data: { FullInstructionData::new([4]) },
}
}
}
50 changes: 21 additions & 29 deletions programs/system/src/instructions/allocate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
ProgramResult,
};
use pinocchio::{account_info::AccountInfo, instruction::AccountMeta};

use crate::{FullInstructionData, InvokeParts};

/// Allocate space in a (possibly new) account without funding.
///
Expand All @@ -17,29 +14,24 @@ pub struct Allocate<'a> {
pub space: u64,
}

impl Allocate<'_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 1] = [AccountMeta::writable_signer(self.account.key())];

// instruction data
// - [0..4 ]: instruction discriminator
// - [4..12]: space
let mut instruction_data = [0; 12];
instruction_data[0] = 8;
instruction_data[4..12].copy_from_slice(&self.space.to_le_bytes());

let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data,
};
const N_ACCOUNTS: usize = 1;
const DATA_LEN: usize = 12;

invoke_signed(&instruction, &[self.account], signers)
impl<'a> From<Allocate<'a>> for InvokeParts<'a, N_ACCOUNTS, FullInstructionData<DATA_LEN>> {
fn from(value: Allocate<'a>) -> Self {
InvokeParts {
program_id: crate::ID,
accounts: [value.account],
account_metas: [AccountMeta::writable_signer(value.account.key())],
instruction_data: {
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..12]: space
let mut instruction_data = [0; DATA_LEN];
instruction_data[0] = 8;
instruction_data[4..12].copy_from_slice(&value.space.to_le_bytes());
FullInstructionData::new(instruction_data)
},
}
}
}
78 changes: 36 additions & 42 deletions programs/system/src/instructions/allocate_with_seed.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};
use pinocchio::{account_info::AccountInfo, instruction::AccountMeta, pubkey::Pubkey};

use crate::{InvokeParts, TruncatedInstructionData};

/// Allocate space for and assign an account at an address derived
/// from a base public key and a seed.
Expand Down Expand Up @@ -33,42 +29,40 @@ pub struct AllocateWithSeed<'a, 'b, 'c> {
pub owner: &'c Pubkey,
}

impl AllocateWithSeed<'_, '_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 2] = [
AccountMeta::writable_signer(self.account.key()),
AccountMeta::readonly_signer(self.base.key()),
];

// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36 ]: base pubkey
// - [36..44]: seed length
// - [44.. ]: seed (max 32)
// - [.. +8]: account space
// - [.. +32]: owner pubkey
let mut instruction_data = [0; 112];
instruction_data[0] = 9;
instruction_data[4..36].copy_from_slice(self.base.key());
instruction_data[36..44].copy_from_slice(&u64::to_le_bytes(self.seed.len() as u64));

let offset = 44 + self.seed.len();
instruction_data[44..offset].copy_from_slice(self.seed.as_bytes());
instruction_data[offset..offset + 8].copy_from_slice(&self.space.to_le_bytes());
instruction_data[offset + 8..offset + 40].copy_from_slice(self.owner.as_ref());
const N_ACCOUNTS: usize = 2;
const DATA_LEN: usize = 112;

let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data[..offset + 40],
};
impl<'a, 'b, 'c> From<AllocateWithSeed<'a, 'b, 'c>>
for InvokeParts<'a, N_ACCOUNTS, TruncatedInstructionData<DATA_LEN>>
{
fn from(value: AllocateWithSeed<'a, 'b, 'c>) -> Self {
Self {
program_id: crate::ID,
accounts: [value.account, value.base],
account_metas: [
AccountMeta::writable_signer(value.account.key()),
AccountMeta::readonly_signer(value.base.key()),
],
instruction_data: {
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36 ]: base pubkey
// - [36..44]: seed length
// - [44.. ]: seed (max 32)
// - [.. +8]: account space
// - [.. +32]: owner pubkey
let mut instruction_data = [0; DATA_LEN];
instruction_data[0] = 9;
instruction_data[4..36].copy_from_slice(value.base.key());
instruction_data[36..44]
.copy_from_slice(&u64::to_le_bytes(value.seed.len() as u64));

invoke_signed(&instruction, &[self.account, self.base], signers)
let offset = 44 + value.seed.len();
instruction_data[44..offset].copy_from_slice(value.seed.as_bytes());
instruction_data[offset..offset + 8].copy_from_slice(&value.space.to_le_bytes());
instruction_data[offset + 8..offset + 40].copy_from_slice(value.owner.as_ref());
TruncatedInstructionData::new(instruction_data, offset + 40)
},
}
}
}
50 changes: 21 additions & 29 deletions programs/system/src/instructions/assign.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};
use pinocchio::{account_info::AccountInfo, instruction::AccountMeta, pubkey::Pubkey};

use crate::{FullInstructionData, InvokeParts};

/// Assign account to a program
///
Expand All @@ -18,29 +14,25 @@ pub struct Assign<'a, 'b> {
pub owner: &'b Pubkey,
}

impl Assign<'_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 1] = [AccountMeta::writable_signer(self.account.key())];

// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36]: owner pubkey
let mut instruction_data = [0; 36];
instruction_data[0] = 1;
instruction_data[4..36].copy_from_slice(self.owner.as_ref());
const N_ACCOUNTS: usize = 1;
const DATA_LEN: usize = 36;

let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data,
};
impl<'a, 'b> From<Assign<'a, 'b>> for InvokeParts<'a, N_ACCOUNTS, FullInstructionData<DATA_LEN>> {
fn from(value: Assign<'a, 'b>) -> Self {
InvokeParts {
program_id: crate::ID,
accounts: [value.account],
account_metas: [AccountMeta::writable_signer(value.account.key())],
instruction_data: {
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36]: owner pubkey
let mut instruction_data = [0; DATA_LEN];
instruction_data[0] = 1;
instruction_data[4..36].copy_from_slice(value.owner.as_ref());

invoke_signed(&instruction, &[self.account], signers)
FullInstructionData::new(instruction_data)
},
}
}
}
75 changes: 36 additions & 39 deletions programs/system/src/instructions/assign_with_seed.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};
use pinocchio::{account_info::AccountInfo, instruction::AccountMeta, pubkey::Pubkey};

use crate::{InvokeParts, TruncatedInstructionData};

use super::AllocateWithSeed;

/// Assign account to a program based on a seed.
///
Expand All @@ -29,40 +27,39 @@ pub struct AssignWithSeed<'a, 'b, 'c> {
pub owner: &'c Pubkey,
}

impl AssignWithSeed<'_, '_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 2] = [
AccountMeta::writable_signer(self.account.key()),
AccountMeta::readonly_signer(self.base.key()),
];

// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36 ]: base pubkey
// - [36..44]: seed length
// - [44.. ]: seed (max 32)
// - [.. +32]: owner pubkey
let mut instruction_data = [0; 104];
instruction_data[0] = 10;
instruction_data[4..36].copy_from_slice(self.base.key());
instruction_data[36..44].copy_from_slice(&u64::to_le_bytes(self.seed.len() as u64));
const N_ACCOUNTS: usize = 2;
const DATA_LEN: usize = 104;

let offset = 44 + self.seed.len();
instruction_data[44..offset].copy_from_slice(self.seed.as_bytes());
instruction_data[offset..offset + 32].copy_from_slice(self.owner.as_ref());
impl<'a, 'b, 'c> From<AllocateWithSeed<'a, 'b, 'c>>
for InvokeParts<'a, N_ACCOUNTS, TruncatedInstructionData<DATA_LEN>>
{
fn from(value: AllocateWithSeed<'a, 'b, 'c>) -> Self {
InvokeParts {
program_id: crate::ID,
accounts: [value.account, value.base],
account_metas: [
AccountMeta::writable_signer(value.account.key()),
AccountMeta::readonly_signer(value.base.key()),
],
instruction_data: {
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..36 ]: base pubkey
// - [36..44]: seed length
// - [44.. ]: seed (max 32)
// - [.. +32]: owner pubkey
let mut instruction_data = [0; DATA_LEN];
instruction_data[0] = 10;
instruction_data[4..36].copy_from_slice(value.base.key());
instruction_data[36..44]
.copy_from_slice(&u64::to_le_bytes(value.seed.len() as u64));

let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data[..offset + 32],
};
let offset = 44 + value.seed.len();
instruction_data[44..offset].copy_from_slice(value.seed.as_bytes());
instruction_data[offset..offset + 32].copy_from_slice(value.owner.as_ref());

invoke_signed(&instruction, &[self.account, self.base], signers)
TruncatedInstructionData::new(instruction_data, offset + 32)
},
}
}
}
Loading