add epoch_schedule sysvar (#6256)

* add epoch_schedule sysvar

* book sheesh!
This commit is contained in:
Rob Walker
2019-10-08 22:34:26 -07:00
committed by GitHub
parent f2ee01ace3
commit 7cf90766a3
46 changed files with 572 additions and 427 deletions

View File

@@ -0,0 +1,57 @@
//! This account contains the current cluster rent
//!
use crate::{
account::{Account, KeyedAccount},
account_info::AccountInfo,
epoch_schedule::EpochSchedule,
instruction::InstructionError,
sysvar,
};
/// epoch_schedule account pubkey
const ID: [u8; 32] = [
6, 167, 213, 23, 24, 220, 63, 238, 2, 211, 228, 127, 1, 0, 248, 176, 84, 247, 148, 46, 96, 89,
30, 63, 80, 135, 25, 168, 5, 0, 0, 0,
];
crate::solana_name_id!(ID, "SysvarEpochSchedu1e111111111111111111111111");
impl EpochSchedule {
pub fn deserialize(account: &Account) -> Result<Self, bincode::Error> {
account.deserialize_data()
}
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to_account(&self, account: &mut Account) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn from_account_info(account: &AccountInfo) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to_account_info(&self, account: &mut AccountInfo) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn from_keyed_account(account: &KeyedAccount) -> Result<EpochSchedule, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
EpochSchedule::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
}
pub fn create_account(lamports: u64, epoch_schedule: &EpochSchedule) -> Account {
Account::new_data(lamports, epoch_schedule, &sysvar::id()).unwrap()
}
#[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

@@ -3,6 +3,7 @@
use crate::pubkey::Pubkey;
pub mod clock;
pub mod epoch_schedule;
pub mod fees;
pub mod rent;
pub mod rewards;

View File

@@ -1,9 +1,12 @@
//! This account contains the current cluster rent
//!
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::rent_calculator::RentCalculator;
use crate::sysvar;
use crate::{
account::{Account, KeyedAccount},
account_info::AccountInfo,
instruction::InstructionError,
rent_calculator::RentCalculator,
sysvar,
};
use bincode::serialized_size;
/// rent account pubkey
@@ -49,9 +52,6 @@ pub fn create_account(lamports: u64, rent_calculator: &RentCalculator) -> Accoun
.unwrap()
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rent, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);

View File

@@ -58,7 +58,6 @@ use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rewards, InstructionError> {
if !check_id(account.unsigned_key()) {
dbg!(account.unsigned_key());
return Err(InstructionError::InvalidArgument);
}
Rewards::from_account(account.account).ok_or(InstructionError::InvalidAccountData)

View File

@@ -2,14 +2,9 @@
//!
//! this account carries the Bank's most recent blockhashes for some N parents
//!
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::hash::Hash;
use crate::sysvar;
pub use crate::slot_hashes::{SlotHash, SlotHashes};
use crate::{account::Account, account_info::AccountInfo, sysvar};
use bincode::serialized_size;
use std::ops::Deref;
pub use crate::clock::Slot;
const ID: [u8; 32] = [
6, 167, 213, 23, 25, 47, 10, 175, 198, 242, 101, 227, 251, 119, 204, 122, 218, 130, 197, 41,
@@ -20,11 +15,6 @@ crate::solana_name_id!(ID, "SysvarS1otHashes111111111111111111111111111");
pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in
pub type SlotHash = (Slot, Hash);
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct SlotHashes(Vec<SlotHash>);
impl SlotHashes {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
@@ -39,30 +29,7 @@ impl SlotHashes {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&SlotHashes(vec![(0, Hash::default()); MAX_SLOT_HASHES])).unwrap() as usize
}
pub fn add(&mut self, slot: Slot, hash: Hash) {
match self.binary_search_by(|probe| slot.cmp(&probe.0)) {
Ok(index) => (self.0)[index] = (slot, hash),
Err(index) => (self.0).insert(index, (slot, hash)),
}
(self.0).truncate(MAX_SLOT_HASHES);
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get(&self, slot: &Slot) -> Option<&Hash> {
self.binary_search_by(|probe| slot.cmp(&probe.0))
.ok()
.map(|index| &self[index].1)
}
pub fn new(slot_hashes: &[SlotHash]) -> Self {
Self(slot_hashes.to_vec())
}
}
impl Deref for SlotHashes {
type Target = Vec<SlotHash>;
fn deref(&self) -> &Self::Target {
&self.0
serialized_size(&SlotHashes::new(&[SlotHash::default(); MAX_SLOT_HASHES])).unwrap() as usize
}
}
@@ -86,7 +53,6 @@ pub fn from_keyed_account(account: &KeyedAccount) -> Result<SlotHashes, Instruct
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::hash;
#[test]
fn test_create_account() {
@@ -94,18 +60,6 @@ mod tests {
let account = create_account(lamports, &[]);
assert_eq!(account.data.len(), SlotHashes::size_of());
let slot_hashes = SlotHashes::from_account(&account);
assert_eq!(slot_hashes, Some(SlotHashes(vec![])));
let mut slot_hashes = slot_hashes.unwrap();
for i in 0..MAX_SLOT_HASHES + 1 {
slot_hashes.add(
i as u64,
hash(&[(i >> 24) as u8, (i >> 16) as u8, (i >> 8) as u8, i as u8]),
);
}
for i in 0..MAX_SLOT_HASHES {
assert_eq!(slot_hashes[i].0, (MAX_SLOT_HASHES - i) as u64);
}
assert_eq!(slot_hashes.len(), MAX_SLOT_HASHES);
assert_eq!(slot_hashes, Some(SlotHashes::default()));
}
}

View File

@@ -24,6 +24,7 @@ pub struct StakeHistoryEntry {
pub deactivating: u64, // requested to be cooled down, not fully deactivated yet
}
#[repr(C)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);