Add support for SDK sysvar types (#5876)

This commit is contained in:
Jack May
2019-09-10 18:53:02 -07:00
committed by GitHub
parent 772ee4b29d
commit 1853771930
19 changed files with 124 additions and 75 deletions

View File

@ -60,7 +60,7 @@ pub fn sol_log_slice(slice: &[u8]) {
/// Prints the hexadecimal representation of the program's input parameters
///
/// @param ka - A pointer to an array of `SolKeyedAccounts` to print
/// @param ka - A pointer to an array of `AccountInfo` to print
/// @param data - A pointer to the instruction data to print
#[allow(dead_code)]
pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
@ -73,8 +73,8 @@ pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
account.key.log();
sol_log("- Lamports");
sol_log_64(0, 0, 0, 0, *account.lamports);
sol_log("- AccountData");
sol_log_slice(account.data);
sol_log("- Account data length");
sol_log_64(0, 0, 0, 0, account.data.len() as u64);
sol_log("- Owner");
account.owner.log();
}

View File

@ -1,6 +1,7 @@
//! This account contains the current cluster fees
//!
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::fee_calculator::FeeCalculator;
use crate::sysvar;
use bincode::serialized_size;
@ -20,13 +21,18 @@ pub struct Fees {
}
impl Fees {
pub fn from(account: &Account) -> Option<Self> {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
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(&Fees::default()).unwrap() as usize
}
@ -51,7 +57,7 @@ mod tests {
fn test_fees_create_account() {
let lamports = 42;
let account = create_account(lamports, &FeeCalculator::default());
let fees = Fees::from(&account).unwrap();
let fees = Fees::from_account(&account).unwrap();
assert_eq!(fees.fee_calculator, FeeCalculator::default());
}
}

View File

@ -1,6 +1,7 @@
//! 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;
@ -20,10 +21,16 @@ pub struct Rewards {
}
impl Rewards {
pub fn from(account: &Account) -> Option<Self> {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
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 {
@ -54,7 +61,7 @@ pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rewards, Instruction
dbg!(account.unsigned_key());
return Err(InstructionError::InvalidArgument);
}
Rewards::from(account.account).ok_or(InstructionError::InvalidAccountData)
Rewards::from_account(account.account).ok_or(InstructionError::InvalidAccountData)
}
#[cfg(test)]
@ -64,7 +71,7 @@ mod tests {
#[test]
fn test_create_account() {
let account = create_account(1, 0.0, 0.0);
let rewards = Rewards::from(&account).unwrap();
let rewards = Rewards::from_account(&account).unwrap();
assert_eq!(rewards, Rewards::default());
}
}

View File

@ -3,6 +3,7 @@
//! 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;
use bincode::serialized_size;
@ -25,13 +26,18 @@ pub type SlotHash = (Slot, Hash);
pub struct SlotHashes(Vec<SlotHash>);
impl SlotHashes {
pub fn from(account: &Account) -> Option<Self> {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
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(&SlotHashes(vec![(0, Hash::default()); MAX_SLOT_HASHES])).unwrap() as usize
}
@ -62,7 +68,9 @@ impl Deref for SlotHashes {
pub fn create_account(lamports: u64, slot_hashes: &[SlotHash]) -> Account {
let mut account = Account::new(lamports, SlotHashes::size_of(), &sysvar::id());
SlotHashes::new(slot_hashes).to(&mut account).unwrap();
SlotHashes::new(slot_hashes)
.to_account(&mut account)
.unwrap();
account
}
@ -72,7 +80,7 @@ pub fn from_keyed_account(account: &KeyedAccount) -> Result<SlotHashes, Instruct
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
SlotHashes::from(account.account).ok_or(InstructionError::InvalidArgument)
SlotHashes::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
@ -85,7 +93,7 @@ mod tests {
let lamports = 42;
let account = create_account(lamports, &[]);
assert_eq!(account.data.len(), SlotHashes::size_of());
let slot_hashes = SlotHashes::from(&account);
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 {

View File

@ -2,6 +2,7 @@
//!
//! this account carries history about stake activations and de-activations
//!
use crate::account_info::AccountInfo;
pub use crate::clock::Epoch;
use crate::{account::Account, sysvar};
use bincode::serialized_size;
@ -27,13 +28,18 @@ pub struct StakeHistoryEntry {
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
impl StakeHistory {
pub fn from(account: &Account) -> Option<Self> {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to(&self, account: &mut Account) -> Option<()> {
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(&StakeHistory(vec![
(0, StakeHistoryEntry::default());
@ -67,7 +73,7 @@ impl Deref for StakeHistory {
pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
let mut account = Account::new(lamports, StakeHistory::size_of(), &sysvar::id());
stake_history.to(&mut account).unwrap();
stake_history.to_account(&mut account).unwrap();
account
}
@ -77,7 +83,7 @@ pub fn from_keyed_account(account: &KeyedAccount) -> Result<StakeHistory, Instru
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
StakeHistory::from(account.account).ok_or(InstructionError::InvalidArgument)
StakeHistory::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
@ -90,7 +96,7 @@ mod tests {
let account = create_account(lamports, &StakeHistory::default());
assert_eq!(account.data.len(), StakeHistory::size_of());
let stake_history = StakeHistory::from(&account);
let stake_history = StakeHistory::from_account(&account);
assert_eq!(stake_history, Some(StakeHistory::default()));
let mut stake_history = stake_history.unwrap();
@ -115,7 +121,7 @@ mod tests {
);
// verify the account can hold a full instance
assert_eq!(
StakeHistory::from(&create_account(lamports, &stake_history)),
StakeHistory::from_account(&create_account(lamports, &stake_history)),
Some(stake_history)
);
}