Skip to main content

solana_loader_v2_interface/
lib.rs

1//! Instructions for the non-upgradable BPF loader.
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4#[cfg(feature = "stable-abi")]
5use solana_frozen_abi_macro::{frozen_abi, StableAbi, StableAbiSample};
6#[cfg(feature = "wincode")]
7use wincode::{SchemaRead, SchemaWrite};
8#[cfg(any(feature = "bincode", feature = "wincode"))]
9use {
10    solana_instruction::{AccountMeta, Instruction},
11    solana_pubkey::Pubkey,
12    solana_sdk_ids::sysvar::rent,
13};
14
15#[cfg_attr(
16    feature = "stable-abi",
17    frozen_abi(
18        abi_digest = "C8z9CxbjNT9UwGVkCkubvUsZH7ZUJPNkNn9TmRp6ZsPC",
19        abi_serializer = ["bincode", "wincode"],
20        test_roundtrip = "eq_and_wire"
21    ),
22    derive(StableAbi, StableAbiSample)
23)]
24#[cfg_attr(
25    feature = "serde",
26    derive(serde_derive::Deserialize, serde_derive::Serialize)
27)]
28#[cfg_attr(feature = "wincode", derive(SchemaRead, SchemaWrite))]
29#[derive(Debug, PartialEq, Eq, Clone)]
30pub enum LoaderInstruction {
31    /// Write program data into an account
32    ///
33    /// # Account references
34    ///   0. [WRITE, SIGNER] Account to write to
35    Write {
36        /// Offset at which to write the given bytes
37        offset: u32,
38
39        /// Serialized program data
40        #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
41        bytes: Vec<u8>,
42    },
43
44    /// Finalize an account loaded with program data for execution
45    ///
46    /// The exact preparation steps is loader specific but on success the loader must set the executable
47    /// bit of the account.
48    ///
49    /// # Account references
50    ///   0. [WRITE, SIGNER] The account to prepare for execution
51    ///   1. [] Rent sysvar
52    Finalize,
53}
54
55#[cfg(all(feature = "bincode", not(feature = "wincode")))]
56#[inline(always)]
57fn create_instruction(
58    program_id: Pubkey,
59    data: &LoaderInstruction,
60    accounts: Vec<AccountMeta>,
61) -> Instruction {
62    Instruction::new_with_bincode(program_id, data, accounts)
63}
64
65#[cfg(feature = "wincode")]
66#[inline(always)]
67fn create_instruction(
68    program_id: Pubkey,
69    data: &LoaderInstruction,
70    accounts: Vec<AccountMeta>,
71) -> Instruction {
72    Instruction::new_with_wincode(program_id, data, accounts)
73}
74
75#[deprecated(since = "2.2.0", note = "Use loader-v4 instead")]
76#[cfg(any(feature = "bincode", feature = "wincode"))]
77pub fn write(
78    account_pubkey: &Pubkey,
79    program_id: &Pubkey,
80    offset: u32,
81    bytes: Vec<u8>,
82) -> Instruction {
83    let account_metas = vec![AccountMeta::new(*account_pubkey, true)];
84    create_instruction(
85        *program_id,
86        &LoaderInstruction::Write { offset, bytes },
87        account_metas,
88    )
89}
90
91#[deprecated(since = "2.2.0", note = "Use loader-v4 instead")]
92#[cfg(any(feature = "bincode", feature = "wincode"))]
93pub fn finalize(account_pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
94    let account_metas = vec![
95        AccountMeta::new(*account_pubkey, true),
96        AccountMeta::new_readonly(rent::id(), false),
97    ];
98    create_instruction(*program_id, &LoaderInstruction::Finalize, account_metas)
99}
100
101#[cfg(all(test, feature = "bincode", feature = "wincode"))]
102mod tests {
103    use super::*;
104
105    #[test]
106    #[allow(deprecated)]
107    fn test_builders_match_bincode_encoding() {
108        let account = Pubkey::new_unique();
109        let program_id = Pubkey::new_unique();
110        for (ix, expected) in [
111            (
112                write(&account, &program_id, 7, vec![9, 8, 7]),
113                LoaderInstruction::Write {
114                    offset: 7,
115                    bytes: vec![9, 8, 7],
116                },
117            ),
118            (finalize(&account, &program_id), LoaderInstruction::Finalize),
119        ] {
120            assert_eq!(ix.program_id, program_id);
121            assert_eq!(ix.data, bincode::serialize(&expected).unwrap());
122        }
123    }
124}