Skip to content

Commit

Permalink
Expose compute settings to allow for rollover upgrade configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed Aug 21, 2020
1 parent b8ba5de commit 7978eb6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
8 changes: 7 additions & 1 deletion genesis-programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ extern crate solana_exchange_program;
extern crate solana_vest_program;

use log::*;
use solana_runtime::bank::{Bank, EnteredEpochCallback};
use solana_runtime::{
bank::{Bank, EnteredEpochCallback},
message_processor::{DEFAULT_COMPUTE_BUDGET, DEFAULT_MAX_INVOKE_DEPTH},
};
use solana_sdk::{
clock::Epoch, entrypoint_native::ProcessInstructionWithContext, genesis_config::OperatingMode,
inflation::Inflation, pubkey::Pubkey,
Expand Down Expand Up @@ -145,6 +148,9 @@ pub fn get_entered_epoch_callback(operating_mode: OperatingMode) -> EnteredEpoch
} else {
bank.set_cross_program_support(true);
}

bank.set_max_invoke_depth(DEFAULT_MAX_INVOKE_DEPTH);
bank.set_compute_budget(DEFAULT_COMPUTE_BUDGET);
})
}

Expand Down
9 changes: 9 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,15 @@ impl Bank {
.set_cross_program_support(is_supported);
}

pub fn set_max_invoke_depth(&mut self, max_invoke_depth: usize) {
self.message_processor
.set_max_invoke_depth(max_invoke_depth);
}

pub fn set_compute_budget(&mut self, compute_units: u64) {
self.message_processor.set_compute_budget(compute_units);
}

/// Return the last block hash registered.
pub fn last_blockhash(&self) -> Hash {
self.blockhash_queue.read().unwrap().last_hash()
Expand Down
41 changes: 31 additions & 10 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use solana_sdk::{
};
use std::{cell::RefCell, rc::Rc};

// Number of compute units that an instruction is allowed. Compute units
// are consumed by program execution, resources they use, etc...
const COMPUTE_BUDGET: u64 = 100_000;
pub const DEFAULT_MAX_INVOKE_DEPTH: usize = 2;
pub const DEFAULT_COMPUTE_BUDGET: u64 = 100_000;

// The relevant state of an account before an Instruction executes, used
// to verify account integrity after the Instruction completes
Expand Down Expand Up @@ -183,20 +182,21 @@ pub struct ThisInvokeContext {
programs: Vec<(Pubkey, ProcessInstruction)>,
logger: Rc<RefCell<dyn Logger>>,
is_cross_program_supported: bool,
max_invoke_depth: usize,
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
}
impl ThisInvokeContext {
const MAX_INVOCATION_DEPTH: usize = 2;
pub fn new(
program_id: &Pubkey,
rent: Rent,
pre_accounts: Vec<PreAccount>,
programs: Vec<(Pubkey, ProcessInstruction)>,
log_collector: Option<Rc<LogCollector>>,
is_cross_program_supported: bool,
max_invoke_depth: usize,
compute_budget: u64,
) -> Self {
let mut program_ids = Vec::with_capacity(Self::MAX_INVOCATION_DEPTH);
let mut program_ids = Vec::with_capacity(max_invoke_depth);
program_ids.push(*program_id);
Self {
program_ids,
Expand All @@ -205,6 +205,7 @@ impl ThisInvokeContext {
programs,
logger: Rc::new(RefCell::new(ThisLogger { log_collector })),
is_cross_program_supported,
max_invoke_depth,
compute_meter: Rc::new(RefCell::new(ThisComputeMeter {
remaining: compute_budget,
})),
Expand All @@ -213,7 +214,7 @@ impl ThisInvokeContext {
}
impl InvokeContext for ThisInvokeContext {
fn push(&mut self, key: &Pubkey) -> Result<(), InstructionError> {
if self.program_ids.len() >= Self::MAX_INVOCATION_DEPTH {
if self.program_ids.len() >= self.max_invoke_depth {
return Err(InstructionError::CallDepth);
}
if self.program_ids.contains(key) && self.program_ids.last() != Some(key) {
Expand Down Expand Up @@ -287,6 +288,10 @@ pub struct MessageProcessor {
native_loader: NativeLoader,
#[serde(skip)]
is_cross_program_supported: bool,
#[serde(skip)]
max_invoke_depth: usize,
#[serde(skip)]
compute_budget: u64,
}
impl Default for MessageProcessor {
fn default() -> Self {
Expand All @@ -295,6 +300,11 @@ impl Default for MessageProcessor {
loaders: vec![],
native_loader: NativeLoader::default(),
is_cross_program_supported: true,
// Maximum cross-program invocation depth allowed including the orignal caller
max_invoke_depth: DEFAULT_MAX_INVOKE_DEPTH,
// Number of compute units that an instruction is allowed. Compute units
// are consumed by program execution, resources they use, etc...
compute_budget: DEFAULT_COMPUTE_BUDGET,
}
}
}
Expand All @@ -304,7 +314,7 @@ impl Clone for MessageProcessor {
programs: self.programs.clone(),
loaders: self.loaders.clone(),
native_loader: NativeLoader::default(),
is_cross_program_supported: self.is_cross_program_supported,
..*self
}
}
}
Expand Down Expand Up @@ -342,6 +352,14 @@ impl MessageProcessor {
self.is_cross_program_supported = is_supported;
}

pub fn set_max_invoke_depth(&mut self, max_invoke_depth: usize) {
self.max_invoke_depth = max_invoke_depth;
}

pub fn set_compute_budget(&mut self, compute_budget: u64) {
self.compute_budget = compute_budget;
}

/// Create the KeyedAccounts that will be passed to the program
fn create_keyed_accounts<'a>(
message: &'a Message,
Expand Down Expand Up @@ -589,7 +607,8 @@ impl MessageProcessor {
self.programs.clone(), // get rid of clone
log_collector,
self.is_cross_program_supported,
COMPUTE_BUDGET,
self.max_invoke_depth,
self.compute_budget,
);
let keyed_accounts =
Self::create_keyed_accounts(message, instruction, executable_accounts, accounts)?;
Expand Down Expand Up @@ -669,7 +688,8 @@ mod tests {
vec![],
None,
true,
u64::MAX,
DEFAULT_MAX_INVOKE_DEPTH,
DEFAULT_COMPUTE_BUDGET,
);

// Check call depth increases and has a limit
Expand Down Expand Up @@ -1440,7 +1460,8 @@ mod tests {
vec![],
None,
true,
u64::MAX,
DEFAULT_MAX_INVOKE_DEPTH,
DEFAULT_COMPUTE_BUDGET,
);
let metas = vec![
AccountMeta::new(owned_key, false),
Expand Down

0 comments on commit 7978eb6

Please sign in to comment.