Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
AccountSharedData.set_data (#15781)
Browse files Browse the repository at this point in the history
* account.set_data and resize_data

* remove data_resize
  • Loading branch information
jeffwashington authored Mar 11, 2021
1 parent e1ceb43 commit 3419a54
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 25 deletions.
14 changes: 8 additions & 6 deletions genesis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
14 changes: 7 additions & 7 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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();

Expand Down Expand Up @@ -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)];
Expand Down Expand Up @@ -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)];

Expand Down Expand Up @@ -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)];

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 8 additions & 6 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

{
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/system_instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))"
Expand Down

0 comments on commit 3419a54

Please sign in to comment.