// file: kb-program-ids/src/programs.rs // version: 4 //! Enumerable canonical program registry. /// One enumerable program identifier entry. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct ProgramIdEntry { /// Stable lower snake case registry code. pub name: &'static str, /// Base58 Solana program identifier. pub address: &'static str, } impl ProgramIdEntry { /// Returns the stable lower snake case registry code. pub const fn code(&self) -> &'static str { return self.name; } /// Returns the Base58 Solana program identifier. pub const fn program_id(&self) -> &'static str { return self.address; } } /// Returns every program identifier currently registered by this crate. pub fn entries() -> &'static [crate::ProgramIdEntry] { return crate::PROGRAM_ID_ENTRIES; } /// Returns every program identifier currently registered by this crate. pub fn registered_program_ids() -> &'static [crate::ProgramIdEntry] { return crate::PROGRAM_ID_ENTRIES; } /// Returns every executable runtime-native, loader, precompile and historical native program identifier. pub fn native_program_ids() -> &'static [crate::ProgramIdEntry] { return crate::NATIVE_PROGRAM_ID_ENTRIES; } /// Returns native well-known accounts that are not executable programs. pub fn native_well_known_account_ids() -> &'static [crate::ProgramIdEntry] { return crate::NATIVE_WELL_KNOWN_ACCOUNT_ID_ENTRIES; } /// Finds one registered program or well-known account by exact Base58 identifier. pub fn find_registered_program_id( program_id: &str, ) -> std::option::Option<&'static crate::ProgramIdEntry> { return crate::PROGRAM_ID_ENTRIES .iter() .find(|entry| return entry.address == program_id); } #[cfg(test)] mod tests { #[test] fn registry_entries_are_unique_and_non_empty() { let mut codes = std::collections::BTreeSet::new(); let mut program_ids = std::collections::BTreeSet::new(); for entry in crate::registered_program_ids() { assert!(!entry.code().is_empty()); assert!(!entry.program_id().is_empty()); assert!(codes.insert(entry.code())); assert!(program_ids.insert(entry.program_id())); } for window in crate::registered_program_ids().windows(2) { assert!(window[0].code() < window[1].code()); } assert_eq!(crate::registered_program_ids().len(), 137); } #[test] fn native_registry_contains_exactly_every_solana_core_surface() { let codes = crate::native_program_ids() .iter() .map(crate::ProgramIdEntry::code) .collect::>(); assert!(codes.contains("vote")); assert!(codes.contains("slashing")); assert!(codes.contains("bpf_loader_deprecated")); assert!(codes.contains("bpf_loader")); assert!(codes.contains("bpf_loader_upgradeable")); assert!(codes.contains("loader_v4")); assert_eq!(codes.len(), 18); assert_eq!(codes.len(), crate::native_program_ids().len()); } #[test] fn stake_config_is_a_well_known_account_not_an_executable_program() { assert!(crate::native_well_known_account_ids().iter().any(|entry| { return entry.program_id() == crate::STAKE_CONFIG_ACCOUNT_ID; })); assert!(!crate::native_program_ids().iter().any(|entry| { return entry.program_id() == crate::STAKE_CONFIG_ACCOUNT_ID; })); } #[test] fn registry_lookup_finds_system_program() { let entry = crate::find_registered_program_id(crate::SYSTEM_PROGRAM_ID); assert_eq!(entry.map(crate::ProgramIdEntry::code), std::option::Option::Some("system")); } #[test] fn compatibility_api_and_core_constants_are_complete() { assert_eq!(crate::entries(), crate::registered_program_ids()); assert_eq!(crate::STAKE_CONFIG_PROGRAM_ID, crate::STAKE_CONFIG_ACCOUNT_ID); let expected = [ crate::ADDRESS_LOOKUP_TABLE_PROGRAM_ID, crate::COMPUTE_BUDGET_PROGRAM_ID, crate::CONFIG_PROGRAM_ID, crate::FEATURE_PROGRAM_ID, crate::SYSTEM_PROGRAM_ID, crate::VOTE_PROGRAM_ID, crate::STAKE_PROGRAM_ID, crate::SLASHING_PROGRAM_ID, crate::ZK_ELGAMAL_PROOF_PROGRAM_ID, crate::ZK_TOKEN_PROOF_PROGRAM_ID, ]; for program_id in expected { assert!(crate::find_registered_program_id(program_id).is_some()); } } #[test] fn protocol_primitives_are_registered_without_joining_native_surfaces() { let protocol_program_ids = [ crate::SPL_MEMO_V1_PROGRAM_ID, crate::SPL_MEMO_V3_PROGRAM_ID, crate::SPL_MEMO_V4_PROGRAM_ID, crate::SPL_TOKEN_PROGRAM_ID, crate::SPL_TOKEN_2022_PROGRAM_ID, crate::SPL_TOKEN_2022_ELGAMAL_REGISTRY_PROGRAM_ID, crate::ASSOCIATED_TOKEN_PROGRAM_ID, crate::METADATA_METAPLEX_TOKEN_METADATA_PROGRAM_ID, ]; for program_id in protocol_program_ids { assert!(crate::find_registered_program_id(program_id).is_some()); assert!( !crate::native_program_ids() .iter() .any(|entry| return entry.program_id() == program_id) ); } } }