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

* Stop caching sysvars, instead load them ahead of time. (#21108)

(cherry picked from commit 29ad081555)

# Conflicts:
#	programs/bpf/tests/programs.rs
#	programs/bpf_loader/src/syscalls.rs
#	programs/stake/src/stake_instruction.rs
#	runtime/src/bank.rs
#	runtime/src/message_processor.rs
#	sdk/program/src/sysvar/mod.rs
#	sdk/src/process_instruction.rs

* resolve conflicts

Co-authored-by: Alexander Meißner <AlexanderMeissner@gmx.net>
Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
mergify[bot]
2022-01-13 17:51:13 +00:00
committed by GitHub
parent 42531a11a5
commit 5d3f3bc9b1
8 changed files with 112 additions and 167 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::{
stable_log, BpfComputeBudget, InvokeContext, ProcessInstructionWithContext, self, 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,24 +196,6 @@ 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()
@ -224,8 +206,13 @@ 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) {
SUCCESS Ok(sysvar_data) => unsafe {
*(var_addr as *mut _ as *mut T) = sysvar_data;
SUCCESS
},
Err(_) => UNSUPPORTED_SYSVAR,
}
} }
struct SyscallStubs {} struct SyscallStubs {}

View File

@ -3650,9 +3650,7 @@ 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 invoke_context.sysvars = vec![(sysvar::clock::id(), data)];
.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)),
@ -3695,9 +3693,7 @@ 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 invoke_context.sysvars = vec![(sysvar::epoch_schedule::id(), data)];
.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)),
@ -3746,9 +3742,7 @@ 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 invoke_context.sysvars = vec![(sysvar::fees::id(), data)];
.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)),
@ -3789,9 +3783,7 @@ 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 invoke_context.sysvars = vec![(sysvar::rent::id(), data)];
.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::{mock_set_sysvar, MockInvokeContext}, process_instruction::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::{stake_history::StakeHistory, Sysvar},
}, },
std::{cell::RefCell, rc::Rc, str::FromStr}, std::{cell::RefCell, str::FromStr},
}; };
fn create_default_account() -> RefCell<AccountSharedData> { fn create_default_account() -> RefCell<AccountSharedData> {
@ -370,12 +370,9 @@ mod tests {
.collect(); .collect();
let mut invoke_context = MockInvokeContext::new(keyed_accounts); let mut invoke_context = MockInvokeContext::new(keyed_accounts);
mock_set_sysvar( let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
&mut invoke_context, bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
sysvar::clock::id(), invoke_context.sysvars = vec![(sysvar::clock::id(), data)];
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)
} }
} }
@ -1038,12 +1035,9 @@ mod tests {
]; ];
let mut invoke_context = MockInvokeContext::new(keyed_accounts); let mut invoke_context = MockInvokeContext::new(keyed_accounts);
let clock = Clock::default(); let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
let mut data = vec![]; bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
bincode::serialize_into(&mut data, &clock).unwrap(); invoke_context.sysvars = vec![(sysvar::clock::id(), data)];
invoke_context
.sysvars
.push((sysvar::clock::id(), Some(Rc::new(data))));
assert_eq!( assert_eq!(
super::process_instruction( super::process_instruction(

View File

@ -402,8 +402,9 @@ mod tests {
bincode::serialize, bincode::serialize,
solana_sdk::{ solana_sdk::{
account::{self, Account, AccountSharedData}, account::{self, Account, AccountSharedData},
process_instruction::{mock_set_sysvar, MockInvokeContext}, process_instruction::MockInvokeContext,
rent::Rent, rent::Rent,
sysvar::Sysvar,
}, },
std::{cell::RefCell, str::FromStr}, std::{cell::RefCell, str::FromStr},
}; };
@ -462,12 +463,9 @@ 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);
mock_set_sysvar( let mut data = Vec::with_capacity(sysvar::rent::Rent::size_of());
&mut invoke_context, bincode::serialize_into(&mut data, &sysvar::rent::Rent::default()).unwrap();
sysvar::rent::id(), invoke_context.sysvars = vec![(sysvar::rent::id(), data)];
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,6 +1184,8 @@ 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 {
@ -1316,6 +1318,7 @@ 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
} }
@ -1456,6 +1459,7 @@ 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());
@ -1497,6 +1501,7 @@ 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();
@ -1631,6 +1636,7 @@ 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,
@ -1806,6 +1812,14 @@ 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(
@ -1943,6 +1957,17 @@ 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()
} }
@ -3598,8 +3623,7 @@ impl Bank {
bpf_compute_budget, bpf_compute_budget,
compute_meter, compute_meter,
&mut timings.details, &mut timings.details,
self.rc.accounts.clone(), &*self.sysvar_cache.read().unwrap(),
&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,8 +1,7 @@
use { use {
crate::{ crate::{
accounts::Accounts, ancestors::Ancestors, bank::TransactionAccountRefCell, bank::TransactionAccountRefCell, instruction_recorder::InstructionRecorder,
instruction_recorder::InstructionRecorder, log_collector::LogCollector, log_collector::LogCollector, native_loader::NativeLoader, rent_collector::RentCollector,
native_loader::NativeLoader, rent_collector::RentCollector,
}, },
log::*, log::*,
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
@ -371,6 +370,7 @@ 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,10 +378,6 @@ 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>)>,
} }
@ -395,14 +391,13 @@ 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(
@ -425,6 +420,7 @@ 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,
@ -432,9 +428,6 @@ 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
@ -607,25 +600,8 @@ 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_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> { fn get_sysvars(&self) -> &[(Pubkey, Vec<u8>)] {
if let Ok(mut sysvars) = self.sysvars.try_borrow_mut() { self.sysvars
// 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;
@ -1298,6 +1274,7 @@ 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>,
@ -1306,8 +1283,6 @@ 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
@ -1349,14 +1324,13 @@ 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");
@ -1411,8 +1385,7 @@ 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>, sysvars: &[(Pubkey, Vec<u8>)],
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
@ -1425,6 +1398,7 @@ 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,
@ -1433,8 +1407,6 @@ 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));
@ -1492,7 +1464,6 @@ 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(),
@ -1501,14 +1472,13 @@ 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
@ -2092,7 +2062,6 @@ 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),
@ -2119,8 +2088,7 @@ 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);
@ -2147,8 +2115,7 @@ 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,
@ -2179,8 +2146,7 @@ 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,
@ -2274,7 +2240,6 @@ 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),
@ -2303,8 +2268,7 @@ 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,
@ -2335,8 +2299,7 @@ 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(()));
@ -2352,7 +2315,6 @@ 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,
@ -2365,8 +2327,7 @@ 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);
@ -2467,7 +2428,6 @@ 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(),
@ -2476,14 +2436,13 @@ 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)
@ -2538,7 +2497,6 @@ 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(),
@ -2547,14 +2505,13 @@ 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
@ -2691,7 +2648,6 @@ 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(),
@ -2700,14 +2656,13 @@ 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
@ -2758,7 +2713,6 @@ 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(),
@ -2767,14 +2721,13 @@ 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,6 +1,7 @@
//! 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;
@ -13,17 +14,25 @@ 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 {
clock::check_id(id) ALL_IDS.iter().any(|key| key == 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 sysvar data /// Get sysvars
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>>; fn get_sysvars(&self) -> &[(Pubkey, 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,15 +145,20 @@ pub fn get_sysvar<T: Sysvar>(
invoke_context: &dyn InvokeContext, invoke_context: &dyn InvokeContext,
id: &Pubkey, id: &Pubkey,
) -> Result<T, InstructionError> { ) -> Result<T, InstructionError> {
let sysvar_data = invoke_context.get_sysvar_data(id).ok_or_else(|| { invoke_context
ic_msg!(invoke_context, "Unable to get sysvar {}", id); .get_sysvars()
InstructionError::UnsupportedSysvar .iter()
})?; .find_map(|(key, data)| {
if id == key {
bincode::deserialize(&sysvar_data).map_err(|err| { bincode::deserialize(data).ok()
ic_msg!(invoke_context, "Unable to get sysvar {}: {:?}", id, err); } else {
InstructionError::UnsupportedSysvar None
}) }
})
.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)]
@ -386,7 +391,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, Option<Rc<Vec<u8>>>)>, pub sysvars: Vec<(Pubkey, 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>)>,
} }
@ -417,21 +422,6 @@ 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,
@ -519,10 +509,8 @@ impl<'a> InvokeContext for MockInvokeContext<'a> {
_deserialize_us: u64, _deserialize_us: u64,
) { ) {
} }
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> { fn get_sysvars(&self) -> &[(Pubkey, 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;