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

* 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

(cherry picked from commit 6d5c6c17c5)

# Conflicts:
#	programs/bpf_loader/src/lib.rs
#	programs/stake/src/stake_instruction.rs
#	programs/vote/src/vote_instruction.rs
#	runtime/src/accounts.rs
#	runtime/src/bank.rs
#	runtime/src/message_processor.rs
#	runtime/src/system_instruction_processor.rs
#	sdk/benches/slot_hashes.rs
#	sdk/benches/slot_history.rs
#	sdk/src/account.rs
#	sdk/src/keyed_account.rs
#	sdk/src/native_loader.rs
#	sdk/src/recent_blockhashes_account.rs

* Fix conflicts

* rustfmt

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
This commit is contained in:
mergify[bot]
2021-03-25 18:12:33 +09:00
committed by GitHub
parent 173ca7b448
commit 4e6d175697
16 changed files with 180 additions and 96 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

@@ -62,6 +62,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::RefCell, cmp, fmt, rc::Rc};
@@ -119,14 +122,33 @@ 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>(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 a `Sysvar` from an `Account`'s data.
pub fn from_account<S: Sysvar>(account: &Account) -> Option<S> {
bincode::deserialize(&account.data).ok()

View File

@@ -219,7 +219,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;
@@ -243,7 +243,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,30 @@
use crate::account::Account;
use crate::account::{Account, 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) -> Account {
create_loadable_account_with_fields(name, (lamports, INITIAL_RENT_EPOCH))
}
pub fn create_loadable_account_with_fields(
name: &str,
(lamports, rent_epoch): InheritableAccountFields,
) -> Account {
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) -> Account {
create_loadable_account_with_fields(name, DUMMY_INHERITABLE_ACCOUNT_FIELDS)
}

View File

@@ -1,4 +1,8 @@
use crate::account::{create_account, to_account, Account};
use crate::account::{
create_account_with_fields, to_account, Account, InheritableAccountFields,
DUMMY_INHERITABLE_ACCOUNT_FIELDS,
};
use crate::clock::INITIAL_RENT_EPOCH;
use solana_program::sysvar::recent_blockhashes::{
IntoIterSorted, IterItem, RecentBlockhashes, MAX_ENTRIES,
};
@@ -15,15 +19,37 @@ 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) -> Account
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account = create_account::<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,
) -> Account
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut account =
create_account_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) -> Account
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::*;
@@ -37,7 +63,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());
}
@@ -46,8 +72,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();
@@ -58,8 +83,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();
@@ -81,8 +105,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)),