Refactor: move sysvar cache to new module

This commit is contained in:
Justin Starry
2022-01-12 11:19:11 +08:00
committed by Trent Nelson
parent b27333e52d
commit 7171c95bdd
12 changed files with 144 additions and 87 deletions

View File

@ -3405,6 +3405,8 @@ dependencies = [
"num-traits",
"rustc_version 0.4.0",
"serde",
"solana-frozen-abi 1.10.0",
"solana-frozen-abi-macro 1.10.0",
"solana-measure",
"solana-sdk",
"thiserror",

View File

@ -2921,7 +2921,7 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
mod tests {
use {
super::*,
solana_program_runtime::invoke_context::InvokeContext,
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache},
solana_rbpf::{
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
},
@ -2929,7 +2929,7 @@ mod tests {
account::AccountSharedData, bpf_loader, fee_calculator::FeeCalculator, hash::hashv,
transaction_context::TransactionContext,
},
std::str::FromStr,
std::{borrow::Cow, str::FromStr},
};
macro_rules! assert_access_violation {
@ -3792,8 +3792,6 @@ mod tests {
leader_schedule_epoch: 4,
unix_timestamp: 5,
};
let mut data_clock = vec![];
bincode::serialize_into(&mut data_clock, &src_clock).unwrap();
let src_epochschedule = EpochSchedule {
slots_per_epoch: 1,
leader_schedule_slot_offset: 2,
@ -3801,35 +3799,33 @@ mod tests {
first_normal_epoch: 3,
first_normal_slot: 4,
};
let mut data_epochschedule = vec![];
bincode::serialize_into(&mut data_epochschedule, &src_epochschedule).unwrap();
let src_fees = Fees {
fee_calculator: FeeCalculator {
lamports_per_signature: 1,
},
};
let mut data_fees = vec![];
bincode::serialize_into(&mut data_fees, &src_fees).unwrap();
let src_rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 2.0,
burn_percent: 3,
};
let mut data_rent = vec![];
bincode::serialize_into(&mut data_rent, &src_rent).unwrap();
let sysvars = [
(sysvar::clock::id(), data_clock),
(sysvar::epoch_schedule::id(), data_epochschedule),
(sysvar::fees::id(), data_fees),
(sysvar::rent::id(), data_rent),
];
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());
let program_id = Pubkey::new_unique();
let mut transaction_context = TransactionContext::new(
vec![(program_id, AccountSharedData::new(0, 0, &bpf_loader::id()))],
1,
);
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
invoke_context.sysvars = &sysvars;
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context.push(&[], &[0], &[]).unwrap();
// Test clock sysvar

View File

@ -341,8 +341,9 @@ mod tests {
super::*,
crate::stake_state::{Meta, StakeState},
bincode::serialize,
solana_program_runtime::invoke_context::{
mock_process_instruction, mock_process_instruction_with_sysvars,
solana_program_runtime::{
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
sysvar_cache::SysvarCache,
},
solana_sdk::{
account::{self, AccountSharedData},
@ -354,7 +355,7 @@ mod tests {
instruction::{self, LockupArgs},
state::{Authorized, Lockup, StakeAuthorize},
},
sysvar::{stake_history::StakeHistory, Sysvar},
sysvar::stake_history::StakeHistory,
},
std::str::FromStr,
};
@ -436,8 +437,9 @@ mod tests {
)
})
.collect();
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut sysvar_cache = SysvarCache::default();
let clock = Clock::default();
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
@ -445,7 +447,7 @@ mod tests {
transaction_accounts,
instruction.accounts.clone(),
expected_result,
&[(sysvar::clock::id(), data)],
&sysvar_cache,
super::process_instruction,
)
}
@ -1186,8 +1188,9 @@ mod tests {
)
.unwrap();
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut sysvar_cache = SysvarCache::default();
let clock = Clock::default();
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
mock_process_instruction_with_sysvars(
&id(),
Vec::new(),
@ -1215,7 +1218,7 @@ mod tests {
},
],
Ok(()),
&[(sysvar::clock::id(), data)],
&sysvar_cache,
super::process_instruction,
);
}

View File

@ -518,8 +518,9 @@ mod tests {
use {
super::*,
bincode::serialize,
solana_program_runtime::invoke_context::{
mock_process_instruction, mock_process_instruction_with_sysvars,
solana_program_runtime::{
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
sysvar_cache::SysvarCache,
},
solana_sdk::{
account::{self, Account, AccountSharedData},
@ -579,12 +580,13 @@ mod tests {
)
})
.collect();
let mut sysvar_cache = SysvarCache::default();
let rent = Rent::default();
let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap());
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&rent).unwrap());
let clock = Clock::default();
let clock_sysvar = (sysvar::clock::id(), bincode::serialize(&clock).unwrap());
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
let slot_hashes = SlotHashes::default();
let slot_hashes_sysvar = (
sysvar_cache.push_entry(
sysvar::slot_hashes::id(),
bincode::serialize(&slot_hashes).unwrap(),
);
@ -595,7 +597,7 @@ mod tests {
transaction_accounts,
instruction.accounts.clone(),
expected_result,
&[rent_sysvar, clock_sysvar, slot_hashes_sysvar],
&sysvar_cache,
super::process_instruction,
)
}