sysvar trait (#6667)

* sysvar trait

* get the new guy in on it
This commit is contained in:
Rob Walker
2019-11-04 12:31:24 -08:00
committed by GitHub
parent b9b535c30f
commit efe260f12e
18 changed files with 183 additions and 303 deletions

View File

@@ -1,9 +1,6 @@
//! This account contains the current cluster rewards point values
//!
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::sysvar;
use bincode::serialized_size;
use crate::{account::Account, sysvar::Sysvar};
/// account pubkey
const ID: [u8; 32] = [
@@ -11,7 +8,7 @@ const ID: [u8; 32] = [
130, 184, 161, 97, 145, 87, 141, 128, 0, 0, 0,
];
crate::solana_sysvar_id!(ID, "SysvarRewards111111111111111111111111111111");
crate::solana_sysvar_id!(ID, "SysvarRewards111111111111111111111111111111", Rewards);
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
@@ -20,47 +17,18 @@ pub struct Rewards {
pub storage_point_value: f64,
}
impl Rewards {
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 size_of() -> usize {
serialized_size(&Self::default()).unwrap() as usize
}
}
impl Sysvar for Rewards {}
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()) {
return Err(InstructionError::InvalidArgument);
Rewards {
validator_point_value,
storage_point_value,
}
Rewards::from_account(account.account).ok_or(InstructionError::InvalidAccountData)
.create_account(lamports)
}
#[cfg(test)]