Fetch sysvars from invoke context for vote program (backport #22444) (#22469)

* Fetch sysvars from invoke context for vote program (#22444)

* resolve conflicts

Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
mergify[bot]
2022-01-15 03:56:00 +00:00
committed by GitHub
parent 054e475c6c
commit a7623ad18c
2 changed files with 68 additions and 46 deletions

View File

@ -7,6 +7,7 @@ use {
std::{
cell::{Ref, RefCell, RefMut},
iter::FromIterator,
ops::Deref,
rc::Rc,
},
};
@ -248,14 +249,20 @@ where
}
}
pub fn from_keyed_account<S: Sysvar>(
keyed_account: &crate::keyed_account::KeyedAccount,
) -> Result<S, InstructionError> {
pub fn check_sysvar_keyed_account<'a, S: Sysvar>(
keyed_account: &'a crate::keyed_account::KeyedAccount<'_>,
) -> Result<impl Deref<Target = AccountSharedData> + 'a, InstructionError> {
if !S::check_id(keyed_account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
from_account::<S, AccountSharedData>(&*keyed_account.try_account_ref()?)
.ok_or(InstructionError::InvalidArgument)
keyed_account.try_account_ref()
}
pub fn from_keyed_account<S: Sysvar>(
keyed_account: &crate::keyed_account::KeyedAccount,
) -> Result<S, InstructionError> {
let sysvar_account = check_sysvar_keyed_account::<S>(keyed_account)?;
from_account::<S, AccountSharedData>(&*sysvar_account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]