Skip to content

Commit

Permalink
mut data refs as slice (#15782)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington authored Mar 10, 2021
1 parent 56923c9 commit 1135ffd
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 68 deletions.
8 changes: 6 additions & 2 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,11 @@ pub mod test {
},
};
use solana_sdk::{
account::AccountSharedData, clock::Slot, hash::Hash, pubkey::Pubkey, signature::Signer,
account::{AccountSharedData, WritableAccount},
clock::Slot,
hash::Hash,
pubkey::Pubkey,
signature::Signer,
slot_history::SlotHistory,
};
use solana_vote_program::{
Expand Down Expand Up @@ -1582,7 +1586,7 @@ pub mod test {
}
VoteState::serialize(
&VoteStateVersions::new_current(vote_state),
&mut account.data,
&mut account.data_as_mut_slice(),
)
.expect("serialize state");
stakes.push((
Expand Down
4 changes: 2 additions & 2 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ pub mod tests {
self, create_genesis_config_with_vote_accounts, ValidatorVoteKeypairs,
};
use solana_sdk::{
account::AccountSharedData,
account::{AccountSharedData, WritableAccount},
epoch_schedule::EpochSchedule,
hash::Hash,
pubkey::Pubkey,
Expand Down Expand Up @@ -3353,7 +3353,7 @@ pub mod tests {
&solana_vote_program::id(),
);
let versioned = VoteStateVersions::new_current(vote_state);
VoteState::serialize(&versioned, &mut vote_account.data).unwrap();
VoteState::serialize(&versioned, vote_account.data_as_mut_slice()).unwrap();
(
solana_sdk::pubkey::new_rand(),
(stake, ArcVoteAccount::from(vote_account)),
Expand Down
27 changes: 15 additions & 12 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use solana_rbpf::{
};
use solana_runtime::message_processor::MessageProcessor;
use solana_sdk::{
account::ReadableAccount,
account::{ReadableAccount, WritableAccount},
account_utils::State,
bpf_loader, bpf_loader_deprecated,
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
Expand Down Expand Up @@ -330,7 +330,7 @@ fn process_loader_upgradeable_instruction(
return Err(InstructionError::InvalidAccountData);
}
write_program_data(
&mut buffer.try_account_ref_mut()?.data,
buffer.try_account_ref_mut()?.data_as_mut_slice(),
UpgradeableLoaderState::buffer_data_offset()? + offset as usize,
&bytes,
invoke_context,
Expand Down Expand Up @@ -434,7 +434,7 @@ fn process_loader_upgradeable_instruction(
slot: clock.slot,
upgrade_authority_address,
})?;
programdata.try_account_ref_mut()?.data
programdata.try_account_ref_mut()?.data_as_mut_slice()
[programdata_data_offset..programdata_data_offset + buffer_data_len]
.copy_from_slice(&buffer.try_account_ref()?.data()[buffer_data_offset..]);

Expand Down Expand Up @@ -562,10 +562,10 @@ fn process_loader_upgradeable_instruction(
slot: clock.slot,
upgrade_authority_address: Some(*authority.unsigned_key()),
})?;
programdata.try_account_ref_mut()?.data
programdata.try_account_ref_mut()?.data_as_mut_slice()
[programdata_data_offset..programdata_data_offset + buffer_data_len]
.copy_from_slice(&buffer.try_account_ref()?.data()[buffer_data_offset..]);
for i in &mut programdata.try_account_ref_mut()?.data
for i in &mut programdata.try_account_ref_mut()?.data_as_mut_slice()
[programdata_data_offset + buffer_data_len..]
{
*i = 0
Expand Down Expand Up @@ -668,7 +668,7 @@ fn process_loader_instruction(
return Err(InstructionError::MissingRequiredSignature);
}
write_program_data(
&mut program.try_account_ref_mut()?.data,
&mut program.try_account_ref_mut()?.data_as_mut_slice(),
offset as usize,
&bytes,
invoke_context,
Expand Down Expand Up @@ -1023,7 +1023,7 @@ mod tests {
program_account.borrow_mut().executable = false; // Un-finalize the account

// Case: Finalize
program_account.borrow_mut().data[0] = 0; // bad elf
program_account.borrow_mut().data_as_mut_slice()[0] = 0; // bad elf
let keyed_accounts = vec![KeyedAccount::new(&program_key, true, &program_account)];
assert_eq!(
Err(InstructionError::InvalidAccountData),
Expand Down Expand Up @@ -1552,7 +1552,7 @@ mod tests {
authority_address: Some(upgrade_authority_keypair.pubkey()),
})
.unwrap();
buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
buffer_account.data_as_mut_slice()[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
.copy_from_slice(&elf);
let program_account = AccountSharedData::new(
min_programdata_balance,
Expand Down Expand Up @@ -2039,7 +2039,8 @@ mod tests {
authority_address: Some(upgrade_authority_keypair.pubkey()),
})
.unwrap();
modified_buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
modified_buffer_account.data_as_mut_slice()
[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
.copy_from_slice(&elf);
modified_buffer_account.data.truncate(5);
bank.store_account(&buffer_address, &modified_buffer_account);
Expand Down Expand Up @@ -2080,7 +2081,8 @@ mod tests {
authority_address: Some(buffer_address),
})
.unwrap();
modified_buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
modified_buffer_account.data_as_mut_slice()
[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
.copy_from_slice(&elf);
bank.store_account(&buffer_address, &modified_buffer_account);
bank.store_account(&program_keypair.pubkey(), &AccountSharedData::default());
Expand Down Expand Up @@ -2120,7 +2122,8 @@ mod tests {
authority_address: None,
})
.unwrap();
modified_buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
modified_buffer_account.data_as_mut_slice()
[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
.copy_from_slice(&elf);
bank.store_account(&buffer_address, &modified_buffer_account);
bank.store_account(&program_keypair.pubkey(), &AccountSharedData::default());
Expand Down Expand Up @@ -2209,7 +2212,7 @@ mod tests {
authority_address: Some(*buffer_authority),
})
.unwrap();
buffer_account.borrow_mut().data
buffer_account.borrow_mut().data_as_mut_slice()
[UpgradeableLoaderState::buffer_data_offset().unwrap()..]
.copy_from_slice(&elf_new);
let programdata_account = AccountSharedData::new_ref(
Expand Down
16 changes: 11 additions & 5 deletions programs/bpf_loader/src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use solana_sdk::{
account::ReadableAccount, bpf_loader_deprecated, entrypoint::MAX_PERMITTED_DATA_INCREASE,
instruction::InstructionError, keyed_account::KeyedAccount, pubkey::Pubkey,
account::{ReadableAccount, WritableAccount},
bpf_loader_deprecated,
entrypoint::MAX_PERMITTED_DATA_INCREASE,
instruction::InstructionError,
keyed_account::KeyedAccount,
pubkey::Pubkey,
};
use std::{
io::prelude::*,
Expand Down Expand Up @@ -120,7 +124,7 @@ pub fn deserialize_parameters_unaligned(
let end = start + keyed_account.data_len()?;
keyed_account
.try_account_ref_mut()?
.data
.data_as_mut_slice()
.clone_from_slice(&buffer[start..end]);
start += keyed_account.data_len()? // data
+ size_of::<Pubkey>() // owner
Expand Down Expand Up @@ -222,7 +226,7 @@ pub fn deserialize_parameters_aligned(
start += size_of::<Pubkey>(); // owner
account.lamports = LittleEndian::read_u64(&buffer[start..]);
start += size_of::<u64>(); // lamports
let pre_len = account.data.len();
let pre_len = account.data_as_mut_slice().len();
let post_len = LittleEndian::read_u64(&buffer[start..]) as usize;
start += size_of::<u64>(); // data length
let mut data_end = start + pre_len;
Expand All @@ -232,7 +236,9 @@ pub fn deserialize_parameters_aligned(
account.data.resize(post_len, 0);
data_end = start + post_len;
}
account.data.clone_from_slice(&buffer[start..data_end]);
account
.data_as_mut_slice()
.clone_from_slice(&buffer[start..data_end]);
start += pre_len + MAX_PERMITTED_DATA_INCREASE; // data
start += (start as *const u8).align_offset(align_of::<u128>());
start += size_of::<u64>(); // rent_epoch
Expand Down
26 changes: 21 additions & 5 deletions programs/budget/src/budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use chrono::prelude::{DateTime, Utc};
use log::*;
use solana_sdk::{
account::ReadableAccount,
account::{ReadableAccount, WritableAccount},
hash::hash,
instruction::InstructionError,
keyed_account::{next_keyed_account, KeyedAccount},
Expand Down Expand Up @@ -147,7 +147,11 @@ pub fn process_instruction(
pending_budget: Some(*expr),
initialized: true,
};
budget_state.serialize(&mut contract_keyed_account.try_account_ref_mut()?.data)
budget_state.serialize(
&mut contract_keyed_account
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
BudgetInstruction::ApplyTimestamp(dt) => {
let witness_keyed_account = next_keyed_account(keyed_accounts_iter)?;
Expand All @@ -173,7 +177,11 @@ pub fn process_instruction(
dt,
)?;
trace!("apply timestamp committed");
budget_state.serialize(&mut contract_keyed_account.try_account_ref_mut()?.data)
budget_state.serialize(
&mut contract_keyed_account
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
BudgetInstruction::ApplySignature => {
let witness_keyed_account = next_keyed_account(keyed_accounts_iter)?;
Expand All @@ -198,7 +206,11 @@ pub fn process_instruction(
next_keyed_account(keyed_accounts_iter),
)?;
trace!("apply signature committed");
budget_state.serialize(&mut contract_keyed_account.try_account_ref_mut()?.data)
budget_state.serialize(
&mut contract_keyed_account
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
BudgetInstruction::ApplyAccountData => {
let witness_keyed_account = next_keyed_account(keyed_accounts_iter)?;
Expand All @@ -219,7 +231,11 @@ pub fn process_instruction(
next_keyed_account(keyed_accounts_iter),
)?;
trace!("apply account data committed");
budget_state.serialize(&mut contract_keyed_account.try_account_ref_mut()?.data)
budget_state.serialize(
&mut contract_keyed_account
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::ConfigKeys;
use bincode::deserialize;
use solana_sdk::{
account::ReadableAccount,
account::{ReadableAccount, WritableAccount},
feature_set, ic_msg,
instruction::InstructionError,
keyed_account::{next_keyed_account, KeyedAccount},
Expand Down Expand Up @@ -120,7 +120,10 @@ pub fn process_instruction(
return Err(InstructionError::InvalidInstructionData);
}

config_keyed_account.try_account_ref_mut()?.data[..data.len()].copy_from_slice(&data);
config_keyed_account
.try_account_ref_mut()?
.data_as_mut_slice()[..data.len()]
.copy_from_slice(&data);
Ok(())
}

Expand Down
Loading

0 comments on commit 1135ffd

Please sign in to comment.