mut data refs as slice (#15782)

This commit is contained in:
Jeff Washington (jwash) 2021-03-10 15:28:03 -06:00 committed by GitHub
parent 56923c91bf
commit 1135ffd595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 125 additions and 68 deletions

View File

@ -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::{
@ -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((

View File

@ -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,
@ -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)),

View File

@ -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},
@ -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,
@ -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..]);
@ -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
@ -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,
@ -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),
@ -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,
@ -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);
@ -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());
@ -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());
@ -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(

View File

@ -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::*,
@ -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
@ -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;
@ -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

View File

@ -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},
@ -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)?;
@ -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)?;
@ -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)?;
@ -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(),
)
}
}
}

View File

@ -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},
@ -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(())
}

View File

@ -8,9 +8,13 @@ use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::Serialize;
use solana_metrics::inc_new_counter_info;
use solana_sdk::{
account::ReadableAccount, decode_error::DecodeError, instruction::InstructionError,
keyed_account::KeyedAccount, process_instruction::InvokeContext,
program_utils::limited_deserialize, pubkey::Pubkey,
account::{ReadableAccount, WritableAccount},
decode_error::DecodeError,
instruction::InstructionError,
keyed_account::KeyedAccount,
process_instruction::InvokeContext,
program_utils::limited_deserialize,
pubkey::Pubkey,
};
use std::cmp;
use thiserror::Error;
@ -198,7 +202,7 @@ impl ExchangeProcessor {
),
&mut keyed_accounts[NEW_ACCOUNT_INDEX]
.try_account_ref_mut()?
.data,
.data_as_mut_slice(),
)
}
@ -244,7 +248,7 @@ impl ExchangeProcessor {
&ExchangeState::Account(from_account),
&mut keyed_accounts[FROM_ACCOUNT_INDEX]
.try_account_ref_mut()?
.data,
.data_as_mut_slice(),
)?;
}
ExchangeState::Trade(mut from_trade) => {
@ -274,7 +278,7 @@ impl ExchangeProcessor {
&ExchangeState::Trade(from_trade),
&mut keyed_accounts[FROM_ACCOUNT_INDEX]
.try_account_ref_mut()?
.data,
.data_as_mut_slice(),
)?;
}
_ => {
@ -286,7 +290,9 @@ impl ExchangeProcessor {
Self::serialize(
&ExchangeState::Account(to_account),
&mut keyed_accounts[TO_ACCOUNT_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[TO_ACCOUNT_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
@ -340,11 +346,15 @@ impl ExchangeProcessor {
price: info.price,
tokens_settled: 0,
}),
&mut keyed_accounts[ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)?;
Self::serialize(
&ExchangeState::Account(account),
&mut keyed_accounts[ACCOUNT_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[ACCOUNT_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
@ -377,7 +387,9 @@ impl ExchangeProcessor {
// Turn trade order into a token account
Self::serialize(
&ExchangeState::Account(account),
&mut keyed_accounts[ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)
}
@ -434,12 +446,16 @@ impl ExchangeProcessor {
// Turn into token account
Self::serialize(
&ExchangeState::Account(Self::trade_to_token_account(&from_order)),
&mut keyed_accounts[TO_ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[TO_ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)?;
} else {
Self::serialize(
&ExchangeState::Trade(to_order),
&mut keyed_accounts[TO_ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[TO_ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)?;
}
@ -447,12 +463,16 @@ impl ExchangeProcessor {
// Turn into token account
Self::serialize(
&ExchangeState::Account(Self::trade_to_token_account(&from_order)),
&mut keyed_accounts[FROM_ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[FROM_ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)?;
} else {
Self::serialize(
&ExchangeState::Trade(from_order),
&mut keyed_accounts[FROM_ORDER_INDEX].try_account_ref_mut()?.data,
&mut keyed_accounts[FROM_ORDER_INDEX]
.try_account_ref_mut()?
.data_as_mut_slice(),
)?;
}
@ -460,7 +480,7 @@ impl ExchangeProcessor {
&ExchangeState::Account(profit_account),
&mut keyed_accounts[PROFIT_ACCOUNT_INDEX]
.try_account_ref_mut()?
.data,
.data_as_mut_slice(),
)
}
}

View File

@ -3,7 +3,7 @@
use crate::ownable_instruction::OwnableError;
use bincode::serialize_into;
use solana_sdk::{
account::ReadableAccount,
account::{ReadableAccount, WritableAccount},
instruction::InstructionError,
keyed_account::{next_keyed_account, KeyedAccount},
process_instruction::InvokeContext,
@ -52,7 +52,7 @@ pub fn process_instruction(
}
let mut account = account_keyed_account.try_account_ref_mut()?;
serialize_into(&mut account.data[..], &account_owner_pubkey)
serialize_into(account.data_as_mut_slice(), &account_owner_pubkey)
.map_err(|_| InstructionError::AccountDataTooSmall)
}

View File

@ -7,7 +7,7 @@ use chrono::prelude::*;
use solana_config_program::date_instruction::DateConfig;
use solana_config_program::get_config_data;
use solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account::{AccountSharedData, ReadableAccount, WritableAccount},
feature_set,
instruction::InstructionError,
keyed_account::{next_keyed_account, KeyedAccount},
@ -147,7 +147,7 @@ pub fn process_instruction(
}
}
vest_state.serialize(&mut contract_account.data)
vest_state.serialize(contract_account.data_as_mut_slice())
}
#[cfg(test)]

View File

@ -119,14 +119,14 @@ impl VestState {
mod test {
use super::*;
use crate::id;
use solana_sdk::account::{AccountSharedData, ReadableAccount};
use solana_sdk::account::{AccountSharedData, ReadableAccount, WritableAccount};
use solana_sdk::system_program;
#[test]
fn test_serializer() {
let mut a = AccountSharedData::new(0, 512, &id());
let b = VestState::default();
b.serialize(&mut a.data).unwrap();
b.serialize(a.data_as_mut_slice()).unwrap();
let c = VestState::deserialize(&a.data()).unwrap();
assert_eq!(b, c);
}
@ -136,7 +136,7 @@ mod test {
let mut a = AccountSharedData::new(0, 1, &id());
let b = VestState::default();
assert_eq!(
b.serialize(&mut a.data),
b.serialize(a.data_as_mut_slice()),
Err(InstructionError::AccountDataTooSmall)
);
}
@ -151,7 +151,9 @@ mod test {
start_date_time: Utc.ymd(2019, 1, 1).and_hms(0, 0, 0),
..VestState::default()
};
vest_state.serialize(&mut contract_account.data).unwrap();
vest_state
.serialize(contract_account.data_as_mut_slice())
.unwrap();
let current_date = Utc.ymd(2020, 1, 1);
assert_eq!(vest_state.calc_vested_lamports(current_date), 1);
@ -177,7 +179,9 @@ mod test {
start_date_time: Utc.ymd(2019, 1, 1).and_hms(0, 0, 0),
..VestState::default()
};
vest_state.serialize(&mut contract_account.data).unwrap();
vest_state
.serialize(contract_account.data_as_mut_slice())
.unwrap();
let current_date = Utc.ymd(2020, 1, 1);
assert_eq!(vest_state.calc_vested_lamports(current_date), 1);

View File

@ -5052,7 +5052,7 @@ pub mod tests {
use assert_matches::assert_matches;
use rand::{thread_rng, Rng};
use solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account::{AccountSharedData, ReadableAccount, WritableAccount},
hash::HASH_BYTES,
pubkey::PUBKEY_BYTES,
};
@ -6784,7 +6784,7 @@ pub mod tests {
// Account data may not be modified
let mut account_modified = account.clone();
account_modified.data[0] = 42;
account_modified.data_as_mut_slice()[0] = 42;
assert_ne!(
hash,
AccountsDb::hash_frozen_account_data(&account_modified)
@ -6888,7 +6888,7 @@ pub mod tests {
let ancestors = vec![(0, 0)].into_iter().collect();
db.freeze_accounts(&ancestors, &[frozen_pubkey]);
account.data[0] = 42;
account.data_as_mut_slice()[0] = 42;
db.store_uncached(0, &[(&frozen_pubkey, &account)]);
}

View File

@ -5,7 +5,7 @@ use crate::{
use log::*;
use serde::{Deserialize, Serialize};
use solana_sdk::{
account::{AccountSharedData, ReadableAccount},
account::{AccountSharedData, ReadableAccount, WritableAccount},
account_utils::StateMut,
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
feature_set::{instructions_sysvar_enabled, FeatureSet},
@ -210,7 +210,7 @@ impl PreAccount {
pre.data = account.data.clone();
} else {
// Copy without allocate
pre.data.clone_from_slice(&account.data);
pre.data_as_mut_slice().clone_from_slice(&account.data());
}
self.changed = true;
@ -1003,7 +1003,7 @@ impl MessageProcessor {
if solana_sdk::sysvar::instructions::check_id(key) {
let mut mut_account_ref = accounts[i].borrow_mut();
solana_sdk::sysvar::instructions::store_current_index(
&mut mut_account_ref.data,
mut_account_ref.data_as_mut_slice(),
instruction_index as u16,
);
break;
@ -1163,7 +1163,8 @@ mod tests {
);
// modify account owned by the program
accounts[owned_index].borrow_mut().data[0] = (MAX_DEPTH + owned_index) as u8;
accounts[owned_index].borrow_mut().data_as_mut_slice()[0] =
(MAX_DEPTH + owned_index) as u8;
let mut these_accounts = accounts[not_owned_index..owned_index + 1].to_vec();
these_accounts.push(Rc::new(RefCell::new(AccountSharedData::new(
1,
@ -1183,7 +1184,8 @@ mod tests {
// modify account not owned by the program
let data = accounts[not_owned_index].borrow_mut().data()[0];
accounts[not_owned_index].borrow_mut().data[0] = (MAX_DEPTH + not_owned_index) as u8;
accounts[not_owned_index].borrow_mut().data_as_mut_slice()[0] =
(MAX_DEPTH + not_owned_index) as u8;
assert_eq!(
invoke_context.verify_and_update(
&message,
@ -1200,7 +1202,7 @@ mod tests {
.data()[0],
data
);
accounts[not_owned_index].borrow_mut().data[0] = data;
accounts[not_owned_index].borrow_mut().data_as_mut_slice()[0] = data;
invoke_context.pop();
}
@ -1969,10 +1971,10 @@ mod tests {
MockInstruction::NoopSuccess => (),
MockInstruction::NoopFail => return Err(InstructionError::GenericError),
MockInstruction::ModifyOwned => {
keyed_accounts[0].try_account_ref_mut()?.data[0] = 1
keyed_accounts[0].try_account_ref_mut()?.data_as_mut_slice()[0] = 1
}
MockInstruction::ModifyNotOwned => {
keyed_accounts[1].try_account_ref_mut()?.data[0] = 1
keyed_accounts[1].try_account_ref_mut()?.data_as_mut_slice()[0] = 1
}
}
} else {
@ -2027,7 +2029,7 @@ mod tests {
];
// not owned account modified by the caller (before the invoke)
accounts[0].borrow_mut().data[0] = 1;
accounts[0].borrow_mut().data_as_mut_slice()[0] = 1;
let instruction = Instruction::new_with_bincode(
callee_program_id,
&MockInstruction::NoopSuccess,
@ -2050,7 +2052,7 @@ mod tests {
),
Err(InstructionError::ExternalAccountDataModified)
);
accounts[0].borrow_mut().data[0] = 0;
accounts[0].borrow_mut().data_as_mut_slice()[0] = 0;
let cases = vec![
(MockInstruction::NoopSuccess, Ok(())),

View File

@ -1,5 +1,4 @@
use crate::account::AccountSharedData;
use crate::account::ReadableAccount;
use crate::account::{AccountSharedData, ReadableAccount, WritableAccount};
pub use solana_program::feature::*;
pub fn from_account<T: ReadableAccount>(account: &T) -> Option<Feature> {
@ -11,7 +10,7 @@ pub fn from_account<T: ReadableAccount>(account: &T) -> Option<Feature> {
}
pub fn to_account(feature: &Feature, account: &mut AccountSharedData) -> Option<()> {
bincode::serialize_into(&mut account.data[..], feature).ok()
bincode::serialize_into(account.data_as_mut_slice(), feature).ok()
}
pub fn create_account(feature: &Feature, lamports: u64) -> AccountSharedData {