-
Notifications
You must be signed in to change notification settings - Fork 17
Reimplementation of the world program with Pinocchio #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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} |
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 | ||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
/// 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, | ||||||||||||||||||
) | ||||||||||||||||||
} | ||||||||||||||||||
} |
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, | ||
) | ||
} | ||
} |
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::*; |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: Typo: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'
Suggested change
|
||||||
|
||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: Typo: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'
Suggested change
|
||||||||||
|
||||||||||
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, | ||||||||||
) | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
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