Perf: Store deserialized sysvars in the sysvars cache (#22455)
* Perf: Store deserialized sysvars in sysvars cache * add bench
This commit is contained in:
@ -15,13 +15,7 @@ use {
|
||||
keyed_account::keyed_account_at_index,
|
||||
program_utils::limited_deserialize,
|
||||
pubkey::{Pubkey, PUBKEY_BYTES},
|
||||
slot_hashes::SlotHashes,
|
||||
system_instruction,
|
||||
sysvar::{
|
||||
clock::{self, Clock},
|
||||
rent::{self, Rent},
|
||||
slot_hashes,
|
||||
},
|
||||
},
|
||||
std::convert::TryFrom,
|
||||
};
|
||||
@ -92,7 +86,7 @@ impl Processor {
|
||||
})?;
|
||||
|
||||
let derivation_slot = {
|
||||
let slot_hashes: SlotHashes = invoke_context.get_sysvar(&slot_hashes::id())?;
|
||||
let slot_hashes = invoke_context.get_sysvar_cache().get_slot_hashes()?;
|
||||
if slot_hashes.get(&untrusted_recent_slot).is_some() {
|
||||
Ok(untrusted_recent_slot)
|
||||
} else {
|
||||
@ -127,7 +121,7 @@ impl Processor {
|
||||
}
|
||||
|
||||
let table_account_data_len = LOOKUP_TABLE_META_SIZE;
|
||||
let rent: Rent = invoke_context.get_sysvar(&rent::id())?;
|
||||
let rent = invoke_context.get_sysvar_cache().get_rent()?;
|
||||
let required_lamports = rent
|
||||
.minimum_balance(table_account_data_len)
|
||||
.max(1)
|
||||
@ -281,7 +275,7 @@ impl Processor {
|
||||
return Err(InstructionError::InvalidInstructionData);
|
||||
}
|
||||
|
||||
let clock: Clock = invoke_context.get_sysvar(&clock::id())?;
|
||||
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
||||
if clock.slot != lookup_table.meta.last_extended_slot {
|
||||
lookup_table.meta.last_extended_slot = clock.slot;
|
||||
lookup_table.meta.last_extended_slot_start_index =
|
||||
@ -313,7 +307,7 @@ impl Processor {
|
||||
}
|
||||
}
|
||||
|
||||
let rent: Rent = invoke_context.get_sysvar(&rent::id())?;
|
||||
let rent = invoke_context.get_sysvar_cache().get_rent()?;
|
||||
let required_lamports = rent
|
||||
.minimum_balance(new_table_data_len)
|
||||
.max(1)
|
||||
@ -367,7 +361,7 @@ impl Processor {
|
||||
let mut lookup_table_meta = lookup_table.meta;
|
||||
drop(lookup_table_account_ref);
|
||||
|
||||
let clock: Clock = invoke_context.get_sysvar(&clock::id())?;
|
||||
let clock = invoke_context.get_sysvar_cache().get_clock()?;
|
||||
lookup_table_meta.deactivation_slot = clock.slot;
|
||||
|
||||
AddressLookupTable::overwrite_meta_data(
|
||||
@ -420,8 +414,9 @@ impl Processor {
|
||||
return Err(InstructionError::IncorrectAuthority);
|
||||
}
|
||||
|
||||
let clock: Clock = invoke_context.get_sysvar(&clock::id())?;
|
||||
let slot_hashes: SlotHashes = invoke_context.get_sysvar(&slot_hashes::id())?;
|
||||
let sysvar_cache = invoke_context.get_sysvar_cache();
|
||||
let clock = sysvar_cache.get_clock()?;
|
||||
let slot_hashes = sysvar_cache.get_slot_hashes()?;
|
||||
|
||||
match lookup_table.meta.status(clock.slot, &slot_hashes) {
|
||||
LookupTableStatus::Activated => {
|
||||
|
@ -20,9 +20,7 @@ use {
|
||||
account::{ReadableAccount, WritableAccount},
|
||||
account_info::AccountInfo,
|
||||
blake3, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
|
||||
clock::Clock,
|
||||
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||
epoch_schedule::EpochSchedule,
|
||||
feature_set::{
|
||||
self, blake3_syscall_enabled, disable_fees_sysvar, do_support_realloc,
|
||||
fixed_memcpy_nonoverlapping_check, libsecp256k1_0_5_upgrade_enabled,
|
||||
@ -36,11 +34,10 @@ use {
|
||||
program::MAX_RETURN_DATA,
|
||||
program_stubs::is_nonoverlapping,
|
||||
pubkey::{Pubkey, PubkeyError, MAX_SEEDS, MAX_SEED_LEN},
|
||||
rent::Rent,
|
||||
secp256k1_recover::{
|
||||
Secp256k1RecoverError, SECP256K1_PUBLIC_KEY_LENGTH, SECP256K1_SIGNATURE_LENGTH,
|
||||
},
|
||||
sysvar::{self, fees::Fees, Sysvar, SysvarId},
|
||||
sysvar::{Sysvar, SysvarId},
|
||||
transaction_context::InstructionAccount,
|
||||
},
|
||||
std::{
|
||||
@ -50,6 +47,7 @@ use {
|
||||
rc::Rc,
|
||||
slice::from_raw_parts_mut,
|
||||
str::{from_utf8, Utf8Error},
|
||||
sync::Arc,
|
||||
},
|
||||
thiserror::Error as ThisError,
|
||||
};
|
||||
@ -1070,8 +1068,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallSha256<'a, 'b> {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sysvar<T: std::fmt::Debug + Sysvar + SysvarId>(
|
||||
id: &Pubkey,
|
||||
fn get_sysvar<T: std::fmt::Debug + Sysvar + SysvarId + Clone>(
|
||||
sysvar: Result<Arc<T>, InstructionError>,
|
||||
var_addr: u64,
|
||||
loader_id: &Pubkey,
|
||||
memory_mapping: &MemoryMapping,
|
||||
@ -1082,9 +1080,8 @@ fn get_sysvar<T: std::fmt::Debug + Sysvar + SysvarId>(
|
||||
.consume(invoke_context.get_compute_budget().sysvar_base_cost + size_of::<T>() as u64)?;
|
||||
let var = translate_type_mut::<T>(memory_mapping, var_addr, loader_id)?;
|
||||
|
||||
*var = invoke_context
|
||||
.get_sysvar::<T>(id)
|
||||
.map_err(SyscallError::InstructionError)?;
|
||||
let sysvar: Arc<T> = sysvar.map_err(SyscallError::InstructionError)?;
|
||||
*var = T::clone(sysvar.as_ref());
|
||||
|
||||
Ok(SUCCESS)
|
||||
}
|
||||
@ -1117,8 +1114,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallGetClockSysvar<'a, 'b> {
|
||||
.map_err(SyscallError::InstructionError),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar::<Clock>(
|
||||
&sysvar::clock::id(),
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_clock(),
|
||||
var_addr,
|
||||
&loader_id,
|
||||
memory_mapping,
|
||||
@ -1154,8 +1151,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallGetEpochScheduleSysvar<'a, 'b> {
|
||||
.map_err(SyscallError::InstructionError),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar::<EpochSchedule>(
|
||||
&sysvar::epoch_schedule::id(),
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_epoch_schedule(),
|
||||
var_addr,
|
||||
&loader_id,
|
||||
memory_mapping,
|
||||
@ -1192,8 +1189,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallGetFeesSysvar<'a, 'b> {
|
||||
.map_err(SyscallError::InstructionError),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar::<Fees>(
|
||||
&sysvar::fees::id(),
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_fees(),
|
||||
var_addr,
|
||||
&loader_id,
|
||||
memory_mapping,
|
||||
@ -1229,8 +1226,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallGetRentSysvar<'a, 'b> {
|
||||
.map_err(SyscallError::InstructionError),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar::<Rent>(
|
||||
&sysvar::rent::id(),
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_rent(),
|
||||
var_addr,
|
||||
&loader_id,
|
||||
memory_mapping,
|
||||
@ -2919,6 +2916,8 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[allow(deprecated)]
|
||||
use solana_sdk::sysvar::fees::Fees;
|
||||
use {
|
||||
super::*,
|
||||
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache},
|
||||
@ -2926,7 +2925,11 @@ mod tests {
|
||||
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
|
||||
},
|
||||
solana_sdk::{
|
||||
account::AccountSharedData, bpf_loader, fee_calculator::FeeCalculator, hash::hashv,
|
||||
account::AccountSharedData,
|
||||
bpf_loader,
|
||||
fee_calculator::FeeCalculator,
|
||||
hash::hashv,
|
||||
sysvar::{clock::Clock, epoch_schedule::EpochSchedule, rent::Rent},
|
||||
transaction_context::TransactionContext,
|
||||
},
|
||||
std::{borrow::Cow, str::FromStr},
|
||||
@ -3811,13 +3814,10 @@ mod tests {
|
||||
};
|
||||
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&src_clock).unwrap());
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::epoch_schedule::id(),
|
||||
bincode::serialize(&src_epochschedule).unwrap(),
|
||||
);
|
||||
sysvar_cache.push_entry(sysvar::fees::id(), bincode::serialize(&src_fees).unwrap());
|
||||
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&src_rent).unwrap());
|
||||
sysvar_cache.set_clock(src_clock.clone());
|
||||
sysvar_cache.set_epoch_schedule(src_epochschedule);
|
||||
sysvar_cache.set_fees(src_fees.clone());
|
||||
sysvar_cache.set_rent(src_rent);
|
||||
|
||||
let program_id = Pubkey::new_unique();
|
||||
let mut transaction_context = TransactionContext::new(
|
||||
|
@ -17,7 +17,7 @@ use {
|
||||
program::id,
|
||||
state::{Authorized, Lockup},
|
||||
},
|
||||
sysvar::{self, clock::Clock, rent::Rent, stake_history::StakeHistory},
|
||||
sysvar::{clock::Clock, rent::Rent, stake_history::StakeHistory},
|
||||
},
|
||||
};
|
||||
|
||||
@ -204,11 +204,11 @@ pub fn process_instruction(
|
||||
.feature_set
|
||||
.is_active(&feature_set::stake_program_v4::id())
|
||||
{
|
||||
Some(invoke_context.get_sysvar::<Clock>(&sysvar::clock::id())?)
|
||||
Some(invoke_context.get_sysvar_cache().get_clock()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
me.set_lockup(&lockup, &signers, clock.as_ref())
|
||||
me.set_lockup(&lockup, &signers, clock.as_deref())
|
||||
}
|
||||
StakeInstruction::InitializeChecked => {
|
||||
if invoke_context
|
||||
@ -326,8 +326,8 @@ pub fn process_instruction(
|
||||
epoch: lockup_checked.epoch,
|
||||
custodian,
|
||||
};
|
||||
let clock = Some(invoke_context.get_sysvar::<Clock>(&sysvar::clock::id())?);
|
||||
me.set_lockup(&lockup, &signers, clock.as_ref())
|
||||
let clock = Some(invoke_context.get_sysvar_cache().get_clock()?);
|
||||
me.set_lockup(&lockup, &signers, clock.as_deref())
|
||||
} else {
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
}
|
||||
@ -355,7 +355,7 @@ mod tests {
|
||||
instruction::{self, LockupArgs},
|
||||
state::{Authorized, Lockup, StakeAuthorize},
|
||||
},
|
||||
sysvar::stake_history::StakeHistory,
|
||||
sysvar::{self, stake_history::StakeHistory},
|
||||
},
|
||||
std::str::FromStr,
|
||||
};
|
||||
@ -438,8 +438,7 @@ mod tests {
|
||||
})
|
||||
.collect();
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
let clock = Clock::default();
|
||||
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||
sysvar_cache.set_clock(Clock::default());
|
||||
mock_process_instruction_with_sysvars(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
@ -1189,8 +1188,7 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
let clock = Clock::default();
|
||||
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||
sysvar_cache.set_clock(Clock::default());
|
||||
mock_process_instruction_with_sysvars(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
|
@ -23,9 +23,9 @@ use {
|
||||
program_utils::limited_deserialize,
|
||||
pubkey::Pubkey,
|
||||
system_instruction,
|
||||
sysvar::{self, clock::Clock, rent::Rent, slot_hashes::SlotHashes, Sysvar},
|
||||
sysvar::{self, clock::Clock, rent::Rent, slot_hashes::SlotHashes},
|
||||
},
|
||||
std::collections::HashSet,
|
||||
std::{collections::HashSet, sync::Arc},
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
@ -391,17 +391,37 @@ fn verify_rent_exemption(
|
||||
}
|
||||
}
|
||||
|
||||
/// This method facilitates a transition from fetching sysvars from keyed
|
||||
/// 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, it continues to enforce the same checks
|
||||
/// 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.
|
||||
fn get_sysvar_with_keyed_account_check<S: Sysvar>(
|
||||
keyed_account: &KeyedAccount,
|
||||
invoke_context: &InvokeContext,
|
||||
) -> Result<S, InstructionError> {
|
||||
check_sysvar_keyed_account::<S>(keyed_account)?;
|
||||
invoke_context.get_sysvar(keyed_account.unsigned_key())
|
||||
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(
|
||||
@ -422,19 +442,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: Rent = get_sysvar_with_keyed_account_check(
|
||||
let rent = get_sysvar_with_keyed_account_check::rent(
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
verify_rent_exemption(me, &rent)?;
|
||||
let clock: Clock = get_sysvar_with_keyed_account_check(
|
||||
let clock = get_sysvar_with_keyed_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: Clock = get_sysvar_with_keyed_account_check(
|
||||
let clock = get_sysvar_with_keyed_account_check::clock(
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
@ -450,11 +470,11 @@ pub fn process_instruction(
|
||||
}
|
||||
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
|
||||
inc_new_counter_info!("vote-native", 1);
|
||||
let slot_hashes: SlotHashes = get_sysvar_with_keyed_account_check(
|
||||
let slot_hashes = get_sysvar_with_keyed_account_check::slot_hashes(
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
let clock: Clock = get_sysvar_with_keyed_account_check(
|
||||
let clock = get_sysvar_with_keyed_account_check::clock(
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
@ -467,12 +487,13 @@ pub fn process_instruction(
|
||||
.is_active(&feature_set::allow_votes_to_directly_update_vote_state::id())
|
||||
{
|
||||
inc_new_counter_info!("vote-state-native", 1);
|
||||
let slot_hashes: SlotHashes =
|
||||
invoke_context.get_sysvar(&sysvar::slot_hashes::id())?;
|
||||
let sysvar_cache = invoke_context.get_sysvar_cache();
|
||||
let slot_hashes = sysvar_cache.get_slot_hashes()?;
|
||||
let clock = sysvar_cache.get_clock()?;
|
||||
vote_state::process_vote_state_update(
|
||||
me,
|
||||
slot_hashes.slot_hashes(),
|
||||
&invoke_context.get_sysvar(&sysvar::clock::id())?,
|
||||
&clock,
|
||||
vote_state_update,
|
||||
&signers,
|
||||
)
|
||||
@ -486,11 +507,11 @@ pub fn process_instruction(
|
||||
.feature_set
|
||||
.is_active(&feature_set::reject_non_rent_exempt_vote_withdraws::id())
|
||||
{
|
||||
Some(invoke_context.get_sysvar(&sysvar::rent::id())?)
|
||||
Some(invoke_context.get_sysvar_cache().get_rent()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
vote_state::withdraw(me, lamports, to, &signers, rent_sysvar)
|
||||
vote_state::withdraw(me, lamports, to, &signers, rent_sysvar.as_deref())
|
||||
}
|
||||
VoteInstruction::AuthorizeChecked(vote_authorize) => {
|
||||
if invoke_context
|
||||
@ -583,15 +604,9 @@ mod tests {
|
||||
})
|
||||
.collect();
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
let rent = Rent::free();
|
||||
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&rent).unwrap());
|
||||
let clock = Clock::default();
|
||||
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||
let slot_hashes = SlotHashes::default();
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::slot_hashes::id(),
|
||||
bincode::serialize(&slot_hashes).unwrap(),
|
||||
);
|
||||
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(),
|
||||
|
@ -973,7 +973,7 @@ pub fn withdraw<S: std::hash::BuildHasher>(
|
||||
lamports: u64,
|
||||
to_account: &KeyedAccount,
|
||||
signers: &HashSet<Pubkey, S>,
|
||||
rent_sysvar: Option<Rent>,
|
||||
rent_sysvar: Option<&Rent>,
|
||||
) -> Result<(), InstructionError> {
|
||||
let vote_state: VoteState =
|
||||
State::<VoteStateVersions>::state(vote_account)?.convert_to_current();
|
||||
@ -2072,7 +2072,7 @@ mod tests {
|
||||
&RefCell::new(AccountSharedData::default()),
|
||||
),
|
||||
&signers,
|
||||
Some(rent_sysvar),
|
||||
Some(&rent_sysvar),
|
||||
);
|
||||
assert_eq!(res, Err(InstructionError::InsufficientFunds));
|
||||
}
|
||||
@ -2095,7 +2095,7 @@ mod tests {
|
||||
withdraw_lamports,
|
||||
&KeyedAccount::new(&solana_sdk::pubkey::new_rand(), false, &to_account),
|
||||
&signers,
|
||||
Some(rent_sysvar),
|
||||
Some(&rent_sysvar),
|
||||
);
|
||||
assert_eq!(res, Ok(()));
|
||||
assert_eq!(
|
||||
@ -2108,7 +2108,7 @@ mod tests {
|
||||
// full withdraw, before/after activation
|
||||
{
|
||||
let rent_sysvar = Rent::default();
|
||||
for rent_sysvar in [None, Some(rent_sysvar)] {
|
||||
for rent_sysvar in [None, Some(&rent_sysvar)] {
|
||||
let to_account = RefCell::new(AccountSharedData::default());
|
||||
let (vote_pubkey, vote_account) = create_test_account();
|
||||
let lamports = vote_account.borrow().lamports();
|
||||
|
Reference in New Issue
Block a user