Initial population of solana-program-sdk

This commit is contained in:
Michael Vines
2020-10-19 13:19:24 -07:00
parent 3718771ffb
commit 63db324204
79 changed files with 1474 additions and 1220 deletions

View File

@@ -0,0 +1,9 @@
//! This account contains the clock slot, epoch, and leader_schedule_epoch
//!
pub use crate::clock::Clock;
use crate::sysvar::Sysvar;
crate::declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);
impl Sysvar for Clock {}

View File

@@ -0,0 +1,24 @@
//! This account contains the current cluster rent
//!
pub use crate::epoch_schedule::EpochSchedule;
use crate::{account::Account, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarEpochSchedu1e111111111111111111111111", EpochSchedule);
impl Sysvar for EpochSchedule {}
pub fn create_account(lamports: u64, epoch_schedule: &EpochSchedule) -> Account {
epoch_schedule.create_account(lamports)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = create_account(42, &EpochSchedule::default());
let epoch_schedule = EpochSchedule::from_account(&account).unwrap();
assert_eq!(epoch_schedule, EpochSchedule::default());
}
}

View File

@@ -0,0 +1,33 @@
//! This account contains the current cluster fees
//!
use crate::{account::Account, fee_calculator::FeeCalculator, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees);
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Fees {
pub fee_calculator: FeeCalculator,
}
impl Sysvar for Fees {}
pub fn create_account(lamports: u64, fee_calculator: &FeeCalculator) -> Account {
Fees {
fee_calculator: fee_calculator.clone(),
}
.create_account(lamports)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fees_create_account() {
let lamports = 42;
let account = create_account(lamports, &FeeCalculator::default());
let fees = Fees::from_account(&account).unwrap();
assert_eq!(fees.fee_calculator, FeeCalculator::default());
}
}

View File

@@ -0,0 +1,38 @@
//! This account contains the serialized transaction instructions
use crate::{instruction::Instruction, sanitize::SanitizeError, sysvar::Sysvar};
pub type Instructions = Vec<Instruction>;
crate::declare_sysvar_id!("Sysvar1nstructions1111111111111111111111111", Instructions);
impl Sysvar for Instructions {}
pub fn load_current_index(data: &[u8]) -> u16 {
let mut instr_fixed_data = [0u8; 2];
let len = data.len();
instr_fixed_data.copy_from_slice(&data[len - 2..len]);
u16::from_le_bytes(instr_fixed_data)
}
pub fn store_current_index(data: &mut [u8], instruction_index: u16) {
let last_index = data.len() - 2;
data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
}
pub fn load_instruction_at(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
crate::message::Message::deserialize_instruction(index, data)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_load_store_instruction() {
let mut data = [4u8; 10];
store_current_index(&mut data, 3);
assert_eq!(load_current_index(&data), 3);
assert_eq!([4u8; 8], data[0..8]);
}
}

View File

@@ -0,0 +1,95 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
use crate::{
account::{Account, KeyedAccount},
account_info::AccountInfo,
instruction::InstructionError,
program_error::ProgramError,
pubkey::Pubkey,
};
pub mod clock;
pub mod epoch_schedule;
pub mod fees;
pub mod instructions;
pub mod recent_blockhashes;
pub mod rent;
pub mod rewards;
pub mod slot_hashes;
pub mod slot_history;
pub mod stake_history;
pub fn is_sysvar_id(id: &Pubkey) -> bool {
clock::check_id(id)
|| epoch_schedule::check_id(id)
|| fees::check_id(id)
|| recent_blockhashes::check_id(id)
|| rent::check_id(id)
|| rewards::check_id(id)
|| slot_hashes::check_id(id)
|| slot_history::check_id(id)
|| stake_history::check_id(id)
|| instructions::check_id(id)
}
#[macro_export]
macro_rules! declare_sysvar_id(
($name:expr, $type:ty) => (
$crate::declare_id!($name);
impl $crate::sysvar::SysvarId for $type {
fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
check_id(pubkey)
}
}
#[cfg(test)]
#[test]
fn test_sysvar_id() {
if !$crate::sysvar::is_sysvar_id(&id()) {
panic!("sysvar::is_sysvar_id() doesn't know about {}", $name);
}
}
)
);
// owner pubkey for sysvar accounts
crate::declare_id!("Sysvar1111111111111111111111111111111111111");
pub trait SysvarId {
fn check_id(pubkey: &Pubkey) -> bool;
}
// utilities for moving into and out of Accounts
pub trait Sysvar:
SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
{
fn size_of() -> usize {
bincode::serialized_size(&Self::default()).unwrap() as usize
}
fn from_account(account: &Account) -> Option<Self> {
bincode::deserialize(&account.data).ok()
}
fn to_account(&self, account: &mut Account) -> Option<()> {
bincode::serialize_into(&mut account.data[..], self).ok()
}
fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
}
fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
}
fn from_keyed_account(keyed_account: &KeyedAccount) -> Result<Self, InstructionError> {
if !Self::check_id(keyed_account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Self::from_account(&*keyed_account.try_account_ref()?)
.ok_or(InstructionError::InvalidArgument)
}
fn create_account(&self, lamports: u64) -> Account {
let data_len = Self::size_of().max(bincode::serialized_size(self).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &id());
self.to_account(&mut account).unwrap();
account
}
}

View File

@@ -0,0 +1,253 @@
use crate::{
account::Account,
declare_sysvar_id,
fee_calculator::FeeCalculator,
hash::{hash, Hash},
sysvar::Sysvar,
};
use std::{cmp::Ordering, collections::BinaryHeap, iter::FromIterator, ops::Deref};
pub const MAX_ENTRIES: usize = 150;
declare_sysvar_id!(
"SysvarRecentB1ockHashes11111111111111111111",
RecentBlockhashes
);
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Entry {
pub blockhash: Hash,
pub fee_calculator: FeeCalculator,
}
impl Entry {
pub fn new(blockhash: &Hash, fee_calculator: &FeeCalculator) -> Self {
Self {
blockhash: *blockhash,
fee_calculator: fee_calculator.clone(),
}
}
}
#[derive(Clone, Debug)]
pub struct IterItem<'a>(pub u64, pub &'a Hash, pub &'a FeeCalculator);
impl<'a> Eq for IterItem<'a> {}
impl<'a> PartialEq for IterItem<'a> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<'a> Ord for IterItem<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<'a> PartialOrd for IterItem<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RecentBlockhashes(Vec<Entry>);
impl Default for RecentBlockhashes {
fn default() -> Self {
Self(Vec::with_capacity(MAX_ENTRIES))
}
}
impl<'a> FromIterator<IterItem<'a>> for RecentBlockhashes {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut new = Self::default();
for i in iter {
new.0.push(Entry::new(i.1, i.2))
}
new
}
}
// This is cherry-picked from HEAD of rust-lang's master (ref1) because it's
// a nightly-only experimental API.
// (binary_heap_into_iter_sorted [rustc issue #59278])
// Remove this and use the standard API once BinaryHeap::into_iter_sorted (ref2)
// is stabilized.
// ref1: https://github.com/rust-lang/rust/blob/2f688ac602d50129388bb2a5519942049096cbff/src/liballoc/collections/binary_heap.rs#L1149
// ref2: https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#into_iter_sorted.v
#[derive(Clone, Debug)]
pub struct IntoIterSorted<T> {
inner: BinaryHeap<T>,
}
impl<T: Ord> Iterator for IntoIterSorted<T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
self.inner.pop()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let exact = self.inner.len();
(exact, Some(exact))
}
}
impl Sysvar for RecentBlockhashes {
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
6008 // golden, update if MAX_ENTRIES changes
}
}
impl Deref for RecentBlockhashes {
type Target = Vec<Entry>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub fn create_account(lamports: u64) -> Account {
RecentBlockhashes::default().create_account(lamports)
}
pub fn update_account<'a, I>(account: &mut Account, recent_blockhash_iter: I) -> Option<()>
where
I: IntoIterator<Item = IterItem<'a>>,
{
let sorted = BinaryHeap::from_iter(recent_blockhash_iter);
let sorted_iter = IntoIterSorted { inner: sorted };
let recent_blockhash_iter = sorted_iter.take(MAX_ENTRIES);
let recent_blockhashes = RecentBlockhashes::from_iter(recent_blockhash_iter);
recent_blockhashes.to_account(account)
}
pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> Account
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account = create_account(lamports);
update_account(&mut account, recent_blockhash_iter).unwrap();
account
}
pub fn create_test_recent_blockhashes(start: usize) -> RecentBlockhashes {
let blocks: Vec<_> = (start..start + MAX_ENTRIES)
.map(|i| {
(
i as u64,
hash(&bincode::serialize(&i).unwrap()),
FeeCalculator::new(i as u64 * 100),
)
})
.collect();
let bhq: Vec<_> = blocks
.iter()
.map(|(i, hash, fee_calc)| IterItem(*i, hash, fee_calc))
.collect();
RecentBlockhashes::from_iter(bhq.into_iter())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{clock::MAX_PROCESSING_AGE, hash::HASH_BYTES};
use rand::seq::SliceRandom;
use rand::thread_rng;
#[test]
#[allow(clippy::assertions_on_constants)]
fn test_sysvar_can_hold_all_active_blockhashes() {
// Ensure we can still hold all of the active entries in `BlockhashQueue`
assert!(MAX_PROCESSING_AGE <= MAX_ENTRIES);
}
#[test]
fn test_size_of() {
let entry = Entry::new(&Hash::default(), &FeeCalculator::default());
assert_eq!(
bincode::serialized_size(&RecentBlockhashes(vec![entry; MAX_ENTRIES])).unwrap()
as usize,
RecentBlockhashes::size_of()
);
}
#[test]
fn test_create_account_empty() {
let account = create_account_with_data(42, vec![].into_iter());
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
assert_eq!(recent_blockhashes, RecentBlockhashes::default());
}
#[test]
fn test_create_account_full() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES].into_iter(),
);
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
}
#[test]
fn test_create_account_truncate() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES + 1].into_iter(),
);
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
}
#[test]
fn test_create_account_unsorted() {
let def_fees = FeeCalculator::default();
let mut unsorted_blocks: Vec<_> = (0..MAX_ENTRIES)
.map(|i| {
(i as u64, {
// create hash with visibly recognizable ordering
let mut h = [0; HASH_BYTES];
h[HASH_BYTES - 1] = i as u8;
Hash::new(&h)
})
})
.collect();
unsorted_blocks.shuffle(&mut thread_rng());
let account = create_account_with_data(
42,
unsorted_blocks
.iter()
.map(|(i, hash)| IterItem(*i, hash, &def_fees)),
);
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
let mut unsorted_recent_blockhashes: Vec<_> = unsorted_blocks
.iter()
.map(|(i, hash)| IterItem(*i, hash, &def_fees))
.collect();
unsorted_recent_blockhashes.sort();
unsorted_recent_blockhashes.reverse();
let expected_recent_blockhashes: Vec<_> = (unsorted_recent_blockhashes
.into_iter()
.map(|IterItem(_, b, f)| Entry::new(b, f)))
.collect();
assert_eq!(*recent_blockhashes, expected_recent_blockhashes);
}
}

View File

@@ -0,0 +1,42 @@
//! This account contains the current cluster rent
//!
pub use crate::rent::Rent;
use crate::{
account::{Account, KeyedAccount},
instruction::InstructionError,
sysvar::Sysvar,
};
crate::declare_sysvar_id!("SysvarRent111111111111111111111111111111111", Rent);
impl Sysvar for Rent {}
pub fn create_account(lamports: u64, rent: &Rent) -> Account {
rent.create_account(lamports)
}
pub fn verify_rent_exemption(
keyed_account: &KeyedAccount,
rent_sysvar_account: &KeyedAccount,
) -> Result<(), InstructionError> {
let rent = Rent::from_keyed_account(rent_sysvar_account)?;
if !rent.is_exempt(keyed_account.lamports()?, keyed_account.data_len()?) {
Err(InstructionError::InsufficientFunds)
} else {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rent_create_account() {
let lamports = 42;
let account = create_account(lamports, &Rent::default());
let rent = Rent::from_account(&account).unwrap();
assert_eq!(rent, Rent::default());
}
}

View File

@@ -0,0 +1,34 @@
//! DEPRECATED: This sysvar can be removed once the pico-inflation feature is enabled
//!
use crate::{account::Account, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarRewards111111111111111111111111111111", Rewards);
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Rewards {
pub validator_point_value: f64,
pub unused: f64,
}
impl Sysvar for Rewards {}
pub fn create_account(lamports: u64, validator_point_value: f64) -> Account {
Rewards {
validator_point_value,
unused: 0.0,
}
.create_account(lamports)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = create_account(1, 0.0);
let rewards = Rewards::from_account(&account).unwrap();
assert_eq!(rewards, Rewards::default());
}
}

View File

@@ -0,0 +1,45 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries the Bank's most recent bank hashes for some N parents
//!
pub use crate::slot_hashes::SlotHashes;
use crate::sysvar::Sysvar;
crate::declare_sysvar_id!("SysvarS1otHashes111111111111111111111111111", SlotHashes);
impl Sysvar for SlotHashes {
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
20_488 // golden, update if MAX_ENTRIES changes
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{clock::Slot, hash::Hash, slot_hashes::MAX_ENTRIES};
#[test]
fn test_size_of() {
assert_eq!(
SlotHashes::size_of(),
bincode::serialized_size(
&(0..MAX_ENTRIES)
.map(|slot| (slot as Slot, Hash::default()))
.collect::<SlotHashes>()
)
.unwrap() as usize
);
}
#[test]
fn test_create_account() {
let lamports = 42;
let account = SlotHashes::new(&[]).create_account(lamports);
assert_eq!(account.data.len(), SlotHashes::size_of());
let slot_hashes = SlotHashes::from_account(&account);
assert_eq!(slot_hashes, Some(SlotHashes::default()));
}
}

View File

@@ -0,0 +1,30 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries a bitvector of slots present over the past
//! epoch
//!
pub use crate::slot_history::SlotHistory;
use crate::sysvar::Sysvar;
crate::declare_sysvar_id!("SysvarS1otHistory11111111111111111111111111", SlotHistory);
impl Sysvar for SlotHistory {
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
131_097 // golden, update if MAX_ENTRIES changes
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_size_of() {
assert_eq!(
SlotHistory::size_of(),
bincode::serialized_size(&SlotHistory::default()).unwrap() as usize
);
}
}

View File

@@ -0,0 +1,82 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries history about stake activations and de-activations
//!
pub use crate::stake_history::StakeHistory;
use crate::{account::Account, sysvar::Sysvar};
crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
impl Sysvar for StakeHistory {
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
16392 // golden, update if MAX_ENTRIES changes
}
}
pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
stake_history.create_account(lamports)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::stake_history::*;
#[test]
fn test_size_of() {
let mut stake_history = StakeHistory::default();
for i in 0..MAX_ENTRIES as u64 {
stake_history.add(
i,
StakeHistoryEntry {
activating: i,
..StakeHistoryEntry::default()
},
);
}
assert_eq!(
bincode::serialized_size(&stake_history).unwrap() as usize,
StakeHistory::size_of()
);
}
#[test]
fn test_create_account() {
let lamports = 42;
let account = StakeHistory::default().create_account(lamports);
assert_eq!(account.data.len(), StakeHistory::size_of());
let stake_history = StakeHistory::from_account(&account);
assert_eq!(stake_history, Some(StakeHistory::default()));
let mut stake_history = stake_history.unwrap();
for i in 0..MAX_ENTRIES as u64 + 1 {
stake_history.add(
i,
StakeHistoryEntry {
activating: i,
..StakeHistoryEntry::default()
},
);
}
assert_eq!(stake_history.len(), MAX_ENTRIES);
assert_eq!(stake_history.iter().map(|entry| entry.0).min().unwrap(), 1);
assert_eq!(stake_history.get(&0), None);
assert_eq!(
stake_history.get(&1),
Some(&StakeHistoryEntry {
activating: 1,
..StakeHistoryEntry::default()
})
);
// verify the account can hold a full instance
assert_eq!(
StakeHistory::from_account(&stake_history.create_account(lamports)),
Some(stake_history)
);
}
}