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
11 changed files with 2907 additions and 2342 deletions

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