syscall work, rename syscall to sysvar, rename current to clock (#5074)
* syscall work, rename syscall to sysvar, rename current to clock * missed one * nit
This commit is contained in:
79
sdk/src/sysvar/clock.rs
Normal file
79
sdk/src/sysvar/clock.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
//! This account contains the clock slot, epoch, and stakers_epoch
|
||||
//!
|
||||
use crate::account::Account;
|
||||
use crate::sysvar;
|
||||
use bincode::serialized_size;
|
||||
|
||||
pub use crate::timing::{Epoch, 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,
|
||||
75, 109, 92, 115, 85, 91, 33, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
crate::solana_name_id!(ID, "SysvarC1ock11111111111111111111111111111111");
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
|
||||
pub struct Clock {
|
||||
pub slot: Slot,
|
||||
pub segment: Segment,
|
||||
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::instruction::InstructionError;
|
||||
use crate::timing::Segment;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
57
sdk/src/sysvar/fees.rs
Normal file
57
sdk/src/sysvar/fees.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//! This account contains the current cluster fees
|
||||
//!
|
||||
use crate::account::Account;
|
||||
use crate::fee_calculator::FeeCalculator;
|
||||
use crate::sysvar;
|
||||
use bincode::serialized_size;
|
||||
|
||||
/// fees account pubkey
|
||||
const ID: [u8; 32] = [
|
||||
6, 167, 213, 23, 24, 226, 90, 141, 131, 80, 60, 37, 26, 122, 240, 113, 38, 253, 114, 0, 223,
|
||||
111, 196, 237, 82, 106, 156, 144, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
crate::solana_name_id!(ID, "SysvarFees111111111111111111111111111111111");
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
pub struct Fees {
|
||||
pub fee_calculator: FeeCalculator,
|
||||
}
|
||||
|
||||
impl Fees {
|
||||
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(&Fees::default()).unwrap() as usize
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64, fee_calculator: &FeeCalculator) -> Account {
|
||||
Account::new_data(
|
||||
lamports,
|
||||
&Fees {
|
||||
fee_calculator: fee_calculator.clone(),
|
||||
},
|
||||
&sysvar::id(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[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).unwrap();
|
||||
assert_eq!(fees.fee_calculator, FeeCalculator::default());
|
||||
}
|
||||
}
|
21
sdk/src/sysvar/mod.rs
Normal file
21
sdk/src/sysvar/mod.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! named accounts for synthesized data accounts for bank state, etc.
|
||||
//!
|
||||
use crate::pubkey::Pubkey;
|
||||
|
||||
pub mod clock;
|
||||
pub mod fees;
|
||||
pub mod rewards;
|
||||
pub mod slot_hashes;
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/// "Sysvar1111111111111111111111111111111111111"
|
||||
/// owner pubkey for sysvar accounts
|
||||
const ID: [u8; 32] = [
|
||||
6, 167, 213, 23, 24, 117, 247, 41, 199, 61, 147, 64, 143, 33, 97, 32, 6, 126, 216, 140, 118,
|
||||
224, 140, 40, 127, 193, 148, 96, 0, 0, 0, 0,
|
||||
];
|
||||
|
||||
crate::solana_name_id!(ID, "Sysvar1111111111111111111111111111111111111");
|
70
sdk/src/sysvar/rewards.rs
Normal file
70
sdk/src/sysvar/rewards.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
//! This account contains the current cluster rewards point values
|
||||
//!
|
||||
use crate::account::Account;
|
||||
use crate::sysvar;
|
||||
use bincode::serialized_size;
|
||||
|
||||
/// account pubkey
|
||||
const ID: [u8; 32] = [
|
||||
6, 167, 213, 23, 25, 44, 97, 55, 206, 224, 146, 217, 182, 146, 62, 225, 204, 214, 25, 3, 250,
|
||||
130, 184, 161, 97, 145, 87, 141, 128, 0, 0, 0,
|
||||
];
|
||||
|
||||
crate::solana_name_id!(ID, "SysvarRewards111111111111111111111111111111");
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
|
||||
pub struct Rewards {
|
||||
pub validator_point_value: f64,
|
||||
pub storage_point_value: f64,
|
||||
}
|
||||
|
||||
impl Rewards {
|
||||
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,
|
||||
validator_point_value: f64,
|
||||
storage_point_value: f64,
|
||||
) -> Account {
|
||||
Account::new_data(
|
||||
lamports,
|
||||
&Rewards {
|
||||
validator_point_value,
|
||||
storage_point_value,
|
||||
},
|
||||
&sysvar::id(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
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).ok_or(InstructionError::InvalidAccountData)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let account = create_account(1, 0.0, 0.0);
|
||||
let rewards = Rewards::from(&account).unwrap();
|
||||
assert_eq!(rewards, Rewards::default());
|
||||
}
|
||||
}
|
100
sdk/src/sysvar/slot_hashes.rs
Normal file
100
sdk/src/sysvar/slot_hashes.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
//! named accounts for synthesized data accounts for bank state, etc.
|
||||
//!
|
||||
//! this account carries the Bank's most recent blockhashes for some N parents
|
||||
//!
|
||||
use crate::account::Account;
|
||||
use crate::hash::Hash;
|
||||
use crate::sysvar;
|
||||
use bincode::serialized_size;
|
||||
use std::ops::Deref;
|
||||
|
||||
pub use crate::timing::Slot;
|
||||
|
||||
const ID: [u8; 32] = [
|
||||
6, 167, 213, 23, 25, 44, 97, 55, 206, 224, 146, 217, 182, 146, 62, 225, 204, 214, 25, 3, 250,
|
||||
130, 184, 161, 97, 145, 87, 141, 128, 0, 0, 0,
|
||||
];
|
||||
|
||||
crate::solana_name_id!(ID, "SysvarRewards111111111111111111111111111111");
|
||||
|
||||
pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
pub struct SlotHashes {
|
||||
// non-pub to keep control of size
|
||||
inner: Vec<(Slot, Hash)>,
|
||||
}
|
||||
|
||||
impl SlotHashes {
|
||||
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(&SlotHashes {
|
||||
inner: vec![(0, Hash::default()); MAX_SLOT_HASHES],
|
||||
})
|
||||
.unwrap() as usize
|
||||
}
|
||||
pub fn add(&mut self, slot: Slot, hash: Hash) {
|
||||
self.inner.insert(0, (slot, hash));
|
||||
self.inner.truncate(MAX_SLOT_HASHES);
|
||||
}
|
||||
pub fn new(slot_hashes: &[(Slot, Hash)]) -> Self {
|
||||
Self {
|
||||
inner: slot_hashes.to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SlotHashes {
|
||||
type Target = Vec<(u64, Hash)>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64, slot_hashes: &[(Slot, Hash)]) -> Account {
|
||||
let mut account = Account::new(lamports, SlotHashes::size_of(), &sysvar::id());
|
||||
SlotHashes::new(slot_hashes).to(&mut account).unwrap();
|
||||
account
|
||||
}
|
||||
|
||||
use crate::account::KeyedAccount;
|
||||
use crate::instruction::InstructionError;
|
||||
pub fn from_keyed_account(account: &KeyedAccount) -> Result<SlotHashes, InstructionError> {
|
||||
if !check_id(account.unsigned_key()) {
|
||||
return Err(InstructionError::InvalidArgument);
|
||||
}
|
||||
SlotHashes::from(account.account).ok_or(InstructionError::InvalidArgument)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::hash::hash;
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let lamports = 42;
|
||||
let account = create_account(lamports, &[]);
|
||||
assert_eq!(account.data.len(), SlotHashes::size_of());
|
||||
let slot_hashes = SlotHashes::from(&account);
|
||||
assert_eq!(slot_hashes, Some(SlotHashes { inner: 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user