AccountSharedData.set_data (#15781)

* account.set_data and resize_data

* remove data_resize
This commit is contained in:
Jeff Washington (jwash) 2021-03-11 16:40:45 -06:00 committed by GitHub
parent e1ceb430e3
commit 3419a5446e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 25 deletions

View File

@ -84,12 +84,14 @@ pub fn load_genesis_accounts(file: &str, genesis_config: &mut GenesisConfig) ->
let mut account = AccountSharedData::new(account_details.balance, 0, &owner_program_id);
if account_details.data != "~" {
account.data = base64::decode(account_details.data.as_str()).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Invalid account data: {}: {:?}", account_details.data, err),
)
})?;
account.set_data(
base64::decode(account_details.data.as_str()).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Invalid account data: {}: {:?}", account_details.data, err),
)
})?,
);
}
account.executable = account_details.executable;
lamports += account.lamports;

View File

@ -161,7 +161,7 @@ pub fn builtin_process_instruction(
let key = keyed_account.unsigned_key();
let (lamports, data, _owner) = &account_refs[key];
account.lamports = **lamports.borrow();
account.data = data.borrow().to_vec();
account.set_data(data.borrow().to_vec());
}
}

View File

@ -941,7 +941,7 @@ mod tests {
// Case: Write bytes to an offset
#[allow(unused_mut)]
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, true, &program_account)];
keyed_accounts[0].account.borrow_mut().data = vec![0; 6];
keyed_accounts[0].account.borrow_mut().set_data(vec![0; 6]);
assert_eq!(
Ok(()),
process_instruction(
@ -959,7 +959,7 @@ mod tests {
// Case: Overflow
#[allow(unused_mut)]
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, true, &program_account)];
keyed_accounts[0].account.borrow_mut().data = vec![0; 5];
keyed_accounts[0].account.borrow_mut().set_data(vec![0; 5]);
assert_eq!(
Err(InstructionError::AccountDataTooSmall),
process_instruction(
@ -981,7 +981,7 @@ mod tests {
file.read_to_end(&mut elf).unwrap();
let program_account =
AccountSharedData::new_ref(rent.minimum_balance(elf.len()), 0, &program_id);
program_account.borrow_mut().data = elf;
program_account.borrow_mut().set_data(elf);
let keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)];
let instruction_data = bincode::serialize(&LoaderInstruction::Finalize).unwrap();
@ -1046,7 +1046,7 @@ mod tests {
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let program_account = AccountSharedData::new_ref(1, 0, &program_id);
program_account.borrow_mut().data = elf;
program_account.borrow_mut().set_data(elf);
program_account.borrow_mut().executable = true;
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)];
@ -1141,7 +1141,7 @@ mod tests {
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let program_account = AccountSharedData::new_ref(1, 0, &program_id);
program_account.borrow_mut().data = elf;
program_account.borrow_mut().set_data(elf);
program_account.borrow_mut().executable = true;
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)];
@ -1185,7 +1185,7 @@ mod tests {
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let program_account = AccountSharedData::new_ref(1, 0, &program_id);
program_account.borrow_mut().data = elf;
program_account.borrow_mut().set_data(elf);
program_account.borrow_mut().executable = true;
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)];
@ -3247,7 +3247,7 @@ mod tests {
0..255,
|bytes: &mut [u8]| {
let program_account = AccountSharedData::new_ref(1, 0, &program_id);
program_account.borrow_mut().data = bytes.to_vec();
program_account.borrow_mut().set_data(bytes.to_vec());
program_account.borrow_mut().executable = true;
let parameter_account = AccountSharedData::new_ref(1, 0, &program_id);

View File

@ -6084,10 +6084,10 @@ pub mod tests {
let mut normal_account = AccountSharedData::new(1, 0, &AccountSharedData::default().owner);
normal_account.owner = inline_spl_token_v2_0::id();
normal_account.data = account_data_with_mint.clone();
normal_account.set_data(account_data_with_mint.clone());
let mut zero_account = AccountSharedData::new(0, 0, &AccountSharedData::default().owner);
zero_account.owner = inline_spl_token_v2_0::id();
zero_account.data = account_data_with_mint;
zero_account.set_data(account_data_with_mint);
//store an account
accounts.store_uncached(0, &[(&pubkey1, &normal_account)]);

View File

@ -541,7 +541,7 @@ pub mod test_utils {
pub fn create_test_account(sample: usize) -> (StoredMeta, AccountSharedData) {
let data_len = sample % 256;
let mut account = AccountSharedData::new(sample as u64, 0, &Pubkey::default());
account.data = (0..data_len).map(|_| data_len as u8).collect();
account.set_data((0..data_len).map(|_| data_len as u8).collect());
let stored_meta = StoredMeta {
write_version: 0,
pubkey: Pubkey::default(),

View File

@ -207,7 +207,7 @@ impl PreAccount {
pre.executable = account.executable;
if pre.data().len() != account.data().len() {
// Only system account can change data size, copy with alloc
pre.data = account.data.clone();
pre.set_data(account.data().clone());
} else {
// Copy without allocate
pre.data_as_mut_slice().clone_from_slice(&account.data());
@ -806,7 +806,9 @@ impl MessageProcessor {
);
return Err(InstructionError::InvalidRealloc);
}
account_ref.try_account_ref_mut()?.data = account.data.clone();
account_ref
.try_account_ref_mut()?
.set_data(account.data().clone());
}
}
}
@ -1319,8 +1321,8 @@ mod tests {
self
}
pub fn data(mut self, pre: Vec<u8>, post: Vec<u8>) -> Self {
self.pre.account.borrow_mut().data = pre;
self.post.data = post;
self.pre.account.borrow_mut().set_data(pre);
self.post.set_data(post);
self
}
pub fn rent_epoch(mut self, pre: u64, post: u64) -> Self {
@ -1682,7 +1684,7 @@ mod tests {
}
// Change data in a read-only account
MockSystemInstruction::AttemptDataChange { data } => {
keyed_accounts[1].account.borrow_mut().data = vec![data];
keyed_accounts[1].account.borrow_mut().set_data(vec![data]);
Ok(())
}
}
@ -1849,7 +1851,7 @@ mod tests {
let mut dup_account = keyed_accounts[2].try_account_ref_mut()?;
dup_account.lamports -= lamports;
to_account.lamports += lamports;
dup_account.data = vec![data];
dup_account.set_data(vec![data]);
}
keyed_accounts[0].try_account_ref_mut()?.lamports -= lamports;
keyed_accounts[1].try_account_ref_mut()?.lamports += lamports;

View File

@ -432,7 +432,7 @@ pub mod tests {
assert!(vote_accounts.get(&vote_pubkey).is_none());
}
vote_account.data = cache_data;
vote_account.set_data(cache_data);
stakes.store(&vote_pubkey, &vote_account, true, true);
{

View File

@ -98,7 +98,7 @@ fn allocate(
return Err(SystemError::InvalidAccountDataLength.into());
}
account.data = vec![0; space as usize];
account.set_data(vec![0; space as usize]);
Ok(())
}

View File

@ -394,6 +394,9 @@ impl Account {
}
impl AccountSharedData {
pub fn set_data(&mut self, data: Vec<u8>) {
self.data = data;
}
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self {
shared_new(lamports, space, owner)
}
@ -530,6 +533,17 @@ pub mod tests {
(account1, account2)
}
#[test]
fn test_account_data_set_data() {
let key = Pubkey::new_unique();
let (_, mut account) = make_two_accounts(&key);
assert_eq!(account.data(), &vec![0, 0]);
account.set_data(vec![1, 2]);
assert_eq!(account.data(), &vec![1, 2]);
account.set_data(vec![]);
assert_eq!(account.data().len(), 0);
}
#[test]
#[should_panic(
expected = "called `Result::unwrap()` on an `Err` value: Io(Kind(UnexpectedEof))"