solana_program/
loader_instruction.rs

1//! Instructions for the [non-upgradable BPF loader][nubpfl].
2//!
3//! [nubpfl]: crate::bpf_loader
4
5use crate::{
6    instruction::{AccountMeta, Instruction},
7    pubkey::Pubkey,
8    sysvar::rent,
9};
10
11#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
12pub enum LoaderInstruction {
13    /// Write program data into an account
14    ///
15    /// # Account references
16    ///   0. [WRITE, SIGNER] Account to write to
17    Write {
18        /// Offset at which to write the given bytes
19        offset: u32,
20
21        /// Serialized program data
22        #[serde(with = "serde_bytes")]
23        bytes: Vec<u8>,
24    },
25
26    /// Finalize an account loaded with program data for execution
27    ///
28    /// The exact preparation steps is loader specific but on success the loader must set the executable
29    /// bit of the account.
30    ///
31    /// # Account references
32    ///   0. [WRITE, SIGNER] The account to prepare for execution
33    ///   1. [] Rent sysvar
34    Finalize,
35}
36
37pub fn write(
38    account_pubkey: &Pubkey,
39    program_id: &Pubkey,
40    offset: u32,
41    bytes: Vec<u8>,
42) -> Instruction {
43    let account_metas = vec![AccountMeta::new(*account_pubkey, true)];
44    Instruction::new_with_bincode(
45        *program_id,
46        &LoaderInstruction::Write { offset, bytes },
47        account_metas,
48    )
49}
50
51pub fn finalize(account_pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
52    let account_metas = vec![
53        AccountMeta::new(*account_pubkey, true),
54        AccountMeta::new_readonly(rent::id(), false),
55    ];
56    Instruction::new_with_bincode(*program_id, &LoaderInstruction::Finalize, account_metas)
57}