Skip to content

Commit

Permalink
pass feature_set to BorrowedAccount (solana-labs#34542)
Browse files Browse the repository at this point in the history
Co-authored-by: HaoranYi <[email protected]>
  • Loading branch information
HaoranYi and HaoranYi authored Dec 21, 2023
1 parent 3af306e commit 531d69f
Show file tree
Hide file tree
Showing 20 changed files with 540 additions and 269 deletions.
1 change: 1 addition & 0 deletions ledger-tool/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ pub fn program(ledger_path: &Path, matches: &ArgMatches<'_>) {
.get_current_instruction_context()
.unwrap(),
true, // copy_account_data
&invoke_context.feature_set,
)
.unwrap();

Expand Down
14 changes: 7 additions & 7 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> InvokeContext<'a> {
})?;
let borrowed_program_account = instruction_context
.try_borrow_instruction_account(self.transaction_context, program_account_index)?;
if !borrowed_program_account.is_executable() {
if !borrowed_program_account.is_executable(&self.feature_set) {
ic_msg!(self, "Account {} is not executable", callee_program_id);
return Err(InstructionError::AccountNotExecutable);
}
Expand Down Expand Up @@ -821,17 +821,17 @@ mod tests {
MockInstruction::NoopFail => return Err(InstructionError::GenericError),
MockInstruction::ModifyOwned => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::ModifyNotOwned => instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::ModifyReadonly => instruction_context
.try_borrow_instruction_account(transaction_context, 2)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::UnbalancedPush => {
instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_add_lamports(1)?;
.checked_add_lamports(1, &invoke_context.feature_set)?;
let program_id = *transaction_context.get_key_of_account_at_index(3)?;
let metas = vec![
AccountMeta::new_readonly(
Expand Down Expand Up @@ -862,7 +862,7 @@ mod tests {
}
MockInstruction::UnbalancedPop => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_add_lamports(1)?,
.checked_add_lamports(1, &invoke_context.feature_set)?,
MockInstruction::ConsumeComputeUnits {
compute_units_to_consume,
desired_result,
Expand All @@ -874,7 +874,7 @@ mod tests {
}
MockInstruction::Resize { new_len } => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.set_data(vec![0; new_len as usize])?,
.set_data(vec![0; new_len as usize], &invoke_context.feature_set)?,
}
} else {
return Err(InstructionError::InvalidInstructionData);
Expand Down
16 changes: 8 additions & 8 deletions program-runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ mod tests {
MockSystemInstruction::TransferLamports { lamports } => {
instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_sub_lamports(lamports)?;
.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.checked_add_lamports(lamports)?;
.checked_add_lamports(lamports, &invoke_context.feature_set)?;
Ok(())
}
MockSystemInstruction::ChangeData { data } => {
instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.set_data(vec![data])?;
.set_data(vec![data], &invoke_context.feature_set)?;
Ok(())
}
}
Expand Down Expand Up @@ -457,14 +457,14 @@ mod tests {
MockSystemInstruction::DoWork { lamports, data } => {
let mut dup_account = instruction_context
.try_borrow_instruction_account(transaction_context, 2)?;
dup_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
dup_account.set_data(vec![data])?;
dup_account.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
to_account.checked_add_lamports(lamports, &invoke_context.feature_set)?;
dup_account.set_data(vec![data], &invoke_context.feature_set)?;
drop(dup_account);
let mut from_account = instruction_context
.try_borrow_instruction_account(transaction_context, 0)?;
from_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
from_account.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
to_account.checked_add_lamports(lamports, &invoke_context.feature_set)?;
Ok(())
}
}
Expand Down
24 changes: 16 additions & 8 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn invoke_builtin_function(
.transaction_context
.get_current_instruction_context()?,
true, // copy_account_data // There is no VM so direct mapping can not be implemented here
&invoke_context.feature_set,
)?;

// Deserialize data back into instruction params
Expand Down Expand Up @@ -160,18 +161,25 @@ pub fn invoke_builtin_function(
if borrowed_account.is_writable() {
if let Some(account_info) = account_info_map.get(borrowed_account.get_key()) {
if borrowed_account.get_lamports() != account_info.lamports() {
borrowed_account.set_lamports(account_info.lamports())?;
borrowed_account
.set_lamports(account_info.lamports(), &invoke_context.feature_set)?;
}

if borrowed_account
.can_data_be_resized(account_info.data_len())
.is_ok()
&& borrowed_account.can_data_be_changed().is_ok()
&& borrowed_account
.can_data_be_changed(&invoke_context.feature_set)
.is_ok()
{
borrowed_account.set_data_from_slice(&account_info.data.borrow())?;
borrowed_account.set_data_from_slice(
&account_info.data.borrow(),
&invoke_context.feature_set,
)?;
}
if borrowed_account.get_owner() != account_info.owner {
borrowed_account.set_owner(account_info.owner.as_ref())?;
borrowed_account
.set_owner(account_info.owner.as_ref(), &invoke_context.feature_set)?;
}
}
}
Expand Down Expand Up @@ -282,17 +290,17 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
.unwrap();
if borrowed_account.get_lamports() != account_info.lamports() {
borrowed_account
.set_lamports(account_info.lamports())
.set_lamports(account_info.lamports(), &invoke_context.feature_set)
.unwrap();
}
let account_info_data = account_info.try_borrow_data().unwrap();
// The redundant check helps to avoid the expensive data comparison if we can
match borrowed_account
.can_data_be_resized(account_info_data.len())
.and_then(|_| borrowed_account.can_data_be_changed())
.and_then(|_| borrowed_account.can_data_be_changed(&invoke_context.feature_set))
{
Ok(()) => borrowed_account
.set_data_from_slice(&account_info_data)
.set_data_from_slice(&account_info_data, &invoke_context.feature_set)
.unwrap(),
Err(err) if borrowed_account.get_data() != *account_info_data => {
panic!("{err:?}");
Expand All @@ -302,7 +310,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
// Change the owner at the end so that we are allowed to change the lamports and data before
if borrowed_account.get_owner() != account_info.owner {
borrowed_account
.set_owner(account_info.owner.as_ref())
.set_owner(account_info.owner.as_ref(), &invoke_context.feature_set)
.unwrap();
}
if instruction_account.is_writable {
Expand Down
22 changes: 12 additions & 10 deletions programs/address-lookup-table/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ impl Processor {
let instruction_context = transaction_context.get_current_instruction_context()?;
let mut lookup_table_account =
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
lookup_table_account.set_state(&ProgramState::LookupTable(LookupTableMeta::new(
authority_key,
)))?;
lookup_table_account.set_state(
&ProgramState::LookupTable(LookupTableMeta::new(authority_key)),
&invoke_context.feature_set,
)?;

Ok(())
}
Expand Down Expand Up @@ -213,7 +214,7 @@ impl Processor {
let mut lookup_table_meta = lookup_table.meta;
lookup_table_meta.authority = None;
AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;

Expand Down Expand Up @@ -305,11 +306,12 @@ impl Processor {
)?;
{
AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;
for new_address in new_addresses {
lookup_table_account.extend_from_slice(new_address.as_ref())?;
lookup_table_account
.extend_from_slice(new_address.as_ref(), &invoke_context.feature_set)?;
}
}
drop(lookup_table_account);
Expand Down Expand Up @@ -381,7 +383,7 @@ impl Processor {
lookup_table_meta.deactivation_slot = clock.slot;

AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;

Expand Down Expand Up @@ -456,13 +458,13 @@ impl Processor {

let mut recipient_account =
instruction_context.try_borrow_instruction_account(transaction_context, 2)?;
recipient_account.checked_add_lamports(withdrawn_lamports)?;
recipient_account.checked_add_lamports(withdrawn_lamports, &invoke_context.feature_set)?;
drop(recipient_account);

let mut lookup_table_account =
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
lookup_table_account.set_data_length(0)?;
lookup_table_account.set_lamports(0)?;
lookup_table_account.set_data_length(0, &invoke_context.feature_set)?;
lookup_table_account.set_lamports(0, &invoke_context.feature_set)?;

Ok(())
}
Expand Down
49 changes: 43 additions & 6 deletions programs/bpf_loader/benches/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
solana_sdk::{
account::{Account, AccountSharedData},
bpf_loader, bpf_loader_deprecated,
feature_set::FeatureSet,
pubkey::Pubkey,
sysvar::rent::Rent,
transaction_context::{IndexOfAccount, InstructionAccount, TransactionContext},
Expand Down Expand Up @@ -126,7 +127,13 @@ fn bench_serialize_unaligned(bencher: &mut Bencher) {
.get_current_instruction_context()
.unwrap();
bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, false).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
false,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}

Expand All @@ -137,7 +144,13 @@ fn bench_serialize_unaligned_copy_account_data(bencher: &mut Bencher) {
.get_current_instruction_context()
.unwrap();
bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
true,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}

Expand All @@ -149,7 +162,13 @@ fn bench_serialize_aligned(bencher: &mut Bencher) {
.unwrap();

bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, false).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
false,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}

Expand All @@ -161,7 +180,13 @@ fn bench_serialize_aligned_copy_account_data(bencher: &mut Bencher) {
.unwrap();

bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, true).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
true,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}

Expand All @@ -172,7 +197,13 @@ fn bench_serialize_unaligned_max_accounts(bencher: &mut Bencher) {
.get_current_instruction_context()
.unwrap();
bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, false).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
false,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}

Expand All @@ -184,6 +215,12 @@ fn bench_serialize_aligned_max_accounts(bencher: &mut Bencher) {
.unwrap();

bencher.iter(|| {
let _ = serialize_parameters(&transaction_context, instruction_context, false).unwrap();
let _ = serialize_parameters(
&transaction_context,
instruction_context,
false,
&FeatureSet::all_enabled(),
)
.unwrap();
});
}
Loading

0 comments on commit 531d69f

Please sign in to comment.