Skip to content

Commit

Permalink
impl AbiExample for BuiltinPrograms
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Nov 30, 2021
1 parent f1575b7 commit b58b9d7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 50 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions program-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ log = "0.4.14"
num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
serde = { version = "1.0.129", features = ["derive", "rc"] }
solana-frozen-abi = { path = "../frozen-abi", version = "=1.9.0" }
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.9.0" }
solana-logger = { path = "../logger", version = "=1.9.0" }
solana-sdk = { path = "../sdk", version = "=1.9.0" }
thiserror = "1.0"
Expand Down
38 changes: 19 additions & 19 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pub type ProcessInstructionWithContext =
fn(usize, &[u8], &mut dyn InvokeContext) -> Result<(), InstructionError>;

#[derive(Clone)]
pub struct ProgramEntry {
pub struct BuiltinProgram {
pub program_id: Pubkey,
pub process_instruction: ProcessInstructionWithContext,
}

impl std::fmt::Debug for ProgramEntry {
impl std::fmt::Debug for BuiltinProgram {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// These are just type aliases for work around of Debug-ing above pointers
type ErasedProcessInstructionWithContext = fn(
Expand Down Expand Up @@ -130,7 +130,7 @@ pub struct ThisInvokeContext<'a> {
rent: Rent,
pre_accounts: Vec<PreAccount>,
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
programs: &'a [ProgramEntry],
builtin_programs: &'a [BuiltinProgram],
sysvars: &'a [(Pubkey, Vec<u8>)],
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
Expand All @@ -149,7 +149,7 @@ impl<'a> ThisInvokeContext<'a> {
pub fn new(
rent: Rent,
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
programs: &'a [ProgramEntry],
builtin_programs: &'a [BuiltinProgram],
sysvars: &'a [(Pubkey, Vec<u8>)],
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
Expand All @@ -166,7 +166,7 @@ impl<'a> ThisInvokeContext<'a> {
rent,
pre_accounts: Vec::new(),
accounts,
programs,
builtin_programs,
sysvars,
log_collector,
current_compute_budget: compute_budget,
Expand All @@ -184,14 +184,14 @@ impl<'a> ThisInvokeContext<'a> {

pub fn new_mock_with_sysvars_and_features(
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
programs: &'a [ProgramEntry],
builtin_programs: &'a [BuiltinProgram],
sysvars: &'a [(Pubkey, Vec<u8>)],
feature_set: Arc<FeatureSet>,
) -> Self {
Self::new(
Rent::default(),
accounts,
programs,
builtin_programs,
sysvars,
None,
ComputeBudget::default(),
Expand All @@ -206,11 +206,11 @@ impl<'a> ThisInvokeContext<'a> {

pub fn new_mock(
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
programs: &'a [ProgramEntry],
builtin_programs: &'a [BuiltinProgram],
) -> Self {
Self::new_mock_with_sysvars_and_features(
accounts,
programs,
builtin_programs,
&[],
Arc::new(FeatureSet::all_enabled()),
)
Expand Down Expand Up @@ -763,7 +763,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
let root_id = root_account.unsigned_key();
let owner_id = &root_account.owner()?;
if solana_sdk::native_loader::check_id(owner_id) {
for entry in self.programs {
for entry in self.builtin_programs {
if entry.program_id == *root_id {
// Call the builtin program
return (entry.process_instruction)(
Expand All @@ -779,7 +779,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
return native_loader.process_instruction(0, instruction_data, self);
}
} else {
for entry in self.programs {
for entry in self.builtin_programs {
if entry.program_id == *owner_id {
// Call the program via a builtin loader
return (entry.process_instruction)(
Expand Down Expand Up @@ -1067,17 +1067,17 @@ mod tests {
) -> Result<(), InstructionError> {
Ok(())
}
let programs = &[
ProgramEntry {
let builtin_programs = &[
BuiltinProgram {
program_id: solana_sdk::pubkey::new_rand(),
process_instruction: mock_process_instruction,
},
ProgramEntry {
BuiltinProgram {
program_id: solana_sdk::pubkey::new_rand(),
process_instruction: mock_ix_processor,
},
];
assert!(!format!("{:?}", programs).is_empty());
assert!(!format!("{:?}", builtin_programs).is_empty());
}

#[allow(clippy::integer_arithmetic)]
Expand Down Expand Up @@ -1306,11 +1306,11 @@ mod tests {
);
let message = Message::new(&[callee_instruction], None);

let programs = &[ProgramEntry {
let builtin_programs = &[BuiltinProgram {
program_id: callee_program_id,
process_instruction: mock_process_instruction,
}];
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, programs);
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, builtin_programs);
invoke_context
.push(&message, &caller_instruction, &program_indices[..1], None)
.unwrap();
Expand Down Expand Up @@ -1433,11 +1433,11 @@ mod tests {
);
let message = Message::new(&[callee_instruction.clone()], None);

let programs = &[ProgramEntry {
let builtin_programs = &[BuiltinProgram {
program_id: callee_program_id,
process_instruction: mock_process_instruction,
}];
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, programs);
let mut invoke_context = ThisInvokeContext::new_mock(&accounts, builtin_programs);
invoke_context
.push(&message, &caller_instruction, &program_indices, None)
.unwrap();
Expand Down
2 changes: 0 additions & 2 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 26 additions & 11 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use solana_metrics::{inc_new_counter_debug, inc_new_counter_info};
use solana_program_runtime::{
instruction_recorder::InstructionRecorder,
invoke_context::{
ComputeMeter, Executor, Executors, ProcessInstructionWithContext, ProgramEntry,
BuiltinProgram, ComputeMeter, Executor, Executors, ProcessInstructionWithContext,
},
log_collector::LogCollector,
timings::ExecuteDetailsTimings,
Expand Down Expand Up @@ -873,6 +873,18 @@ 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 @@ -991,7 +1003,7 @@ pub struct Bank {
is_delta: AtomicBool,

/// The builtin programs
programs: Vec<ProgramEntry>,
builtin_programs: BuiltinPrograms,

compute_budget: Option<ComputeBudget>,

Expand Down Expand Up @@ -1150,7 +1162,7 @@ impl Bank {
stakes: RwLock::<Stakes>::default(),
epoch_stakes: HashMap::<Epoch, EpochStakes>::default(),
is_delta: AtomicBool::default(),
programs: Vec::default(),
builtin_programs: BuiltinPrograms::default(),
compute_budget: Option::<ComputeBudget>::default(),
feature_builtins: Arc::<Vec<(Builtin, Pubkey, ActivationType)>>::default(),
rewards: RwLock::<Vec<(Pubkey, RewardInfo)>>::default(),
Expand Down Expand Up @@ -1390,7 +1402,7 @@ impl Bank {
is_delta: AtomicBool::new(false),
tick_height: AtomicU64::new(parent.tick_height.load(Relaxed)),
signature_count: AtomicU64::new(0),
programs: parent.programs.clone(),
builtin_programs: parent.builtin_programs.clone(),
compute_budget: parent.compute_budget,
feature_builtins: parent.feature_builtins.clone(),
hard_forks: parent.hard_forks.clone(),
Expand Down Expand Up @@ -1599,7 +1611,7 @@ impl Bank {
stakes: RwLock::new(fields.stakes),
epoch_stakes: fields.epoch_stakes,
is_delta: AtomicBool::new(fields.is_delta),
programs: new(),
builtin_programs: new(),
compute_budget: None,
feature_builtins: new(),
rewards: new(),
Expand Down Expand Up @@ -3868,7 +3880,7 @@ impl Bank {

if let Some(legacy_message) = tx.message().legacy_message() {
process_result = MessageProcessor::process_message(
&self.programs,
&self.builtin_programs.vec,
legacy_message,
&loaded_transaction.program_indices,
&account_refcells,
Expand Down Expand Up @@ -5898,13 +5910,14 @@ impl Bank {
debug!("Adding program {} under {:?}", name, program_id);
self.add_builtin_account(name, program_id, false);
if let Some(entry) = self
.programs
.builtin_programs
.vec
.iter_mut()
.find(|entry| entry.program_id == *program_id)
{
entry.process_instruction = process_instruction;
} else {
self.programs.push(ProgramEntry {
self.builtin_programs.vec.push(BuiltinProgram {
program_id: *program_id,
process_instruction,
});
Expand All @@ -5922,7 +5935,8 @@ impl Bank {
debug!("Replacing program {} under {:?}", name, program_id);
self.add_builtin_account(name, program_id, true);
if let Some(entry) = self
.programs
.builtin_programs
.vec
.iter_mut()
.find(|entry| entry.program_id == *program_id)
{
Expand All @@ -5937,11 +5951,12 @@ impl Bank {
// Don't remove the account since the bank expects the account state to
// be idempotent
if let Some(position) = self
.programs
.builtin_programs
.vec
.iter()
.position(|entry| entry.program_id == *program_id)
{
self.programs.remove(position);
self.builtin_programs.vec.remove(position);
}
debug!("Removed program {} under {:?}", name, program_id);
}
Expand Down
26 changes: 13 additions & 13 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};
use solana_measure::measure::Measure;
use solana_program_runtime::{
instruction_recorder::InstructionRecorder,
invoke_context::{ComputeMeter, Executors, InvokeContext, ProgramEntry, ThisInvokeContext},
invoke_context::{BuiltinProgram, ComputeMeter, Executors, InvokeContext, ThisInvokeContext},
log_collector::LogCollector,
timings::ExecuteDetailsTimings,
};
Expand Down Expand Up @@ -40,7 +40,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(
programs: &[ProgramEntry],
builtin_programs: &[BuiltinProgram],
message: &Message,
program_indices: &[Vec<usize>],
accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
Expand All @@ -59,7 +59,7 @@ impl MessageProcessor {
let mut invoke_context = ThisInvokeContext::new(
rent,
accounts,
programs,
builtin_programs,
sysvars,
log_collector,
compute_budget,
Expand Down Expand Up @@ -198,7 +198,7 @@ mod tests {

let mock_system_program_id = Pubkey::new(&[2u8; 32]);
let rent_collector = RentCollector::default();
let programs = &[ProgramEntry {
let builtin_programs = &[BuiltinProgram {
program_id: mock_system_program_id,
process_instruction: mock_system_process_instruction,
}];
Expand Down Expand Up @@ -235,7 +235,7 @@ mod tests {
);

let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -265,7 +265,7 @@ mod tests {
);

let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -299,7 +299,7 @@ mod tests {
);

let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -405,7 +405,7 @@ mod tests {

let mock_program_id = Pubkey::new(&[2u8; 32]);
let rent_collector = RentCollector::default();
let programs = &[ProgramEntry {
let builtin_programs = &[BuiltinProgram {
program_id: mock_program_id,
process_instruction: mock_system_process_instruction,
}];
Expand Down Expand Up @@ -444,7 +444,7 @@ mod tests {
Some(&accounts[0].0),
);
let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -478,7 +478,7 @@ mod tests {
Some(&accounts[0].0),
);
let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -509,7 +509,7 @@ mod tests {
Some(&accounts[0].0),
);
let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&program_indices,
&accounts,
Expand Down Expand Up @@ -541,7 +541,7 @@ mod tests {
) -> Result<(), InstructionError> {
Err(InstructionError::Custom(0xbabb1e))
}
let programs = &[ProgramEntry {
let builtin_programs = &[BuiltinProgram {
program_id: mock_program_id,
process_instruction: mock_process_instruction,
}];
Expand All @@ -567,7 +567,7 @@ mod tests {
);

let result = MessageProcessor::process_message(
programs,
builtin_programs,
&message,
&[vec![0], vec![1]],
&accounts,
Expand Down
Loading

0 comments on commit b58b9d7

Please sign in to comment.