Perf: Store deserialized sysvars in the sysvars cache (backport #22455) (#22480)

* Perf: Store deserialized sysvars in the sysvars cache (#22455)

* Perf: Store deserialized sysvars in sysvars cache

* add bench

* resolve conflicts

Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
mergify[bot]
2022-01-15 08:48:34 +00:00
committed by GitHub
parent e121b94524
commit 4079f12a3e
11 changed files with 388 additions and 171 deletions

View File

@@ -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,
};
@@ -342,17 +342,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(
@@ -373,19 +393,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,
)?;
@@ -401,11 +421,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,
)?;
@@ -417,11 +437,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
@@ -517,18 +537,9 @@ mod tests {
.collect();
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());
solana_program_runtime::invoke_context::mock_process_instruction_with_sysvars(
&id(),
Vec::new(),

View File

@@ -890,7 +890,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();
@@ -1951,7 +1951,7 @@ mod tests {
&RefCell::new(AccountSharedData::default()),
),
&signers,
Some(rent_sysvar),
Some(&rent_sysvar),
);
assert_eq!(res, Err(InstructionError::InsufficientFunds));
}
@@ -1974,7 +1974,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!(
@@ -1987,7 +1987,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();