Refactor: Unify `SysvarCache` (#22843)

* Unifies SysvarCache filling in the runtime and tests.
Removes new_mock_with_sysvars_and_features()
Removes mock_process_instruction_with_sysvars().
Replaces from_keyed_account() by SysvarCache in vote processor.

* Replaces from_keyed_account() by SysvarCache in BPF loader.
This commit is contained in:
Alexander Meißner 2022-01-31 17:53:50 +01:00 committed by GitHub
parent 6a0c45fa2e
commit bc800a8d5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 246 additions and 239 deletions

View File

@ -232,35 +232,30 @@ impl<'a> InvokeContext<'a> {
} }
} }
pub fn new_mock_with_sysvars_and_features(
transaction_context: &'a mut TransactionContext,
sysvar_cache: &'a SysvarCache,
feature_set: Arc<FeatureSet>,
) -> Self {
Self::new(
transaction_context,
Rent::default(),
&[],
Cow::Borrowed(sysvar_cache),
Some(LogCollector::new_ref()),
ComputeBudget::default(),
Rc::new(RefCell::new(Executors::default())),
feature_set,
Hash::default(),
0,
0,
)
}
pub fn new_mock( pub fn new_mock(
transaction_context: &'a mut TransactionContext, transaction_context: &'a mut TransactionContext,
builtin_programs: &'a [BuiltinProgram], builtin_programs: &'a [BuiltinProgram],
) -> Self { ) -> Self {
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.fill_missing_entries(|pubkey| {
(0..transaction_context.get_number_of_accounts()).find_map(|index| {
if transaction_context.get_key_of_account_at_index(index) == pubkey {
Some(
transaction_context
.get_account_at_index(index)
.borrow()
.clone(),
)
} else {
None
}
})
});
Self::new( Self::new(
transaction_context, transaction_context,
Rent::default(), Rent::default(),
builtin_programs, builtin_programs,
Cow::Owned(SysvarCache::default()), Cow::Owned(sysvar_cache),
Some(LogCollector::new_ref()), Some(LogCollector::new_ref()),
ComputeBudget::default(), ComputeBudget::default(),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
@ -1072,14 +1067,13 @@ pub fn with_mock_invoke_context<R, F: FnMut(&mut InvokeContext) -> R>(
callback(&mut invoke_context) callback(&mut invoke_context)
} }
pub fn mock_process_instruction_with_sysvars( pub fn mock_process_instruction(
loader_id: &Pubkey, loader_id: &Pubkey,
mut program_indices: Vec<usize>, mut program_indices: Vec<usize>,
instruction_data: &[u8], instruction_data: &[u8],
transaction_accounts: Vec<TransactionAccount>, transaction_accounts: Vec<TransactionAccount>,
instruction_accounts: Vec<AccountMeta>, instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>, expected_result: Result<(), InstructionError>,
sysvar_cache: &SysvarCache,
process_instruction: ProcessInstructionWithContext, process_instruction: ProcessInstructionWithContext,
) -> Vec<AccountSharedData> { ) -> Vec<AccountSharedData> {
program_indices.insert(0, transaction_accounts.len()); program_indices.insert(0, transaction_accounts.len());
@ -1095,7 +1089,6 @@ pub fn mock_process_instruction_with_sysvars(
1, 1,
); );
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]); let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
invoke_context.sysvar_cache = Cow::Borrowed(sysvar_cache);
let result = invoke_context let result = invoke_context
.push( .push(
&preparation.instruction_accounts, &preparation.instruction_accounts,
@ -1110,27 +1103,6 @@ pub fn mock_process_instruction_with_sysvars(
transaction_accounts transaction_accounts
} }
pub fn mock_process_instruction(
loader_id: &Pubkey,
program_indices: Vec<usize>,
instruction_data: &[u8],
transaction_accounts: Vec<TransactionAccount>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
) -> Vec<AccountSharedData> {
mock_process_instruction_with_sysvars(
loader_id,
program_indices,
instruction_data,
transaction_accounts,
instruction_accounts,
expected_result,
&SysvarCache::default(),
process_instruction,
)
}
/// Visit each unique instruction account index once /// Visit each unique instruction account index once
fn visit_each_account_once( fn visit_each_account_once(
instruction_accounts: &[InstructionAccount], instruction_accounts: &[InstructionAccount],

View File

@ -1,10 +1,15 @@
use crate::invoke_context::InvokeContext;
#[allow(deprecated)] #[allow(deprecated)]
use solana_sdk::sysvar::fees::Fees; use solana_sdk::sysvar::{fees::Fees, recent_blockhashes::RecentBlockhashes};
use { use {
solana_sdk::{ solana_sdk::{
account::{AccountSharedData, ReadableAccount},
instruction::InstructionError, instruction::InstructionError,
keyed_account::{check_sysvar_keyed_account, KeyedAccount},
pubkey::Pubkey,
sysvar::{ sysvar::{
clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes, clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes,
stake_history::StakeHistory, SysvarId,
}, },
}, },
std::sync::Arc, std::sync::Arc,
@ -26,6 +31,9 @@ pub struct SysvarCache {
fees: Option<Arc<Fees>>, fees: Option<Arc<Fees>>,
rent: Option<Arc<Rent>>, rent: Option<Arc<Rent>>,
slot_hashes: Option<Arc<SlotHashes>>, slot_hashes: Option<Arc<SlotHashes>>,
#[allow(deprecated)]
recent_blockhashes: Option<Arc<RecentBlockhashes>>,
stake_history: Option<Arc<StakeHistory>>,
} }
impl SysvarCache { impl SysvarCache {
@ -78,4 +86,139 @@ impl SysvarCache {
pub fn set_slot_hashes(&mut self, slot_hashes: SlotHashes) { pub fn set_slot_hashes(&mut self, slot_hashes: SlotHashes) {
self.slot_hashes = Some(Arc::new(slot_hashes)); self.slot_hashes = Some(Arc::new(slot_hashes));
} }
#[deprecated]
#[allow(deprecated)]
pub fn get_recent_blockhashes(&self) -> Result<Arc<RecentBlockhashes>, InstructionError> {
self.recent_blockhashes
.clone()
.ok_or(InstructionError::UnsupportedSysvar)
}
#[deprecated]
#[allow(deprecated)]
pub fn set_recent_blockhashes(&mut self, recent_blockhashes: RecentBlockhashes) {
self.recent_blockhashes = Some(Arc::new(recent_blockhashes));
}
pub fn get_stake_history(&self) -> Result<Arc<StakeHistory>, InstructionError> {
self.stake_history
.clone()
.ok_or(InstructionError::UnsupportedSysvar)
}
pub fn set_stake_history(&mut self, stake_history: StakeHistory) {
self.stake_history = Some(Arc::new(stake_history));
}
pub fn fill_missing_entries<F: FnMut(&Pubkey) -> Option<AccountSharedData>>(
&mut self,
mut load_sysvar_account: F,
) {
if self.get_clock().is_err() {
if let Some(clock) = load_sysvar_account(&Clock::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_clock(clock);
}
}
if self.get_epoch_schedule().is_err() {
if let Some(epoch_schedule) = load_sysvar_account(&EpochSchedule::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_epoch_schedule(epoch_schedule);
}
}
#[allow(deprecated)]
if self.get_fees().is_err() {
if let Some(fees) = load_sysvar_account(&Fees::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_fees(fees);
}
}
if self.get_rent().is_err() {
if let Some(rent) = load_sysvar_account(&Rent::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_rent(rent);
}
}
if self.get_slot_hashes().is_err() {
if let Some(slot_hashes) = load_sysvar_account(&SlotHashes::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_slot_hashes(slot_hashes);
}
}
#[allow(deprecated)]
if self.get_recent_blockhashes().is_err() {
if let Some(recent_blockhashes) = load_sysvar_account(&RecentBlockhashes::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_recent_blockhashes(recent_blockhashes);
}
}
if self.get_stake_history().is_err() {
if let Some(stake_history) = load_sysvar_account(&StakeHistory::id())
.and_then(|account| bincode::deserialize(account.data()).ok())
{
self.set_stake_history(stake_history);
}
}
}
pub fn reset(&mut self) {
*self = SysvarCache::default();
}
}
/// These methods facilitate a transition from fetching sysvars from keyed
/// accounts to fetching from the sysvar cache without breaking consensus. In
/// order to keep consistent behavior, they continue to enforce the same checks
/// as `solana_sdk::keyed_account::from_keyed_account` despite dynamically
/// loading them instead of deserializing from account data.
pub mod get_sysvar_with_account_check {
use super::*;
pub fn clock(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<Clock>, InstructionError> {
check_sysvar_keyed_account::<Clock>(keyed_account)?;
invoke_context.get_sysvar_cache().get_clock()
}
pub fn rent(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<Rent>, InstructionError> {
check_sysvar_keyed_account::<Rent>(keyed_account)?;
invoke_context.get_sysvar_cache().get_rent()
}
pub fn slot_hashes(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<SlotHashes>, InstructionError> {
check_sysvar_keyed_account::<SlotHashes>(keyed_account)?;
invoke_context.get_sysvar_cache().get_slot_hashes()
}
#[allow(deprecated)]
pub fn recent_blockhashes(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<RecentBlockhashes>, InstructionError> {
check_sysvar_keyed_account::<RecentBlockhashes>(keyed_account)?;
invoke_context.get_sysvar_cache().get_recent_blockhashes()
}
pub fn stake_history(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<StakeHistory>, InstructionError> {
check_sysvar_keyed_account::<StakeHistory>(keyed_account)?;
invoke_context.get_sysvar_cache().get_stake_history()
}
} }

View File

@ -23,6 +23,7 @@ use {
invoke_context::{ComputeMeter, Executor, InvokeContext}, invoke_context::{ComputeMeter, Executor, InvokeContext},
log_collector::LogCollector, log_collector::LogCollector,
stable_log, stable_log,
sysvar_cache::get_sysvar_with_account_check,
}, },
solana_rbpf::{ solana_rbpf::{
aligned_memory::AlignedMemory, aligned_memory::AlignedMemory,
@ -38,7 +39,6 @@ use {
account_utils::State, account_utils::State,
bpf_loader, bpf_loader_deprecated, bpf_loader, bpf_loader_deprecated,
bpf_loader_upgradeable::{self, UpgradeableLoaderState}, bpf_loader_upgradeable::{self, UpgradeableLoaderState},
clock::Clock,
entrypoint::{HEAP_LENGTH, SUCCESS}, entrypoint::{HEAP_LENGTH, SUCCESS},
feature_set::{ feature_set::{
cap_accounts_data_len, do_support_realloc, reduce_required_deploy_balance, cap_accounts_data_len, do_support_realloc, reduce_required_deploy_balance,
@ -47,13 +47,12 @@ use {
start_verify_shift32_imm, stop_verify_mul64_imm_nonzero, start_verify_shift32_imm, stop_verify_mul64_imm_nonzero,
}, },
instruction::{AccountMeta, InstructionError}, instruction::{AccountMeta, InstructionError},
keyed_account::{from_keyed_account, keyed_account_at_index, KeyedAccount}, keyed_account::{keyed_account_at_index, KeyedAccount},
loader_instruction::LoaderInstruction, loader_instruction::LoaderInstruction,
loader_upgradeable_instruction::UpgradeableLoaderInstruction, loader_upgradeable_instruction::UpgradeableLoaderInstruction,
program_error::ACCOUNTS_DATA_BUDGET_EXCEEDED, program_error::ACCOUNTS_DATA_BUDGET_EXCEEDED,
program_utils::limited_deserialize, program_utils::limited_deserialize,
pubkey::Pubkey, pubkey::Pubkey,
rent::Rent,
saturating_add_assign, saturating_add_assign,
system_instruction::{self, MAX_PERMITTED_DATA_LENGTH}, system_instruction::{self, MAX_PERMITTED_DATA_LENGTH},
}, },
@ -460,14 +459,14 @@ fn process_loader_upgradeable_instruction(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; let program = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
let buffer = keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?; let buffer = keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?;
let rent = from_keyed_account::<Rent>(keyed_account_at_index( let rent = get_sysvar_with_account_check::rent(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
first_instruction_account + 4, invoke_context,
)?)?; )?;
let clock = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 5)?,
first_instruction_account + 5, invoke_context,
)?)?; )?;
let authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 7)?; let authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 7)?;
let upgrade_authority_address = Some(*authority.unsigned_key()); let upgrade_authority_address = Some(*authority.unsigned_key());
let upgrade_authority_signer = authority.signer_key().is_none(); let upgrade_authority_signer = authority.signer_key().is_none();
@ -614,14 +613,14 @@ fn process_loader_upgradeable_instruction(
let programdata = keyed_account_at_index(keyed_accounts, first_instruction_account)?; let programdata = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; let program = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let buffer = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; let buffer = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
let rent = from_keyed_account::<Rent>(keyed_account_at_index( let rent = get_sysvar_with_account_check::rent(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
first_instruction_account + 4, invoke_context,
)?)?; )?;
let clock = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 5)?,
first_instruction_account + 5, invoke_context,
)?)?; )?;
let authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 6)?; let authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 6)?;
// Verify Program account // Verify Program account
@ -2588,7 +2587,7 @@ mod tests {
let mut elf_new = Vec::new(); let mut elf_new = Vec::new();
file.read_to_end(&mut elf_new).unwrap(); file.read_to_end(&mut elf_new).unwrap();
assert_ne!(elf_orig.len(), elf_new.len()); assert_ne!(elf_orig.len(), elf_new.len());
let slot = 42; const SLOT: u64 = 42;
let buffer_address = Pubkey::new_unique(); let buffer_address = Pubkey::new_unique();
let upgrade_authority_address = Pubkey::new_unique(); let upgrade_authority_address = Pubkey::new_unique();
@ -2596,7 +2595,6 @@ mod tests {
buffer_address: &Pubkey, buffer_address: &Pubkey,
buffer_authority: &Pubkey, buffer_authority: &Pubkey,
upgrade_authority_address: &Pubkey, upgrade_authority_address: &Pubkey,
slot: u64,
elf_orig: &[u8], elf_orig: &[u8],
elf_new: &[u8], elf_new: &[u8],
) -> (Vec<(Pubkey, AccountSharedData)>, Vec<AccountMeta>) { ) -> (Vec<(Pubkey, AccountSharedData)>, Vec<AccountMeta>) {
@ -2631,7 +2629,7 @@ mod tests {
); );
programdata_account programdata_account
.set_state(&UpgradeableLoaderState::ProgramData { .set_state(&UpgradeableLoaderState::ProgramData {
slot, slot: SLOT,
upgrade_authority_address: Some(*upgrade_authority_address), upgrade_authority_address: Some(*upgrade_authority_address),
}) })
.unwrap(); .unwrap();
@ -2649,7 +2647,7 @@ mod tests {
let spill_account = AccountSharedData::new(0, 0, &Pubkey::new_unique()); let spill_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let rent_account = create_account_for_test(&rent); let rent_account = create_account_for_test(&rent);
let clock_account = create_account_for_test(&Clock { let clock_account = create_account_for_test(&Clock {
slot, slot: SLOT,
..Clock::default() ..Clock::default()
}); });
let upgrade_authority_account = AccountSharedData::new(1, 0, &Pubkey::new_unique()); let upgrade_authority_account = AccountSharedData::new(1, 0, &Pubkey::new_unique());
@ -2725,7 +2723,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2740,7 +2737,7 @@ mod tests {
assert_eq!( assert_eq!(
state, state,
UpgradeableLoaderState::ProgramData { UpgradeableLoaderState::ProgramData {
slot, slot: SLOT,
upgrade_authority_address: Some(upgrade_authority_address) upgrade_authority_address: Some(upgrade_authority_address)
} }
); );
@ -2758,14 +2755,13 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
transaction_accounts[0] transaction_accounts[0]
.1 .1
.set_state(&UpgradeableLoaderState::ProgramData { .set_state(&UpgradeableLoaderState::ProgramData {
slot, slot: SLOT,
upgrade_authority_address: None, upgrade_authority_address: None,
}) })
.unwrap(); .unwrap();
@ -2780,7 +2776,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2798,7 +2793,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2814,7 +2808,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2830,7 +2823,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2846,7 +2838,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2862,7 +2853,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2881,7 +2871,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2899,7 +2888,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2918,7 +2906,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2944,7 +2931,6 @@ mod tests {
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2966,7 +2952,6 @@ mod tests {
&buffer_address, &buffer_address,
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -2981,7 +2966,6 @@ mod tests {
&buffer_address, &buffer_address,
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
@ -3002,14 +2986,13 @@ mod tests {
&buffer_address, &buffer_address,
&buffer_address, &buffer_address,
&upgrade_authority_address, &upgrade_authority_address,
slot,
&elf_orig, &elf_orig,
&elf_new, &elf_new,
); );
transaction_accounts[0] transaction_accounts[0]
.1 .1
.set_state(&UpgradeableLoaderState::ProgramData { .set_state(&UpgradeableLoaderState::ProgramData {
slot, slot: SLOT,
upgrade_authority_address: None, upgrade_authority_address: None,
}) })
.unwrap(); .unwrap();

View File

@ -323,10 +323,7 @@ mod tests {
super::*, super::*,
crate::stake_state::{Meta, StakeState}, crate::stake_state::{Meta, StakeState},
bincode::serialize, bincode::serialize,
solana_program_runtime::{ solana_program_runtime::invoke_context::mock_process_instruction,
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
sysvar_cache::SysvarCache,
},
solana_sdk::{ solana_sdk::{
account::{self, AccountSharedData}, account::{self, AccountSharedData},
instruction::{AccountMeta, Instruction}, instruction::{AccountMeta, Instruction},
@ -339,7 +336,7 @@ mod tests {
}, },
sysvar::{self, stake_history::StakeHistory}, sysvar::{self, stake_history::StakeHistory},
}, },
std::str::FromStr, std::{collections::HashSet, str::FromStr},
}; };
fn create_default_account() -> AccountSharedData { fn create_default_account() -> AccountSharedData {
@ -387,31 +384,36 @@ mod tests {
instruction: &Instruction, instruction: &Instruction,
expected_result: Result<(), InstructionError>, expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> { ) -> Vec<AccountSharedData> {
let transaction_accounts = instruction let mut pubkeys: HashSet<Pubkey> = instruction
.accounts .accounts
.iter() .iter()
.map(|meta| { .map(|meta| meta.pubkey)
.collect();
pubkeys.insert(sysvar::clock::id());
let transaction_accounts = pubkeys
.iter()
.map(|pubkey| {
( (
meta.pubkey, *pubkey,
if sysvar::clock::check_id(&meta.pubkey) { if sysvar::clock::check_id(pubkey) {
account::create_account_shared_data_for_test( account::create_account_shared_data_for_test(
&sysvar::clock::Clock::default(), &sysvar::clock::Clock::default(),
) )
} else if sysvar::rewards::check_id(&meta.pubkey) { } else if sysvar::rewards::check_id(pubkey) {
account::create_account_shared_data_for_test( account::create_account_shared_data_for_test(
&sysvar::rewards::Rewards::new(0.0), &sysvar::rewards::Rewards::new(0.0),
) )
} else if sysvar::stake_history::check_id(&meta.pubkey) { } else if sysvar::stake_history::check_id(pubkey) {
account::create_account_shared_data_for_test(&StakeHistory::default()) account::create_account_shared_data_for_test(&StakeHistory::default())
} else if stake_config::check_id(&meta.pubkey) { } else if stake_config::check_id(pubkey) {
config::create_account(0, &stake_config::Config::default()) config::create_account(0, &stake_config::Config::default())
} else if sysvar::rent::check_id(&meta.pubkey) { } else if sysvar::rent::check_id(pubkey) {
account::create_account_shared_data_for_test(&Rent::default()) account::create_account_shared_data_for_test(&Rent::default())
} else if meta.pubkey == invalid_stake_state_pubkey() { } else if *pubkey == invalid_stake_state_pubkey() {
AccountSharedData::new(0, 0, &id()) AccountSharedData::new(0, 0, &id())
} else if meta.pubkey == invalid_vote_state_pubkey() { } else if *pubkey == invalid_vote_state_pubkey() {
AccountSharedData::new(0, 0, &solana_vote_program::id()) AccountSharedData::new(0, 0, &solana_vote_program::id())
} else if meta.pubkey == spoofed_stake_state_pubkey() { } else if *pubkey == spoofed_stake_state_pubkey() {
AccountSharedData::new(0, 0, &spoofed_stake_program_id()) AccountSharedData::new(0, 0, &spoofed_stake_program_id())
} else { } else {
AccountSharedData::new(0, 0, &id()) AccountSharedData::new(0, 0, &id())
@ -419,17 +421,11 @@ mod tests {
) )
}) })
.collect(); .collect();
let mut sysvar_cache = SysvarCache::default(); process_instruction(
sysvar_cache.set_clock(Clock::default());
mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
&instruction.data, &instruction.data,
transaction_accounts, transaction_accounts,
instruction.accounts.clone(), instruction.accounts.clone(),
expected_result, expected_result,
&sysvar_cache,
super::process_instruction,
) )
} }
@ -1116,7 +1112,7 @@ mod tests {
vec![ vec![
(address_with_seed, stake_account), (address_with_seed, stake_account),
(authorized_owner, authorized_account), (authorized_owner, authorized_account),
(clock_address, clock_account), (clock_address, clock_account.clone()),
(withdrawer, new_authorized_account), (withdrawer, new_authorized_account),
], ],
vec![ vec![
@ -1169,13 +1165,10 @@ mod tests {
) )
.unwrap(); .unwrap();
let mut sysvar_cache = SysvarCache::default(); process_instruction(
sysvar_cache.set_clock(Clock::default());
mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
&instruction.data, &instruction.data,
vec![ vec![
(clock_address, clock_account),
(stake_address, stake_account), (stake_address, stake_account),
(withdrawer, withdrawer_account), (withdrawer, withdrawer_account),
(custodian, custodian_account), (custodian, custodian_account),
@ -1198,8 +1191,6 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
&sysvar_cache,
super::process_instruction,
); );
} }
} }

View File

@ -3,7 +3,7 @@
extern crate test; extern crate test;
use { use {
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache}, solana_program_runtime::invoke_context::InvokeContext,
solana_sdk::{ solana_sdk::{
account::{create_account_for_test, Account, AccountSharedData}, account::{create_account_for_test, Account, AccountSharedData},
clock::{Clock, Slot}, clock::{Clock, Slot},
@ -148,10 +148,6 @@ fn do_bench_process_vote_instruction(bencher: &mut Bencher, feature: Option<Pubk
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.set_clock(clock);
sysvar_cache.set_slot_hashes(slot_hashes);
bencher.iter(|| { bencher.iter(|| {
let mut transaction_context = TransactionContext::new( let mut transaction_context = TransactionContext::new(
vec![ vec![
@ -174,12 +170,8 @@ fn do_bench_process_vote_instruction(bencher: &mut Bencher, feature: Option<Pubk
1, 1,
); );
let mut invoke_context = InvokeContext::new_mock_with_sysvars_and_features( let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
&mut transaction_context, invoke_context.feature_set = feature_set.clone();
&sysvar_cache,
feature_set.clone(),
);
invoke_context invoke_context
.push(&instruction_accounts, &program_indices, &[]) .push(&instruction_accounts, &program_indices, &[])
.unwrap(); .unwrap();

View File

@ -4,54 +4,20 @@ use {
crate::{id, vote_instruction::VoteInstruction, vote_state}, crate::{id, vote_instruction::VoteInstruction, vote_state},
log::*, log::*,
solana_metrics::inc_new_counter_info, solana_metrics::inc_new_counter_info,
solana_program_runtime::invoke_context::InvokeContext, solana_program_runtime::{
invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,
},
solana_sdk::{ solana_sdk::{
feature_set, feature_set,
instruction::InstructionError, instruction::InstructionError,
keyed_account::{ keyed_account::{get_signers, keyed_account_at_index, KeyedAccount},
check_sysvar_keyed_account, from_keyed_account, get_signers, keyed_account_at_index,
KeyedAccount,
},
program_utils::limited_deserialize, program_utils::limited_deserialize,
pubkey::Pubkey, pubkey::Pubkey,
sysvar::{clock::Clock, rent::Rent, slot_hashes::SlotHashes}, sysvar::rent::Rent,
}, },
std::{collections::HashSet, sync::Arc}, std::collections::HashSet,
}; };
/// These methods facilitate a transition from fetching sysvars from keyed
/// accounts to fetching from the sysvar cache without breaking consensus. In
/// order to keep consistent behavior, they continue to enforce the same checks
/// as `solana_sdk::keyed_account::from_keyed_account` despite dynamically
/// loading them instead of deserializing from account data.
mod get_sysvar_with_keyed_account_check {
use super::*;
pub fn clock(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<Clock>, InstructionError> {
check_sysvar_keyed_account::<Clock>(keyed_account)?;
invoke_context.get_sysvar_cache().get_clock()
}
pub fn rent(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<Rent>, InstructionError> {
check_sysvar_keyed_account::<Rent>(keyed_account)?;
invoke_context.get_sysvar_cache().get_rent()
}
pub fn slot_hashes(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<SlotHashes>, InstructionError> {
check_sysvar_keyed_account::<SlotHashes>(keyed_account)?;
invoke_context.get_sysvar_cache().get_slot_hashes()
}
}
pub fn process_instruction( pub fn process_instruction(
first_instruction_account: usize, first_instruction_account: usize,
data: &[u8], data: &[u8],
@ -70,19 +36,19 @@ pub fn process_instruction(
let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[first_instruction_account..]); let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[first_instruction_account..]);
match limited_deserialize(data)? { match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => { VoteInstruction::InitializeAccount(vote_init) => {
let rent = get_sysvar_with_keyed_account_check::rent( let rent = get_sysvar_with_account_check::rent(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context, invoke_context,
)?; )?;
verify_rent_exemption(me, &rent)?; verify_rent_exemption(me, &rent)?;
let clock = get_sysvar_with_keyed_account_check::clock( let clock = get_sysvar_with_account_check::clock(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context, invoke_context,
)?; )?;
vote_state::initialize_account(me, &vote_init, &signers, &clock) vote_state::initialize_account(me, &vote_init, &signers, &clock)
} }
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => { VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
let clock = get_sysvar_with_keyed_account_check::clock( let clock = get_sysvar_with_account_check::clock(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context, invoke_context,
)?; )?;
@ -105,11 +71,11 @@ pub fn process_instruction(
} }
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => { VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
inc_new_counter_info!("vote-native", 1); inc_new_counter_info!("vote-native", 1);
let slot_hashes = get_sysvar_with_keyed_account_check::slot_hashes( let slot_hashes = get_sysvar_with_account_check::slot_hashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context, invoke_context,
)?; )?;
let clock = get_sysvar_with_keyed_account_check::clock( let clock = get_sysvar_with_account_check::clock(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context, invoke_context,
)?; )?;
@ -164,15 +130,16 @@ pub fn process_instruction(
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)? &keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
.signer_key() .signer_key()
.ok_or(InstructionError::MissingRequiredSignature)?; .ok_or(InstructionError::MissingRequiredSignature)?;
let clock = get_sysvar_with_account_check::clock(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context,
)?;
vote_state::authorize( vote_state::authorize(
me, me,
voter_pubkey, voter_pubkey,
vote_authorize, vote_authorize,
&signers, &signers,
&from_keyed_account::<Clock>(keyed_account_at_index( &clock,
keyed_accounts,
first_instruction_account + 1,
)?)?,
&invoke_context.feature_set, &invoke_context.feature_set,
) )
} else { } else {
@ -206,15 +173,12 @@ mod tests {
vote_state::{Vote, VoteAuthorize, VoteInit, VoteState, VoteStateUpdate}, vote_state::{Vote, VoteAuthorize, VoteInit, VoteState, VoteStateUpdate},
}, },
bincode::serialize, bincode::serialize,
solana_program_runtime::{ solana_program_runtime::invoke_context::mock_process_instruction,
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
sysvar_cache::SysvarCache,
},
solana_sdk::{ solana_sdk::{
account::{self, Account, AccountSharedData}, account::{self, Account, AccountSharedData},
hash::Hash, hash::Hash,
instruction::{AccountMeta, Instruction}, instruction::{AccountMeta, Instruction},
sysvar, sysvar::{self, clock::Clock, slot_hashes::SlotHashes},
}, },
std::str::FromStr, std::str::FromStr,
}; };
@ -244,19 +208,26 @@ mod tests {
instruction: &Instruction, instruction: &Instruction,
expected_result: Result<(), InstructionError>, expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> { ) -> Vec<AccountSharedData> {
let transaction_accounts: Vec<_> = instruction let mut pubkeys: HashSet<Pubkey> = instruction
.accounts .accounts
.iter() .iter()
.map(|meta| { .map(|meta| meta.pubkey)
.collect();
pubkeys.insert(sysvar::clock::id());
pubkeys.insert(sysvar::rent::id());
pubkeys.insert(sysvar::slot_hashes::id());
let transaction_accounts: Vec<_> = pubkeys
.iter()
.map(|pubkey| {
( (
meta.pubkey, *pubkey,
if sysvar::clock::check_id(&meta.pubkey) { if sysvar::clock::check_id(pubkey) {
account::create_account_shared_data_for_test(&Clock::default()) account::create_account_shared_data_for_test(&Clock::default())
} else if sysvar::slot_hashes::check_id(&meta.pubkey) { } else if sysvar::slot_hashes::check_id(pubkey) {
account::create_account_shared_data_for_test(&SlotHashes::default()) account::create_account_shared_data_for_test(&SlotHashes::default())
} else if sysvar::rent::check_id(&meta.pubkey) { } else if sysvar::rent::check_id(pubkey) {
account::create_account_shared_data_for_test(&Rent::free()) account::create_account_shared_data_for_test(&Rent::free())
} else if meta.pubkey == invalid_vote_state_pubkey() { } else if *pubkey == invalid_vote_state_pubkey() {
AccountSharedData::from(Account { AccountSharedData::from(Account {
owner: invalid_vote_state_pubkey(), owner: invalid_vote_state_pubkey(),
..Account::default() ..Account::default()
@ -270,19 +241,11 @@ mod tests {
) )
}) })
.collect(); .collect();
let mut sysvar_cache = SysvarCache::default(); process_instruction(
sysvar_cache.set_rent(Rent::free());
sysvar_cache.set_clock(Clock::default());
sysvar_cache.set_slot_hashes(SlotHashes::default());
mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
&instruction.data, &instruction.data,
transaction_accounts, transaction_accounts,
instruction.accounts.clone(), instruction.accounts.clone(),
expected_result, expected_result,
&sysvar_cache,
super::process_instruction,
) )
} }

View File

@ -1,51 +1,14 @@
use { use super::Bank;
super::Bank,
solana_program_runtime::sysvar_cache::SysvarCache,
solana_sdk::{account::ReadableAccount, sysvar::Sysvar},
};
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();
if sysvar_cache.get_clock().is_err() { sysvar_cache.fill_missing_entries(|pubkey| self.get_account_with_fixed_root(pubkey));
if let Some(clock) = self.load_sysvar_account() {
sysvar_cache.set_clock(clock);
}
}
if sysvar_cache.get_epoch_schedule().is_err() {
if let Some(epoch_schedule) = self.load_sysvar_account() {
sysvar_cache.set_epoch_schedule(epoch_schedule);
}
}
#[allow(deprecated)]
if sysvar_cache.get_fees().is_err() {
if let Some(fees) = self.load_sysvar_account() {
sysvar_cache.set_fees(fees);
}
}
if sysvar_cache.get_rent().is_err() {
if let Some(rent) = self.load_sysvar_account() {
sysvar_cache.set_rent(rent);
}
}
if sysvar_cache.get_slot_hashes().is_err() {
if let Some(slot_hashes) = self.load_sysvar_account() {
sysvar_cache.set_slot_hashes(slot_hashes);
}
}
} }
pub(crate) fn reset_sysvar_cache(&self) { pub(crate) fn reset_sysvar_cache(&self) {
let mut sysvar_cache = self.sysvar_cache.write().unwrap(); let mut sysvar_cache = self.sysvar_cache.write().unwrap();
*sysvar_cache = SysvarCache::default(); sysvar_cache.reset();
}
fn load_sysvar_account<T: Sysvar>(&self) -> Option<T> {
if let Some(account) = self.get_account_with_fixed_root(&T::id()) {
bincode::deserialize(account.data()).ok()
} else {
None
}
} }
} }