2023-04-21 09:08:32 -07:00
|
|
|
use {
|
2023-05-02 14:01:28 -07:00
|
|
|
solana_program_runtime::{builtin_program::create_builtin, loaded_programs::LoadedProgram},
|
2023-04-26 05:44:19 -07:00
|
|
|
solana_sdk::{
|
|
|
|
bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, feature_set, pubkey::Pubkey,
|
|
|
|
},
|
2023-05-02 14:01:28 -07:00
|
|
|
std::sync::Arc,
|
2021-12-03 09:00:31 -08:00
|
|
|
};
|
2020-11-12 12:44:37 -08:00
|
|
|
|
2021-09-02 21:29:11 -07:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Builtins {
|
|
|
|
/// Builtin programs that are always available
|
2023-05-02 14:01:28 -07:00
|
|
|
pub genesis_builtins: Vec<(Pubkey, Arc<LoadedProgram>)>,
|
2021-09-02 21:29:11 -07:00
|
|
|
|
2022-02-18 18:36:59 -08:00
|
|
|
/// Dynamic feature transitions for builtin programs
|
|
|
|
pub feature_transitions: Vec<BuiltinFeatureTransition>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Actions taken by a bank when managing the list of active builtin programs.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum BuiltinAction {
|
2023-05-02 14:01:28 -07:00
|
|
|
Add(Pubkey, Arc<LoadedProgram>),
|
2022-02-18 18:36:59 -08:00
|
|
|
Remove(Pubkey),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// State transition enum used for adding and removing builtin programs through
|
|
|
|
/// feature activations.
|
|
|
|
#[derive(Debug, Clone, AbiExample)]
|
2022-06-23 05:22:30 -07:00
|
|
|
pub enum BuiltinFeatureTransition {
|
2022-02-18 18:36:59 -08:00
|
|
|
/// Add a builtin program if a feature is activated.
|
|
|
|
Add {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id: Pubkey,
|
|
|
|
builtin: Arc<LoadedProgram>,
|
2022-02-18 18:36:59 -08:00
|
|
|
feature_id: Pubkey,
|
|
|
|
},
|
|
|
|
/// Remove a builtin program if a feature is activated or
|
|
|
|
/// retain a previously added builtin.
|
|
|
|
RemoveOrRetain {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id: Pubkey,
|
|
|
|
previously_added_builtin: Arc<LoadedProgram>,
|
2022-02-22 04:54:08 -08:00
|
|
|
addition_feature_id: Pubkey,
|
2022-02-18 18:36:59 -08:00
|
|
|
removal_feature_id: Pubkey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltinFeatureTransition {
|
|
|
|
pub fn to_action(
|
|
|
|
&self,
|
|
|
|
should_apply_action_for_feature: &impl Fn(&Pubkey) -> bool,
|
|
|
|
) -> Option<BuiltinAction> {
|
2022-06-23 05:22:30 -07:00
|
|
|
match self {
|
|
|
|
Self::Add {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id,
|
2022-02-18 18:36:59 -08:00
|
|
|
builtin,
|
2022-06-23 05:22:30 -07:00
|
|
|
feature_id,
|
2022-02-18 18:36:59 -08:00
|
|
|
} => {
|
|
|
|
if should_apply_action_for_feature(feature_id) {
|
2023-05-02 14:01:28 -07:00
|
|
|
Some(BuiltinAction::Add(*program_id, builtin.clone()))
|
2022-02-18 18:36:59 -08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 05:22:30 -07:00
|
|
|
Self::RemoveOrRetain {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id,
|
2022-02-22 04:54:08 -08:00
|
|
|
previously_added_builtin,
|
2022-06-23 05:22:30 -07:00
|
|
|
addition_feature_id,
|
|
|
|
removal_feature_id,
|
2022-02-18 18:36:59 -08:00
|
|
|
} => {
|
|
|
|
if should_apply_action_for_feature(removal_feature_id) {
|
2023-05-02 14:01:28 -07:00
|
|
|
Some(BuiltinAction::Remove(*program_id))
|
2022-02-22 04:54:08 -08:00
|
|
|
} else if should_apply_action_for_feature(addition_feature_id) {
|
2022-02-18 18:36:59 -08:00
|
|
|
// Retaining is no different from adding a new builtin.
|
2023-05-02 14:01:28 -07:00
|
|
|
Some(BuiltinAction::Add(
|
|
|
|
*program_id,
|
|
|
|
previously_added_builtin.clone(),
|
|
|
|
))
|
2022-02-22 04:54:08 -08:00
|
|
|
} else {
|
|
|
|
None
|
2022-02-18 18:36:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-02 21:29:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-24 10:01:40 -07:00
|
|
|
/// Built-in programs that are always available
|
2023-05-02 14:01:28 -07:00
|
|
|
fn genesis_builtins() -> Vec<(Pubkey, Arc<LoadedProgram>)> {
|
2020-09-24 12:23:09 -07:00
|
|
|
vec![
|
2023-05-02 14:01:28 -07:00
|
|
|
(
|
|
|
|
solana_system_program::id(),
|
|
|
|
create_builtin(
|
|
|
|
"system_program".to_string(),
|
|
|
|
solana_system_program::system_processor::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_vote_program::id(),
|
|
|
|
create_builtin(
|
|
|
|
"vote_program".to_string(),
|
|
|
|
solana_vote_program::vote_processor::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_stake_program::id(),
|
|
|
|
create_builtin(
|
|
|
|
"stake_program".to_string(),
|
|
|
|
solana_stake_program::stake_instruction::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_config_program::id(),
|
|
|
|
create_builtin(
|
|
|
|
"config_program".to_string(),
|
|
|
|
solana_config_program::config_processor::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
bpf_loader_deprecated::id(),
|
|
|
|
create_builtin(
|
|
|
|
"solana_bpf_loader_deprecated_program".to_string(),
|
|
|
|
solana_bpf_loader_program::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
bpf_loader::id(),
|
|
|
|
create_builtin(
|
|
|
|
"solana_bpf_loader_program".to_string(),
|
|
|
|
solana_bpf_loader_program::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
bpf_loader_upgradeable::id(),
|
|
|
|
create_builtin(
|
|
|
|
"solana_bpf_loader_upgradeable_program".to_string(),
|
|
|
|
solana_bpf_loader_program::process_instruction,
|
|
|
|
),
|
|
|
|
),
|
2020-09-24 12:23:09 -07:00
|
|
|
]
|
2020-08-14 12:32:45 -07:00
|
|
|
}
|
|
|
|
|
2022-02-18 18:36:59 -08:00
|
|
|
/// Dynamic feature transitions for builtin programs
|
|
|
|
fn builtin_feature_transitions() -> Vec<BuiltinFeatureTransition> {
|
2021-09-03 14:35:38 -07:00
|
|
|
vec![
|
2022-06-23 05:22:30 -07:00
|
|
|
BuiltinFeatureTransition::Add {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id: solana_sdk::compute_budget::id(),
|
|
|
|
builtin: create_builtin(
|
|
|
|
"compute_budget_program".to_string(),
|
|
|
|
solana_compute_budget_program::process_instruction,
|
|
|
|
),
|
2022-02-18 18:36:59 -08:00
|
|
|
feature_id: feature_set::add_compute_budget_program::id(),
|
2022-06-23 05:22:30 -07:00
|
|
|
},
|
|
|
|
BuiltinFeatureTransition::Add {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id: solana_address_lookup_table_program::id(),
|
|
|
|
builtin: create_builtin(
|
|
|
|
"address_lookup_table_program".to_string(),
|
|
|
|
solana_address_lookup_table_program::processor::process_instruction,
|
|
|
|
),
|
2022-02-18 18:36:59 -08:00
|
|
|
feature_id: feature_set::versioned_tx_message_enabled::id(),
|
2022-06-23 05:22:30 -07:00
|
|
|
},
|
|
|
|
BuiltinFeatureTransition::Add {
|
2023-05-02 14:01:28 -07:00
|
|
|
program_id: solana_zk_token_sdk::zk_token_proof_program::id(),
|
|
|
|
builtin: create_builtin(
|
|
|
|
"zk_token_proof_program".to_string(),
|
|
|
|
solana_zk_token_proof_program::process_instruction,
|
|
|
|
),
|
2022-02-18 18:36:59 -08:00
|
|
|
feature_id: feature_set::zk_token_sdk_enabled::id(),
|
2022-06-23 05:22:30 -07:00
|
|
|
},
|
2021-09-03 14:35:38 -07:00
|
|
|
]
|
2020-09-21 22:36:23 -07:00
|
|
|
}
|
|
|
|
|
2020-09-24 12:23:09 -07:00
|
|
|
pub(crate) fn get() -> Builtins {
|
|
|
|
Builtins {
|
|
|
|
genesis_builtins: genesis_builtins(),
|
2022-02-18 18:36:59 -08:00
|
|
|
feature_transitions: builtin_feature_transitions(),
|
2020-08-25 09:49:15 -07:00
|
|
|
}
|
2020-08-14 12:32:45 -07:00
|
|
|
}
|
2022-06-22 10:17:43 -07:00
|
|
|
|
|
|
|
/// Returns the addresses of all builtin programs.
|
|
|
|
pub fn get_pubkeys() -> Vec<Pubkey> {
|
|
|
|
let builtins = get();
|
|
|
|
|
|
|
|
let mut pubkeys = Vec::new();
|
2023-05-02 14:01:28 -07:00
|
|
|
pubkeys.extend(
|
|
|
|
builtins
|
|
|
|
.genesis_builtins
|
|
|
|
.iter()
|
|
|
|
.map(|(program_id, _builtin)| program_id),
|
|
|
|
);
|
2022-06-23 09:56:09 -07:00
|
|
|
pubkeys.extend(builtins.feature_transitions.iter().filter_map(|f| match f {
|
2023-05-02 14:01:28 -07:00
|
|
|
BuiltinFeatureTransition::Add { program_id, .. } => Some(program_id),
|
2022-06-23 09:56:09 -07:00
|
|
|
BuiltinFeatureTransition::RemoveOrRetain { .. } => None,
|
|
|
|
}));
|
2022-06-22 10:17:43 -07:00
|
|
|
pubkeys
|
|
|
|
}
|