Skip to main content

solana_program_binaries/
lib.rs

1#![cfg(feature = "agave-unstable-api")]
2#![allow(clippy::arithmetic_side_effects)]
3
4use {
5    solana_account::{Account, AccountSharedData, ReadableAccount},
6    solana_loader_v3_interface::{get_program_data_address, state::UpgradeableLoaderState},
7    solana_pubkey::Pubkey,
8    solana_rent::Rent,
9    solana_sdk_ids::{bpf_loader, bpf_loader_upgradeable},
10};
11
12mod spl_memo_1_0 {
13    solana_pubkey::declare_id!("Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo");
14}
15mod spl_memo_3_0 {
16    solana_pubkey::declare_id!("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
17}
18mod spl_memo_4_0 {
19    solana_pubkey::declare_id!("Memo4c2pN8afCj432Lb7RMVKi9PbQnnW7ewFFaV3oAH");
20}
21
22static SPL_PROGRAMS: &[(Pubkey, Pubkey, &[u8])] = &[
23    (
24        spl_generic_token::token::ID,
25        solana_sdk_ids::bpf_loader_upgradeable::ID,
26        include_bytes!("programs/spl_p_token-1.0.0.so"),
27    ),
28    (
29        spl_generic_token::token_2022::ID,
30        solana_sdk_ids::bpf_loader_upgradeable::ID,
31        include_bytes!("programs/spl_token_2022-10.0.0.so"),
32    ),
33    (
34        spl_memo_1_0::ID,
35        solana_sdk_ids::bpf_loader::ID,
36        include_bytes!("programs/spl_memo-1.0.0.so"),
37    ),
38    (
39        spl_memo_3_0::ID,
40        solana_sdk_ids::bpf_loader::ID,
41        include_bytes!("programs/spl_memo-3.0.0.so"),
42    ),
43    (
44        spl_memo_4_0::ID,
45        solana_sdk_ids::bpf_loader_upgradeable::ID,
46        include_bytes!("programs/spl_memo-4.0.0.so"),
47    ),
48    (
49        spl_generic_token::associated_token_account::ID,
50        solana_sdk_ids::bpf_loader::ID,
51        include_bytes!("programs/spl_associated_token_account-1.1.1.so"),
52    ),
53];
54
55// Programs that were previously builtins but have been migrated to Core BPF.
56// All Core BPF programs are owned by BPF loader v3.
57// Note the second pubkey is the migration feature ID. A `None` value denotes
58// activation on all clusters, therefore no feature gate.
59static CORE_BPF_PROGRAMS: &[(Pubkey, Option<Pubkey>, &[u8])] = &[
60    (
61        solana_sdk_ids::address_lookup_table::ID,
62        None,
63        include_bytes!("programs/core_bpf_address_lookup_table-3.0.0.so"),
64    ),
65    (
66        solana_sdk_ids::config::ID,
67        None,
68        include_bytes!("programs/core_bpf_config-3.0.0.so"),
69    ),
70    (
71        solana_sdk_ids::feature::ID,
72        None,
73        include_bytes!("programs/core_bpf_feature_gate-0.0.1.so"),
74    ),
75    (
76        solana_sdk_ids::stake::ID,
77        None,
78        include_bytes!("programs/core_bpf_stake-5.1.0.so"),
79    ),
80    // Add more programs here post-migration...
81];
82
83/// Returns a tuple `(Pubkey, Account)` for a BPF program, where the key is the
84/// provided program ID and the account is a valid BPF Loader program account
85/// containing the ELF.
86pub fn bpf_loader_program_account(
87    program_id: &Pubkey,
88    elf: &[u8],
89    rent: &Rent,
90) -> (Pubkey, Account) {
91    (
92        *program_id,
93        Account {
94            lamports: rent.minimum_balance(elf.len()).max(1),
95            data: elf.to_vec(),
96            owner: bpf_loader::id(),
97            executable: true,
98            rent_epoch: u64::MAX,
99        },
100    )
101}
102
103/// Returns two tuples `(Pubkey, Account)` for a BPF upgradeable program.
104/// The first tuple is the program account. It contains the provided program ID
105/// and an account with a pointer to its program data account.
106/// The second tuple is the program data account. It contains the program data
107/// address and an account with the program data - a valid BPF Loader Upgradeable
108/// program data account containing the ELF.
109pub fn bpf_loader_upgradeable_program_accounts(
110    program_id: &Pubkey,
111    elf: &[u8],
112    rent: &Rent,
113) -> [(Pubkey, Account); 2] {
114    let programdata_address = get_program_data_address(program_id);
115    let program_account = {
116        let space = UpgradeableLoaderState::size_of_program();
117        let lamports = rent.minimum_balance(space);
118        let data = bincode::serialize(&UpgradeableLoaderState::Program {
119            programdata_address,
120        })
121        .unwrap();
122        Account {
123            lamports,
124            data,
125            owner: bpf_loader_upgradeable::id(),
126            executable: true,
127            rent_epoch: u64::MAX,
128        }
129    };
130    let programdata_account = {
131        let space = UpgradeableLoaderState::size_of_programdata_metadata() + elf.len();
132        let lamports = rent.minimum_balance(space);
133        let mut data = bincode::serialize(&UpgradeableLoaderState::ProgramData {
134            slot: 0,
135            upgrade_authority_address: Some(Pubkey::default()),
136        })
137        .unwrap();
138        data.extend_from_slice(elf);
139        Account {
140            lamports,
141            data,
142            owner: bpf_loader_upgradeable::id(),
143            executable: false,
144            rent_epoch: u64::MAX,
145        }
146    };
147    [
148        (*program_id, program_account),
149        (programdata_address, programdata_account),
150    ]
151}
152
153pub fn spl_programs(rent: &Rent) -> Vec<(Pubkey, AccountSharedData)> {
154    SPL_PROGRAMS
155        .iter()
156        .flat_map(|(program_id, loader_id, elf)| {
157            let mut accounts = vec![];
158            if loader_id.eq(&solana_sdk_ids::bpf_loader_upgradeable::ID) {
159                for (key, account) in bpf_loader_upgradeable_program_accounts(program_id, elf, rent)
160                {
161                    accounts.push((key, AccountSharedData::from(account)));
162                }
163            } else {
164                let (key, account) = bpf_loader_program_account(program_id, elf, rent);
165                accounts.push((key, AccountSharedData::from(account)));
166            }
167            accounts
168        })
169        .collect()
170}
171
172pub fn core_bpf_programs<F>(rent: &Rent, is_feature_active: F) -> Vec<(Pubkey, AccountSharedData)>
173where
174    F: Fn(&Pubkey) -> bool,
175{
176    CORE_BPF_PROGRAMS
177        .iter()
178        .flat_map(|(program_id, feature_id, elf)| {
179            let mut accounts = vec![];
180            if feature_id.is_none() || feature_id.is_some_and(|f| is_feature_active(&f)) {
181                for (key, account) in bpf_loader_upgradeable_program_accounts(program_id, elf, rent)
182                {
183                    accounts.push((key, AccountSharedData::from(account)));
184                }
185            }
186            accounts
187        })
188        .collect()
189}
190
191pub fn by_id(program_id: &Pubkey, rent: &Rent) -> Option<Vec<(Pubkey, AccountSharedData)>> {
192    let programs = spl_programs(rent);
193    if let Some(i) = programs.iter().position(|(key, _)| key == program_id) {
194        let n = num_accounts(programs[i].1.owner());
195        return Some(programs.into_iter().skip(i).take(n).collect());
196    }
197
198    let programs = core_bpf_programs(rent, |_| true);
199    if let Some(i) = programs.iter().position(|(key, _)| key == program_id) {
200        let n = num_accounts(programs[i].1.owner());
201        return Some(programs.into_iter().skip(i).take(n).collect());
202    }
203
204    None
205}
206
207fn num_accounts(owner_id: &Pubkey) -> usize {
208    if *owner_id == bpf_loader_upgradeable::id() {
209        2
210    } else {
211        1
212    }
213}