Skip to content
Open
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
59 changes: 59 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"crates/programs/bolt-component",
"crates/programs/bolt-system",
"crates/programs/world",
"crates/pinocchio/*",
"crates/types",
"examples/*",
]
Expand Down Expand Up @@ -63,6 +64,10 @@ which = "7.0.2"
tokio = { version = "1", features = ["full"] }
sysinfo = "0.33.1"
bytemuck_derive = "=1.8.1"
pinocchio = "0.8.2"
pinocchio-pubkey = "0.2.4"
pinocchio-system = "0.2.3"
bolt-cpi-interface = { path = "crates/pinocchio/cpi-interface"}


[profile.release]
Expand Down
11 changes: 11 additions & 0 deletions crates/pinocchio/cpi-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "bolt-cpi-interface"
version = {workspace = true}
edition = {workspace = true}

[lib]
crate-type = ["rlib"]

[dependencies]
pinocchio = {workspace = true}
pinocchio-pubkey = {workspace = true}
68 changes: 68 additions & 0 deletions crates/pinocchio/cpi-interface/src/component/destroy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};

pub struct Destroy<'a> {
/// Authority
pub authority: &'a AccountInfo,
/// Data account
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Documentation comment says 'Data account' but this is the receiver account that will receive the lamports from the destroyed component

Suggested change
/// Data account
/// Receiver account that will receive lamports from the destroyed component

pub receiver: &'a AccountInfo,
/// Entity
pub entity: &'a AccountInfo,
/// Authority
pub component: &'a AccountInfo,
/// Authority
pub component_program_data: &'a AccountInfo,
Comment on lines +16 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: All three fields have identical 'Authority' documentation comments, but they represent different accounts (component, component_program_data). The comments should be more specific.

Suggested change
/// Authority
pub component: &'a AccountInfo,
/// Authority
pub component_program_data: &'a AccountInfo,
/// Component account
pub component: &'a AccountInfo,
/// Component program data account
pub component_program_data: &'a AccountInfo,

/// Instruction sysvar account
pub instruction_sysvar_account: &'a AccountInfo,
/// System program
pub system_program: &'a AccountInfo,
/// Component program
pub component_program: &'a Pubkey,
}

impl Destroy<'_> {
pub const DISCRIMINATOR: [u8; 8] = [157, 40, 96, 3, 135, 203, 143, 74];

#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 7] = [
AccountMeta::readonly_signer(self.authority.key()),
AccountMeta::writable(self.receiver.key()),
AccountMeta::readonly(self.entity.key()),
AccountMeta::writable(self.component.key()),
AccountMeta::readonly(self.component_program_data.key()),
AccountMeta::readonly(self.instruction_sysvar_account.key()),
AccountMeta::readonly(self.system_program.key()),
];

let instruction = Instruction {
program_id: self.component_program,
accounts: &account_metas,
data: Self::DISCRIMINATOR.as_slice(),
};

invoke_signed(
&instruction,
&[
self.authority,
self.receiver,
self.entity,
self.component,
self.component_program_data,
self.instruction_sysvar_account,
self.system_program,
],
signers,
)
}
}
65 changes: 65 additions & 0 deletions crates/pinocchio/cpi-interface/src/component/initialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};


pub struct Initialize<'a> {
/// Payer
pub payer: &'a AccountInfo,
/// Data account
pub data: &'a AccountInfo,
/// Entity
pub entity: &'a AccountInfo,
/// Authority
pub authority: &'a AccountInfo,
/// Instruction sysvar account
pub instruction_sysvar_account: &'a AccountInfo,
/// System program
pub system_program: &'a AccountInfo,
/// Component program
pub component_program: &'a Pubkey,
}

impl Initialize<'_> {
pub const DISCRIMINATOR: [u8; 8] = [175, 175, 109, 31, 13, 152, 155, 237];

#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 6] = [
AccountMeta::writable_signer(self.payer.key()),
AccountMeta::writable(self.data.key()),
AccountMeta::readonly(self.entity.key()),
AccountMeta::readonly(self.authority.key()),
AccountMeta::readonly(self.instruction_sysvar_account.key()),
AccountMeta::readonly(self.system_program.key()),
];

let instruction = Instruction {
program_id: self.component_program,
accounts: &account_metas,
data: Self::DISCRIMINATOR.as_slice(),
};

invoke_signed(
&instruction,
&[
self.payer,
self.data,
self.entity,
self.authority,
self.instruction_sysvar_account,
self.system_program,
],
signers,
)
}
}
11 changes: 11 additions & 0 deletions crates/pinocchio/cpi-interface/src/component/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod destroy;
pub use destroy::*;

mod initialize;
pub use initialize::*;

mod update;
pub use update::*;

mod update_with_session;
pub use update_with_session::*;
63 changes: 63 additions & 0 deletions crates/pinocchio/cpi-interface/src/component/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};


pub struct Update<'a> {
/// Component
pub component: &'a AccountInfo,
/// Authority
pub authority: &'a AccountInfo,
/// Instruction sysvar account
pub instruction_sysvar_account: &'a AccountInfo,
/// Instruction
pub component_program: &'a Pubkey,
/// Component program
pub instruction_data: &'a [u8],
}

impl Update<'_> {
pub const DISCRIMINATOR: [u8; 8] = [219, 200, 88, 176, 158, 63, 253, 127];

#[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.component.key()),
AccountMeta::readonly_signer(self.authority.key()),
AccountMeta::readonly(self.instruction_sysvar_account.key()),
];

const DISCRIMATOR_LENGTH: usize = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'

Suggested change
const DISCRIMATOR_LENGTH: usize = 8;
const DISCRIMINATOR_LENGTH: usize = 8;


let mut instruction_data = [0u8; 1024];

instruction_data[0..DISCRIMATOR_LENGTH].copy_from_slice(Self::DISCRIMINATOR.as_slice());
instruction_data[DISCRIMATOR_LENGTH..DISCRIMATOR_LENGTH + self.instruction_data.len()]
.copy_from_slice(self.instruction_data);

let instruction = Instruction {
program_id: self.component_program,
accounts: &account_metas,
data: &instruction_data[..DISCRIMATOR_LENGTH + self.instruction_data.len()],
};

invoke_signed(
&instruction,
&[
self.component,
self.authority,
self.instruction_sysvar_account,
],
signers,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};

pub struct UpdateWithSession<'a> {
/// Component
pub component: &'a AccountInfo,
/// Authority
pub authority: &'a AccountInfo,
/// Instruction sysvar account
pub instruction_sysvar_account: &'a AccountInfo,
/// Instruction sysvar account
Comment on lines +15 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Comment on line 16 says 'Instruction sysvar account' but should say 'Session token account' to match the field name

Suggested change
pub instruction_sysvar_account: &'a AccountInfo,
/// Instruction sysvar account
pub instruction_sysvar_account: &'a AccountInfo,
/// Session token account

pub session_token: &'a AccountInfo,
/// Instruction
pub component_program: &'a Pubkey,
/// Component program
pub instruction_data: &'a [u8],
}

impl UpdateWithSession<'_> {
pub const DISCRIMINATOR: [u8; 8] = [221, 55, 212, 141, 57, 85, 61, 182];

#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}

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

const DISCRIMATOR_LENGTH: usize = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'

Suggested change
const DISCRIMATOR_LENGTH: usize = 8;
const DISCRIMINATOR_LENGTH: usize = 8;


let mut instruction_data = [0u8; 1024];

instruction_data[0..DISCRIMATOR_LENGTH].copy_from_slice(Self::DISCRIMINATOR.as_slice());
instruction_data[DISCRIMATOR_LENGTH..DISCRIMATOR_LENGTH + self.instruction_data.len()]
.copy_from_slice(self.instruction_data);

let instruction = Instruction {
program_id: self.component_program,
accounts: &account_metas,
data: &instruction_data[..DISCRIMATOR_LENGTH + self.instruction_data.len()],
};

invoke_signed(
&instruction,
&[
self.component,
self.authority,
self.instruction_sysvar_account,
self.session_token,
],
signers,
)
}
}
Loading