InvokeContext: Add get_sysvar() helper to sdk (backport #17360) (#17368)

* Add get_sysvar() helper to sdk

(cherry picked from commit 2c99b23ad7)

# Conflicts:
#	runtime/src/message_processor.rs
#	sdk/src/process_instruction.rs

* Resolve conflicts

Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
mergify[bot]
2021-05-21 03:42:20 +00:00
committed by GitHub
parent b1d294de75
commit 3f6964d264
3 changed files with 58 additions and 29 deletions

View File

@@ -270,7 +270,8 @@ pub struct ThisInvokeContext<'a> {
pub timings: ExecuteDetailsTimings,
account_db: Arc<Accounts>,
ancestors: &'a Ancestors,
sysvars: Vec<(Pubkey, Option<Rc<Vec<u8>>>)>,
#[allow(clippy::type_complexity)]
sysvars: RefCell<Vec<(Pubkey, Option<Rc<Vec<u8>>>)>>,
}
impl<'a> ThisInvokeContext<'a> {
#[allow(clippy::too_many_arguments)]
@@ -309,7 +310,7 @@ impl<'a> ThisInvokeContext<'a> {
timings: ExecuteDetailsTimings::default(),
account_db,
ancestors,
sysvars: vec![],
sysvars: RefCell::new(vec![]),
}
}
}
@@ -429,22 +430,25 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
self.timings.execute_us += execute_us;
self.timings.deserialize_us += deserialize_us;
}
fn get_sysvar_data(&mut self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
// Try share from cache
let mut result =
self.sysvars
fn get_sysvar_data(&self, id: &Pubkey) -> Option<Rc<Vec<u8>>> {
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_slow(self.ancestors, id)
.map(|(account, _)| Rc::new(account.data().clone()));
// Cache it
self.sysvars.push((*id, result.clone()));
if result.is_none() {
// Load it
result = self
.account_db
.load_slow(self.ancestors, id)
.map(|(account, _)| Rc::new(account.data().clone()));
// Cache it
sysvars.push((*id, result.clone()));
}
result
} else {
None
}
result
}
}
pub struct ThisLogger {