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:
Rob Walker
2019-07-12 16:38:15 -07:00
committed by GitHub
parent 7aecb87bce
commit d2b6c2e0ce
19 changed files with 229 additions and 271 deletions

70
sdk/src/sysvar/rewards.rs Normal file
View 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());
}
}