Skip to content

Commit

Permalink
Moves BuiltinPrograms from bank.rs into the program runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Apr 24, 2023
1 parent 956eba2 commit f39000f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 81 deletions.
27 changes: 27 additions & 0 deletions program-runtime/src/builtin_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,30 @@ impl AbiExample for BuiltinProgram {
}
}
}

#[derive(Debug, Clone, Default)]
pub struct BuiltinPrograms {
pub vec: Vec<BuiltinProgram>,
}

#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl AbiExample for BuiltinPrograms {
fn example() -> Self {
Self::default()
}
}

impl BuiltinPrograms {
pub fn new_mock(
program_id: Pubkey,
process_instruction: ProcessInstructionWithContext,
) -> Self {
Self {
vec: vec![BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id,
process_instruction,
}],
}
}
}
41 changes: 15 additions & 26 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
accounts_data_meter::AccountsDataMeter,
builtin_program::{BuiltinProgram, ProcessInstructionWithContext},
builtin_program::{BuiltinPrograms, ProcessInstructionWithContext},
compute_budget::ComputeBudget,
executor_cache::TransactionExecutorCache,
ic_logger_msg, ic_msg,
Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct InvokeContext<'a> {
pub transaction_context: &'a mut TransactionContext,
rent: Rent,
pre_accounts: Vec<PreAccount>,
builtin_programs: &'a [BuiltinProgram],
builtin_programs: &'a BuiltinPrograms,
sysvar_cache: &'a SysvarCache,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
Expand All @@ -172,7 +172,7 @@ impl<'a> InvokeContext<'a> {
pub fn new(
transaction_context: &'a mut TransactionContext,
rent: Rent,
builtin_programs: &'a [BuiltinProgram],
builtin_programs: &'a BuiltinPrograms,
sysvar_cache: &'a SysvarCache,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
Expand Down Expand Up @@ -714,7 +714,7 @@ impl<'a> InvokeContext<'a> {
}
};

for entry in self.builtin_programs {
for entry in self.builtin_programs.vec.iter() {
if entry.program_id == builtin_id {
let program_id =
*instruction_context.get_last_program_key(self.transaction_context)?;
Expand Down Expand Up @@ -877,9 +877,9 @@ macro_rules! with_mock_invoke_context {
},
std::{cell::RefCell, rc::Rc, sync::Arc},
$crate::{
compute_budget::ComputeBudget, executor_cache::TransactionExecutorCache,
invoke_context::InvokeContext, log_collector::LogCollector,
sysvar_cache::SysvarCache,
builtin_program::BuiltinPrograms, compute_budget::ComputeBudget,
executor_cache::TransactionExecutorCache, invoke_context::InvokeContext,
log_collector::LogCollector, sysvar_cache::SysvarCache,
},
};
let compute_budget = ComputeBudget::default();
Expand All @@ -890,6 +890,7 @@ macro_rules! with_mock_invoke_context {
compute_budget.max_instruction_trace_length,
);
$transaction_context.enable_cap_accounts_data_allocations_per_transaction();
let builtin_programs = BuiltinPrograms::default();
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.fill_missing_entries(|pubkey, callback| {
for index in 0..$transaction_context.get_number_of_accounts() {
Expand All @@ -911,7 +912,7 @@ macro_rules! with_mock_invoke_context {
let mut $invoke_context = InvokeContext::new(
&mut $transaction_context,
Rent::default(),
&[],
&builtin_programs,
&sysvar_cache,
Some(LogCollector::new_ref()),
compute_budget,
Expand Down Expand Up @@ -963,12 +964,8 @@ pub fn mock_process_instruction<F: FnMut(&mut InvokeContext), G: FnMut(&mut Invo
let processor_account = AccountSharedData::new(0, 0, &native_loader::id());
transaction_accounts.push((*loader_id, processor_account));
with_mock_invoke_context!(invoke_context, transaction_context, transaction_accounts);
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: *loader_id,
process_instruction,
}];
invoke_context.builtin_programs = builtin_programs;
let builtin_programs = BuiltinPrograms::new_mock(*loader_id, process_instruction);
invoke_context.builtin_programs = &builtin_programs;
pre_adjustments(&mut invoke_context);
let result = invoke_context.process_instruction(
instruction_data,
Expand Down Expand Up @@ -1217,12 +1214,8 @@ mod tests {
})
.collect::<Vec<_>>();
with_mock_invoke_context!(invoke_context, transaction_context, transaction_accounts);
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: callee_program_id,
process_instruction,
}];
invoke_context.builtin_programs = builtin_programs;
let builtin_programs = BuiltinPrograms::new_mock(callee_program_id, process_instruction);
invoke_context.builtin_programs = &builtin_programs;

// Account modification tests
let cases = vec![
Expand Down Expand Up @@ -1363,12 +1356,8 @@ mod tests {
},
];
with_mock_invoke_context!(invoke_context, transaction_context, transaction_accounts);
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: program_key,
process_instruction,
}];
invoke_context.builtin_programs = builtin_programs;
let builtin_programs = BuiltinPrograms::new_mock(program_key, process_instruction);
invoke_context.builtin_programs = &builtin_programs;

// Test: Resize the account to *the same size*, so not consuming any additional size; this must succeed
{
Expand Down
34 changes: 17 additions & 17 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use {
solana_banks_server::banks_server::start_local_server,
solana_bpf_loader_program::serialization::serialize_parameters,
solana_program_runtime::{
compute_budget::ComputeBudget, ic_msg, invoke_context::ProcessInstructionWithContext,
stable_log, timings::ExecuteTimings,
builtin_program::{BuiltinProgram, BuiltinPrograms, ProcessInstructionWithContext},
compute_budget::ComputeBudget,
ic_msg, stable_log,
timings::ExecuteTimings,
},
solana_runtime::{
accounts_background_service::{AbsRequestSender, SnapshotRequestType},
bank::Bank,
bank_forks::BankForks,
builtins::Builtin,
commitment::BlockCommitmentCache,
epoch_accounts_hash::EpochAccountsHash,
genesis_utils::{create_genesis_config_with_leader_ex, GenesisConfigInfo},
Expand Down Expand Up @@ -436,7 +437,7 @@ pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> {

pub struct ProgramTest {
accounts: Vec<(Pubkey, AccountSharedData)>,
builtins: Vec<Builtin>,
builtin_programs: BuiltinPrograms,
compute_max_units: Option<u64>,
prefer_bpf: bool,
use_bpf_jit: bool,
Expand Down Expand Up @@ -474,7 +475,7 @@ impl Default for ProgramTest {

Self {
accounts: vec![],
builtins: vec![],
builtin_programs: BuiltinPrograms::default(),
compute_max_units: None,
prefer_bpf,
use_bpf_jit: false,
Expand Down Expand Up @@ -628,12 +629,6 @@ impl ProgramTest {
);
};

let add_native = |this: &mut ProgramTest, process_fn: ProcessInstructionWithContext| {
info!("\"{}\" program loaded as native code", program_name);
this.builtins
.push(Builtin::new(program_name, program_id, process_fn));
};

let warn_invalid_program_name = || {
let valid_program_names = default_shared_object_dirs()
.iter()
Expand Down Expand Up @@ -679,7 +674,9 @@ impl ProgramTest {
// processor function as is.
//
// TODO: figure out why tests hang if a processor panics when running native code.
(false, _, Some(process)) => add_native(self, process),
(false, _, Some(process)) => {
self.add_builtin_program(program_name, program_id, process)
}

// Invalid: `test-sbf` invocation with no matching SBF shared object.
(true, None, _) => {
Expand All @@ -704,8 +701,11 @@ impl ProgramTest {
process_instruction: ProcessInstructionWithContext,
) {
info!("\"{}\" builtin program", program_name);
self.builtins
.push(Builtin::new(program_name, program_id, process_instruction));
self.builtin_programs.vec.push(BuiltinProgram {
name: program_name.to_string(),
program_id,
process_instruction,
});
}

/// Deactivate a runtime feature.
Expand Down Expand Up @@ -816,11 +816,11 @@ impl ProgramTest {
}

// User-supplied additional builtins
for builtin in self.builtins.iter() {
for builtin in self.builtin_programs.vec.iter() {
bank.add_builtin(
&builtin.name,
&builtin.id,
builtin.process_instruction_with_context,
&builtin.program_id,
builtin.process_instruction,
);
}

Expand Down
16 changes: 2 additions & 14 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use {
solana_perf::perf_libs,
solana_program_runtime::{
accounts_data_meter::MAX_ACCOUNTS_DATA_LEN,
builtin_program::{BuiltinProgram, ProcessInstructionWithContext},
builtin_program::{BuiltinProgram, BuiltinPrograms, ProcessInstructionWithContext},
compute_budget::{self, ComputeBudget},
executor_cache::{BankExecutorCache, TransactionExecutorCache, MAX_CACHED_EXECUTORS},
loaded_programs::{LoadedProgram, LoadedProgramType, LoadedPrograms, WorkingSlot},
Expand Down Expand Up @@ -887,18 +887,6 @@ impl AbiExample for OptionalDropCallback {
}
}

#[derive(Debug, Clone, Default)]
pub struct BuiltinPrograms {
pub vec: Vec<BuiltinProgram>,
}

#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl AbiExample for BuiltinPrograms {
fn example() -> Self {
Self::default()
}
}

/// Manager for the state of all accounts and programs after processing its entries.
/// AbiExample is needed even without Serialize/Deserialize; actual (de-)serialization
/// are implemented elsewhere for versioning
Expand Down Expand Up @@ -4282,7 +4270,7 @@ impl Bank {

let mut process_message_time = Measure::start("process_message_time");
let process_result = MessageProcessor::process_message(
&self.builtin_programs.vec,
&self.builtin_programs,
tx.message(),
&loaded_transaction.program_indices,
&mut transaction_context,
Expand Down
37 changes: 13 additions & 24 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
serde::{Deserialize, Serialize},
solana_measure::measure::Measure,
solana_program_runtime::{
builtin_program::BuiltinProgram,
builtin_program::BuiltinPrograms,
compute_budget::ComputeBudget,
executor_cache::TransactionExecutorCache,
invoke_context::InvokeContext,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl MessageProcessor {
/// The accounts are committed back to the bank only if every instruction succeeds.
#[allow(clippy::too_many_arguments)]
pub fn process_message(
builtin_programs: &[BuiltinProgram],
builtin_programs: &BuiltinPrograms,
message: &SanitizedMessage,
program_indices: &[Vec<IndexOfAccount>],
transaction_context: &mut TransactionContext,
Expand Down Expand Up @@ -252,11 +252,8 @@ mod tests {
let mock_system_program_id = Pubkey::new_unique();

let rent_collector = RentCollector::default();
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: mock_system_program_id,
process_instruction,
}];
let builtin_programs =
BuiltinPrograms::new_mock(mock_system_program_id, process_instruction);

let accounts = vec![
(
Expand Down Expand Up @@ -305,7 +302,7 @@ mod tests {
)));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -355,7 +352,7 @@ mod tests {
]),
)));
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -395,7 +392,7 @@ mod tests {
]),
)));
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -480,11 +477,7 @@ mod tests {

let mock_program_id = Pubkey::from([2u8; 32]);
let rent_collector = RentCollector::default();
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: mock_program_id,
process_instruction,
}];
let builtin_programs = BuiltinPrograms::new_mock(mock_program_id, process_instruction);

let accounts = vec![
(
Expand Down Expand Up @@ -530,7 +523,7 @@ mod tests {
)));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -564,7 +557,7 @@ mod tests {
Some(transaction_context.get_key_of_account_at_index(0).unwrap()),
)));
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -595,7 +588,7 @@ mod tests {
Some(transaction_context.get_key_of_account_at_index(0).unwrap()),
)));
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&program_indices,
&mut transaction_context,
Expand Down Expand Up @@ -644,11 +637,7 @@ mod tests {
declare_process_instruction!(process_instruction, 1, |_invoke_context| {
Err(InstructionError::Custom(0xbabb1e))
});
let builtin_programs = &[BuiltinProgram {
name: "mock instruction processor".to_string(),
program_id: mock_program_id,
process_instruction,
}];
let builtin_programs = BuiltinPrograms::new_mock(mock_program_id, process_instruction);

let mut secp256k1_account = AccountSharedData::new(1, 0, &native_loader::id());
secp256k1_account.set_executable(true);
Expand All @@ -673,7 +662,7 @@ mod tests {
)));
let sysvar_cache = SysvarCache::default();
let result = MessageProcessor::process_message(
builtin_programs,
&builtin_programs,
&message,
&[vec![0], vec![1]],
&mut transaction_context,
Expand Down

0 comments on commit f39000f

Please sign in to comment.