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}, 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 TransactionAccountRefCells = Vec<TransactionAccountRefCell>;
pub type ProcessInstructionWithContext = pub type ProcessInstructionWithContext =
@ -841,35 +841,23 @@ pub struct MockInvokeContextPreparation {
pub fn prepare_mock_invoke_context( pub fn prepare_mock_invoke_context(
program_indices: &[usize], program_indices: &[usize],
instruction_data: &[u8], instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)], transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
) -> MockInvokeContextPreparation { ) -> MockInvokeContextPreparation {
#[allow(clippy::type_complexity)] let transaction_accounts: TransactionAccountRefCells = transaction_accounts
let (accounts, mut metas): (TransactionAccountRefCells, Vec<AccountMeta>) = keyed_accounts .into_iter()
.iter() .map(|(pubkey, account)| (pubkey, RefCell::new(account)))
.map(|(is_signer, is_writable, pubkey, account)| { .collect();
(
(*pubkey, account.clone()),
AccountMeta {
pubkey: *pubkey,
is_signer: *is_signer,
is_writable: *is_writable,
},
)
})
.unzip();
let program_id = if let Some(program_index) = program_indices.last() { let program_id = if let Some(program_index) = program_indices.last() {
accounts[*program_index].0 transaction_accounts[*program_index].0
} else { } else {
Pubkey::default() Pubkey::default()
}; };
for program_index in program_indices.iter().rev() {
metas.remove(*program_index);
}
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes( &[Instruction::new_with_bytes(
program_id, program_id,
instruction_data, instruction_data,
metas, instruction_accounts,
)], )],
None, None,
); );
@ -877,14 +865,14 @@ pub fn prepare_mock_invoke_context(
.account_keys .account_keys
.iter() .iter()
.map(|search_key| { .map(|search_key| {
accounts transaction_accounts
.iter() .iter()
.position(|(key, _account)| key == search_key) .position(|(key, _account)| key == search_key)
.unwrap_or(accounts.len()) .unwrap_or(transaction_accounts.len())
}) })
.collect(); .collect();
MockInvokeContextPreparation { MockInvokeContextPreparation {
accounts, accounts: transaction_accounts,
message, message,
account_indices, account_indices,
} }
@ -896,27 +884,31 @@ pub fn with_mock_invoke_context<R, F: FnMut(&mut InvokeContext) -> R>(
mut callback: F, mut callback: F,
) -> R { ) -> R {
let program_indices = vec![0, 1]; let program_indices = vec![0, 1];
let keyed_accounts = [ let transaction_accounts = vec![
( (
false,
false,
loader_id, 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(), Pubkey::new_unique(),
AccountSharedData::new_ref(1, 0, &loader_id), AccountSharedData::new(1, 0, &loader_id),
), ),
( (
false,
false,
Pubkey::new_unique(), 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, &[]); let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context invoke_context
.push( .push(
@ -933,38 +925,60 @@ pub fn mock_process_instruction_with_sysvars(
loader_id: &Pubkey, loader_id: &Pubkey,
mut program_indices: Vec<usize>, mut program_indices: Vec<usize>,
instruction_data: &[u8], 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>)], sysvars: &[(Pubkey, Vec<u8>)],
process_instruction: ProcessInstructionWithContext, process_instruction: ProcessInstructionWithContext,
) -> Result<(), InstructionError> { ) -> Vec<AccountSharedData> {
let mut preparation = let mut preparation = prepare_mock_invoke_context(
prepare_mock_invoke_context(&program_indices, instruction_data, keyed_accounts); &program_indices,
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id()); 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()); program_indices.insert(0, preparation.accounts.len());
preparation.accounts.push((*loader_id, processor_account)); preparation.accounts.push((*loader_id, processor_account));
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]); let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context.sysvars = sysvars; invoke_context.sysvars = sysvars;
invoke_context.push( let result = invoke_context
&preparation.message, .push(
&preparation.message.instructions[0], &preparation.message,
&program_indices, &preparation.message.instructions[0],
&preparation.account_indices, &program_indices,
)?; &preparation.account_indices,
process_instruction(1, instruction_data, &mut invoke_context) )
.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( pub fn mock_process_instruction(
loader_id: &Pubkey, loader_id: &Pubkey,
program_indices: Vec<usize>, program_indices: Vec<usize>,
instruction_data: &[u8], 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, process_instruction: ProcessInstructionWithContext,
) -> Result<(), InstructionError> { ) -> Vec<AccountSharedData> {
mock_process_instruction_with_sysvars( mock_process_instruction_with_sysvars(
loader_id, loader_id,
program_indices, program_indices,
instruction_data, instruction_data,
keyed_accounts, transaction_accounts,
instruction_accounts,
expected_result,
&[], &[],
process_instruction, process_instruction,
) )
@ -1076,22 +1090,18 @@ mod tests {
invoke_stack.push(solana_sdk::pubkey::new_rand()); invoke_stack.push(solana_sdk::pubkey::new_rand());
accounts.push(( accounts.push((
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::new( RefCell::new(AccountSharedData::new(i as u64, 1, &invoke_stack[i])),
i as u64,
1,
&invoke_stack[i],
))),
)); ));
metas.push(AccountMeta::new(accounts[i].0, false)); metas.push(AccountMeta::new(accounts[i].0, false));
} }
for program_id in invoke_stack.iter() { for program_id in invoke_stack.iter() {
accounts.push(( accounts.push((
*program_id, *program_id,
Rc::new(RefCell::new(AccountSharedData::new( RefCell::new(AccountSharedData::new(
1, 1,
1, 1,
&solana_sdk::pubkey::Pubkey::default(), &solana_sdk::pubkey::Pubkey::default(),
))), )),
)); ));
metas.push(AccountMeta::new(*program_id, false)); metas.push(AccountMeta::new(*program_id, false));
} }
@ -1178,7 +1188,7 @@ mod tests {
fn test_invoke_context_verify() { fn test_invoke_context_verify() {
let accounts = vec![( let accounts = vec![(
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::default())), RefCell::new(AccountSharedData::default()),
)]; )];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bincode( &[Instruction::new_with_bincode(
@ -1216,20 +1226,17 @@ mod tests {
program_account.set_executable(true); program_account.set_executable(true);
let accounts = vec![ let accounts = vec![
(solana_sdk::pubkey::new_rand(), RefCell::new(owned_account)),
( (
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(owned_account)), RefCell::new(not_owned_account),
), ),
( (
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(not_owned_account)), RefCell::new(readonly_account),
), ),
( (caller_program_id, RefCell::new(loader_account)),
solana_sdk::pubkey::new_rand(), (callee_program_id, RefCell::new(program_account)),
Rc::new(RefCell::new(readonly_account)),
),
(caller_program_id, Rc::new(RefCell::new(loader_account))),
(callee_program_id, Rc::new(RefCell::new(program_account))),
]; ];
let account_indices = [0, 1, 2]; let account_indices = [0, 1, 2];
let program_indices = [3, 4]; let program_indices = [3, 4];
@ -1346,20 +1353,17 @@ mod tests {
program_account.set_executable(true); program_account.set_executable(true);
let accounts = vec![ let accounts = vec![
(solana_sdk::pubkey::new_rand(), RefCell::new(owned_account)),
( (
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(owned_account)), RefCell::new(not_owned_account),
), ),
( (
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(not_owned_account)), RefCell::new(readonly_account),
), ),
( (caller_program_id, RefCell::new(loader_account)),
solana_sdk::pubkey::new_rand(), (callee_program_id, RefCell::new(program_account)),
Rc::new(RefCell::new(readonly_account)),
),
(caller_program_id, Rc::new(RefCell::new(loader_account))),
(callee_program_id, Rc::new(RefCell::new(program_account))),
]; ];
let program_indices = [3]; let program_indices = [3];
let metas = vec![ let metas = vec![
@ -1441,11 +1445,11 @@ mod tests {
let accounts = vec![ let accounts = vec![
( (
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::default())), RefCell::new(AccountSharedData::default()),
), ),
( (
crate::neon_evm_program::id(), 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, account_info::AccountInfo,
bpf_loader, bpf_loader,
entrypoint::deserialize, entrypoint::deserialize,
instruction::AccountMeta,
}, },
std::{ std::{
cell::RefCell, cell::RefCell,
@ -333,122 +334,128 @@ mod tests {
#[test] #[test]
fn test_serialize_parameters() { fn test_serialize_parameters() {
let program_id = solana_sdk::pubkey::new_rand(); let program_id = solana_sdk::pubkey::new_rand();
let dup_key = solana_sdk::pubkey::new_rand(); let transaction_accounts = vec![
let dup_key2 = solana_sdk::pubkey::new_rand();
let keyed_accounts = [
( (
false,
false,
program_id, program_id,
Rc::new(RefCell::new(AccountSharedData::from(Account { AccountSharedData::from(Account {
lamports: 0, lamports: 0,
data: vec![], data: vec![],
owner: bpf_loader::id(), owner: bpf_loader::id(),
executable: true, executable: true,
rent_epoch: 0, 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(), 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, lamports: 2,
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19], data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
owner: bpf_loader::id(), owner: bpf_loader::id(),
executable: true, executable: true,
rent_epoch: 200, rent_epoch: 200,
}))), }),
), ),
( (
false,
false,
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::from(Account { AccountSharedData::from(Account {
lamports: 3, lamports: 3,
data: vec![], data: vec![],
owner: bpf_loader::id(), owner: bpf_loader::id(),
executable: false, executable: false,
rent_epoch: 3100, 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(), 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, lamports: 5,
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19], data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
owner: bpf_loader::id(), owner: bpf_loader::id(),
executable: true, executable: true,
rent_epoch: 200, rent_epoch: 200,
}))), }),
), ),
( (
false,
true,
solana_sdk::pubkey::new_rand(), solana_sdk::pubkey::new_rand(),
Rc::new(RefCell::new(AccountSharedData::from(Account { AccountSharedData::from(Account {
lamports: 6, lamports: 6,
data: vec![], data: vec![],
owner: bpf_loader::id(), owner: bpf_loader::id(),
executable: false, executable: false,
rent_epoch: 3100, 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 instruction_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let program_indices = [0]; let program_indices = [0];
let preparation = let preparation = prepare_mock_invoke_context(
prepare_mock_invoke_context(&program_indices, &instruction_data, &keyed_accounts); &program_indices,
&instruction_data,
transaction_accounts.clone(),
instruction_accounts,
);
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]); let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
invoke_context invoke_context
.push( .push(
@ -479,9 +486,12 @@ mod tests {
(&de_instruction_data[0] as *const u8).align_offset(BPF_ALIGN_OF_U128), (&de_instruction_data[0] as *const u8).align_offset(BPF_ALIGN_OF_U128),
0 0
); );
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) { for account_info in de_accounts {
assert_eq!(key, account_info.key); let account = &transaction_accounts
let account = account.borrow(); .iter()
.find(|(key, _account)| key == account_info.key)
.unwrap()
.1;
assert_eq!(account.lamports(), account_info.lamports()); assert_eq!(account.lamports(), account_info.lamports());
assert_eq!(account.data(), &account_info.data.borrow()[..]); assert_eq!(account.data(), &account_info.data.borrow()[..]);
assert_eq!(account.owner(), account_info.owner); assert_eq!(account.owner(), account_info.owner);
@ -511,12 +521,14 @@ mod tests {
true, true,
) )
.unwrap(); .unwrap();
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts) for keyed_account in de_keyed_accounts {
{ let account = &transaction_accounts
assert_eq!(key, de_keyed_account.unsigned_key()); .iter()
let account = account.borrow(); .find(|(key, _account)| key == keyed_account.unsigned_key())
assert_eq!(account.executable(), de_keyed_account.executable().unwrap()); .unwrap()
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap()); .1;
assert_eq!(account.executable(), keyed_account.executable().unwrap());
assert_eq!(account.rent_epoch(), keyed_account.rent_epoch().unwrap());
} }
// check serialize_parameters_unaligned // check serialize_parameters_unaligned
@ -534,9 +546,12 @@ mod tests {
unsafe { deserialize_unaligned(&mut serialized.as_slice_mut()[0] as *mut u8) }; unsafe { deserialize_unaligned(&mut serialized.as_slice_mut()[0] as *mut u8) };
assert_eq!(&program_id, de_program_id); assert_eq!(&program_id, de_program_id);
assert_eq!(instruction_data, de_instruction_data); assert_eq!(instruction_data, de_instruction_data);
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) { for account_info in de_accounts {
assert_eq!(key, account_info.key); let account = &transaction_accounts
let account = account.borrow(); .iter()
.find(|(key, _account)| key == account_info.key)
.unwrap()
.1;
assert_eq!(account.lamports(), account_info.lamports()); assert_eq!(account.lamports(), account_info.lamports());
assert_eq!(account.data(), &account_info.data.borrow()[..]); assert_eq!(account.data(), &account_info.data.borrow()[..]);
assert_eq!(account.owner(), account_info.owner); assert_eq!(account.owner(), account_info.owner);
@ -553,18 +568,20 @@ mod tests {
true, true,
) )
.unwrap(); .unwrap();
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts) for keyed_account in de_keyed_accounts {
{ let account = &transaction_accounts
assert_eq!(key, de_keyed_account.unsigned_key()); .iter()
let account = account.borrow(); .find(|(key, _account)| key == keyed_account.unsigned_key())
assert_eq!(account.lamports(), de_keyed_account.lamports().unwrap()); .unwrap()
.1;
assert_eq!(account.lamports(), keyed_account.lamports().unwrap());
assert_eq!( assert_eq!(
account.data(), 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.owner(), keyed_account.owner().unwrap());
assert_eq!(account.executable(), de_keyed_account.executable().unwrap()); assert_eq!(account.executable(), keyed_account.executable().unwrap());
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().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)))")] #[should_panic(expected = "UserError(SyscallError(Panic(\"Gaggablaghblagh!\", 42, 84)))")]
fn test_syscall_sol_panic() { fn test_syscall_sol_panic() {
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3057,7 +3057,7 @@ mod tests {
#[test] #[test]
fn test_syscall_sol_log() { fn test_syscall_sol_log() {
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3161,7 +3161,7 @@ mod tests {
#[test] #[test]
fn test_syscall_sol_log_u64() { fn test_syscall_sol_log_u64() {
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3203,7 +3203,7 @@ mod tests {
#[test] #[test]
fn test_syscall_sol_pubkey() { fn test_syscall_sol_pubkey() {
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3415,7 +3415,8 @@ mod tests {
fn test_syscall_sha256() { fn test_syscall_sha256() {
let config = Config::default(); let config = Config::default();
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3544,7 +3545,7 @@ mod tests {
fn test_syscall_get_sysvar() { fn test_syscall_get_sysvar() {
let config = Config::default(); let config = Config::default();
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3854,7 +3855,7 @@ mod tests {
// These tests duplicate the direct tests in solana_program::pubkey // These tests duplicate the direct tests in solana_program::pubkey
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],
@ -3970,7 +3971,7 @@ mod tests {
#[test] #[test]
fn test_find_program_address() { fn test_find_program_address() {
let program_id = Pubkey::new_unique(); 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 accounts = [(program_id, program_account)];
let message = Message::new( let message = Message::new(
&[Instruction::new_with_bytes(program_id, &[], vec![])], &[Instruction::new_with_bytes(program_id, &[], vec![])],

View File

@ -147,22 +147,26 @@ mod tests {
solana_program_runtime::invoke_context::mock_process_instruction, solana_program_runtime::invoke_context::mock_process_instruction,
solana_sdk::{ solana_sdk::{
account::AccountSharedData, account::AccountSharedData,
instruction::AccountMeta,
pubkey::Pubkey, pubkey::Pubkey,
signature::{Keypair, Signer}, signature::{Keypair, Signer},
system_instruction::SystemInstruction, system_instruction::SystemInstruction,
}, },
std::{cell::RefCell, rc::Rc},
}; };
fn process_instruction( fn process_instruction(
instruction_data: &[u8], instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)], transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
) -> Result<(), InstructionError> { instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> {
mock_process_instruction( mock_process_instruction(
&id(), &id(),
Vec::new(), Vec::new(),
instruction_data, instruction_data,
keyed_accounts, transaction_accounts,
instruction_accounts,
expected_result,
super::process_instruction, super::process_instruction,
) )
} }
@ -191,16 +195,12 @@ mod tests {
} }
} }
fn create_config_account( fn create_config_account(keys: Vec<(Pubkey, bool)>) -> (Keypair, AccountSharedData) {
keys: Vec<(Pubkey, bool)>,
) -> (Keypair, Rc<RefCell<AccountSharedData>>) {
let from_pubkey = Pubkey::new_unique(); let from_pubkey = Pubkey::new_unique();
let config_keypair = Keypair::new(); let config_keypair = Keypair::new();
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let instructions = let instructions =
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, keys); config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, keys);
let system_instruction = limited_deserialize(&instructions[0].data).unwrap(); let system_instruction = limited_deserialize(&instructions[0].data).unwrap();
let space = match system_instruction { let space = match system_instruction {
SystemInstruction::CreateAccount { SystemInstruction::CreateAccount {
@ -210,14 +210,18 @@ mod tests {
} => space, } => space,
_ => panic!("Not a CreateAccount system instruction"), _ => panic!("Not a CreateAccount system instruction"),
}; };
let config_account = AccountSharedData::new_ref(0, space as usize, &id()); let config_account = AccountSharedData::new(0, space as usize, &id());
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())]; let accounts = process_instruction(
assert_eq!( &instructions[1].data,
process_instruction(&instructions[1].data, &keyed_accounts), vec![(config_pubkey, config_account)],
Ok(()) vec![AccountMeta {
pubkey: config_pubkey,
is_signer: true,
is_writable: false,
}],
Ok(()),
); );
(config_keypair, accounts[0].clone())
(config_keypair, config_account)
} }
#[test] #[test]
@ -226,7 +230,7 @@ mod tests {
let (_, config_account) = create_config_account(vec![]); let (_, config_account) = create_config_account(vec![]);
assert_eq!( assert_eq!(
Some(MyConfig::default()), 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 my_config = MyConfig::new(42);
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
let keyed_accounts = [(true, false, config_pubkey, config_account.clone())]; let accounts = process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![(config_pubkey, config_account)],
Ok(()) vec![AccountMeta {
pubkey: config_pubkey,
is_signer: true,
is_writable: false,
}],
Ok(()),
); );
assert_eq!( assert_eq!(
Some(my_config), 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); 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 instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
let keyed_accounts = [(true, false, config_pubkey, config_account)]; process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![(config_pubkey, config_account)],
Err(InstructionError::InvalidInstructionData) 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); let mut instruction = config_instruction::store(&config_pubkey, true, vec![], &my_config);
instruction.accounts[0].is_signer = false; // <----- not a signer instruction.accounts[0].is_signer = false; // <----- not a signer
let keyed_accounts = [(false, false, config_pubkey, config_account)]; process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![(config_pubkey, config_account)],
Err(InstructionError::MissingRequiredSignature) 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_keypair, config_account) = create_config_account(keys.clone());
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); 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 instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
let signer0_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let accounts = process_instruction(
let signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); &instruction.data,
let keyed_accounts = [ vec![
(true, false, config_pubkey, config_account.clone()), (config_pubkey, config_account),
(true, false, signer0_pubkey, signer0_account), (signer0_pubkey, signer0_account),
(true, false, signer1_pubkey, signer1_account), (signer1_pubkey, signer1_account),
]; ],
assert_eq!( vec![
process_instruction(&instruction.data, &keyed_accounts), AccountMeta {
Ok(()) 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!(meta_data.keys, keys);
assert_eq!( assert_eq!(
Some(my_config), 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_keypair, _) = create_config_account(keys.clone());
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); 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 instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
let signer0_account = AccountSharedData::new_ref(0, 0, &id()); process_instruction(
let keyed_accounts = [(true, false, signer0_pubkey, signer0_account)]; &instruction.data,
assert_eq!( vec![(signer0_pubkey, signer0_account)],
process_instruction(&instruction.data, &keyed_accounts), vec![AccountMeta {
Err(InstructionError::InvalidAccountData) pubkey: signer0_pubkey,
is_signer: true,
is_writable: false,
}],
Err(InstructionError::InvalidAccountData),
); );
} }
@ -343,30 +384,56 @@ mod tests {
solana_logger::setup(); solana_logger::setup();
let signer0_pubkey = Pubkey::new_unique(); let signer0_pubkey = Pubkey::new_unique();
let signer1_pubkey = Pubkey::new_unique(); let signer1_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 signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let keys = vec![(signer0_pubkey, true)]; let keys = vec![(signer0_pubkey, true)];
let (config_keypair, config_account) = create_config_account(keys.clone()); let (config_keypair, config_account) = create_config_account(keys.clone());
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
// Config-data pubkey doesn't match signer // Config-data pubkey doesn't match signer
let mut keyed_accounts = [ let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
(true, false, config_pubkey, config_account), process_instruction(
(true, false, signer1_pubkey, signer1_account), &instruction.data,
]; vec![
assert_eq!( (config_pubkey, config_account.clone()),
process_instruction(&instruction.data, &keyed_accounts), (signer1_pubkey, signer1_account),
Err(InstructionError::MissingRequiredSignature) ],
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 // Config-data pubkey not a signer
keyed_accounts[1] = (false, false, signer0_pubkey, signer0_account); process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![
Err(InstructionError::MissingRequiredSignature) (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 signer0_pubkey = Pubkey::new_unique();
let signer1_pubkey = Pubkey::new_unique(); let signer1_pubkey = Pubkey::new_unique();
let signer2_pubkey = Pubkey::new_unique(); let signer2_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 signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let signer2_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let signer2_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let keys = vec![ let keys = vec![
(pubkey, false), (pubkey, false),
(signer0_pubkey, true), (signer0_pubkey, true),
@ -390,40 +457,98 @@ mod tests {
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
let mut keyed_accounts = [ let accounts = process_instruction(
(true, false, config_pubkey, config_account.clone()), &instruction.data,
(true, false, signer0_pubkey, signer0_account), vec![
(true, false, signer1_pubkey, signer1_account), (config_pubkey, config_account),
]; (signer0_pubkey, signer0_account.clone()),
assert_eq!( (signer1_pubkey, signer1_account.clone()),
process_instruction(&instruction.data, &keyed_accounts), ],
Ok(()) 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 // Update with expected signatures
let new_config = MyConfig::new(84); let new_config = MyConfig::new(84);
let instruction = let instruction =
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config); config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
keyed_accounts[0].0 = false; let accounts = process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![
Ok(()) (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!(meta_data.keys, keys);
assert_eq!( assert_eq!(
new_config, new_config,
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap()) MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
.unwrap()
); );
// Attempt update with incomplete signatures // Attempt update with incomplete signatures
let keys = vec![(pubkey, false), (signer0_pubkey, true)]; let keys = vec![(pubkey, false), (signer0_pubkey, true)];
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
keyed_accounts[2].0 = false; process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![
Err(InstructionError::MissingRequiredSignature) (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 // Attempt update with incorrect signatures
@ -433,10 +558,31 @@ mod tests {
(signer2_pubkey, true), (signer2_pubkey, true),
]; ];
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
keyed_accounts[2] = (true, false, signer2_pubkey, signer2_account); process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), vec![
Err(InstructionError::MissingRequiredSignature) (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(); solana_logger::setup();
let config_address = Pubkey::new_unique(); let config_address = Pubkey::new_unique();
let signer0_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![ let keys = vec![
(config_address, false), (config_address, false),
(signer0_pubkey, true), (signer0_pubkey, true),
@ -457,13 +603,29 @@ mod tests {
// Attempt initialization with duplicate signer inputs // Attempt initialization with duplicate signer inputs
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
let keyed_accounts = [ process_instruction(
(true, false, config_pubkey, config_account), &instruction.data,
(true, false, signer0_pubkey, signer0_account.clone()), vec![
(true, false, signer0_pubkey, signer0_account), (config_pubkey, config_account),
]; (signer0_pubkey, signer0_account),
assert_eq!( ],
process_instruction(&instruction.data, &keyed_accounts), 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), Err(InstructionError::InvalidArgument),
); );
} }
@ -474,8 +636,8 @@ mod tests {
let config_address = Pubkey::new_unique(); let config_address = Pubkey::new_unique();
let signer0_pubkey = Pubkey::new_unique(); let signer0_pubkey = Pubkey::new_unique();
let signer1_pubkey = Pubkey::new_unique(); let signer1_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 signer1_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let signer1_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let keys = vec![ let keys = vec![
(config_address, false), (config_address, false),
(signer0_pubkey, true), (signer0_pubkey, true),
@ -486,13 +648,30 @@ mod tests {
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
let mut keyed_accounts = [ let accounts = process_instruction(
(true, false, config_pubkey, config_account), &instruction.data,
(true, false, signer0_pubkey, signer0_account), vec![
(true, false, signer1_pubkey, signer1_account), (config_pubkey, config_account),
]; (signer0_pubkey, signer0_account.clone()),
assert_eq!( (signer1_pubkey, signer1_account),
process_instruction(&instruction.data, &keyed_accounts), ],
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(()), Ok(()),
); );
@ -504,9 +683,29 @@ mod tests {
(signer0_pubkey, true), (signer0_pubkey, true),
]; ];
let instruction = config_instruction::store(&config_pubkey, false, dupe_keys, &new_config); let instruction = config_instruction::store(&config_pubkey, false, dupe_keys, &new_config);
keyed_accounts[2] = keyed_accounts[1].clone(); process_instruction(
assert_eq!( &instruction.data,
process_instruction(&instruction.data, &keyed_accounts), 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), Err(InstructionError::InvalidArgument),
); );
} }
@ -516,7 +715,7 @@ mod tests {
solana_logger::setup(); solana_logger::setup();
let pubkey = Pubkey::new_unique(); let pubkey = Pubkey::new_unique();
let signer0_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![ let keys = vec![
(pubkey, false), (pubkey, false),
(signer0_pubkey, true), (signer0_pubkey, true),
@ -525,7 +724,6 @@ mod tests {
let (config_keypair, config_account) = create_config_account(keys); let (config_keypair, config_account) = create_config_account(keys);
let config_pubkey = config_keypair.pubkey(); let config_pubkey = config_keypair.pubkey();
let my_config = MyConfig::new(42); let my_config = MyConfig::new(42);
let keys = vec![ let keys = vec![
(pubkey, false), (pubkey, false),
(signer0_pubkey, true), (signer0_pubkey, true),
@ -533,37 +731,70 @@ mod tests {
]; ];
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
let keyed_accounts = [ let accounts = process_instruction(
(true, false, config_pubkey, config_account.clone()), &instruction.data,
(true, false, signer0_pubkey, signer0_account), vec![
]; (config_pubkey, config_account),
assert_eq!( (signer0_pubkey, signer0_account.clone()),
process_instruction(&instruction.data, &keyed_accounts), ],
Ok(()) 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 // Update with expected signatures
let new_config = MyConfig::new(84); let new_config = MyConfig::new(84);
let instruction = let instruction =
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config); config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
assert_eq!( let accounts = process_instruction(
process_instruction(&instruction.data, &keyed_accounts), &instruction.data,
Ok(()) 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!(meta_data.keys, keys);
assert_eq!( assert_eq!(
new_config, new_config,
MyConfig::deserialize(get_config_data(config_account.borrow().data()).unwrap()) MyConfig::deserialize(get_config_data(accounts[0].data()).unwrap()).unwrap()
.unwrap()
); );
// Attempt update with incomplete signatures // Attempt update with incomplete signatures
let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)]; let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)];
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config); let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
assert_eq!( process_instruction(
process_instruction(&instruction.data, &keyed_accounts[0..1]), &instruction.data,
Err(InstructionError::MissingRequiredSignature) 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 (_, _config_account) = create_config_account(vec![]);
let instructions = let instructions =
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]); config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]);
assert_eq!( process_instruction(
process_instruction(&instructions[1].data, &[]), &instructions[1].data,
Err(InstructionError::NotEnoughAccountKeys) Vec::new(),
Vec::new(),
Err(InstructionError::NotEnoughAccountKeys),
); );
} }
@ -586,8 +819,8 @@ mod tests {
let config_pubkey = Pubkey::new_unique(); let config_pubkey = Pubkey::new_unique();
let new_config = MyConfig::new(84); let new_config = MyConfig::new(84);
let signer0_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 config_account = AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()); let config_account = AccountSharedData::new(0, 0, &Pubkey::new_unique());
let (_, _config_account) = create_config_account(vec![]); let (_, _config_account) = create_config_account(vec![]);
let keys = vec![ let keys = vec![
(from_pubkey, false), (from_pubkey, false),
@ -596,13 +829,25 @@ mod tests {
]; ];
let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config); let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config);
let keyed_accounts = [ process_instruction(
(true, false, config_pubkey, config_account), &instruction.data,
(true, false, signer0_pubkey, signer0_account), vec![
]; (config_pubkey, config_account),
assert_eq!( (signer0_pubkey, signer0_account),
process_instruction(&instruction.data, &keyed_accounts), ],
Err(InstructionError::InvalidAccountOwner) 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 { use {
super::*, super::*,
bincode::serialize, 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::{ solana_sdk::{
account::{self, Account, AccountSharedData}, account::{self, Account, AccountSharedData},
rent::Rent, rent::Rent,
}, },
std::{cell::RefCell, rc::Rc, str::FromStr}, std::str::FromStr,
}; };
fn create_default_account() -> Rc<RefCell<AccountSharedData>> { fn create_default_account() -> AccountSharedData {
AccountSharedData::new_ref(0, 0, &Pubkey::new_unique()) AccountSharedData::new(0, 0, &Pubkey::new_unique())
} }
fn process_instruction( fn process_instruction(
instruction_data: &[u8], instruction_data: &[u8],
keyed_accounts: &[(bool, bool, Pubkey, Rc<RefCell<AccountSharedData>>)], transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
) -> Result<(), InstructionError> { instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> {
mock_process_instruction( mock_process_instruction(
&id(), &id(),
Vec::new(), Vec::new(),
instruction_data, instruction_data,
keyed_accounts, transaction_accounts,
instruction_accounts,
expected_result,
super::process_instruction, super::process_instruction,
) )
} }
fn process_instruction_as_one_arg(instruction: &Instruction) -> Result<(), InstructionError> { fn process_instruction_as_one_arg(
let mut accounts: Vec<_> = instruction instruction: &Instruction,
expected_result: Result<(), InstructionError>,
) -> Vec<AccountSharedData> {
let transaction_accounts: Vec<_> = instruction
.accounts .accounts
.iter() .iter()
.map(|meta| { .map(|meta| {
Rc::new(RefCell::new(if sysvar::clock::check_id(&meta.pubkey) { (
account::create_account_shared_data_for_test(&Clock::default()) meta.pubkey,
} else if sysvar::slot_hashes::check_id(&meta.pubkey) { if sysvar::clock::check_id(&meta.pubkey) {
account::create_account_shared_data_for_test(&SlotHashes::default()) account::create_account_shared_data_for_test(&Clock::default())
} else if sysvar::rent::check_id(&meta.pubkey) { } else if sysvar::slot_hashes::check_id(&meta.pubkey) {
account::create_account_shared_data_for_test(&Rent::free()) account::create_account_shared_data_for_test(&SlotHashes::default())
} else if meta.pubkey == invalid_vote_state_pubkey() { } else if sysvar::rent::check_id(&meta.pubkey) {
AccountSharedData::from(Account { account::create_account_shared_data_for_test(&Rent::free())
owner: invalid_vote_state_pubkey(), } else if meta.pubkey == invalid_vote_state_pubkey() {
..Account::default() AccountSharedData::from(Account {
}) owner: invalid_vote_state_pubkey(),
} else { ..Account::default()
AccountSharedData::from(Account { })
owner: id(), } else {
..Account::default() AccountSharedData::from(Account {
}) owner: id(),
})) ..Account::default()
})
},
)
}) })
.collect(); .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 = Rent::default();
let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap()); let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap());
let clock = Clock::default(); let clock = Clock::default();
@ -587,11 +588,13 @@ mod tests {
sysvar::slot_hashes::id(), sysvar::slot_hashes::id(),
bincode::serialize(&slot_hashes).unwrap(), bincode::serialize(&slot_hashes).unwrap(),
); );
solana_program_runtime::invoke_context::mock_process_instruction_with_sysvars( mock_process_instruction_with_sysvars(
&id(), &id(),
Vec::new(), Vec::new(),
&instruction.data, &instruction.data,
&keyed_accounts, transaction_accounts,
instruction.accounts.clone(),
expected_result,
&[rent_sysvar, clock_sysvar, slot_hashes_sysvar], &[rent_sysvar, clock_sysvar, slot_hashes_sysvar],
super::process_instruction, super::process_instruction,
) )
@ -604,28 +607,30 @@ mod tests {
// these are for 100% coverage in this file // these are for 100% coverage in this file
#[test] #[test]
fn test_vote_process_instruction_decode_bail() { fn test_vote_process_instruction_decode_bail() {
assert_eq!( process_instruction(
process_instruction(&[], &[]), &[],
Vec::new(),
Vec::new(),
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
); );
} }
#[test] #[test]
fn test_spoofed_vote() { fn test_spoofed_vote() {
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&vote( &vote(
&invalid_vote_state_pubkey(), &invalid_vote_state_pubkey(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
Vote::default(), Vote::default(),
)), ),
Err(InstructionError::InvalidAccountOwner), Err(InstructionError::InvalidAccountOwner),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&update_vote_state( &update_vote_state(
&invalid_vote_state_pubkey(), &invalid_vote_state_pubkey(),
&Pubkey::default(), &Pubkey::default(),
VoteStateUpdate::default(), VoteStateUpdate::default(),
)), ),
Err(InstructionError::InvalidAccountOwner), Err(InstructionError::InvalidAccountOwner),
); );
} }
@ -639,79 +644,72 @@ mod tests {
&VoteInit::default(), &VoteInit::default(),
101, 101,
); );
assert_eq!( process_instruction_as_one_arg(&instructions[1], Err(InstructionError::InvalidAccountData));
process_instruction_as_one_arg(&instructions[1]), process_instruction_as_one_arg(
Err(InstructionError::InvalidAccountData), &vote(
);
assert_eq!(
process_instruction_as_one_arg(&vote(
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
Vote::default(), Vote::default(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&vote_switch( &vote_switch(
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
Vote::default(), Vote::default(),
Hash::default(), Hash::default(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&authorize( &authorize(
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
VoteAuthorize::Voter, VoteAuthorize::Voter,
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&update_vote_state( &update_vote_state(
&Pubkey::default(), &Pubkey::default(),
&Pubkey::default(), &Pubkey::default(),
VoteStateUpdate::default(), VoteStateUpdate::default(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&update_vote_state_switch( &update_vote_state_switch(
&Pubkey::default(), &Pubkey::default(),
&Pubkey::default(), &Pubkey::default(),
VoteStateUpdate::default(), VoteStateUpdate::default(),
Hash::default(), Hash::default(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&update_validator_identity( &update_validator_identity(
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&update_commission( &update_commission(&Pubkey::new_unique(), &Pubkey::new_unique(), 0),
&Pubkey::new_unique(),
&Pubkey::new_unique(),
0,
)),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&withdraw( &withdraw(
&Pubkey::new_unique(), &Pubkey::new_unique(),
&Pubkey::new_unique(), &Pubkey::new_unique(),
0, 0,
&Pubkey::new_unique() &Pubkey::new_unique(),
)), ),
Err(InstructionError::InvalidAccountData), Err(InstructionError::InvalidAccountData),
); );
} }
@ -730,10 +728,7 @@ mod tests {
VoteAuthorize::Voter, VoteAuthorize::Voter,
); );
instruction.accounts = instruction.accounts[0..2].to_vec(); 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( let mut instruction = authorize_checked(
&vote_pubkey, &vote_pubkey,
@ -742,10 +737,7 @@ mod tests {
VoteAuthorize::Withdrawer, VoteAuthorize::Withdrawer,
); );
instruction.accounts = instruction.accounts[0..2].to_vec(); 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 // Test with non-signing new_authorized_pubkey
let mut instruction = authorize_checked( let mut instruction = authorize_checked(
@ -755,8 +747,8 @@ mod tests {
VoteAuthorize::Voter, VoteAuthorize::Voter,
); );
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false); instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&instruction), &instruction,
Err(InstructionError::MissingRequiredSignature), Err(InstructionError::MissingRequiredSignature),
); );
@ -767,43 +759,60 @@ mod tests {
VoteAuthorize::Withdrawer, VoteAuthorize::Withdrawer,
); );
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false); instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
assert_eq!( process_instruction_as_one_arg(
process_instruction_as_one_arg(&instruction), &instruction,
Err(InstructionError::MissingRequiredSignature), Err(InstructionError::MissingRequiredSignature),
); );
// Test with new_authorized_pubkey signer // 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_address = sysvar::clock::id();
let clock_account = Rc::new(RefCell::new(account::create_account_shared_data_for_test( let clock_account = account::create_account_shared_data_for_test(&Clock::default());
&Clock::default(),
)));
let default_authorized_pubkey = Pubkey::default(); let default_authorized_pubkey = Pubkey::default();
let authorized_account = create_default_account(); let authorized_account = create_default_account();
let new_authorized_account = create_default_account(); let new_authorized_account = create_default_account();
let keyed_accounts = [ let transaction_accounts = vec![
(false, false, vote_pubkey, vote_account), (vote_pubkey, vote_account),
(false, false, clock_address, clock_account), (clock_address, clock_account),
(true, false, default_authorized_pubkey, authorized_account), (default_authorized_pubkey, authorized_account),
(true, false, new_authorized_pubkey, new_authorized_account), (new_authorized_pubkey, new_authorized_account),
]; ];
assert_eq!( let instruction_accounts = vec![
process_instruction( AccountMeta {
&serialize(&VoteInstruction::AuthorizeChecked(VoteAuthorize::Voter)).unwrap(), pubkey: vote_pubkey,
&keyed_accounts, is_signer: false,
), is_writable: false,
Ok(()) },
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(()),
); );
process_instruction(
assert_eq!( &serialize(&VoteInstruction::AuthorizeChecked(
process_instruction( VoteAuthorize::Withdrawer,
&serialize(&VoteInstruction::AuthorizeChecked( ))
VoteAuthorize::Withdrawer .unwrap(),
)) transaction_accounts,
.unwrap(), instruction_accounts,
&keyed_accounts, Ok(()),
),
Ok(())
); );
} }

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff