Revert "Stop caching sysvars, instead load them ahead of time (backport #21108) (#22466)"

This reverts commit 5d3f3bc9b1.
This commit is contained in:
Justin Starry
2022-01-14 11:13:04 +08:00
parent 5d3f3bc9b1
commit 304afd42c6
8 changed files with 167 additions and 112 deletions

View File

@ -32,7 +32,7 @@ use {
native_token::sol_to_lamports, native_token::sol_to_lamports,
poh_config::PohConfig, poh_config::PohConfig,
process_instruction::{ process_instruction::{
self, stable_log, BpfComputeBudget, InvokeContext, ProcessInstructionWithContext, stable_log, BpfComputeBudget, InvokeContext, ProcessInstructionWithContext,
}, },
program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR}, program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR},
pubkey::Pubkey, pubkey::Pubkey,
@ -196,6 +196,24 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
var_addr: *mut u8, var_addr: *mut u8,
) -> u64 { ) -> u64 {
let invoke_context = get_invoke_context(); let invoke_context = get_invoke_context();
let sysvar_data = match invoke_context.get_sysvar_data(id).ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get Sysvar {}", id);
UNSUPPORTED_SYSVAR
}) {
Ok(sysvar_data) => sysvar_data,
Err(err) => return err,
};
let var: T = match bincode::deserialize(&sysvar_data) {
Ok(sysvar_data) => sysvar_data,
Err(_) => return UNSUPPORTED_SYSVAR,
};
unsafe {
*(var_addr as *mut _ as *mut T) = var;
}
if invoke_context if invoke_context
.get_compute_meter() .get_compute_meter()
.try_borrow_mut() .try_borrow_mut()
@ -206,13 +224,8 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
{ {
panic!("Exceeded compute budget"); panic!("Exceeded compute budget");
} }
match process_instruction::get_sysvar::<T>(invoke_context, id) {
Ok(sysvar_data) => unsafe { SUCCESS
*(var_addr as *mut _ as *mut T) = sysvar_data;
SUCCESS
},
Err(_) => UNSUPPORTED_SYSVAR,
}
} }
struct SyscallStubs {} struct SyscallStubs {}

View File

@ -3650,7 +3650,9 @@ mod tests {
let mut invoke_context = MockInvokeContext::new(vec![]); let mut invoke_context = MockInvokeContext::new(vec![]);
let mut data = vec![]; let mut data = vec![];
bincode::serialize_into(&mut data, &src_clock).unwrap(); bincode::serialize_into(&mut data, &src_clock).unwrap();
invoke_context.sysvars = vec![(sysvar::clock::id(), data)]; invoke_context
.sysvars
.push((sysvar::clock::id(), Some(Rc::new(data))));
let mut syscall = SyscallGetClockSysvar { let mut syscall = SyscallGetClockSysvar {
invoke_context: Rc::new(RefCell::new(&mut invoke_context)), invoke_context: Rc::new(RefCell::new(&mut invoke_context)),
@ -3693,7 +3695,9 @@ mod tests {
let mut invoke_context = MockInvokeContext::new(vec![]); let mut invoke_context = MockInvokeContext::new(vec![]);
let mut data = vec![]; let mut data = vec![];
bincode::serialize_into(&mut data, &src_epochschedule).unwrap(); bincode::serialize_into(&mut data, &src_epochschedule).unwrap();
invoke_context.sysvars = vec![(sysvar::epoch_schedule::id(), data)]; invoke_context
.sysvars
.push((sysvar::epoch_schedule::id(), Some(Rc::new(data))));
let mut syscall = SyscallGetEpochScheduleSysvar { let mut syscall = SyscallGetEpochScheduleSysvar {
invoke_context: Rc::new(RefCell::new(&mut invoke_context)), invoke_context: Rc::new(RefCell::new(&mut invoke_context)),
@ -3742,7 +3746,9 @@ mod tests {
let mut invoke_context = MockInvokeContext::new(vec![]); let mut invoke_context = MockInvokeContext::new(vec![]);
let mut data = vec![]; let mut data = vec![];
bincode::serialize_into(&mut data, &src_fees).unwrap(); bincode::serialize_into(&mut data, &src_fees).unwrap();
invoke_context.sysvars = vec![(sysvar::fees::id(), data)]; invoke_context
.sysvars
.push((sysvar::fees::id(), Some(Rc::new(data))));
let mut syscall = SyscallGetFeesSysvar { let mut syscall = SyscallGetFeesSysvar {
invoke_context: Rc::new(RefCell::new(&mut invoke_context)), invoke_context: Rc::new(RefCell::new(&mut invoke_context)),
@ -3783,7 +3789,9 @@ mod tests {
let mut invoke_context = MockInvokeContext::new(vec![]); let mut invoke_context = MockInvokeContext::new(vec![]);
let mut data = vec![]; let mut data = vec![];
bincode::serialize_into(&mut data, &src_rent).unwrap(); bincode::serialize_into(&mut data, &src_rent).unwrap();
invoke_context.sysvars = vec![(sysvar::rent::id(), data)]; invoke_context
.sysvars
.push((sysvar::rent::id(), Some(Rc::new(data))));
let mut syscall = SyscallGetRentSysvar { let mut syscall = SyscallGetRentSysvar {
invoke_context: Rc::new(RefCell::new(&mut invoke_context)), invoke_context: Rc::new(RefCell::new(&mut invoke_context)),

View File

@ -281,16 +281,16 @@ mod tests {
account::{self, Account, AccountSharedData, WritableAccount}, account::{self, Account, AccountSharedData, WritableAccount},
instruction::{AccountMeta, Instruction}, instruction::{AccountMeta, Instruction},
keyed_account::KeyedAccount, keyed_account::KeyedAccount,
process_instruction::MockInvokeContext, process_instruction::{mock_set_sysvar, MockInvokeContext},
rent::Rent, rent::Rent,
stake::{ stake::{
config as stake_config, config as stake_config,
instruction::{self, LockupArgs}, instruction::{self, LockupArgs},
state::{Authorized, Lockup, StakeAuthorize}, state::{Authorized, Lockup, StakeAuthorize},
}, },
sysvar::{stake_history::StakeHistory, Sysvar}, sysvar::stake_history::StakeHistory,
}, },
std::{cell::RefCell, str::FromStr}, std::{cell::RefCell, rc::Rc, str::FromStr},
}; };
fn create_default_account() -> RefCell<AccountSharedData> { fn create_default_account() -> RefCell<AccountSharedData> {
@ -370,9 +370,12 @@ mod tests {
.collect(); .collect();
let mut invoke_context = MockInvokeContext::new(keyed_accounts); let mut invoke_context = MockInvokeContext::new(keyed_accounts);
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of()); mock_set_sysvar(
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap(); &mut invoke_context,
invoke_context.sysvars = vec![(sysvar::clock::id(), data)]; sysvar::clock::id(),
sysvar::clock::Clock::default(),
)
.unwrap();
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context) super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
} }
} }
@ -1035,9 +1038,12 @@ mod tests {
]; ];
let mut invoke_context = MockInvokeContext::new(keyed_accounts); let mut invoke_context = MockInvokeContext::new(keyed_accounts);
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of()); let clock = Clock::default();
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap(); let mut data = vec![];
invoke_context.sysvars = vec![(sysvar::clock::id(), data)]; bincode::serialize_into(&mut data, &clock).unwrap();
invoke_context
.sysvars
.push((sysvar::clock::id(), Some(Rc::new(data))));
assert_eq!( assert_eq!(
super::process_instruction( super::process_instruction(

View File

@ -402,9 +402,8 @@ mod tests {
bincode::serialize, bincode::serialize,
solana_sdk::{ solana_sdk::{
account::{self, Account, AccountSharedData}, account::{self, Account, AccountSharedData},
process_instruction::MockInvokeContext, process_instruction::{mock_set_sysvar, MockInvokeContext},
rent::Rent, rent::Rent,
sysvar::Sysvar,
}, },
std::{cell::RefCell, str::FromStr}, std::{cell::RefCell, str::FromStr},
}; };
@ -463,9 +462,12 @@ mod tests {
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account)) .map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
.collect(); .collect();
let mut invoke_context = MockInvokeContext::new(keyed_accounts); let mut invoke_context = MockInvokeContext::new(keyed_accounts);
let mut data = Vec::with_capacity(sysvar::rent::Rent::size_of()); mock_set_sysvar(
bincode::serialize_into(&mut data, &sysvar::rent::Rent::default()).unwrap(); &mut invoke_context,
invoke_context.sysvars = vec![(sysvar::rent::id(), data)]; sysvar::rent::id(),
sysvar::rent::Rent::default(),
)
.unwrap();
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context) super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
} }
} }

View File

@ -1184,8 +1184,6 @@ pub struct Bank {
vote_only_bank: bool, vote_only_bank: bool,
pub cost_tracker: RwLock<CostTracker>, pub cost_tracker: RwLock<CostTracker>,
sysvar_cache: RwLock<Vec<(Pubkey, Vec<u8>)>>,
} }
impl Default for BlockhashQueue { impl Default for BlockhashQueue {
@ -1318,7 +1316,6 @@ impl Bank {
bank.update_rent(); bank.update_rent();
bank.update_epoch_schedule(); bank.update_epoch_schedule();
bank.update_recent_blockhashes(); bank.update_recent_blockhashes();
bank.fill_sysvar_cache();
bank bank
} }
@ -1459,7 +1456,6 @@ impl Bank {
)), )),
freeze_started: AtomicBool::new(false), freeze_started: AtomicBool::new(false),
cost_tracker: RwLock::new(CostTracker::default()), cost_tracker: RwLock::new(CostTracker::default()),
sysvar_cache: RwLock::new(Vec::new()),
}; };
let mut ancestors = Vec::with_capacity(1 + new.parents().len()); let mut ancestors = Vec::with_capacity(1 + new.parents().len());
@ -1501,7 +1497,6 @@ impl Bank {
if !new.fix_recent_blockhashes_sysvar_delay() { if !new.fix_recent_blockhashes_sysvar_delay() {
new.update_recent_blockhashes(); new.update_recent_blockhashes();
} }
new.fill_sysvar_cache();
time.stop(); time.stop();
@ -1636,7 +1631,6 @@ impl Bank {
freeze_started: AtomicBool::new(fields.hash != Hash::default()), freeze_started: AtomicBool::new(fields.hash != Hash::default()),
vote_only_bank: false, vote_only_bank: false,
cost_tracker: RwLock::new(CostTracker::default()), cost_tracker: RwLock::new(CostTracker::default()),
sysvar_cache: RwLock::new(Vec::new()),
}; };
bank.finish_init( bank.finish_init(
genesis_config, genesis_config,
@ -1812,14 +1806,6 @@ impl Bank {
} }
self.store_account_and_update_capitalization(pubkey, &new_account); self.store_account_and_update_capitalization(pubkey, &new_account);
// Update the entry in the cache
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
if let Some(position) = sysvar_cache.iter().position(|(id, _data)| id == pubkey) {
sysvar_cache[position].1 = new_account.data().to_vec();
} else {
sysvar_cache.push((*pubkey, new_account.data().to_vec()));
}
} }
fn inherit_specially_retained_account_fields( fn inherit_specially_retained_account_fields(
@ -1957,17 +1943,6 @@ impl Bank {
}); });
} }
fn fill_sysvar_cache(&mut self) {
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
for id in sysvar::ALL_IDS.iter() {
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
if let Some(account) = self.get_account_with_fixed_root(id) {
sysvar_cache.push((*id, account.data().to_vec()));
}
}
}
}
pub fn get_slot_history(&self) -> SlotHistory { pub fn get_slot_history(&self) -> SlotHistory {
from_account(&self.get_account(&sysvar::slot_history::id()).unwrap()).unwrap() from_account(&self.get_account(&sysvar::slot_history::id()).unwrap()).unwrap()
} }
@ -3623,7 +3598,8 @@ impl Bank {
bpf_compute_budget, bpf_compute_budget,
compute_meter, compute_meter,
&mut timings.details, &mut timings.details,
&*self.sysvar_cache.read().unwrap(), self.rc.accounts.clone(),
&self.ancestors,
); );
transaction_log_messages.push(Self::collect_log_messages(log_collector)); transaction_log_messages.push(Self::collect_log_messages(log_collector));

View File

@ -1,7 +1,8 @@
use { use {
crate::{ crate::{
bank::TransactionAccountRefCell, instruction_recorder::InstructionRecorder, accounts::Accounts, ancestors::Ancestors, bank::TransactionAccountRefCell,
log_collector::LogCollector, native_loader::NativeLoader, rent_collector::RentCollector, instruction_recorder::InstructionRecorder, log_collector::LogCollector,
native_loader::NativeLoader, rent_collector::RentCollector,
}, },
log::*, log::*,
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
@ -370,7 +371,6 @@ pub struct ThisInvokeContext<'a> {
pre_accounts: Vec<PreAccount>, pre_accounts: Vec<PreAccount>,
accounts: &'a [TransactionAccountRefCell], accounts: &'a [TransactionAccountRefCell],
programs: &'a [(Pubkey, ProcessInstructionWithContext)], programs: &'a [(Pubkey, ProcessInstructionWithContext)],
sysvars: &'a [(Pubkey, Vec<u8>)],
logger: Rc<RefCell<dyn Logger>>, logger: Rc<RefCell<dyn Logger>>,
bpf_compute_budget: BpfComputeBudget, bpf_compute_budget: BpfComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>, compute_meter: Rc<RefCell<dyn ComputeMeter>>,
@ -378,6 +378,10 @@ pub struct ThisInvokeContext<'a> {
instruction_recorder: Option<InstructionRecorder>, instruction_recorder: Option<InstructionRecorder>,
feature_set: Arc<FeatureSet>, feature_set: Arc<FeatureSet>,
pub timings: ExecuteDetailsTimings, pub timings: ExecuteDetailsTimings,
account_db: Arc<Accounts>,
ancestors: &'a Ancestors,
#[allow(clippy::type_complexity)]
sysvars: RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>,
// return data and program_id that set it // return data and program_id that set it
return_data: Option<(Pubkey, Vec<u8>)>, return_data: Option<(Pubkey, Vec<u8>)>,
} }
@ -391,13 +395,14 @@ impl<'a> ThisInvokeContext<'a> {
executable_accounts: &'a [TransactionAccountRefCell], executable_accounts: &'a [TransactionAccountRefCell],
accounts: &'a [TransactionAccountRefCell], accounts: &'a [TransactionAccountRefCell],
programs: &'a [(Pubkey, ProcessInstructionWithContext)], programs: &'a [(Pubkey, ProcessInstructionWithContext)],
sysvars: &'a [(Pubkey, Vec<u8>)],
log_collector: Option<Rc<LogCollector>>, log_collector: Option<Rc<LogCollector>>,
bpf_compute_budget: BpfComputeBudget, bpf_compute_budget: BpfComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>, compute_meter: Rc<RefCell<dyn ComputeMeter>>,
executors: Rc<RefCell<Executors>>, executors: Rc<RefCell<Executors>>,
instruction_recorder: Option<InstructionRecorder>, instruction_recorder: Option<InstructionRecorder>,
feature_set: Arc<FeatureSet>, feature_set: Arc<FeatureSet>,
account_db: Arc<Accounts>,
ancestors: &'a Ancestors,
) -> Self { ) -> Self {
let pre_accounts = MessageProcessor::create_pre_accounts(message, instruction, accounts); let pre_accounts = MessageProcessor::create_pre_accounts(message, instruction, accounts);
let keyed_accounts = MessageProcessor::create_keyed_accounts( let keyed_accounts = MessageProcessor::create_keyed_accounts(
@ -420,7 +425,6 @@ impl<'a> ThisInvokeContext<'a> {
pre_accounts, pre_accounts,
accounts, accounts,
programs, programs,
sysvars,
logger: Rc::new(RefCell::new(ThisLogger { log_collector })), logger: Rc::new(RefCell::new(ThisLogger { log_collector })),
bpf_compute_budget, bpf_compute_budget,
compute_meter, compute_meter,
@ -428,6 +432,9 @@ impl<'a> ThisInvokeContext<'a> {
instruction_recorder, instruction_recorder,
feature_set, feature_set,
timings: ExecuteDetailsTimings::default(), timings: ExecuteDetailsTimings::default(),
account_db,
ancestors,
sysvars: RefCell::new(vec![]),
return_data: None, return_data: None,
}; };
invoke_context invoke_context
@ -600,8 +607,25 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
self.timings.execute_us += execute_us; self.timings.execute_us += execute_us;
self.timings.deserialize_us += deserialize_us; self.timings.deserialize_us += deserialize_us;
} }
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)] { fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
self.sysvars if let Ok(mut sysvars) = self.sysvars.try_borrow_mut() {
// Try share from cache
let mut result = sysvars
.iter()
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None });
if result.is_none() {
// Load it
result = self
.account_db
.load_with_fixed_root(self.ancestors, id)
.map(|(account, _)| Rc::new(account.data().to_vec()));
// Cache it
sysvars.push((*id, result.clone()));
}
result
} else {
None
}
} }
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) { fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) {
self.return_data = return_data; self.return_data = return_data;
@ -1274,7 +1298,6 @@ impl MessageProcessor {
executable_accounts: &[TransactionAccountRefCell], executable_accounts: &[TransactionAccountRefCell],
accounts: &[TransactionAccountRefCell], accounts: &[TransactionAccountRefCell],
rent_collector: &RentCollector, rent_collector: &RentCollector,
sysvars: &[(Pubkey, Vec<u8>)],
log_collector: Option<Rc<LogCollector>>, log_collector: Option<Rc<LogCollector>>,
executors: Rc<RefCell<Executors>>, executors: Rc<RefCell<Executors>>,
instruction_recorder: Option<InstructionRecorder>, instruction_recorder: Option<InstructionRecorder>,
@ -1283,6 +1306,8 @@ impl MessageProcessor {
bpf_compute_budget: BpfComputeBudget, bpf_compute_budget: BpfComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>, compute_meter: Rc<RefCell<dyn ComputeMeter>>,
timings: &mut ExecuteDetailsTimings, timings: &mut ExecuteDetailsTimings,
account_db: Arc<Accounts>,
ancestors: &Ancestors,
) -> Result<(), InstructionError> { ) -> Result<(), InstructionError> {
// Fixup the special instructions key if present // Fixup the special instructions key if present
// before the account pre-values are taken care of // before the account pre-values are taken care of
@ -1324,13 +1349,14 @@ impl MessageProcessor {
executable_accounts, executable_accounts,
accounts, accounts,
&self.programs, &self.programs,
sysvars,
log_collector, log_collector,
bpf_compute_budget, bpf_compute_budget,
compute_meter, compute_meter,
executors, executors,
instruction_recorder, instruction_recorder,
feature_set, feature_set,
account_db,
ancestors,
); );
let pre_remaining_units = invoke_context.get_compute_meter().borrow().get_remaining(); let pre_remaining_units = invoke_context.get_compute_meter().borrow().get_remaining();
let mut time = Measure::start("execute_instruction"); let mut time = Measure::start("execute_instruction");
@ -1385,7 +1411,8 @@ impl MessageProcessor {
bpf_compute_budget: BpfComputeBudget, bpf_compute_budget: BpfComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>, compute_meter: Rc<RefCell<dyn ComputeMeter>>,
timings: &mut ExecuteDetailsTimings, timings: &mut ExecuteDetailsTimings,
sysvars: &[(Pubkey, Vec<u8>)], account_db: Arc<Accounts>,
ancestors: &Ancestors,
) -> Result<(), TransactionError> { ) -> Result<(), TransactionError> {
for (instruction_index, instruction) in message.instructions.iter().enumerate() { for (instruction_index, instruction) in message.instructions.iter().enumerate() {
let instruction_recorder = instruction_recorders let instruction_recorder = instruction_recorders
@ -1398,7 +1425,6 @@ impl MessageProcessor {
&loaders[instruction_index], &loaders[instruction_index],
accounts, accounts,
rent_collector, rent_collector,
sysvars,
log_collector.clone(), log_collector.clone(),
executors.clone(), executors.clone(),
instruction_recorder, instruction_recorder,
@ -1407,6 +1433,8 @@ impl MessageProcessor {
bpf_compute_budget, bpf_compute_budget,
compute_meter.clone(), compute_meter.clone(),
timings, timings,
account_db.clone(),
ancestors,
) )
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err)); .map_err(|err| TransactionError::InstructionError(instruction_index as u8, err));
@ -1464,6 +1492,7 @@ mod tests {
&[Instruction::new_with_bytes(invoke_stack[0], &[0], metas)], &[Instruction::new_with_bytes(invoke_stack[0], &[0], metas)],
None, None,
); );
let ancestors = Ancestors::default();
let mut invoke_context = ThisInvokeContext::new( let mut invoke_context = ThisInvokeContext::new(
&invoke_stack[0], &invoke_stack[0],
Rent::default(), Rent::default(),
@ -1472,13 +1501,14 @@ mod tests {
&[], &[],
&accounts, &accounts,
&[], &[],
&[],
None, None,
BpfComputeBudget::default(), BpfComputeBudget::default(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
None, None,
Arc::new(FeatureSet::all_enabled()), Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
); );
// Check call depth increases and has a limit // Check call depth increases and has a limit
@ -2062,6 +2092,7 @@ mod tests {
let loaders = vec![vec![(mock_system_program_id, account)]]; let loaders = vec![vec![(mock_system_program_id, account)]];
let executors = Rc::new(RefCell::new(Executors::default())); let executors = Rc::new(RefCell::new(Executors::default()));
let ancestors = Ancestors::default();
let account_metas = vec![ let account_metas = vec![
AccountMeta::new(accounts[0].0, true), AccountMeta::new(accounts[0].0, true),
@ -2088,7 +2119,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!(result, Ok(())); assert_eq!(result, Ok(()));
assert_eq!(accounts[0].1.borrow().lamports(), 100); assert_eq!(accounts[0].1.borrow().lamports(), 100);
@ -2115,7 +2147,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!( assert_eq!(
result, result,
@ -2146,7 +2179,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!( assert_eq!(
result, result,
@ -2240,6 +2274,7 @@ mod tests {
let loaders = vec![vec![(mock_program_id, account)]]; let loaders = vec![vec![(mock_program_id, account)]];
let executors = Rc::new(RefCell::new(Executors::default())); let executors = Rc::new(RefCell::new(Executors::default()));
let ancestors = Ancestors::default();
let account_metas = vec![ let account_metas = vec![
AccountMeta::new(accounts[0].0, true), AccountMeta::new(accounts[0].0, true),
@ -2268,7 +2303,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!( assert_eq!(
result, result,
@ -2299,7 +2335,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!(result, Ok(())); assert_eq!(result, Ok(()));
@ -2315,6 +2352,7 @@ mod tests {
)], )],
Some(&accounts[0].0), Some(&accounts[0].0),
); );
let ancestors = Ancestors::default();
let result = message_processor.process_message( let result = message_processor.process_message(
&message, &message,
&loaders, &loaders,
@ -2327,7 +2365,8 @@ mod tests {
BpfComputeBudget::new(), BpfComputeBudget::new(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
&mut ExecuteDetailsTimings::default(), &mut ExecuteDetailsTimings::default(),
&[], Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!(result, Ok(())); assert_eq!(result, Ok(()));
assert_eq!(accounts[0].1.borrow().lamports(), 80); assert_eq!(accounts[0].1.borrow().lamports(), 80);
@ -2428,6 +2467,7 @@ mod tests {
let feature_set = FeatureSet::all_enabled(); let feature_set = FeatureSet::all_enabled();
let demote_program_write_locks = feature_set.is_active(&demote_program_write_locks::id()); let demote_program_write_locks = feature_set.is_active(&demote_program_write_locks::id());
let ancestors = Ancestors::default();
let mut invoke_context = ThisInvokeContext::new( let mut invoke_context = ThisInvokeContext::new(
&caller_program_id, &caller_program_id,
Rent::default(), Rent::default(),
@ -2436,13 +2476,14 @@ mod tests {
&executable_accounts, &executable_accounts,
&accounts, &accounts,
programs.as_slice(), programs.as_slice(),
&[],
None, None,
BpfComputeBudget::default(), BpfComputeBudget::default(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
None, None,
Arc::new(feature_set), Arc::new(feature_set),
Arc::new(Accounts::default()),
&ancestors,
); );
// not owned account modified by the caller (before the invoke) // not owned account modified by the caller (before the invoke)
@ -2497,6 +2538,7 @@ mod tests {
Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone()); Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone());
let message = Message::new(&[callee_instruction], None); let message = Message::new(&[callee_instruction], None);
let ancestors = Ancestors::default();
let mut invoke_context = ThisInvokeContext::new( let mut invoke_context = ThisInvokeContext::new(
&caller_program_id, &caller_program_id,
Rent::default(), Rent::default(),
@ -2505,13 +2547,14 @@ mod tests {
&executable_accounts, &executable_accounts,
&accounts, &accounts,
programs.as_slice(), programs.as_slice(),
&[],
None, None,
BpfComputeBudget::default(), BpfComputeBudget::default(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
None, None,
Arc::new(FeatureSet::all_enabled()), Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
); );
let caller_write_privileges = message let caller_write_privileges = message
@ -2648,6 +2691,7 @@ mod tests {
); );
let message = Message::new(&[callee_instruction.clone()], None); let message = Message::new(&[callee_instruction.clone()], None);
let ancestors = Ancestors::default();
let mut invoke_context = ThisInvokeContext::new( let mut invoke_context = ThisInvokeContext::new(
&caller_program_id, &caller_program_id,
Rent::default(), Rent::default(),
@ -2656,13 +2700,14 @@ mod tests {
&executable_accounts, &executable_accounts,
&accounts, &accounts,
programs.as_slice(), programs.as_slice(),
&[],
None, None,
BpfComputeBudget::default(), BpfComputeBudget::default(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
None, None,
Arc::new(FeatureSet::all_enabled()), Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
); );
// not owned account modified by the invoker // not owned account modified by the invoker
@ -2713,6 +2758,7 @@ mod tests {
Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone()); Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone());
let message = Message::new(&[callee_instruction.clone()], None); let message = Message::new(&[callee_instruction.clone()], None);
let ancestors = Ancestors::default();
let mut invoke_context = ThisInvokeContext::new( let mut invoke_context = ThisInvokeContext::new(
&caller_program_id, &caller_program_id,
Rent::default(), Rent::default(),
@ -2721,13 +2767,14 @@ mod tests {
&executable_accounts, &executable_accounts,
&accounts, &accounts,
programs.as_slice(), programs.as_slice(),
&[],
None, None,
BpfComputeBudget::default(), BpfComputeBudget::default(),
Rc::new(RefCell::new(MockComputeMeter::default())), Rc::new(RefCell::new(MockComputeMeter::default())),
Rc::new(RefCell::new(Executors::default())), Rc::new(RefCell::new(Executors::default())),
None, None,
Arc::new(FeatureSet::all_enabled()), Arc::new(FeatureSet::all_enabled()),
Arc::new(Accounts::default()),
&ancestors,
); );
assert_eq!( assert_eq!(

View File

@ -1,7 +1,6 @@
//! named accounts for synthesized data accounts for bank state, etc. //! named accounts for synthesized data accounts for bank state, etc.
//! //!
use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey}; use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
use lazy_static::lazy_static;
pub mod clock; pub mod clock;
pub mod epoch_schedule; pub mod epoch_schedule;
@ -14,25 +13,17 @@ pub mod slot_hashes;
pub mod slot_history; pub mod slot_history;
pub mod stake_history; pub mod stake_history;
lazy_static! {
pub static ref ALL_IDS: Vec<Pubkey> = vec![
clock::id(),
epoch_schedule::id(),
#[allow(deprecated)]
fees::id(),
#[allow(deprecated)]
recent_blockhashes::id(),
rent::id(),
rewards::id(),
slot_hashes::id(),
slot_history::id(),
stake_history::id(),
instructions::id(),
];
}
pub fn is_sysvar_id(id: &Pubkey) -> bool { pub fn is_sysvar_id(id: &Pubkey) -> bool {
ALL_IDS.iter().any(|key| key == id) clock::check_id(id)
|| epoch_schedule::check_id(id)
|| fees::check_id(id)
|| recent_blockhashes::check_id(id)
|| rent::check_id(id)
|| rewards::check_id(id)
|| slot_hashes::check_id(id)
|| slot_history::check_id(id)
|| stake_history::check_id(id)
|| instructions::check_id(id)
} }
#[macro_export] #[macro_export]

View File

@ -103,8 +103,8 @@ pub trait InvokeContext {
execute_us: u64, execute_us: u64,
deserialize_us: u64, deserialize_us: u64,
); );
/// Get sysvars /// Get sysvar data
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)]; fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>;
/// Set the return data /// Set the return data
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>); fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>);
/// Get the return data /// Get the return data
@ -145,20 +145,15 @@ pub fn get_sysvar<T: Sysvar>(
invoke_context: &dyn InvokeContext, invoke_context: &dyn InvokeContext,
id: &Pubkey, id: &Pubkey,
) -> Result<T, InstructionError> { ) -> Result<T, InstructionError> {
invoke_context let sysvar_data = invoke_context.get_sysvar_data(id).ok_or_else(|| {
.get_sysvars() ic_msg!(invoke_context, "Unable to get sysvar {}", id);
.iter() InstructionError::UnsupportedSysvar
.find_map(|(key, data)| { })?;
if id == key {
bincode::deserialize(data).ok() bincode::deserialize(&sysvar_data).map_err(|err| {
} else { ic_msg!(invoke_context, "Unable to get sysvar {}: {:?}", id, err);
None InstructionError::UnsupportedSysvar
} })
})
.ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get sysvar {}", id);
InstructionError::UnsupportedSysvar
})
} }
#[derive(Clone, Copy, Debug, AbiExample, PartialEq)] #[derive(Clone, Copy, Debug, AbiExample, PartialEq)]
@ -391,7 +386,7 @@ pub struct MockInvokeContext<'a> {
pub compute_meter: MockComputeMeter, pub compute_meter: MockComputeMeter,
pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>, pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>,
pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>, pub accounts: Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>,
pub sysvars: Vec<(Pubkey, Vec<u8>)>, pub sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
pub disabled_features: HashSet<Pubkey>, pub disabled_features: HashSet<Pubkey>,
pub return_data: Option<(Pubkey, Vec<u8>)>, pub return_data: Option<(Pubkey, Vec<u8>)>,
} }
@ -422,6 +417,21 @@ impl<'a> MockInvokeContext<'a> {
} }
} }
pub fn mock_set_sysvar<T: Sysvar>(
mock_invoke_context: &mut MockInvokeContext,
id: Pubkey,
sysvar: T,
) -> Result<(), InstructionError> {
let mut data = Vec::with_capacity(T::size_of());
bincode::serialize_into(&mut data, &sysvar).map_err(|err| {
ic_msg!(mock_invoke_context, "Unable to serialize sysvar: {:?}", err);
InstructionError::GenericError
})?;
mock_invoke_context.sysvars.push((id, Some(Rc::new(data))));
Ok(())
}
impl<'a> InvokeContext for MockInvokeContext<'a> { impl<'a> InvokeContext for MockInvokeContext<'a> {
fn push( fn push(
&mut self, &mut self,
@ -509,8 +519,10 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
_deserialize_us: u64, _deserialize_us: u64,
) { ) {
} }
fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)] { fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
&self.sysvars self.sysvars
.iter()
.find_map(|(key, sysvar)| if id == key { sysvar.clone() } else { None })
} }
fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) { fn set_return_data(&mut self, return_data: Option<(Pubkey, Vec<u8>)>) {
self.return_data = return_data; self.return_data = return_data;