Refactor: Removes Rc
from Refcell<AccountSharedData>
in the program-runtime (#21927)
* Removes Rc from Rc<RefCell<AccountSharedData>> in the program-runtime. * Adjusts tests in bpf_loader, system_instruction_processor, config_processor, vote_instruction and stake_instruction
This commit is contained in:
committed by
GitHub
parent
56ec5245cc
commit
66fa8f9667
File diff suppressed because it is too large
Load Diff
@ -322,6 +322,7 @@ mod tests {
|
||||
account_info::AccountInfo,
|
||||
bpf_loader,
|
||||
entrypoint::deserialize,
|
||||
instruction::AccountMeta,
|
||||
},
|
||||
std::{
|
||||
cell::RefCell,
|
||||
@ -333,122 +334,128 @@ mod tests {
|
||||
#[test]
|
||||
fn test_serialize_parameters() {
|
||||
let program_id = solana_sdk::pubkey::new_rand();
|
||||
let dup_key = solana_sdk::pubkey::new_rand();
|
||||
let dup_key2 = solana_sdk::pubkey::new_rand();
|
||||
let keyed_accounts = [
|
||||
let transaction_accounts = vec![
|
||||
(
|
||||
false,
|
||||
false,
|
||||
program_id,
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 0,
|
||||
data: vec![],
|
||||
owner: bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 0,
|
||||
}))),
|
||||
}),
|
||||
),
|
||||
(
|
||||
false,
|
||||
false,
|
||||
dup_key,
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
lamports: 1,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}))),
|
||||
),
|
||||
(
|
||||
false,
|
||||
false,
|
||||
dup_key,
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
lamports: 1,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}))),
|
||||
),
|
||||
(
|
||||
false,
|
||||
false,
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 1,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}),
|
||||
),
|
||||
(
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 2,
|
||||
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
||||
owner: bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 200,
|
||||
}))),
|
||||
}),
|
||||
),
|
||||
(
|
||||
false,
|
||||
false,
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 3,
|
||||
data: vec![],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 3100,
|
||||
}))),
|
||||
}),
|
||||
),
|
||||
(
|
||||
false,
|
||||
true,
|
||||
dup_key2,
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
lamports: 4,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}))),
|
||||
),
|
||||
(
|
||||
false,
|
||||
true,
|
||||
dup_key2,
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
lamports: 4,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}))),
|
||||
),
|
||||
(
|
||||
false,
|
||||
true,
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 4,
|
||||
data: vec![1u8, 2, 3, 4, 5],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 100,
|
||||
}),
|
||||
),
|
||||
(
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 5,
|
||||
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
||||
owner: bpf_loader::id(),
|
||||
executable: true,
|
||||
rent_epoch: 200,
|
||||
}))),
|
||||
}),
|
||||
),
|
||||
(
|
||||
false,
|
||||
true,
|
||||
solana_sdk::pubkey::new_rand(),
|
||||
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
||||
AccountSharedData::from(Account {
|
||||
lamports: 6,
|
||||
data: vec![],
|
||||
owner: bpf_loader::id(),
|
||||
executable: false,
|
||||
rent_epoch: 3100,
|
||||
}))),
|
||||
}),
|
||||
),
|
||||
];
|
||||
let instruction_accounts = vec![
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[1].0,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[1].0,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[2].0,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[3].0,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[4].0,
|
||||
is_signer: false,
|
||||
is_writable: true,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[4].0,
|
||||
is_signer: false,
|
||||
is_writable: true,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[5].0,
|
||||
is_signer: false,
|
||||
is_writable: true,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: transaction_accounts[6].0,
|
||||
is_signer: false,
|
||||
is_writable: true,
|
||||
},
|
||||
];
|
||||
let instruction_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
||||
let program_indices = [0];
|
||||
let preparation =
|
||||
prepare_mock_invoke_context(&program_indices, &instruction_data, &keyed_accounts);
|
||||
let preparation = prepare_mock_invoke_context(
|
||||
&program_indices,
|
||||
&instruction_data,
|
||||
transaction_accounts.clone(),
|
||||
instruction_accounts,
|
||||
);
|
||||
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
|
||||
invoke_context
|
||||
.push(
|
||||
@ -479,9 +486,12 @@ mod tests {
|
||||
(&de_instruction_data[0] as *const u8).align_offset(BPF_ALIGN_OF_U128),
|
||||
0
|
||||
);
|
||||
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) {
|
||||
assert_eq!(key, account_info.key);
|
||||
let account = account.borrow();
|
||||
for account_info in de_accounts {
|
||||
let account = &transaction_accounts
|
||||
.iter()
|
||||
.find(|(key, _account)| key == account_info.key)
|
||||
.unwrap()
|
||||
.1;
|
||||
assert_eq!(account.lamports(), account_info.lamports());
|
||||
assert_eq!(account.data(), &account_info.data.borrow()[..]);
|
||||
assert_eq!(account.owner(), account_info.owner);
|
||||
@ -511,12 +521,14 @@ mod tests {
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts)
|
||||
{
|
||||
assert_eq!(key, de_keyed_account.unsigned_key());
|
||||
let account = account.borrow();
|
||||
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
|
||||
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
|
||||
for keyed_account in de_keyed_accounts {
|
||||
let account = &transaction_accounts
|
||||
.iter()
|
||||
.find(|(key, _account)| key == keyed_account.unsigned_key())
|
||||
.unwrap()
|
||||
.1;
|
||||
assert_eq!(account.executable(), keyed_account.executable().unwrap());
|
||||
assert_eq!(account.rent_epoch(), keyed_account.rent_epoch().unwrap());
|
||||
}
|
||||
|
||||
// check serialize_parameters_unaligned
|
||||
@ -534,9 +546,12 @@ mod tests {
|
||||
unsafe { deserialize_unaligned(&mut serialized.as_slice_mut()[0] as *mut u8) };
|
||||
assert_eq!(&program_id, de_program_id);
|
||||
assert_eq!(instruction_data, de_instruction_data);
|
||||
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) {
|
||||
assert_eq!(key, account_info.key);
|
||||
let account = account.borrow();
|
||||
for account_info in de_accounts {
|
||||
let account = &transaction_accounts
|
||||
.iter()
|
||||
.find(|(key, _account)| key == account_info.key)
|
||||
.unwrap()
|
||||
.1;
|
||||
assert_eq!(account.lamports(), account_info.lamports());
|
||||
assert_eq!(account.data(), &account_info.data.borrow()[..]);
|
||||
assert_eq!(account.owner(), account_info.owner);
|
||||
@ -553,18 +568,20 @@ mod tests {
|
||||
true,
|
||||
)
|
||||
.unwrap();
|
||||
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts)
|
||||
{
|
||||
assert_eq!(key, de_keyed_account.unsigned_key());
|
||||
let account = account.borrow();
|
||||
assert_eq!(account.lamports(), de_keyed_account.lamports().unwrap());
|
||||
for keyed_account in de_keyed_accounts {
|
||||
let account = &transaction_accounts
|
||||
.iter()
|
||||
.find(|(key, _account)| key == keyed_account.unsigned_key())
|
||||
.unwrap()
|
||||
.1;
|
||||
assert_eq!(account.lamports(), keyed_account.lamports().unwrap());
|
||||
assert_eq!(
|
||||
account.data(),
|
||||
de_keyed_account.try_account_ref().unwrap().data()
|
||||
keyed_account.try_account_ref().unwrap().data()
|
||||
);
|
||||
assert_eq!(*account.owner(), de_keyed_account.owner().unwrap());
|
||||
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
|
||||
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
|
||||
assert_eq!(*account.owner(), keyed_account.owner().unwrap());
|
||||
assert_eq!(account.executable(), keyed_account.executable().unwrap());
|
||||
assert_eq!(account.rent_epoch(), keyed_account.rent_epoch().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2980,7 +2980,7 @@ mod tests {
|
||||
#[should_panic(expected = "UserError(SyscallError(Panic(\"Gaggablaghblagh!\", 42, 84)))")]
|
||||
fn test_syscall_sol_panic() {
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3057,7 +3057,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_syscall_sol_log() {
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3161,7 +3161,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_syscall_sol_log_u64() {
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3203,7 +3203,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_syscall_sol_pubkey() {
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3415,7 +3415,8 @@ mod tests {
|
||||
fn test_syscall_sha256() {
|
||||
let config = Config::default();
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader_deprecated::id());
|
||||
let program_account =
|
||||
RefCell::new(AccountSharedData::new(0, 0, &bpf_loader_deprecated::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3544,7 +3545,7 @@ mod tests {
|
||||
fn test_syscall_get_sysvar() {
|
||||
let config = Config::default();
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3854,7 +3855,7 @@ mod tests {
|
||||
// These tests duplicate the direct tests in solana_program::pubkey
|
||||
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
@ -3970,7 +3971,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_find_program_address() {
|
||||
let program_id = Pubkey::new_unique();
|
||||
let program_account = AccountSharedData::new_ref(0, 0, &bpf_loader::id());
|
||||
let program_account = RefCell::new(AccountSharedData::new(0, 0, &bpf_loader::id()));
|
||||
let accounts = [(program_id, program_account)];
|
||||
let message = Message::new(
|
||||
&[Instruction::new_with_bytes(program_id, &[], vec![])],
|
||||
|
@ -147,22 +147,26 @@ mod tests {
|
||||
solana_program_runtime::invoke_context::mock_process_instruction,
|
||||
solana_sdk::{
|
||||
account::AccountSharedData,
|
||||
instruction::AccountMeta,
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signer},
|
||||
system_instruction::SystemInstruction,
|
||||
},
|
||||
std::{cell::RefCell, rc::Rc},
|
||||
};
|
||||
|
||||
fn process_instruction(
|
||||
instruction_data: &[u8],
|
||||
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
||||
) -> Result<(), InstructionError> {
|
||||
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
||||
instruction_accounts: Vec<AccountMeta>,
|
||||
expected_result: Result<(), InstructionError>,
|
||||
) -> Vec<AccountSharedData> {
|
||||
mock_process_instruction(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
instruction_data,
|
||||
keyed_accounts,
|
||||
transaction_accounts,
|
||||
instruction_accounts,
|
||||
expected_result,
|
||||
super::process_instruction,
|
||||
)
|
||||
}
|
||||
@ -191,16 +195,12 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_config_account(
|
||||
keys: Vec<(Pubkey, bool)>,
|
||||
) -> (Keypair, Rc<RefCell<AccountSharedData>>) {
|
||||
fn create_config_account(keys: Vec<(Pubkey, bool)>) -> (Keypair, AccountSharedData) {
|
||||
let from_pubkey = Pubkey::new_unique();
|
||||
let config_keypair = Keypair::new();
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
|
||||
let instructions =
|
||||
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, keys);
|
||||
|
||||
let system_instruction = limited_deserialize(&instructions[0].data).unwrap();
|
||||
let space = match system_instruction {
|
||||
SystemInstruction::CreateAccount {
|
||||
@ -210,14 +210,18 @@ mod tests {
|
||||
} => space,
|
||||
_ => panic!("Not a CreateAccount system instruction"),
|
||||
};
|
||||
let config_account = AccountSharedData::new_ref(0, space as usize, &id());
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())];
|
||||
assert_eq!(
|
||||
process_instruction(&instructions[1].data, &keyed_accounts),
|
||||
Ok(())
|
||||
let config_account = AccountSharedData::new(0, space as usize, &id());
|
||||
let accounts = process_instruction(
|
||||
&instructions[1].data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
(config_keypair, config_account)
|
||||
(config_keypair, accounts[0].clone())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -226,7 +230,7 @@ mod tests {
|
||||
let (_, config_account) = create_config_account(vec![]);
|
||||
assert_eq!(
|
||||
Some(MyConfig::default()),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(config_account.data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -239,14 +243,19 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Ok(()),
|
||||
);
|
||||
assert_eq!(
|
||||
Some(my_config),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(accounts[0].data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -260,10 +269,15 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
|
||||
let keyed_accounts = [(true, false, config_pubkey, config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::InvalidInstructionData),
|
||||
);
|
||||
}
|
||||
|
||||
@ -277,10 +291,15 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, vec![], &my_config);
|
||||
instruction.accounts[0].is_signer = false; // <----- not a signer
|
||||
let keyed_accounts = [(false, false, config_pubkey, config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, config_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -298,24 +317,41 @@ mod tests {
|
||||
let (config_keypair, config_account) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
Some(my_config),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
deserialize(get_config_data(accounts[0].data()).unwrap()).ok()
|
||||
);
|
||||
}
|
||||
|
||||
@ -328,13 +364,18 @@ mod tests {
|
||||
let (config_keypair, _) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
let signer0_account = AccountSharedData::new(0, 0, &id());
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &id());
|
||||
let keyed_accounts = [(true, false, signer0_pubkey, signer0_account)];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountData)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(signer0_pubkey, signer0_account)],
|
||||
vec![AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
|
||||
@ -343,30 +384,56 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![(signer0_pubkey, true)];
|
||||
let (config_keypair, config_account) = create_config_account(keys.clone());
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
|
||||
// Config-data pubkey doesn't match signer
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
// Config-data pubkey not a signer
|
||||
keyed_accounts[1] = (false, false, signer0_pubkey, signer0_account);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -377,9 +444,9 @@ mod tests {
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer2_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer2_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer2_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -390,40 +457,98 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
// Update with expected signatures
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
|
||||
keyed_accounts[0].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
new_config,
|
||||
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap())
|
||||
.unwrap()
|
||||
MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
|
||||
);
|
||||
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (signer0_pubkey, true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
keyed_accounts[2].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
// Attempt update with incorrect signatures
|
||||
@ -433,10 +558,31 @@ mod tests {
|
||||
(signer2_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
keyed_accounts[2] = (true, false, signer2_pubkey, signer2_account);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
(signer2_pubkey, signer2_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer2_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -445,7 +591,7 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let config_address = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(config_address, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -457,13 +603,29 @@ mod tests {
|
||||
|
||||
// Attempt initialization with duplicate signer inputs
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -474,8 +636,8 @@ mod tests {
|
||||
let config_address = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer1_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(config_address, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -486,13 +648,30 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let mut keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
(true, false, signer1_pubkey, signer1_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
(signer1_pubkey, signer1_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer1_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
@ -504,9 +683,29 @@ mod tests {
|
||||
(signer0_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, dupe_keys, &new_config);
|
||||
keyed_accounts[2] = keyed_accounts[1].clone();
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -516,7 +715,7 @@ mod tests {
|
||||
solana_logger::setup();
|
||||
let pubkey = Pubkey::new_unique();
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -525,7 +724,6 @@ mod tests {
|
||||
let (config_keypair, config_account) = create_config_account(keys);
|
||||
let config_pubkey = config_keypair.pubkey();
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let keys = vec![
|
||||
(pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -533,37 +731,70 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account.clone()),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account.clone()),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
// Update with expected signatures
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
let accounts = process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, accounts[0].clone()),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
let meta_data: ConfigKeys = deserialize(accounts[0].data()).unwrap();
|
||||
assert_eq!(meta_data.keys, keys);
|
||||
assert_eq!(
|
||||
new_config,
|
||||
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap())
|
||||
.unwrap()
|
||||
MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
|
||||
);
|
||||
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts[0..1]),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![(config_pubkey, accounts[0].clone())],
|
||||
vec![AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
}],
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
}
|
||||
|
||||
@ -574,9 +805,11 @@ mod tests {
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let instructions =
|
||||
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]);
|
||||
assert_eq!(
|
||||
process_instruction(&instructions[1].data, &[]),
|
||||
Err(InstructionError::NotEnoughAccountKeys)
|
||||
process_instruction(
|
||||
&instructions[1].data,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
@ -586,8 +819,8 @@ mod tests {
|
||||
let config_pubkey = Pubkey::new_unique();
|
||||
let new_config = MyConfig::new(84);
|
||||
let signer0_pubkey = Pubkey::new_unique();
|
||||
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let config_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique());
|
||||
let signer0_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let config_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let keys = vec![
|
||||
(from_pubkey, false),
|
||||
@ -596,13 +829,25 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config);
|
||||
let keyed_accounts = [
|
||||
(true, false, config_pubkey, config_account),
|
||||
(true, false, signer0_pubkey, signer0_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(&instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountOwner)
|
||||
process_instruction(
|
||||
&instruction.data,
|
||||
vec![
|
||||
(config_pubkey, config_account),
|
||||
(signer0_pubkey, signer0_account),
|
||||
],
|
||||
vec![
|
||||
AccountMeta {
|
||||
pubkey: config_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: signer0_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
],
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -518,66 +518,67 @@ mod tests {
|
||||
use {
|
||||
super::*,
|
||||
bincode::serialize,
|
||||
solana_program_runtime::invoke_context::mock_process_instruction,
|
||||
solana_program_runtime::invoke_context::{
|
||||
mock_process_instruction, mock_process_instruction_with_sysvars,
|
||||
},
|
||||
solana_sdk::{
|
||||
account::{self, Account, AccountSharedData},
|
||||
rent::Rent,
|
||||
},
|
||||
std::{cell::RefCell, rc::Rc, str::FromStr},
|
||||
std::str::FromStr,
|
||||
};
|
||||
|
||||
fn create_default_account() -> Rc<RefCell<AccountSharedData>> {
|
||||
AccountSharedData::new_ref(0, 0, &Pubkey::new_unique())
|
||||
fn create_default_account() -> AccountSharedData {
|
||||
AccountSharedData::new(0, 0, &Pubkey::new_unique())
|
||||
}
|
||||
|
||||
fn process_instruction(
|
||||
instruction_data: &[u8],
|
||||
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
|
||||
) -> Result<(), InstructionError> {
|
||||
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
|
||||
instruction_accounts: Vec<AccountMeta>,
|
||||
expected_result: Result<(), InstructionError>,
|
||||
) -> Vec<AccountSharedData> {
|
||||
mock_process_instruction(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
instruction_data,
|
||||
keyed_accounts,
|
||||
transaction_accounts,
|
||||
instruction_accounts,
|
||||
expected_result,
|
||||
super::process_instruction,
|
||||
)
|
||||
}
|
||||
|
||||
fn process_instruction_as_one_arg(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
let mut accounts: Vec<_> = instruction
|
||||
fn process_instruction_as_one_arg(
|
||||
instruction: &Instruction,
|
||||
expected_result: Result<(), InstructionError>,
|
||||
) -> Vec<AccountSharedData> {
|
||||
let transaction_accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.map(|meta| {
|
||||
Rc::new(RefCell::new(if sysvar::clock::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&Clock::default())
|
||||
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&SlotHashes::default())
|
||||
} else if sysvar::rent::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&Rent::free())
|
||||
} else if meta.pubkey == invalid_vote_state_pubkey() {
|
||||
AccountSharedData::from(Account {
|
||||
owner: invalid_vote_state_pubkey(),
|
||||
..Account::default()
|
||||
})
|
||||
} else {
|
||||
AccountSharedData::from(Account {
|
||||
owner: id(),
|
||||
..Account::default()
|
||||
})
|
||||
}))
|
||||
(
|
||||
meta.pubkey,
|
||||
if sysvar::clock::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&Clock::default())
|
||||
} else if sysvar::slot_hashes::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&SlotHashes::default())
|
||||
} else if sysvar::rent::check_id(&meta.pubkey) {
|
||||
account::create_account_shared_data_for_test(&Rent::free())
|
||||
} else if meta.pubkey == invalid_vote_state_pubkey() {
|
||||
AccountSharedData::from(Account {
|
||||
owner: invalid_vote_state_pubkey(),
|
||||
..Account::default()
|
||||
})
|
||||
} else {
|
||||
AccountSharedData::from(Account {
|
||||
owner: id(),
|
||||
..Account::default()
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
#[allow(clippy::same_item_push)]
|
||||
for _ in 0..instruction.accounts.len() {
|
||||
accounts.push(AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()));
|
||||
}
|
||||
let keyed_accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
.zip(accounts.into_iter())
|
||||
.map(|(meta, account)| (meta.is_signer, meta.is_writable, meta.pubkey, account))
|
||||
.collect();
|
||||
|
||||
let rent = Rent::default();
|
||||
let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap());
|
||||
let clock = Clock::default();
|
||||
@ -587,11 +588,13 @@ mod tests {
|
||||
sysvar::slot_hashes::id(),
|
||||
bincode::serialize(&slot_hashes).unwrap(),
|
||||
);
|
||||
solana_program_runtime::invoke_context::mock_process_instruction_with_sysvars(
|
||||
mock_process_instruction_with_sysvars(
|
||||
&id(),
|
||||
Vec::new(),
|
||||
&instruction.data,
|
||||
&keyed_accounts,
|
||||
transaction_accounts,
|
||||
instruction.accounts.clone(),
|
||||
expected_result,
|
||||
&[rent_sysvar, clock_sysvar, slot_hashes_sysvar],
|
||||
super::process_instruction,
|
||||
)
|
||||
@ -604,28 +607,30 @@ mod tests {
|
||||
// these are for 100% coverage in this file
|
||||
#[test]
|
||||
fn test_vote_process_instruction_decode_bail() {
|
||||
assert_eq!(
|
||||
process_instruction(&[], &[]),
|
||||
process_instruction(
|
||||
&[],
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spoofed_vote() {
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&vote(
|
||||
process_instruction_as_one_arg(
|
||||
&vote(
|
||||
&invalid_vote_state_pubkey(),
|
||||
&Pubkey::new_unique(),
|
||||
Vote::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&update_vote_state(
|
||||
process_instruction_as_one_arg(
|
||||
&update_vote_state(
|
||||
&invalid_vote_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
VoteStateUpdate::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
}
|
||||
@ -639,79 +644,72 @@ mod tests {
|
||||
&VoteInit::default(),
|
||||
101,
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&instructions[1]),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&vote(
|
||||
process_instruction_as_one_arg(&instructions[1], Err(InstructionError::InvalidAccountData));
|
||||
process_instruction_as_one_arg(
|
||||
&vote(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
Vote::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&vote_switch(
|
||||
process_instruction_as_one_arg(
|
||||
&vote_switch(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
Vote::default(),
|
||||
Hash::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&authorize(
|
||||
process_instruction_as_one_arg(
|
||||
&authorize(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
VoteAuthorize::Voter,
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&update_vote_state(
|
||||
process_instruction_as_one_arg(
|
||||
&update_vote_state(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
VoteStateUpdate::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&update_vote_state_switch(
|
||||
process_instruction_as_one_arg(
|
||||
&update_vote_state_switch(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
VoteStateUpdate::default(),
|
||||
Hash::default(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&update_validator_identity(
|
||||
process_instruction_as_one_arg(
|
||||
&update_validator_identity(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
)),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&update_commission(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
0,
|
||||
)),
|
||||
process_instruction_as_one_arg(
|
||||
&update_commission(&Pubkey::new_unique(), &Pubkey::new_unique(), 0),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&withdraw(
|
||||
process_instruction_as_one_arg(
|
||||
&withdraw(
|
||||
&Pubkey::new_unique(),
|
||||
&Pubkey::new_unique(),
|
||||
0,
|
||||
&Pubkey::new_unique()
|
||||
)),
|
||||
&Pubkey::new_unique(),
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
}
|
||||
@ -730,10 +728,7 @@ mod tests {
|
||||
VoteAuthorize::Voter,
|
||||
);
|
||||
instruction.accounts = instruction.accounts[0..2].to_vec();
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
process_instruction_as_one_arg(&instruction, Err(InstructionError::NotEnoughAccountKeys));
|
||||
|
||||
let mut instruction = authorize_checked(
|
||||
&vote_pubkey,
|
||||
@ -742,10 +737,7 @@ mod tests {
|
||||
VoteAuthorize::Withdrawer,
|
||||
);
|
||||
instruction.accounts = instruction.accounts[0..2].to_vec();
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
process_instruction_as_one_arg(&instruction, Err(InstructionError::NotEnoughAccountKeys));
|
||||
|
||||
// Test with non-signing new_authorized_pubkey
|
||||
let mut instruction = authorize_checked(
|
||||
@ -755,8 +747,8 @@ mod tests {
|
||||
VoteAuthorize::Voter,
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
process_instruction_as_one_arg(
|
||||
&instruction,
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -767,43 +759,60 @@ mod tests {
|
||||
VoteAuthorize::Withdrawer,
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
|
||||
assert_eq!(
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
process_instruction_as_one_arg(
|
||||
&instruction,
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
// Test with new_authorized_pubkey signer
|
||||
let vote_account = AccountSharedData::new_ref(100, VoteState::size_of(), &id());
|
||||
let vote_account = AccountSharedData::new(100, VoteState::size_of(), &id());
|
||||
let clock_address = sysvar::clock::id();
|
||||
let clock_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test(
|
||||
&Clock::default(),
|
||||
)));
|
||||
let clock_account = account::create_account_shared_data_for_test(&Clock::default());
|
||||
let default_authorized_pubkey = Pubkey::default();
|
||||
let authorized_account = create_default_account();
|
||||
let new_authorized_account = create_default_account();
|
||||
let keyed_accounts = [
|
||||
(false, false, vote_pubkey, vote_account),
|
||||
(false, false, clock_address, clock_account),
|
||||
(true, false, default_authorized_pubkey, authorized_account),
|
||||
(true, false, new_authorized_pubkey, new_authorized_account),
|
||||
let transaction_accounts = vec![
|
||||
(vote_pubkey, vote_account),
|
||||
(clock_address, clock_account),
|
||||
(default_authorized_pubkey, authorized_account),
|
||||
(new_authorized_pubkey, new_authorized_account),
|
||||
];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(VoteAuthorize::Voter)).unwrap(),
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(())
|
||||
let instruction_accounts = vec![
|
||||
AccountMeta {
|
||||
pubkey: vote_pubkey,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: clock_address,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: default_authorized_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
AccountMeta {
|
||||
pubkey: new_authorized_pubkey,
|
||||
is_signer: true,
|
||||
is_writable: false,
|
||||
},
|
||||
];
|
||||
process_instruction(
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(VoteAuthorize::Voter)).unwrap(),
|
||||
transaction_accounts.clone(),
|
||||
instruction_accounts.clone(),
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(
|
||||
VoteAuthorize::Withdrawer
|
||||
))
|
||||
.unwrap(),
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(())
|
||||
process_instruction(
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(
|
||||
VoteAuthorize::Withdrawer,
|
||||
))
|
||||
.unwrap(),
|
||||
transaction_accounts,
|
||||
instruction_accounts,
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user