Simplify account.rent_epoch handling for sysvar rent (#16049)

* Add some code for special local testing

* Add comment to store_account_and_update_capitalization

* Simplify account.rent_epoch handling for sysvar rent

* Introduce *_for_test functions

* Add deprecation messages to existing api
This commit is contained in:
Ryo Onodera
2021-03-25 15:23:20 +09:00
committed by GitHub
parent 7f0ac6a67c
commit 6d5c6c17c5
16 changed files with 206 additions and 104 deletions

View File

@ -2,7 +2,7 @@
extern crate test;
use solana_sdk::{
account::{create_account, from_account},
account::{create_account_for_test, from_account},
hash::Hash,
slot_hashes::{Slot, SlotHashes, MAX_ENTRIES},
};
@ -15,7 +15,7 @@ fn bench_to_from_account(b: &mut Bencher) {
slot_hashes.add(i as Slot, Hash::default());
}
b.iter(|| {
let account = create_account(&slot_hashes, 0);
let account = create_account_for_test(&slot_hashes);
slot_hashes = from_account::<SlotHashes, _>(&account).unwrap();
});
}

View File

@ -2,7 +2,7 @@
extern crate test;
use solana_sdk::{
account::{create_account, from_account},
account::{create_account_for_test, from_account},
slot_history::SlotHistory,
};
use test::Bencher;
@ -12,7 +12,7 @@ fn bench_to_from_account(b: &mut Bencher) {
let mut slot_history = SlotHistory::default();
b.iter(|| {
let account = create_account(&slot_history, 0);
let account = create_account_for_test(&slot_history);
slot_history = from_account::<SlotHistory, _>(&account).unwrap();
});
}

View File

@ -63,6 +63,8 @@ pub type Slot = u64;
pub type Epoch = u64;
pub const GENESIS_EPOCH: Epoch = 0;
// must be sync with Account::rent_epoch::default()
pub const INITIAL_RENT_EPOCH: Epoch = 0;
/// SlotIndex is an index to the slots of a epoch
pub type SlotIndex = u64;

View File

@ -1,4 +1,7 @@
use crate::{clock::Epoch, pubkey::Pubkey};
use crate::{
clock::{Epoch, INITIAL_RENT_EPOCH},
pubkey::Pubkey,
};
use solana_program::{account_info::AccountInfo, sysvar::Sysvar};
use std::{cell::Ref, cell::RefCell, cmp, fmt, rc::Rc, sync::Arc};
@ -449,17 +452,57 @@ impl AccountSharedData {
}
}
pub type InheritableAccountFields = (u64, Epoch);
pub const DUMMY_INHERITABLE_ACCOUNT_FIELDS: InheritableAccountFields = (1, INITIAL_RENT_EPOCH);
/// Create an `Account` from a `Sysvar`.
#[deprecated(
since = "1.5.17",
note = "Please use `create_account_for_test` instead"
)]
pub fn create_account<S: Sysvar>(sysvar: &S, lamports: u64) -> Account {
create_account_with_fields(sysvar, (lamports, INITIAL_RENT_EPOCH))
}
pub fn create_account_with_fields<S: Sysvar>(
sysvar: &S,
(lamports, rent_epoch): InheritableAccountFields,
) -> Account {
let data_len = S::size_of().max(bincode::serialized_size(sysvar).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &solana_program::sysvar::id());
to_account::<S, Account>(sysvar, &mut account).unwrap();
account.rent_epoch = rent_epoch;
account
}
pub fn create_account_for_test<S: Sysvar>(sysvar: &S) -> Account {
create_account_with_fields(sysvar, DUMMY_INHERITABLE_ACCOUNT_FIELDS)
}
/// Create an `Account` from a `Sysvar`.
#[deprecated(
since = "1.5.17",
note = "Please use `create_account_shared_data_for_test` instead"
)]
pub fn create_account_shared_data<S: Sysvar>(sysvar: &S, lamports: u64) -> AccountSharedData {
AccountSharedData::from(create_account(sysvar, lamports))
AccountSharedData::from(create_account_with_fields(
sysvar,
(lamports, INITIAL_RENT_EPOCH),
))
}
pub fn create_account_shared_data_with_fields<S: Sysvar>(
sysvar: &S,
fields: InheritableAccountFields,
) -> AccountSharedData {
AccountSharedData::from(create_account_with_fields(sysvar, fields))
}
pub fn create_account_shared_data_for_test<S: Sysvar>(sysvar: &S) -> AccountSharedData {
AccountSharedData::from(create_account_with_fields(
sysvar,
DUMMY_INHERITABLE_ACCOUNT_FIELDS,
))
}
/// Create a `Sysvar` from an `Account`'s data.

View File

@ -225,7 +225,7 @@ pub fn from_keyed_account<S: Sysvar>(
mod tests {
use super::*;
use crate::{
account::{create_account, to_account},
account::{create_account_for_test, to_account},
pubkey::Pubkey,
};
use std::cell::RefCell;
@ -249,7 +249,7 @@ mod tests {
let key = crate::keyed_account::tests::id();
let wrong_key = Pubkey::new_unique();
let account = create_account(&test_sysvar, 42);
let account = create_account_for_test(&test_sysvar);
let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());

View File

@ -1,14 +1,32 @@
use crate::account::{Account, AccountSharedData};
use crate::account::{
Account, AccountSharedData, InheritableAccountFields, DUMMY_INHERITABLE_ACCOUNT_FIELDS,
};
use crate::clock::INITIAL_RENT_EPOCH;
crate::declare_id!("NativeLoader1111111111111111111111111111111");
/// Create an executable account with the given shared object name.
#[deprecated(
since = "1.5.17",
note = "Please use `create_loadable_account_for_test` instead"
)]
pub fn create_loadable_account(name: &str, lamports: u64) -> AccountSharedData {
create_loadable_account_with_fields(name, (lamports, INITIAL_RENT_EPOCH))
}
pub fn create_loadable_account_with_fields(
name: &str,
(lamports, rent_epoch): InheritableAccountFields,
) -> AccountSharedData {
AccountSharedData::from(Account {
lamports,
owner: id(),
data: name.as_bytes().to_vec(),
executable: true,
rent_epoch: 0,
rent_epoch,
})
}
pub fn create_loadable_account_for_test(name: &str) -> AccountSharedData {
create_loadable_account_with_fields(name, DUMMY_INHERITABLE_ACCOUNT_FIELDS)
}

View File

@ -1,4 +1,8 @@
use crate::account::{create_account_shared_data, to_account, AccountSharedData};
use crate::account::{
create_account_shared_data_with_fields, to_account, AccountSharedData,
InheritableAccountFields, DUMMY_INHERITABLE_ACCOUNT_FIELDS,
};
use crate::clock::INITIAL_RENT_EPOCH;
use solana_program::sysvar::recent_blockhashes::{
IntoIterSorted, IterItem, RecentBlockhashes, MAX_ENTRIES,
};
@ -18,16 +22,39 @@ where
to_account(&recent_blockhashes, account)
}
#[deprecated(
since = "1.5.17",
note = "Please use `create_account_with_data_for_test` instead"
)]
pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> AccountSharedData
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account =
create_account_shared_data::<RecentBlockhashes>(&RecentBlockhashes::default(), lamports);
create_account_with_data_and_fields(recent_blockhash_iter, (lamports, INITIAL_RENT_EPOCH))
}
pub fn create_account_with_data_and_fields<'a, I>(
recent_blockhash_iter: I,
fields: InheritableAccountFields,
) -> AccountSharedData
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account = create_account_shared_data_with_fields::<RecentBlockhashes>(
&RecentBlockhashes::default(),
fields,
);
update_account(&mut account, recent_blockhash_iter).unwrap();
account
}
pub fn create_account_with_data_for_test<'a, I>(recent_blockhash_iter: I) -> AccountSharedData
where
I: IntoIterator<Item = IterItem<'a>>,
{
create_account_with_data_and_fields(recent_blockhash_iter, DUMMY_INHERITABLE_ACCOUNT_FIELDS)
}
#[cfg(test)]
mod tests {
use super::*;
@ -41,7 +68,7 @@ mod tests {
#[test]
fn test_create_account_empty() {
let account = create_account_with_data(42, vec![].into_iter());
let account = create_account_with_data_for_test(vec![].into_iter());
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
assert_eq!(recent_blockhashes, RecentBlockhashes::default());
}
@ -50,8 +77,7 @@ mod tests {
fn test_create_account_full() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
let account = create_account_with_data_for_test(
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES].into_iter(),
);
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
@ -62,8 +88,7 @@ mod tests {
fn test_create_account_truncate() {
let def_hash = Hash::default();
let def_fees = FeeCalculator::default();
let account = create_account_with_data(
42,
let account = create_account_with_data_for_test(
vec![IterItem(0u64, &def_hash, &def_fees); MAX_ENTRIES + 1].into_iter(),
);
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
@ -85,8 +110,7 @@ mod tests {
.collect();
unsorted_blocks.shuffle(&mut thread_rng());
let account = create_account_with_data(
42,
let account = create_account_with_data_for_test(
unsorted_blocks
.iter()
.map(|(i, hash)| IterItem(*i, hash, &def_fees)),