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:
Alexander Meißner 2021-12-17 14:01:12 +01:00 committed by GitHub
parent 56ec5245cc
commit 66fa8f9667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 2907 additions and 2342 deletions

View File

@ -24,7 +24,7 @@ use {
std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc},
};
pub type TransactionAccountRefCell = (Pubkey, Rc<RefCell<AccountSharedData>>);
pub type TransactionAccountRefCell = (Pubkey, RefCell<AccountSharedData>);
pub type TransactionAccountRefCells = Vec<TransactionAccountRefCell>;
pub type ProcessInstructionWithContext =
@ -841,35 +841,23 @@ pub struct MockInvokeContextPreparation {
pub fn prepare_mock_invoke_context(
program_indices: &[usize],
instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
) -> MockInvokeContextPreparation {
#[allow(clippy::type_complexity)]
let (accounts, mut metas): (TransactionAccountRefCells, Vec<AccountMeta>) = keyed_accounts
.iter()
.map(|(is_signer, is_writable, pubkey, account)| {
(
(*pubkey, account.clone()),
AccountMeta {
pubkey: *pubkey,
is_signer: *is_signer,
is_writable: *is_writable,
},
)
})
.unzip();
let transaction_accounts: TransactionAccountRefCells = transaction_accounts
.into_iter()
.map(|(pubkey, account)| (pubkey, RefCell::new(account)))
.collect();
let program_id = if let Some(program_index) = program_indices.last() {
accounts[*program_index].0
transaction_accounts[*program_index].0
} else {
Pubkey::default()
};
for program_index in program_indices.iter().rev() {
metas.remove(*program_index);
}
let message = Message::new(
&[Instruction::new_with_bytes(
program_id,
instruction_data,
metas,
instruction_accounts,
)],
None,
);
@ -877,14 +865,14 @@ pub fn prepare_mock_invoke_context(
.account_keys
.iter()
.map(|search_key| {
accounts
transaction_accounts
.iter()
.position(|(key, _account)| key == search_key)
.unwrap_or(accounts.len())
.unwrap_or(transaction_accounts.len())
})
.collect();
MockInvokeContextPreparation {
accounts,
accounts: transaction_accounts,
message,
account_indices,
}
@ -896,27 +884,31 @@ pub fn with_mock_invoke_context<R, F: FnMut(&mut InvokeContext) -> R>(
mut callback: F,
) -> R {
let program_indices = vec![0, 1];
let keyed_accounts = [
let transaction_accounts = vec![
(
false,
false,
loader_id,
AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id()),
AccountSharedData::new(0, 0, &solana_sdk::native_loader::id()),
),
(
false,
false,
Pubkey::new_unique(),
AccountSharedData::new_ref(1, 0, &loader_id),
AccountSharedData::new(1, 0, &loader_id),
),
(
false,
false,
Pubkey::new_unique(),
AccountSharedData::new_ref(2, account_size, &Pubkey::new_unique()),
AccountSharedData::new(2, account_size, &Pubkey::new_unique()),
),
];
let preparation = prepare_mock_invoke_context(&program_indices, &[], &keyed_accounts);
let instruction_accounts = vec![AccountMeta {
pubkey: transaction_accounts[2].0,
is_signer: false,
is_writable: false,
}];
let preparation = prepare_mock_invoke_context(
&program_indices,
&[],
transaction_accounts,
instruction_accounts,
);
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context
.push(
@ -933,38 +925,60 @@ pub fn mock_process_instruction_with_sysvars(
loader_id: &Pubkey,
mut program_indices: Vec<usize>,
instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
sysvars: &[(Pubkey, Vec<u8>)],
process_instruction: ProcessInstructionWithContext,
) -> Result<(), InstructionError> {
let mut preparation =
prepare_mock_invoke_context(&program_indices, instruction_data, keyed_accounts);
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
) -> Vec<AccountSharedData> {
let mut preparation = prepare_mock_invoke_context(
&program_indices,
instruction_data,
transaction_accounts,
instruction_accounts,
);
let processor_account = RefCell::new(AccountSharedData::new(
0,
0,
&solana_sdk::native_loader::id(),
));
program_indices.insert(0, preparation.accounts.len());
preparation.accounts.push((*loader_id, processor_account));
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context.sysvars = sysvars;
invoke_context.push(
&preparation.message,
&preparation.message.instructions[0],
&program_indices,
&preparation.account_indices,
)?;
process_instruction(1, instruction_data, &mut invoke_context)
let result = invoke_context
.push(
&preparation.message,
&preparation.message.instructions[0],
&program_indices,
&preparation.account_indices,
)
.and_then(|_| process_instruction(1, instruction_data, &mut invoke_context));
preparation.accounts.pop();
assert_eq!(result, expected_result);
preparation
.accounts
.into_iter()
.map(|(_key, account)| account.into_inner())
.collect()
}
pub fn mock_process_instruction(
loader_id: &Pubkey,
program_indices: Vec<usize>,
instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)],
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
) -> Result<(), InstructionError> {
) -> Vec<AccountSharedData> {
mock_process_instruction_with_sysvars(
loader_id,
program_indices,
instruction_data,
keyed_accounts,
transaction_accounts,
instruction_accounts,
expected_result,
&[],
process_instruction,
)
@ -1076,22 +1090,18 @@ mod tests {
invoke_stack.push(solana_sdk::pubkey::new_rand());
accounts.push((
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::new(
i as u64,
1,
&invoke_stack[i],
))),
RefCell::new(AccountSharedData::new(i as u64, 1, &invoke_stack[i])),
));
metas.push(AccountMeta::new(accounts[i].0, false));
}
for program_id in invoke_stack.iter() {
accounts.push((
*program_id,
Rc::new(RefCell::new(AccountSharedData::new(
RefCell::new(AccountSharedData::new(
1,
1,
&solana_sdk::pubkey::Pubkey::default(),
))),
)),
));
metas.push(AccountMeta::new(*program_id, false));
}
@ -1178,7 +1188,7 @@ mod tests {
fn test_invoke_context_verify() {
let accounts = vec![(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::default())),
RefCell::new(AccountSharedData::default()),
)];
let message = Message::new(
&[Instruction::new_with_bincode(
@ -1216,20 +1226,17 @@ mod tests {
program_account.set_executable(true);
let accounts = vec![
(solana_sdk::pubkey::new_rand(), RefCell::new(owned_account)),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(owned_account)),
RefCell::new(not_owned_account),
),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(not_owned_account)),
RefCell::new(readonly_account),
),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(readonly_account)),
),
(caller_program_id, Rc::new(RefCell::new(loader_account))),
(callee_program_id, Rc::new(RefCell::new(program_account))),
(caller_program_id, RefCell::new(loader_account)),
(callee_program_id, RefCell::new(program_account)),
];
let account_indices = [0, 1, 2];
let program_indices = [3, 4];
@ -1346,20 +1353,17 @@ mod tests {
program_account.set_executable(true);
let accounts = vec![
(solana_sdk::pubkey::new_rand(), RefCell::new(owned_account)),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(owned_account)),
RefCell::new(not_owned_account),
),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(not_owned_account)),
RefCell::new(readonly_account),
),
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(readonly_account)),
),
(caller_program_id, Rc::new(RefCell::new(loader_account))),
(callee_program_id, Rc::new(RefCell::new(program_account))),
(caller_program_id, RefCell::new(loader_account)),
(callee_program_id, RefCell::new(program_account)),
];
let program_indices = [3];
let metas = vec![
@ -1441,11 +1445,11 @@ mod tests {
let accounts = vec![
(
solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::default())),
RefCell::new(AccountSharedData::default()),
),
(
crate::neon_evm_program::id(),
Rc::new(RefCell::new(AccountSharedData::default())),
RefCell::new(AccountSharedData::default()),
),
];

File diff suppressed because it is too large Load Diff

View File

@ -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());
}
}

View File

@ -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![])],

View File

@ -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

View File

@ -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(()),
);
}

View File

@ -14,7 +14,9 @@ use {
verifier::check,
vm::{Config, DynamicAnalysis},
},
solana_sdk::{account::AccountSharedData, bpf_loader, pubkey::Pubkey},
solana_sdk::{
account::AccountSharedData, bpf_loader, instruction::AccountMeta, pubkey::Pubkey,
},
std::{
fs::File,
io::{Read, Seek, SeekFrom},
@ -162,51 +164,57 @@ native machine code before execting it in the virtual machine.",
..Config::default()
};
let loader_id = bpf_loader::id();
let mut keyed_accounts = vec![
let mut transaction_accounts = vec![
(
false,
false,
loader_id,
AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id()),
AccountSharedData::new(0, 0, &solana_sdk::native_loader::id()),
),
(
false,
false,
Pubkey::new_unique(),
AccountSharedData::new_ref(0, 0, &loader_id),
AccountSharedData::new(0, 0, &loader_id),
),
];
let mut instruction_accounts = Vec::new();
let instruction_data = match matches.value_of("input").unwrap().parse::<usize>() {
Ok(allocation_size) => {
keyed_accounts.push((
false,
true,
Pubkey::new_unique(),
AccountSharedData::new_ref(0, allocation_size, &Pubkey::new_unique()),
let pubkey = Pubkey::new_unique();
transaction_accounts.push((
pubkey,
AccountSharedData::new(0, allocation_size, &Pubkey::new_unique()),
));
instruction_accounts.push(AccountMeta {
pubkey,
is_signer: false,
is_writable: true,
});
vec![]
}
Err(_) => {
let input = load_accounts(Path::new(matches.value_of("input").unwrap())).unwrap();
for account in input.accounts {
let account_refcell = AccountSharedData::new_ref(
account.lamports,
account.data.len(),
&account.owner,
for account_info in input.accounts {
let mut account = AccountSharedData::new(
account_info.lamports,
account_info.data.len(),
&account_info.owner,
);
account_refcell.borrow_mut().set_data(account.data);
keyed_accounts.push((
account.is_signer,
account.is_writable,
account.key,
account_refcell,
));
account.set_data(account_info.data);
instruction_accounts.push(AccountMeta {
pubkey: account_info.key,
is_signer: account_info.is_signer,
is_writable: account_info.is_writable,
});
transaction_accounts.push((account_info.key, account));
}
input.instruction_data
}
};
let program_indices = [0, 1];
let preparation = prepare_mock_invoke_context(&program_indices, &[], &keyed_accounts);
let preparation = prepare_mock_invoke_context(
&program_indices,
&[],
transaction_accounts,
instruction_accounts,
);
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context
.push(

View File

@ -3403,11 +3403,10 @@ impl Bank {
/// Converts Accounts into RefCell<AccountSharedData>, this involves moving
/// ownership by draining the source
fn accounts_to_refcells(accounts: &mut TransactionAccounts) -> TransactionAccountRefCells {
let account_refcells: Vec<_> = accounts
accounts
.drain(..)
.map(|(pubkey, account)| (pubkey, Rc::new(RefCell::new(account))))
.collect();
account_refcells
.map(|(pubkey, account)| (pubkey, RefCell::new(account)))
.collect()
}
/// Converts back from RefCell<AccountSharedData> to AccountSharedData, this involves moving
@ -3415,17 +3414,10 @@ impl Bank {
fn refcells_to_accounts(
accounts: &mut TransactionAccounts,
mut account_refcells: TransactionAccountRefCells,
) -> std::result::Result<(), TransactionError> {
) {
for (pubkey, account_refcell) in account_refcells.drain(..) {
accounts.push((
pubkey,
Rc::try_unwrap(account_refcell)
.map_err(|_| TransactionError::AccountBorrowOutstanding)?
.into_inner(),
))
accounts.push((pubkey, account_refcell.into_inner()))
}
Ok(())
}
/// Get any cached executors needed by the transaction
@ -3641,13 +3633,10 @@ impl Bank {
});
inner_instructions.push(inner_instruction_list);
if let Err(e) = Self::refcells_to_accounts(
Self::refcells_to_accounts(
&mut loaded_transaction.accounts,
account_refcells,
) {
warn!("Account lifetime mismanagement");
process_result = Err(e);
}
);
if process_result.is_ok() {
self.update_executors(executors);

View File

@ -202,19 +202,19 @@ mod tests {
process_instruction: mock_system_process_instruction,
}];
let program_account = Rc::new(RefCell::new(create_loadable_account_for_test(
"mock_system_program",
)));
let accounts = vec![
(
solana_sdk::pubkey::new_rand(),
AccountSharedData::new_ref(100, 1, &mock_system_program_id),
RefCell::new(AccountSharedData::new(100, 1, &mock_system_program_id)),
),
(
solana_sdk::pubkey::new_rand(),
AccountSharedData::new_ref(0, 1, &mock_system_program_id),
RefCell::new(AccountSharedData::new(0, 1, &mock_system_program_id)),
),
(
mock_system_program_id,
RefCell::new(create_loadable_account_for_test("mock_system_program")),
),
(mock_system_program_id, program_account),
];
let program_indices = vec![vec![2]];
@ -406,19 +406,19 @@ mod tests {
process_instruction: mock_system_process_instruction,
}];
let program_account = Rc::new(RefCell::new(create_loadable_account_for_test(
"mock_system_program",
)));
let accounts = vec![
(
solana_sdk::pubkey::new_rand(),
AccountSharedData::new_ref(100, 1, &mock_program_id),
RefCell::new(AccountSharedData::new(100, 1, &mock_program_id)),
),
(
solana_sdk::pubkey::new_rand(),
AccountSharedData::new_ref(0, 1, &mock_program_id),
RefCell::new(AccountSharedData::new(0, 1, &mock_program_id)),
),
(
mock_program_id,
RefCell::new(create_loadable_account_for_test("mock_system_program")),
),
(mock_program_id, program_account),
];
let program_indices = vec![vec![2]];
@ -539,13 +539,13 @@ mod tests {
process_instruction: mock_process_instruction,
}];
let secp256k1_account = AccountSharedData::new_ref(1, 0, &native_loader::id());
secp256k1_account.borrow_mut().set_executable(true);
let mock_program_account = AccountSharedData::new_ref(1, 0, &native_loader::id());
mock_program_account.borrow_mut().set_executable(true);
let mut secp256k1_account = AccountSharedData::new(1, 0, &native_loader::id());
secp256k1_account.set_executable(true);
let mut mock_program_account = AccountSharedData::new(1, 0, &native_loader::id());
mock_program_account.set_executable(true);
let accounts = vec![
(secp256k1_program::id(), secp256k1_account),
(mock_program_id, mock_program_account),
(secp256k1_program::id(), RefCell::new(secp256k1_account)),
(mock_program_id, RefCell::new(mock_program_account)),
];
let message = Message::new(

File diff suppressed because it is too large Load Diff