Fix pr crossing for sysvar keyed-accounts (#13189)

This commit is contained in:
Jack May 2020-10-26 21:59:32 -07:00 committed by GitHub
parent c88ec9657b
commit 26eba5ac7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 28 deletions

View File

@ -94,7 +94,7 @@ mod tests {
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct TestSysvar {
struct TestSysvar {
something: Pubkey,
}
crate::declare_id!("TestSysvar111111111111111111111111111111111");
@ -138,31 +138,4 @@ mod tests {
account_info.data = Rc::new(RefCell::new(&mut small_data));
assert_eq!(test_sysvar.to_account_info(&mut account_info), None);
}
#[test]
fn test_sysvar_keyed_account_to_from() {
let test_sysvar = TestSysvar::default();
let key = crate::sysvar::tests::id();
let wrong_key = Pubkey::new_unique();
let account = test_sysvar.create_account(42);
let test_sysvar = TestSysvar::from_account(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let mut account = Account::new(42, TestSysvar::size_of(), &key);
test_sysvar.to_account(&mut account).unwrap();
let test_sysvar = TestSysvar::from_account(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let account = RefCell::new(account);
let keyed_account = KeyedAccount::new(&key, false, &account);
let new_test_sysvar = TestSysvar::from_keyed_account(&keyed_account).unwrap();
assert_eq!(test_sysvar, new_test_sysvar);
let keyed_account = KeyedAccount::new(&wrong_key, false, &account);
assert_eq!(
TestSysvar::from_keyed_account(&keyed_account),
Err(InstructionError::InvalidArgument)
);
}
}

View File

@ -217,3 +217,50 @@ pub fn from_keyed_account<S: Sysvar>(
}
S::from_account(&*keyed_account.try_account_ref()?).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pubkey::Pubkey;
use std::cell::RefCell;
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
struct TestSysvar {
something: Pubkey,
}
crate::declare_id!("TestSysvar111111111111111111111111111111111");
impl solana_program::sysvar::SysvarId for TestSysvar {
fn check_id(pubkey: &crate::pubkey::Pubkey) -> bool {
check_id(pubkey)
}
}
impl Sysvar for TestSysvar {}
#[test]
fn test_sysvar_keyed_account_to_from() {
let test_sysvar = TestSysvar::default();
let key = crate::keyed_account::tests::id();
let wrong_key = Pubkey::new_unique();
let account = test_sysvar.create_account(42);
let test_sysvar = TestSysvar::from_account(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let mut account = Account::new(42, TestSysvar::size_of(), &key);
test_sysvar.to_account(&mut account).unwrap();
let test_sysvar = TestSysvar::from_account(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default());
let account = RefCell::new(account);
let keyed_account = KeyedAccount::new(&key, false, &account);
let new_test_sysvar = from_keyed_account::<TestSysvar>(&keyed_account).unwrap();
assert_eq!(test_sysvar, new_test_sysvar);
let keyed_account = KeyedAccount::new(&wrong_key, false, &account);
assert_eq!(
from_keyed_account::<TestSysvar>(&keyed_account),
Err(InstructionError::InvalidArgument)
);
}
}