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
7 changed files with 246 additions and 239 deletions

View File

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

View File

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

View File

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

View File

@ -4,54 +4,20 @@ use {
crate::{id, vote_instruction::VoteInstruction, vote_state},
log::*,
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::{
feature_set,
instruction::InstructionError,
keyed_account::{
check_sysvar_keyed_account, from_keyed_account, get_signers, keyed_account_at_index,
KeyedAccount,
},
keyed_account::{get_signers, keyed_account_at_index, KeyedAccount},
program_utils::limited_deserialize,
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(
first_instruction_account: usize,
data: &[u8],
@ -70,19 +36,19 @@ pub fn process_instruction(
let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[first_instruction_account..]);
match limited_deserialize(data)? {
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)?,
invoke_context,
)?;
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)?,
invoke_context,
)?;
vote_state::initialize_account(me, &vote_init, &signers, &clock)
}
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)?,
invoke_context,
)?;
@ -105,11 +71,11 @@ pub fn process_instruction(
}
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
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)?,
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)?,
invoke_context,
)?;
@ -164,15 +130,16 @@ pub fn process_instruction(
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
.signer_key()
.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(
me,
voter_pubkey,
vote_authorize,
&signers,
&from_keyed_account::<Clock>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 1,
)?)?,
&clock,
&invoke_context.feature_set,
)
} else {
@ -206,15 +173,12 @@ mod tests {
vote_state::{Vote, VoteAuthorize, VoteInit, VoteState, VoteStateUpdate},
},
bincode::serialize,
solana_program_runtime::{
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
sysvar_cache::SysvarCache,
},
solana_program_runtime::invoke_context::mock_process_instruction,
solana_sdk::{
account::{self, Account, AccountSharedData},
hash::Hash,
instruction::{AccountMeta, Instruction},
sysvar,
sysvar::{self, clock::Clock, slot_hashes::SlotHashes},
},
std::str::FromStr,
};
@ -244,19 +208,26 @@ mod tests {
instruction: &Instruction,
expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> {
let transaction_accounts: Vec<_> = instruction
let mut pubkeys: HashSet<Pubkey> = instruction
.accounts
.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,
if sysvar::clock::check_id(&meta.pubkey) {
*pubkey,
if sysvar::clock::check_id(pubkey) {
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())
} else if sysvar::rent::check_id(&meta.pubkey) {
} else if sysvar::rent::check_id(pubkey) {
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 {
owner: invalid_vote_state_pubkey(),
..Account::default()
@ -270,19 +241,11 @@ mod tests {
)
})
.collect();
let mut sysvar_cache = SysvarCache::default();
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(),
process_instruction(
&instruction.data,
transaction_accounts,
instruction.accounts.clone(),
expected_result,
&sysvar_cache,
super::process_instruction,
)
}