From e58e48e91935916c630a759eb5d9a0c2b7164bd7 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Wed, 9 Oct 2019 23:22:33 -0700 Subject: [PATCH] make sysvar creation a bit more foolproof (#6294) --- sdk/src/sysvar/clock.rs | 2 +- sdk/src/sysvar/epoch_schedule.rs | 2 +- sdk/src/sysvar/fees.rs | 2 +- sdk/src/sysvar/mod.rs | 23 ++++++++++++++++++++++- sdk/src/sysvar/rent.rs | 2 +- sdk/src/sysvar/rewards.rs | 2 +- sdk/src/sysvar/slot_hashes.rs | 2 +- sdk/src/sysvar/stake_history.rs | 2 +- 8 files changed, 29 insertions(+), 8 deletions(-) diff --git a/sdk/src/sysvar/clock.rs b/sdk/src/sysvar/clock.rs index a48b20e75f..4e43ac8ea2 100644 --- a/sdk/src/sysvar/clock.rs +++ b/sdk/src/sysvar/clock.rs @@ -12,7 +12,7 @@ const ID: [u8; 32] = [ 75, 109, 92, 115, 85, 91, 33, 0, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarC1ock11111111111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarC1ock11111111111111111111111111111111"); #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] diff --git a/sdk/src/sysvar/epoch_schedule.rs b/sdk/src/sysvar/epoch_schedule.rs index bd62bc41d7..ea30e6187b 100644 --- a/sdk/src/sysvar/epoch_schedule.rs +++ b/sdk/src/sysvar/epoch_schedule.rs @@ -14,7 +14,7 @@ const ID: [u8; 32] = [ 30, 63, 80, 135, 25, 168, 5, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarEpochSchedu1e111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarEpochSchedu1e111111111111111111111111"); impl EpochSchedule { pub fn deserialize(account: &Account) -> Result { diff --git a/sdk/src/sysvar/fees.rs b/sdk/src/sysvar/fees.rs index f55cfee6dc..531931ed84 100644 --- a/sdk/src/sysvar/fees.rs +++ b/sdk/src/sysvar/fees.rs @@ -12,7 +12,7 @@ const ID: [u8; 32] = [ 111, 196, 237, 82, 106, 156, 144, 0, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarFees111111111111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarFees111111111111111111111111111111111"); #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default)] diff --git a/sdk/src/sysvar/mod.rs b/sdk/src/sysvar/mod.rs index f1a294ce57..9686ac8be6 100644 --- a/sdk/src/sysvar/mod.rs +++ b/sdk/src/sysvar/mod.rs @@ -11,9 +11,30 @@ pub mod slot_hashes; pub mod stake_history; pub fn is_sysvar_id(id: &Pubkey) -> bool { - clock::check_id(id) || fees::check_id(id) || rewards::check_id(id) || slot_hashes::check_id(id) + clock::check_id(id) + || epoch_schedule::check_id(id) + || fees::check_id(id) + || rent::check_id(id) + || rewards::check_id(id) + || slot_hashes::check_id(id) + || stake_history::check_id(id) } +#[macro_export] +macro_rules! solana_sysvar_id( + ($id:ident, $name:expr) => ( + $crate::solana_name_id!($id, $name); + + #[cfg(test)] + #[test] + fn test_sysvar_id() { + if !$crate::sysvar::is_sysvar_id(&id()) { + panic!("sysvar::is_sysvar_id() doesn't know about {}", $name); + } + } + ) +); + /// "Sysvar1111111111111111111111111111111111111" /// owner pubkey for sysvar accounts const ID: [u8; 32] = [ diff --git a/sdk/src/sysvar/rent.rs b/sdk/src/sysvar/rent.rs index cd6e95da61..a53ad38d1e 100644 --- a/sdk/src/sysvar/rent.rs +++ b/sdk/src/sysvar/rent.rs @@ -15,7 +15,7 @@ const ID: [u8; 32] = [ 253, 68, 227, 219, 217, 138, 0, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarRent111111111111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarRent111111111111111111111111111111111"); #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default)] diff --git a/sdk/src/sysvar/rewards.rs b/sdk/src/sysvar/rewards.rs index c792da6e19..468ecc1562 100644 --- a/sdk/src/sysvar/rewards.rs +++ b/sdk/src/sysvar/rewards.rs @@ -11,7 +11,7 @@ const ID: [u8; 32] = [ 130, 184, 161, 97, 145, 87, 141, 128, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarRewards111111111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarRewards111111111111111111111111111111"); #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] diff --git a/sdk/src/sysvar/slot_hashes.rs b/sdk/src/sysvar/slot_hashes.rs index f6c9fbd5da..43fd669151 100644 --- a/sdk/src/sysvar/slot_hashes.rs +++ b/sdk/src/sysvar/slot_hashes.rs @@ -11,7 +11,7 @@ const ID: [u8; 32] = [ 208, 190, 59, 19, 110, 45, 0, 85, 32, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarS1otHashes111111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarS1otHashes111111111111111111111111111"); pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in diff --git a/sdk/src/sysvar/stake_history.rs b/sdk/src/sysvar/stake_history.rs index 114003a851..25370d2944 100644 --- a/sdk/src/sysvar/stake_history.rs +++ b/sdk/src/sysvar/stake_history.rs @@ -13,7 +13,7 @@ const ID: [u8; 32] = [ 87, 184, 86, 108, 197, 55, 95, 244, 0, 0, 0, ]; -crate::solana_name_id!(ID, "SysvarStakeHistory1111111111111111111111111"); +crate::solana_sysvar_id!(ID, "SysvarStakeHistory1111111111111111111111111"); pub const MAX_STAKE_HISTORY: usize = 512; // it should never take as many as 512 epochs to warm up or cool down