* Perf: Store deserialized sysvars in the sysvars cache (#22455) * resolve conflicts * remove bench
This commit is contained in:
@@ -16,9 +16,7 @@ use {
|
||||
account_utils::StateMut,
|
||||
bpf_loader, bpf_loader_deprecated,
|
||||
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
|
||||
clock::Clock,
|
||||
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||
epoch_schedule::EpochSchedule,
|
||||
feature_set::{
|
||||
allow_native_ids, check_seed_length, close_upgradeable_program_accounts, cpi_data_cost,
|
||||
demote_program_write_locks, enforce_aligned_host_addrs, keccak256_syscall_enabled,
|
||||
@@ -33,14 +31,13 @@ use {
|
||||
keccak,
|
||||
keyed_account::KeyedAccount,
|
||||
native_loader,
|
||||
process_instruction::{self, stable_log, ComputeMeter, InvokeContext, Logger},
|
||||
process_instruction::{stable_log, ComputeMeter, InvokeContext, Logger},
|
||||
program::MAX_RETURN_DATA,
|
||||
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::{self, Sysvar, SysvarId},
|
||||
},
|
||||
std::{
|
||||
alloc::Layout,
|
||||
@@ -50,6 +47,7 @@ use {
|
||||
rc::Rc,
|
||||
slice::from_raw_parts_mut,
|
||||
str::{from_utf8, Utf8Error},
|
||||
sync::Arc,
|
||||
},
|
||||
thiserror::Error as ThisError,
|
||||
};
|
||||
@@ -1142,17 +1140,13 @@ impl<'a> SyscallObject<BpfError> for SyscallSha256<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
invoke_context: Rc<RefCell<&mut dyn InvokeContext>>,
|
||||
invoke_context: &dyn InvokeContext,
|
||||
) -> Result<u64, EbpfError<BpfError>> {
|
||||
let invoke_context = invoke_context
|
||||
.try_borrow()
|
||||
.map_err(|_| SyscallError::InvokeContextBorrowFailed)?;
|
||||
|
||||
invoke_context.get_compute_meter().consume(
|
||||
invoke_context.get_bpf_compute_budget().sysvar_base_cost + size_of::<T>() as u64,
|
||||
)?;
|
||||
@@ -1163,8 +1157,8 @@ fn get_sysvar<T: std::fmt::Debug + Sysvar + SysvarId>(
|
||||
invoke_context.is_feature_active(&enforce_aligned_host_addrs::id()),
|
||||
)?;
|
||||
|
||||
*var = process_instruction::get_sysvar::<T>(*invoke_context, id)
|
||||
.map_err(SyscallError::InstructionError)?;
|
||||
let sysvar: Arc<T> = sysvar.map_err(SyscallError::InstructionError)?;
|
||||
*var = T::clone(sysvar.as_ref());
|
||||
|
||||
Ok(SUCCESS)
|
||||
}
|
||||
@@ -1185,12 +1179,18 @@ impl<'a> SyscallObject<BpfError> for SyscallGetClockSysvar<'a> {
|
||||
memory_mapping: &MemoryMapping,
|
||||
result: &mut Result<u64, EbpfError<BpfError>>,
|
||||
) {
|
||||
*result = get_sysvar::<Clock>(
|
||||
&sysvar::clock::id(),
|
||||
let invoke_context = question_mark!(
|
||||
self.invoke_context
|
||||
.try_borrow()
|
||||
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_clock(),
|
||||
var_addr,
|
||||
self.loader_id,
|
||||
memory_mapping,
|
||||
self.invoke_context.clone(),
|
||||
*invoke_context,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1210,12 +1210,18 @@ impl<'a> SyscallObject<BpfError> for SyscallGetEpochScheduleSysvar<'a> {
|
||||
memory_mapping: &MemoryMapping,
|
||||
result: &mut Result<u64, EbpfError<BpfError>>,
|
||||
) {
|
||||
*result = get_sysvar::<EpochSchedule>(
|
||||
&sysvar::epoch_schedule::id(),
|
||||
let invoke_context = question_mark!(
|
||||
self.invoke_context
|
||||
.try_borrow()
|
||||
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_epoch_schedule(),
|
||||
var_addr,
|
||||
self.loader_id,
|
||||
memory_mapping,
|
||||
self.invoke_context.clone(),
|
||||
*invoke_context,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1235,12 +1241,18 @@ impl<'a> SyscallObject<BpfError> for SyscallGetFeesSysvar<'a> {
|
||||
memory_mapping: &MemoryMapping,
|
||||
result: &mut Result<u64, EbpfError<BpfError>>,
|
||||
) {
|
||||
*result = get_sysvar::<Fees>(
|
||||
&sysvar::fees::id(),
|
||||
let invoke_context = question_mark!(
|
||||
self.invoke_context
|
||||
.try_borrow()
|
||||
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_fees(),
|
||||
var_addr,
|
||||
self.loader_id,
|
||||
memory_mapping,
|
||||
self.invoke_context.clone(),
|
||||
*invoke_context,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1260,12 +1272,18 @@ impl<'a> SyscallObject<BpfError> for SyscallGetRentSysvar<'a> {
|
||||
memory_mapping: &MemoryMapping,
|
||||
result: &mut Result<u64, EbpfError<BpfError>>,
|
||||
) {
|
||||
*result = get_sysvar::<Rent>(
|
||||
&sysvar::rent::id(),
|
||||
let invoke_context = question_mark!(
|
||||
self.invoke_context
|
||||
.try_borrow()
|
||||
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
|
||||
result
|
||||
);
|
||||
*result = get_sysvar(
|
||||
invoke_context.get_sysvar_cache().get_rent(),
|
||||
var_addr,
|
||||
self.loader_id,
|
||||
memory_mapping,
|
||||
self.invoke_context.clone(),
|
||||
*invoke_context,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2771,9 +2789,13 @@ mod tests {
|
||||
},
|
||||
solana_sdk::{
|
||||
bpf_loader,
|
||||
clock::Clock,
|
||||
epoch_schedule::EpochSchedule,
|
||||
fee_calculator::FeeCalculator,
|
||||
hash::hashv,
|
||||
process_instruction::{MockComputeMeter, MockInvokeContext, MockLogger},
|
||||
rent::Rent,
|
||||
sysvar::fees::Fees,
|
||||
sysvar_cache::SysvarCache,
|
||||
},
|
||||
std::{borrow::Cow, str::FromStr},
|
||||
@@ -3647,13 +3669,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);
|
||||
|
||||
// Test clock sysvar
|
||||
{
|
||||
|
@@ -10,7 +10,7 @@ use {
|
||||
feature_set,
|
||||
instruction::InstructionError,
|
||||
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index},
|
||||
process_instruction::{get_sysvar, InvokeContext},
|
||||
process_instruction::InvokeContext,
|
||||
program_utils::limited_deserialize,
|
||||
pubkey::Pubkey,
|
||||
stake::{
|
||||
@@ -18,7 +18,7 @@ use {
|
||||
program::id,
|
||||
state::{Authorized, Lockup},
|
||||
},
|
||||
sysvar::{self, clock::Clock, rent::Rent, stake_history::StakeHistory},
|
||||
sysvar::{clock::Clock, rent::Rent, stake_history::StakeHistory},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -167,11 +167,11 @@ pub fn process_instruction(
|
||||
),
|
||||
StakeInstruction::SetLockup(lockup) => {
|
||||
let clock = if invoke_context.is_feature_active(&feature_set::stake_program_v4::id()) {
|
||||
Some(get_sysvar::<Clock>(invoke_context, &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.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
@@ -262,8 +262,8 @@ pub fn process_instruction(
|
||||
epoch: lockup_checked.epoch,
|
||||
custodian,
|
||||
};
|
||||
let clock = Some(get_sysvar::<Clock>(invoke_context, &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)
|
||||
}
|
||||
@@ -288,7 +288,7 @@ mod tests {
|
||||
instruction::{self, LockupArgs},
|
||||
state::{Authorized, Lockup, StakeAuthorize},
|
||||
},
|
||||
sysvar::stake_history::StakeHistory,
|
||||
sysvar::{self, stake_history::StakeHistory},
|
||||
sysvar_cache::SysvarCache,
|
||||
},
|
||||
std::{borrow::Cow, cell::RefCell, str::FromStr},
|
||||
@@ -372,10 +372,7 @@ mod tests {
|
||||
|
||||
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::clock::id(),
|
||||
bincode::serialize(&Clock::default()).unwrap(),
|
||||
);
|
||||
sysvar_cache.set_clock(Clock::default());
|
||||
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
|
||||
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
|
||||
}
|
||||
@@ -1040,10 +1037,7 @@ mod tests {
|
||||
|
||||
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::clock::id(),
|
||||
bincode::serialize(&Clock::default()).unwrap(),
|
||||
);
|
||||
sysvar_cache.set_clock(Clock::default());
|
||||
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
|
||||
|
||||
assert_eq!(
|
||||
|
@@ -19,13 +19,13 @@ use {
|
||||
check_sysvar_keyed_account, from_keyed_account, get_signers, keyed_account_at_index,
|
||||
KeyedAccount,
|
||||
},
|
||||
process_instruction::{get_sysvar, InvokeContext},
|
||||
process_instruction::InvokeContext,
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -311,17 +311,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: &dyn InvokeContext,
|
||||
) -> Result<S, InstructionError> {
|
||||
check_sysvar_keyed_account::<S>(keyed_account)?;
|
||||
get_sysvar(invoke_context, keyed_account.unsigned_key())
|
||||
mod get_sysvar_with_keyed_account_check {
|
||||
use super::*;
|
||||
|
||||
pub fn clock(
|
||||
keyed_account: &KeyedAccount,
|
||||
invoke_context: &dyn 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: &dyn 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: &dyn InvokeContext,
|
||||
) -> Result<Arc<SlotHashes>, InstructionError> {
|
||||
check_sysvar_keyed_account::<SlotHashes>(keyed_account)?;
|
||||
invoke_context.get_sysvar_cache().get_slot_hashes()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_instruction(
|
||||
@@ -346,19 +366,19 @@ pub fn process_instruction(
|
||||
|
||||
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, 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, 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, 1)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
@@ -374,11 +394,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, 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, 2)?,
|
||||
invoke_context,
|
||||
)?;
|
||||
@@ -389,11 +409,11 @@ pub fn process_instruction(
|
||||
let rent_sysvar = if invoke_context
|
||||
.is_feature_active(&feature_set::reject_non_rent_exempt_vote_withdraws::id())
|
||||
{
|
||||
Some(get_sysvar(invoke_context, &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.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
@@ -484,18 +504,9 @@ mod tests {
|
||||
.collect();
|
||||
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::rent::id(),
|
||||
bincode::serialize(&Rent::free()).unwrap(),
|
||||
);
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::clock::id(),
|
||||
bincode::serialize(&Clock::default()).unwrap(),
|
||||
);
|
||||
sysvar_cache.push_entry(
|
||||
sysvar::slot_hashes::id(),
|
||||
bincode::serialize(&SlotHashes::default()).unwrap(),
|
||||
);
|
||||
sysvar_cache.set_rent(Rent::free());
|
||||
sysvar_cache.set_clock(Clock::default());
|
||||
sysvar_cache.set_slot_hashes(SlotHashes::default());
|
||||
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
|
||||
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
|
||||
}
|
||||
|
@@ -681,7 +681,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();
|
||||
@@ -1742,7 +1742,7 @@ mod tests {
|
||||
&RefCell::new(AccountSharedData::default()),
|
||||
),
|
||||
&signers,
|
||||
Some(rent_sysvar),
|
||||
Some(&rent_sysvar),
|
||||
);
|
||||
assert_eq!(res, Err(InstructionError::InsufficientFunds));
|
||||
}
|
||||
@@ -1765,7 +1765,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!(
|
||||
@@ -1789,7 +1789,7 @@ mod tests {
|
||||
lamports,
|
||||
&KeyedAccount::new(&solana_sdk::pubkey::new_rand(), false, &to_account),
|
||||
&signers,
|
||||
*rent_sysvar,
|
||||
rent_sysvar.as_ref(),
|
||||
);
|
||||
assert_eq!(res, Ok(()));
|
||||
assert_eq!(vote_account.borrow().lamports(), 0);
|
||||
|
Reference in New Issue
Block a user