Removes `AccountSharedData` from `SysvarCache`. (#26712)

* Removes AccountSharedData from SysvarCache.

* Fixes incorrect transaction account index in stake_instruction::test_set_lockup().
This commit is contained in:
Alexander Meißner 2022-07-21 18:32:28 +02:00 committed by GitHub
parent 8465a3aa46
commit 075a5ac44e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 56 deletions

View File

@ -278,24 +278,22 @@ impl<'a> InvokeContext<'a> {
builtin_programs: &'a [BuiltinProgram], builtin_programs: &'a [BuiltinProgram],
) -> Self { ) -> Self {
let mut sysvar_cache = SysvarCache::default(); let mut sysvar_cache = SysvarCache::default();
sysvar_cache.fill_missing_entries(|pubkey| { sysvar_cache.fill_missing_entries(|pubkey, callback| {
(0..transaction_context.get_number_of_accounts()).find_map(|index| { for index in 0..transaction_context.get_number_of_accounts() {
if transaction_context if transaction_context
.get_key_of_account_at_index(index) .get_key_of_account_at_index(index)
.unwrap() .unwrap()
== pubkey == pubkey
{ {
Some( callback(
transaction_context transaction_context
.get_account_at_index(index) .get_account_at_index(index)
.unwrap() .unwrap()
.borrow() .borrow()
.clone(), .data(),
) );
} else {
None
} }
}) }
}); });
Self::new( Self::new(
transaction_context, transaction_context,

View File

@ -3,7 +3,6 @@ use solana_sdk::sysvar::{fees::Fees, recent_blockhashes::RecentBlockhashes};
use { use {
crate::invoke_context::InvokeContext, crate::invoke_context::InvokeContext,
solana_sdk::{ solana_sdk::{
account::{AccountSharedData, ReadableAccount},
instruction::InstructionError, instruction::InstructionError,
pubkey::Pubkey, pubkey::Pubkey,
sysvar::{ sysvar::{
@ -111,60 +110,60 @@ impl SysvarCache {
self.stake_history = Some(Arc::new(stake_history)); self.stake_history = Some(Arc::new(stake_history));
} }
pub fn fill_missing_entries<F: FnMut(&Pubkey) -> Option<AccountSharedData>>( pub fn fill_missing_entries<F: FnMut(&Pubkey, &mut dyn FnMut(&[u8]))>(
&mut self, &mut self,
mut load_sysvar_account: F, mut get_account_data: F,
) { ) {
if self.get_clock().is_err() { if self.clock.is_none() {
if let Some(clock) = load_sysvar_account(&Clock::id()) get_account_data(&Clock::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(clock) = bincode::deserialize(data) {
{ self.set_clock(clock);
self.set_clock(clock); }
} });
} }
if self.get_epoch_schedule().is_err() { if self.epoch_schedule.is_none() {
if let Some(epoch_schedule) = load_sysvar_account(&EpochSchedule::id()) get_account_data(&EpochSchedule::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(epoch_schedule) = bincode::deserialize(data) {
{ self.set_epoch_schedule(epoch_schedule);
self.set_epoch_schedule(epoch_schedule); }
} });
} }
#[allow(deprecated)] #[allow(deprecated)]
if self.get_fees().is_err() { if self.fees.is_none() {
if let Some(fees) = load_sysvar_account(&Fees::id()) get_account_data(&Fees::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(fees) = bincode::deserialize(data) {
{ self.set_fees(fees);
self.set_fees(fees); }
} });
} }
if self.get_rent().is_err() { if self.rent.is_none() {
if let Some(rent) = load_sysvar_account(&Rent::id()) get_account_data(&Rent::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(rent) = bincode::deserialize(data) {
{ self.set_rent(rent);
self.set_rent(rent); }
} });
} }
if self.get_slot_hashes().is_err() { if self.slot_hashes.is_none() {
if let Some(slot_hashes) = load_sysvar_account(&SlotHashes::id()) get_account_data(&SlotHashes::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(slot_hashes) = bincode::deserialize(data) {
{ self.set_slot_hashes(slot_hashes);
self.set_slot_hashes(slot_hashes); }
} });
} }
#[allow(deprecated)] #[allow(deprecated)]
if self.get_recent_blockhashes().is_err() { if self.recent_blockhashes.is_none() {
if let Some(recent_blockhashes) = load_sysvar_account(&RecentBlockhashes::id()) get_account_data(&RecentBlockhashes::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(recent_blockhashes) = bincode::deserialize(data) {
{ self.set_recent_blockhashes(recent_blockhashes);
self.set_recent_blockhashes(recent_blockhashes); }
} });
} }
if self.get_stake_history().is_err() { if self.stake_history.is_none() {
if let Some(stake_history) = load_sysvar_account(&StakeHistory::id()) get_account_data(&StakeHistory::id(), &mut |data: &[u8]| {
.and_then(|account| bincode::deserialize(account.data()).ok()) if let Ok(stake_history) = bincode::deserialize(data) {
{ self.set_stake_history(stake_history);
self.set_stake_history(stake_history); }
} });
} }
} }

View File

@ -3764,7 +3764,7 @@ mod tests {
epoch: Epoch::MAX, epoch: Epoch::MAX,
..Clock::default() ..Clock::default()
}; };
transaction_accounts[3] = ( transaction_accounts[4] = (
sysvar::clock::id(), sysvar::clock::id(),
account::create_account_shared_data_for_test(&clock), account::create_account_shared_data_for_test(&clock),
); );

View File

@ -1,9 +1,16 @@
use {super::Bank, solana_program_runtime::sysvar_cache::SysvarCache}; use {
super::Bank, solana_program_runtime::sysvar_cache::SysvarCache,
solana_sdk::account::ReadableAccount,
};
impl Bank { impl Bank {
pub(crate) fn fill_missing_sysvar_cache_entries(&self) { pub(crate) fn fill_missing_sysvar_cache_entries(&self) {
let mut sysvar_cache = self.sysvar_cache.write().unwrap(); let mut sysvar_cache = self.sysvar_cache.write().unwrap();
sysvar_cache.fill_missing_entries(|pubkey| self.get_account_with_fixed_root(pubkey)); sysvar_cache.fill_missing_entries(|pubkey, callback| {
if let Some(account) = self.get_account_with_fixed_root(pubkey) {
callback(account.data());
}
});
} }
pub(crate) fn reset_sysvar_cache(&self) { pub(crate) fn reset_sysvar_cache(&self) {