Add sysvar support (#5838)

This commit is contained in:
Jack May
2019-09-09 10:55:35 -07:00
committed by GitHub
parent ee0c570d54
commit a317e9513f
17 changed files with 156 additions and 110 deletions

View File

@ -1,5 +1,6 @@
pub mod clock;
pub mod pubkey;
pub mod sysvar;
// On-chain program modules
#[cfg(feature = "program")]
@ -57,8 +58,6 @@ pub mod system_program;
#[cfg(feature = "kitchen_sink")]
pub mod system_transaction;
#[cfg(feature = "kitchen_sink")]
pub mod sysvar;
#[cfg(feature = "kitchen_sink")]
pub mod timing;
#[cfg(feature = "kitchen_sink")]
pub mod transaction;

View File

@ -1,10 +1,7 @@
//! This account contains the clock slot, epoch, and stakers_epoch
//!
use crate::account::Account;
use crate::sysvar;
use bincode::serialized_size;
pub use crate::clock::{Epoch, Slot};
pub use crate::clock::{Epoch, Segment, Slot};
const ID: [u8; 32] = [
6, 167, 213, 23, 24, 199, 116, 201, 40, 86, 99, 152, 105, 29, 94, 182, 139, 94, 184, 163, 155,
@ -21,59 +18,3 @@ pub struct Clock {
pub epoch: Epoch,
pub stakers_epoch: Epoch,
}
impl Clock {
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,
slot: Slot,
segment: Segment,
epoch: Epoch,
stakers_epoch: Epoch,
) -> Account {
Account::new_data(
lamports,
&Clock {
slot,
segment,
epoch,
stakers_epoch,
},
&sysvar::id(),
)
.unwrap()
}
use crate::account::KeyedAccount;
use crate::clock::Segment;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Clock, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Clock::from(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = create_account(1, 0, 0, 0, 0);
let clock = Clock::from(&account).unwrap();
assert_eq!(clock, Clock::default());
}
}

View File

@ -0,0 +1,64 @@
//! Serialize clock support for Account
//!
use crate::account::Account;
use crate::sysvar;
use crate::sysvar::clock::{check_id, Clock};
use bincode::serialized_size;
pub use crate::clock::{Epoch, Slot};
impl Clock {
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 new(
lamports: u64,
slot: Slot,
segment: Segment,
epoch: Epoch,
stakers_epoch: Epoch,
) -> Account {
Account::new_data(
lamports,
&Clock {
slot,
segment,
epoch,
stakers_epoch,
},
&sysvar::id(),
)
.unwrap()
}
use crate::account::KeyedAccount;
use crate::clock::Segment;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Clock, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
Clock::from(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_account() {
let account = new(1, 0, 0, 0, 0);
let clock = Clock::from(&account).unwrap();
assert_eq!(clock, Clock::default());
}
}

View File

@ -0,0 +1,18 @@
//! Serialize clock support for AccountInfo
//!
use crate::account_info::AccountInfo;
use crate::sysvar::clock::Clock;
use bincode::serialized_size;
impl Clock {
pub fn from(account: &AccountInfo) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut AccountInfo) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
}

View File

@ -1,13 +1,25 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
#[cfg(feature = "kitchen_sink")]
use crate::pubkey::Pubkey;
pub mod clock;
#[cfg(feature = "program")]
pub mod clock_account_info;
#[cfg(feature = "kitchen_sink")]
pub mod clock_account;
#[cfg(feature = "kitchen_sink")]
pub mod fees;
#[cfg(feature = "kitchen_sink")]
pub mod rewards;
#[cfg(feature = "kitchen_sink")]
pub mod slot_hashes;
#[cfg(feature = "kitchen_sink")]
pub mod stake_history;
#[cfg(feature = "kitchen_sink")]
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)
}