add solana_name_id, reassociate names with modules, modularize id tests (#4580)

This commit is contained in:
Rob Walker
2019-06-06 19:27:49 -07:00
committed by GitHub
parent 191483f4ee
commit fd9fd43e83
20 changed files with 199 additions and 194 deletions

View File

@ -1,10 +1,6 @@
use crate::pubkey::Pubkey;
const BPF_LOADER_PROGRAM_ID: [u8; 32] = [
const ID: [u8; 32] = [
2, 168, 246, 145, 78, 136, 161, 107, 189, 35, 149, 133, 95, 100, 4, 217, 180, 244, 86, 183,
130, 27, 176, 20, 87, 73, 66, 140, 0, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&BPF_LOADER_PROGRAM_ID)
}
crate::solana_name_id!(ID, "BPFLoader1111111111111111111111111111111111");

View File

@ -20,35 +20,15 @@ macro_rules! solana_entrypoint(
($entrypoint:ident) => (
#[no_mangle]
pub extern "C" fn process(
program_id: &solana_sdk::pubkey::Pubkey,
keyed_accounts: &mut [solana_sdk::account::KeyedAccount],
program_id: &$crate::pubkey::Pubkey,
keyed_accounts: &mut [$crate::account::KeyedAccount],
data: &[u8],
) -> Result<(), solana_sdk::instruction::InstructionError> {
) -> Result<(), $crate::instruction::InstructionError> {
$entrypoint(program_id, keyed_accounts, data)
}
)
);
#[macro_export]
macro_rules! solana_program_id(
($program_id:ident) => (
pub fn check_id(program_id: &solana_sdk::pubkey::Pubkey) -> bool {
program_id.as_ref() == $program_id
}
pub fn id() -> solana_sdk::pubkey::Pubkey {
solana_sdk::pubkey::Pubkey::new(&$program_id)
}
#[cfg(test)]
#[test]
fn test_program_id() {
assert!(check_id(&id()));
}
)
);
pub trait DecodeError<E> {
fn decode_custom_error_to_enum(int: u32) -> Option<E>
where

View File

@ -1,18 +1,11 @@
use crate::account::Account;
use crate::pubkey::Pubkey;
const NATIVE_LOADER_PROGRAM_ID: [u8; 32] = [
const ID: [u8; 32] = [
5, 135, 132, 191, 20, 139, 164, 40, 47, 176, 18, 87, 72, 136, 169, 241, 83, 160, 125, 173, 247,
101, 192, 69, 92, 154, 151, 3, 128, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&NATIVE_LOADER_PROGRAM_ID)
}
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == NATIVE_LOADER_PROGRAM_ID
}
crate::solana_name_id!(ID, "NativeLoader1111111111111111111111111111111");
/// Create an executable account with the given shared object name.
pub fn create_loadable_account(name: &str) -> Account {

View File

@ -88,6 +88,41 @@ pub fn read_pubkey(infile: &str) -> Result<Pubkey, Box<error::Error>> {
Ok(Pubkey::from_str(&printable)?)
}
#[macro_export]
macro_rules! solana_id(
($id:ident) => (
pub fn check_id(id: &$crate::pubkey::Pubkey) -> bool {
id.as_ref() == $id
}
pub fn id() -> $crate::pubkey::Pubkey {
$crate::pubkey::Pubkey::new(&$id)
}
#[cfg(test)]
#[test]
fn test_id() {
assert!(check_id(&id()));
}
)
);
#[macro_export]
macro_rules! solana_name_id(
($id:ident, $name:expr) => (
$crate::solana_id!($id);
#[cfg(test)]
#[test]
fn test_name_id() {
assert_eq!(id().to_string(), $name);
}
)
);
#[cfg(test)]
mod tests {
use super::*;

View File

@ -0,0 +1,50 @@
//! This account contains the current slot, epoch, and stakers_epoch
//!
use crate::account::Account;
use crate::syscall;
use bincode::serialized_size;
crate::solana_name_id!(ID, "Sysca11Current11111111111111111111111111111");
const ID: [u8; 32] = [
6, 167, 211, 138, 69, 218, 14, 184, 34, 50, 188, 33, 201, 49, 63, 13, 15, 193, 33, 132, 208,
238, 129, 224, 101, 67, 14, 11, 160, 0, 0, 0,
];
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Current {
slot: u64,
block: u64,
epoch: u64,
stakers_epoch: u64,
}
impl Current {
pub fn from(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
}
pub fn create_account(lamports: u64) -> Account {
Account::new(lamports, Current::size_of(), &syscall::id())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = create_account(1);
let current = Current::from(&account).unwrap();
assert_eq!(current, Current::default());
}
}

View File

@ -3,24 +3,16 @@
use crate::account::Account;
use crate::account_utils::State;
use crate::fee_calculator::FeeCalculator;
use crate::pubkey::Pubkey;
use crate::syscall;
use bincode::serialized_size;
/// "Sysca11Fees11111111111111111111111111"
/// fees account pubkey
const ID: [u8; 32] = [
6, 167, 211, 138, 69, 218, 104, 33, 3, 92, 89, 173, 16, 89, 109, 253, 49, 97, 98, 165, 87, 222,
119, 112, 253, 90, 76, 184, 0, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&ID)
}
pub fn check_id(pubkey: &Pubkey) -> bool {
pubkey.as_ref() == ID
}
crate::solana_name_id!(ID, "Sysca11Fees11111111111111111111111111111111");
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
@ -49,15 +41,6 @@ pub fn create_account(lamports: u64) -> Account {
mod tests {
use super::*;
#[test]
fn test_fees_id() {
let name = "Sysca11Fees11111111111111111111111111111111";
// To get the bytes above:
// dbg!((name, bs58::decode(name).into_vec().unwrap()));
assert_eq!(name, id().to_string());
assert!(check_id(&id()));
}
#[test]
fn test_fees_create_account() {
let lamports = 42;

View File

@ -2,10 +2,18 @@
//!
use crate::pubkey::Pubkey;
pub mod current;
pub mod fees;
pub mod slot_hashes;
pub mod tick_height;
pub fn is_syscall_id(id: &Pubkey) -> bool {
current::check_id(id)
|| fees::check_id(id)
|| slot_hashes::check_id(id)
|| tick_height::check_id(id)
}
/// "Sysca11111111111111111111111111111111111111"
/// owner pubkey for syscall accounts
const ID: [u8; 32] = [
@ -13,25 +21,4 @@ const ID: [u8; 32] = [
253, 202, 87, 144, 232, 16, 195, 192, 0, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&ID)
}
pub fn check_id(id: &Pubkey) -> bool {
id.as_ref() == ID
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_syscall_ids() {
let ids = [("Sysca11111111111111111111111111111111111111", id())];
// to get the bytes above:
// ids.iter().for_each(|(name, _)| {
// dbg!((name, bs58::decode(name).into_vec().unwrap()));
// });
assert!(ids.iter().all(|(name, id)| *name == id.to_string()));
assert!(check_id(&id()));
}
}
crate::solana_name_id!(ID, "Sysca11111111111111111111111111111111111111");

View File

@ -69,17 +69,6 @@ mod tests {
use super::*;
use crate::hash::hash;
#[test]
fn test_id() {
let ids = [("Sysca11S1otHashes11111111111111111111111111", id())];
// to get the bytes above:
// ids.iter().for_each(|(name, _)| {
// dbg!((name, bs58::decode(name).into_vec().unwrap()));
// });
assert!(ids.iter().all(|(name, id)| *name == id.to_string()));
assert!(check_id(&id()));
}
#[test]
fn test_create_account() {
let lamports = 42;

View File

@ -2,7 +2,6 @@
//!
use crate::account::Account;
use crate::account_utils::State;
use crate::pubkey::Pubkey;
use crate::syscall;
use bincode::serialized_size;
@ -13,13 +12,7 @@ const ID: [u8; 32] = [
208, 229, 34, 163, 11, 168, 45, 109, 60, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&ID)
}
pub fn check_id(pubkey: &Pubkey) -> bool {
pubkey.as_ref() == ID
}
crate::solana_name_id!(ID, "Sysca11TickHeight11111111111111111111111111");
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
@ -46,15 +39,6 @@ pub fn create_account(lamports: u64) -> Account {
mod tests {
use super::*;
#[test]
fn test_tick_height_id() {
let name = "Sysca11TickHeight11111111111111111111111111";
// To get the bytes above:
// dbg!((name, bs58::decode(name).into_vec().unwrap()));
assert_eq!(name, id().to_string());
assert!(check_id(&id()));
}
#[test]
fn test_tick_height_create_account() {
let account = create_account(1);

View File

@ -9,6 +9,8 @@ pub enum SystemError {
AccountAlreadyInUse,
ResultWithNegativeLamports,
SourceNotSystemAccount,
InvalidProgramId,
InvalidAccountId,
}
impl<T> DecodeError<T> for SystemError {

View File

@ -1,13 +1,5 @@
use crate::pubkey::Pubkey;
const SYSTEM_PROGRAM_ID: [u8; 32] = [
const ID: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub fn id() -> Pubkey {
Pubkey::new(&SYSTEM_PROGRAM_ID)
}
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == SYSTEM_PROGRAM_ID
}
crate::solana_name_id!(ID, "11111111111111111111111111111111");