Skip to content

Commit f194f70

Browse files
authored
Runtime: Refactor builtins module (pyth-network#304)
* runtime: builtins: move to new bank submodule * runtime: builtins: change `feature_id` to `enable_feature_id` * runtime: builtins: add stateless builtins
1 parent 54575fe commit f194f70

File tree

7 files changed

+80
-61
lines changed

7 files changed

+80
-61
lines changed

runtime/src/bank.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ use solana_sdk::recent_blockhashes_account;
4242
pub use solana_sdk::reward_type::RewardType;
4343
use {
4444
crate::{
45-
bank::metrics::*,
45+
bank::{
46+
builtins::{BuiltinPrototype, BUILTINS},
47+
metrics::*,
48+
},
4649
bank_forks::BankForks,
47-
builtins::{BuiltinPrototype, BUILTINS},
4850
epoch_rewards_hasher::hash_rewards_into_partitions,
4951
epoch_stakes::{EpochStakes, NodeVoteAccounts},
5052
installed_scheduler_pool::{BankWithScheduler, InstalledSchedulerRwLock},
@@ -210,6 +212,7 @@ struct VerifyAccountsHashConfig {
210212
mod address_lookup_table;
211213
pub mod bank_hash_details;
212214
mod builtin_programs;
215+
pub mod builtins;
213216
pub mod epoch_accounts_hash_utils;
214217
mod fee_distribution;
215218
mod metrics;
@@ -5994,7 +5997,7 @@ impl Bank {
59945997
.iter()
59955998
.chain(additional_builtins.unwrap_or(&[]).iter())
59965999
{
5997-
if builtin.feature_id.is_none() {
6000+
if builtin.enable_feature_id.is_none() {
59986001
self.add_builtin(
59996002
builtin.program_id,
60006003
builtin.name.to_string(),
@@ -7340,7 +7343,7 @@ impl Bank {
73407343
new_feature_activations: &HashSet<Pubkey>,
73417344
) {
73427345
for builtin in BUILTINS.iter() {
7343-
if let Some(feature_id) = builtin.feature_id {
7346+
if let Some(feature_id) = builtin.enable_feature_id {
73447347
let should_apply_action_for_feature_transition =
73457348
if only_apply_transitions_for_new_features {
73467349
new_feature_activations.contains(&feature_id)
Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,78 @@
1-
use {
2-
solana_program_runtime::invoke_context::BuiltinFunctionWithContext,
3-
solana_sdk::{
4-
bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, feature_set, pubkey::Pubkey,
5-
},
6-
};
7-
8-
/// Transitions of built-in programs at epoch bondaries when features are activated.
9-
pub struct BuiltinPrototype {
10-
pub feature_id: Option<Pubkey>,
11-
pub program_id: Pubkey,
12-
pub name: &'static str,
13-
pub entrypoint: BuiltinFunctionWithContext,
14-
}
1+
pub mod prototypes;
152

16-
impl std::fmt::Debug for BuiltinPrototype {
17-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18-
let mut builder = f.debug_struct("BuiltinPrototype");
19-
builder.field("program_id", &self.program_id);
20-
builder.field("name", &self.name);
21-
builder.field("feature_id", &self.feature_id);
22-
builder.finish()
23-
}
24-
}
25-
26-
#[cfg(RUSTC_WITH_SPECIALIZATION)]
27-
impl solana_frozen_abi::abi_example::AbiExample for BuiltinPrototype {
28-
fn example() -> Self {
29-
// BuiltinPrototype isn't serializable by definition.
30-
solana_program_runtime::declare_process_instruction!(MockBuiltin, 0, |_invoke_context| {
31-
// Do nothing
32-
Ok(())
33-
});
34-
Self {
35-
feature_id: None,
36-
program_id: Pubkey::default(),
37-
name: "",
38-
entrypoint: MockBuiltin::vm,
39-
}
40-
}
41-
}
3+
pub use prototypes::{BuiltinPrototype, StatelessBuiltinPrototype};
4+
use solana_sdk::{bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, feature_set};
425

436
pub static BUILTINS: &[BuiltinPrototype] = &[
447
BuiltinPrototype {
45-
feature_id: None,
8+
enable_feature_id: None,
469
program_id: solana_system_program::id(),
4710
name: "system_program",
4811
entrypoint: solana_system_program::system_processor::Entrypoint::vm,
4912
},
5013
BuiltinPrototype {
51-
feature_id: None,
14+
enable_feature_id: None,
5215
program_id: solana_vote_program::id(),
5316
name: "vote_program",
5417
entrypoint: solana_vote_program::vote_processor::Entrypoint::vm,
5518
},
5619
BuiltinPrototype {
57-
feature_id: None,
20+
enable_feature_id: None,
5821
program_id: solana_stake_program::id(),
5922
name: "stake_program",
6023
entrypoint: solana_stake_program::stake_instruction::Entrypoint::vm,
6124
},
6225
BuiltinPrototype {
63-
feature_id: None,
26+
enable_feature_id: None,
6427
program_id: solana_config_program::id(),
6528
name: "config_program",
6629
entrypoint: solana_config_program::config_processor::Entrypoint::vm,
6730
},
6831
BuiltinPrototype {
69-
feature_id: None,
32+
enable_feature_id: None,
7033
program_id: bpf_loader_deprecated::id(),
7134
name: "solana_bpf_loader_deprecated_program",
7235
entrypoint: solana_bpf_loader_program::Entrypoint::vm,
7336
},
7437
BuiltinPrototype {
75-
feature_id: None,
38+
enable_feature_id: None,
7639
program_id: bpf_loader::id(),
7740
name: "solana_bpf_loader_program",
7841
entrypoint: solana_bpf_loader_program::Entrypoint::vm,
7942
},
8043
BuiltinPrototype {
81-
feature_id: None,
44+
enable_feature_id: None,
8245
program_id: bpf_loader_upgradeable::id(),
8346
name: "solana_bpf_loader_upgradeable_program",
8447
entrypoint: solana_bpf_loader_program::Entrypoint::vm,
8548
},
8649
BuiltinPrototype {
87-
feature_id: None,
50+
enable_feature_id: None,
8851
program_id: solana_sdk::compute_budget::id(),
8952
name: "compute_budget_program",
9053
entrypoint: solana_compute_budget_program::Entrypoint::vm,
9154
},
9255
BuiltinPrototype {
93-
feature_id: None,
56+
enable_feature_id: None,
9457
program_id: solana_sdk::address_lookup_table::program::id(),
9558
name: "address_lookup_table_program",
9659
entrypoint: solana_address_lookup_table_program::processor::Entrypoint::vm,
9760
},
9861
BuiltinPrototype {
99-
feature_id: Some(feature_set::zk_token_sdk_enabled::id()),
62+
enable_feature_id: Some(feature_set::zk_token_sdk_enabled::id()),
10063
program_id: solana_zk_token_sdk::zk_token_proof_program::id(),
10164
name: "zk_token_proof_program",
10265
entrypoint: solana_zk_token_proof_program::Entrypoint::vm,
10366
},
10467
BuiltinPrototype {
105-
feature_id: Some(feature_set::enable_program_runtime_v2_and_loader_v4::id()),
68+
enable_feature_id: Some(feature_set::enable_program_runtime_v2_and_loader_v4::id()),
10669
program_id: solana_sdk::loader_v4::id(),
10770
name: "loader_v4",
10871
entrypoint: solana_loader_v4_program::Entrypoint::vm,
10972
},
11073
];
74+
75+
pub static STATELESS_BUILTINS: &[StatelessBuiltinPrototype] = &[StatelessBuiltinPrototype {
76+
program_id: solana_sdk::feature::id(),
77+
name: "feature_gate_program",
78+
}];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use {
2+
solana_program_runtime::invoke_context::BuiltinFunctionWithContext, solana_sdk::pubkey::Pubkey,
3+
};
4+
5+
/// Transitions of built-in programs at epoch boundaries when features are activated.
6+
pub struct BuiltinPrototype {
7+
pub enable_feature_id: Option<Pubkey>,
8+
pub program_id: Pubkey,
9+
pub name: &'static str,
10+
pub entrypoint: BuiltinFunctionWithContext,
11+
}
12+
13+
impl std::fmt::Debug for BuiltinPrototype {
14+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15+
let mut builder = f.debug_struct("BuiltinPrototype");
16+
builder.field("program_id", &self.program_id);
17+
builder.field("name", &self.name);
18+
builder.field("enable_feature_id", &self.enable_feature_id);
19+
builder.finish()
20+
}
21+
}
22+
23+
#[cfg(RUSTC_WITH_SPECIALIZATION)]
24+
impl solana_frozen_abi::abi_example::AbiExample for BuiltinPrototype {
25+
fn example() -> Self {
26+
// BuiltinPrototype isn't serializable by definition.
27+
solana_program_runtime::declare_process_instruction!(MockBuiltin, 0, |_invoke_context| {
28+
// Do nothing
29+
Ok(())
30+
});
31+
Self {
32+
enable_feature_id: None,
33+
program_id: Pubkey::default(),
34+
name: "",
35+
entrypoint: MockBuiltin::vm,
36+
}
37+
}
38+
}
39+
40+
/// Transitions of stateless built-in programs at epoch boundaries when
41+
/// features are activated.
42+
/// These are built-in programs that don't actually exist, but their address
43+
/// is reserved.
44+
#[derive(Debug)]
45+
pub struct StatelessBuiltinPrototype {
46+
pub program_id: Pubkey,
47+
pub name: &'static str,
48+
}

runtime/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub mod bank;
99
pub mod bank_client;
1010
pub mod bank_forks;
1111
pub mod bank_utils;
12-
pub mod builtins;
1312
pub mod commitment;
1413
pub mod compute_budget_details;
1514
mod epoch_rewards_hasher;

runtime/src/serde_snapshot.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
22
crate::{
3-
bank::{Bank, BankFieldsToDeserialize, BankRc},
4-
builtins::BuiltinPrototype,
3+
bank::{builtins::BuiltinPrototype, Bank, BankFieldsToDeserialize, BankRc},
54
epoch_stakes::EpochStakes,
65
serde_snapshot::storage::SerializableAccountStorageEntry,
76
snapshot_utils::{

runtime/src/snapshot_bank_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
22
crate::{
3-
bank::{Bank, BankFieldsToDeserialize, BankSlotDelta},
4-
builtins::BuiltinPrototype,
3+
bank::{builtins::BuiltinPrototype, Bank, BankFieldsToDeserialize, BankSlotDelta},
54
serde_snapshot::{
65
bank_from_streams, bank_to_stream, fields_from_streams,
76
BankIncrementalSnapshotPersistence, SerdeStyle,

runtime/src/snapshot_minimizer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Used to create minimal snapshots - separated here to keep accounts_db simpler
22
33
use {
4-
crate::{bank::Bank, builtins::BUILTINS, static_ids},
4+
crate::{
5+
bank::{builtins::BUILTINS, Bank},
6+
static_ids,
7+
},
58
dashmap::DashSet,
69
log::info,
710
rayon::{

0 commit comments

Comments
 (0)