* move Account to solana-sdk (#13198)
(cherry picked from commit c458d4b213
)
# Conflicts:
# programs/bpf/benches/bpf_loader.rs
* resolve conflicts
Co-authored-by: Jack May <jack@solana.com>
This commit is contained in:
@@ -1,24 +1,8 @@
|
||||
//! This account contains the current cluster rent
|
||||
//!
|
||||
pub use crate::epoch_schedule::EpochSchedule;
|
||||
use crate::{account::Account, sysvar::Sysvar};
|
||||
use crate::sysvar::Sysvar;
|
||||
|
||||
crate::declare_sysvar_id!("SysvarEpochSchedu1e111111111111111111111111", EpochSchedule);
|
||||
|
||||
impl Sysvar for EpochSchedule {}
|
||||
|
||||
pub fn create_account(lamports: u64, epoch_schedule: &EpochSchedule) -> Account {
|
||||
epoch_schedule.create_account(lamports)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let account = create_account(42, &EpochSchedule::default());
|
||||
let epoch_schedule = EpochSchedule::from_account(&account).unwrap();
|
||||
assert_eq!(epoch_schedule, EpochSchedule::default());
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
//! This account contains the current cluster fees
|
||||
//!
|
||||
use crate::{account::Account, fee_calculator::FeeCalculator, sysvar::Sysvar};
|
||||
use crate::{fee_calculator::FeeCalculator, sysvar::Sysvar};
|
||||
|
||||
crate::declare_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees);
|
||||
|
||||
@@ -9,25 +9,12 @@ crate::declare_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees);
|
||||
pub struct Fees {
|
||||
pub fee_calculator: FeeCalculator,
|
||||
}
|
||||
impl Fees {
|
||||
pub fn new(fee_calculator: &FeeCalculator) -> Self {
|
||||
Self {
|
||||
fee_calculator: fee_calculator.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sysvar for Fees {}
|
||||
|
||||
pub fn create_account(lamports: u64, fee_calculator: &FeeCalculator) -> Account {
|
||||
Fees {
|
||||
fee_calculator: fee_calculator.clone(),
|
||||
}
|
||||
.create_account(lamports)
|
||||
}
|
||||
|
||||
#[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(&account).unwrap();
|
||||
assert_eq!(fees.fee_calculator, FeeCalculator::default());
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,6 @@
|
||||
//! named accounts for synthesized data accounts for bank state, etc.
|
||||
//!
|
||||
use crate::{
|
||||
account::Account, account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey,
|
||||
};
|
||||
use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
|
||||
|
||||
pub mod clock;
|
||||
pub mod epoch_schedule;
|
||||
@@ -63,12 +61,6 @@ pub trait Sysvar:
|
||||
fn size_of() -> usize {
|
||||
bincode::serialized_size(&Self::default()).unwrap() as usize
|
||||
}
|
||||
fn from_account(account: &Account) -> Option<Self> {
|
||||
bincode::deserialize(&account.data).ok()
|
||||
}
|
||||
fn to_account(&self, account: &mut Account) -> Option<()> {
|
||||
bincode::serialize_into(&mut account.data[..], self).ok()
|
||||
}
|
||||
fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
|
||||
if !Self::check_id(account_info.unsigned_key()) {
|
||||
return Err(ProgramError::InvalidArgument);
|
||||
@@ -78,12 +70,6 @@ pub trait Sysvar:
|
||||
fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
|
||||
bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
|
||||
}
|
||||
fn create_account(&self, lamports: u64) -> Account {
|
||||
let data_len = Self::size_of().max(bincode::serialized_size(self).unwrap() as usize);
|
||||
let mut account = Account::new(lamports, data_len, &id());
|
||||
self.to_account(&mut account).unwrap();
|
||||
account
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@@ -1,5 +1,4 @@
|
||||
use crate::{
|
||||
account::Account,
|
||||
declare_sysvar_id,
|
||||
fee_calculator::FeeCalculator,
|
||||
hash::{hash, Hash},
|
||||
@@ -88,6 +87,11 @@ impl<'a> FromIterator<IterItem<'a>> for RecentBlockhashes {
|
||||
pub struct IntoIterSorted<T> {
|
||||
inner: BinaryHeap<T>,
|
||||
}
|
||||
impl<T> IntoIterSorted<T> {
|
||||
pub fn new(binary_heap: BinaryHeap<T>) -> Self {
|
||||
Self { inner: binary_heap }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord> Iterator for IntoIterSorted<T> {
|
||||
type Item = T;
|
||||
@@ -118,30 +122,6 @@ impl Deref for RecentBlockhashes {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64) -> Account {
|
||||
RecentBlockhashes::default().create_account(lamports)
|
||||
}
|
||||
|
||||
pub fn update_account<'a, I>(account: &mut Account, recent_blockhash_iter: I) -> Option<()>
|
||||
where
|
||||
I: IntoIterator<Item = IterItem<'a>>,
|
||||
{
|
||||
let sorted = BinaryHeap::from_iter(recent_blockhash_iter);
|
||||
let sorted_iter = IntoIterSorted { inner: sorted };
|
||||
let recent_blockhash_iter = sorted_iter.take(MAX_ENTRIES);
|
||||
let recent_blockhashes = RecentBlockhashes::from_iter(recent_blockhash_iter);
|
||||
recent_blockhashes.to_account(account)
|
||||
}
|
||||
|
||||
pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> Account
|
||||
where
|
||||
I: IntoIterator<Item = IterItem<'a>>,
|
||||
{
|
||||
let mut account = create_account(lamports);
|
||||
update_account(&mut account, recent_blockhash_iter).unwrap();
|
||||
account
|
||||
}
|
||||
|
||||
pub fn create_test_recent_blockhashes(start: usize) -> RecentBlockhashes {
|
||||
let blocks: Vec<_> = (start..start + MAX_ENTRIES)
|
||||
.map(|i| {
|
||||
@@ -162,9 +142,7 @@ pub fn create_test_recent_blockhashes(start: usize) -> RecentBlockhashes {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{clock::MAX_PROCESSING_AGE, hash::HASH_BYTES};
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use crate::clock::MAX_PROCESSING_AGE;
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::assertions_on_constants)]
|
||||
@@ -182,72 +160,4 @@ mod tests {
|
||||
RecentBlockhashes::size_of()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account_empty() {
|
||||
let account = create_account_with_data(42, vec![].into_iter());
|
||||
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
|
||||
assert_eq!(recent_blockhashes, RecentBlockhashes::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account_full() {
|
||||
let def_hash = Hash::default();
|
||||
let def_fees = FeeCalculator::default();
|
||||
let account = create_account_with_data(
|
||||
42,
|
||||
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES].into_iter(),
|
||||
);
|
||||
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
|
||||
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account_truncate() {
|
||||
let def_hash = Hash::default();
|
||||
let def_fees = FeeCalculator::default();
|
||||
let account = create_account_with_data(
|
||||
42,
|
||||
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES + 1].into_iter(),
|
||||
);
|
||||
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
|
||||
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account_unsorted() {
|
||||
let def_fees = FeeCalculator::default();
|
||||
let mut unsorted_blocks: Vec<_> = (0..MAX_ENTRIES)
|
||||
.map(|i| {
|
||||
(i as u64, {
|
||||
// create hash with visibly recognizable ordering
|
||||
let mut h = [0; HASH_BYTES];
|
||||
h[HASH_BYTES - 1] = i as u8;
|
||||
Hash::new(&h)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
unsorted_blocks.shuffle(&mut thread_rng());
|
||||
|
||||
let account = create_account_with_data(
|
||||
42,
|
||||
unsorted_blocks
|
||||
.iter()
|
||||
.map(|(i, hash)| IterItem(*i, hash, &def_fees)),
|
||||
);
|
||||
let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap();
|
||||
|
||||
let mut unsorted_recent_blockhashes: Vec<_> = unsorted_blocks
|
||||
.iter()
|
||||
.map(|(i, hash)| IterItem(*i, hash, &def_fees))
|
||||
.collect();
|
||||
unsorted_recent_blockhashes.sort();
|
||||
unsorted_recent_blockhashes.reverse();
|
||||
let expected_recent_blockhashes: Vec<_> = (unsorted_recent_blockhashes
|
||||
.into_iter()
|
||||
.map(|IterItem(_, b, f)| Entry::new(b, f)))
|
||||
.collect();
|
||||
|
||||
assert_eq!(*recent_blockhashes, expected_recent_blockhashes);
|
||||
}
|
||||
}
|
||||
|
@@ -2,25 +2,8 @@
|
||||
//!
|
||||
pub use crate::rent::Rent;
|
||||
|
||||
use crate::{account::Account, sysvar::Sysvar};
|
||||
use crate::sysvar::Sysvar;
|
||||
|
||||
crate::declare_sysvar_id!("SysvarRent111111111111111111111111111111111", Rent);
|
||||
|
||||
impl Sysvar for Rent {}
|
||||
|
||||
pub fn create_account(lamports: u64, rent: &Rent) -> Account {
|
||||
rent.create_account(lamports)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_rent_create_account() {
|
||||
let lamports = 42;
|
||||
let account = create_account(lamports, &Rent::default());
|
||||
let rent = Rent::from_account(&account).unwrap();
|
||||
assert_eq!(rent, Rent::default());
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
//! DEPRECATED: This sysvar can be removed once the pico-inflation feature is enabled
|
||||
//!
|
||||
use crate::{account::Account, sysvar::Sysvar};
|
||||
use crate::sysvar::Sysvar;
|
||||
|
||||
crate::declare_sysvar_id!("SysvarRewards111111111111111111111111111111", Rewards);
|
||||
|
||||
@@ -10,25 +10,13 @@ pub struct Rewards {
|
||||
pub validator_point_value: f64,
|
||||
pub unused: f64,
|
||||
}
|
||||
impl Rewards {
|
||||
pub fn new(validator_point_value: f64) -> Self {
|
||||
Self {
|
||||
validator_point_value,
|
||||
unused: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sysvar for Rewards {}
|
||||
|
||||
pub fn create_account(lamports: u64, validator_point_value: f64) -> Account {
|
||||
Rewards {
|
||||
validator_point_value,
|
||||
unused: 0.0,
|
||||
}
|
||||
.create_account(lamports)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let account = create_account(1, 0.0);
|
||||
let rewards = Rewards::from_account(&account).unwrap();
|
||||
assert_eq!(rewards, Rewards::default());
|
||||
}
|
||||
}
|
||||
|
@@ -33,13 +33,4 @@ mod tests {
|
||||
.unwrap() as usize
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let lamports = 42;
|
||||
let account = SlotHashes::new(&[]).create_account(lamports);
|
||||
assert_eq!(account.data.len(), SlotHashes::size_of());
|
||||
let slot_hashes = SlotHashes::from_account(&account);
|
||||
assert_eq!(slot_hashes, Some(SlotHashes::default()));
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
//!
|
||||
pub use crate::stake_history::StakeHistory;
|
||||
|
||||
use crate::{account::Account, sysvar::Sysvar};
|
||||
use crate::sysvar::Sysvar;
|
||||
|
||||
crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
|
||||
|
||||
@@ -16,10 +16,6 @@ impl Sysvar for StakeHistory {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
|
||||
stake_history.create_account(lamports)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -46,14 +42,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_create_account() {
|
||||
let lamports = 42;
|
||||
let account = StakeHistory::default().create_account(lamports);
|
||||
assert_eq!(account.data.len(), StakeHistory::size_of());
|
||||
|
||||
let stake_history = StakeHistory::from_account(&account);
|
||||
assert_eq!(stake_history, Some(StakeHistory::default()));
|
||||
|
||||
let mut stake_history = stake_history.unwrap();
|
||||
let mut stake_history = StakeHistory::default();
|
||||
for i in 0..MAX_ENTRIES as u64 + 1 {
|
||||
stake_history.add(
|
||||
i,
|
||||
@@ -73,10 +62,5 @@ mod tests {
|
||||
..StakeHistoryEntry::default()
|
||||
})
|
||||
);
|
||||
// verify the account can hold a full instance
|
||||
assert_eq!(
|
||||
StakeHistory::from_account(&stake_history.create_account(lamports)),
|
||||
Some(stake_history)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user