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