Bump compute budget (#11864)

* Bump compute budget

* nudge
This commit is contained in:
Jack May
2020-08-26 14:48:51 -07:00
committed by GitHub
parent 5c7080c1f4
commit ea179ad762
7 changed files with 150 additions and 65 deletions

View File

@@ -13,7 +13,7 @@ use crate::{
builtins::get_builtins,
epoch_stakes::{EpochStakes, NodeVoteAccounts},
log_collector::LogCollector,
message_processor::{MessageProcessor, DEFAULT_COMPUTE_BUDGET, DEFAULT_MAX_INVOKE_DEPTH},
message_processor::MessageProcessor,
nonce_utils,
rent_collector::RentCollector,
stakes::Stakes,
@@ -35,7 +35,7 @@ use solana_sdk::{
Epoch, Slot, SlotCount, SlotIndex, UnixTimestamp, DEFAULT_TICKS_PER_SECOND,
MAX_PROCESSING_AGE, MAX_RECENT_BLOCKHASHES, SECONDS_PER_DAY,
},
entrypoint_native::{ProcessInstruction, ProcessInstructionWithContext},
entrypoint_native::{ComputeBudget, ProcessInstruction, ProcessInstructionWithContext},
epoch_info::EpochInfo,
epoch_schedule::EpochSchedule,
fee_calculator::{FeeCalculator, FeeRateGovernor},
@@ -1245,13 +1245,8 @@ 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);
pub fn set_compute_budget(&mut self, budget: ComputeBudget) {
self.message_processor.set_compute_budget(budget);
}
/// Return the last block hash registered.
@@ -3210,8 +3205,7 @@ impl Bank {
self.ensure_builtins(init_finish_or_warp);
self.reinvoke_entered_epoch_callback(initiate_callback);
self.recheck_cross_program_support();
self.set_max_invoke_depth(DEFAULT_MAX_INVOKE_DEPTH);
self.set_compute_budget(DEFAULT_COMPUTE_BUDGET);
self.recheck_compute_budget();
}
fn ensure_builtins(&mut self, init_or_warp: bool) {
@@ -3240,6 +3234,27 @@ impl Bank {
}
}
fn recheck_compute_budget(self: &mut Bank) {
let compute_budget = if OperatingMode::Stable == self.operating_mode() {
if self.epoch() >= u64::MAX - 1 {
ComputeBudget::default()
} else {
// Original
ComputeBudget {
max_units: 100_000,
log_units: 0,
log_64_units: 0,
create_program_address_units: 0,
invoke_units: 0,
max_invoke_depth: 2,
}
}
} else {
ComputeBudget::default()
};
self.set_compute_budget(compute_budget);
}
fn fix_recent_blockhashes_sysvar_delay(&self) -> bool {
let activation_slot = match self.operating_mode() {
OperatingMode::Development => 0,

View File

@@ -7,8 +7,8 @@ use solana_sdk::{
account::{create_keyed_readonly_accounts, Account, KeyedAccount},
clock::Epoch,
entrypoint_native::{
ComputeMeter, ErasedProcessInstruction, ErasedProcessInstructionWithContext, InvokeContext,
Logger, ProcessInstruction, ProcessInstructionWithContext,
ComputeBudget, ComputeMeter, ErasedProcessInstruction, ErasedProcessInstructionWithContext,
InvokeContext, Logger, ProcessInstruction, ProcessInstructionWithContext,
},
instruction::{CompiledInstruction, InstructionError},
message::Message,
@@ -20,9 +20,6 @@ use solana_sdk::{
};
use std::{cell::RefCell, rc::Rc};
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
#[derive(Clone, Debug, Default)]
@@ -183,7 +180,7 @@ pub struct ThisInvokeContext {
programs: Vec<(Pubkey, ProcessInstruction)>,
logger: Rc<RefCell<dyn Logger>>,
is_cross_program_supported: bool,
max_invoke_depth: usize,
compute_budget: ComputeBudget,
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
}
impl ThisInvokeContext {
@@ -194,10 +191,9 @@ impl ThisInvokeContext {
programs: Vec<(Pubkey, ProcessInstruction)>,
log_collector: Option<Rc<LogCollector>>,
is_cross_program_supported: bool,
max_invoke_depth: usize,
compute_budget: u64,
compute_budget: ComputeBudget,
) -> Self {
let mut program_ids = Vec::with_capacity(max_invoke_depth);
let mut program_ids = Vec::with_capacity(compute_budget.max_invoke_depth);
program_ids.push(*program_id);
Self {
program_ids,
@@ -206,16 +202,16 @@ impl ThisInvokeContext {
programs,
logger: Rc::new(RefCell::new(ThisLogger { log_collector })),
is_cross_program_supported,
max_invoke_depth,
compute_budget,
compute_meter: Rc::new(RefCell::new(ThisComputeMeter {
remaining: compute_budget,
remaining: compute_budget.max_units,
})),
}
}
}
impl InvokeContext for ThisInvokeContext {
fn push(&mut self, key: &Pubkey) -> Result<(), InstructionError> {
if self.program_ids.len() >= self.max_invoke_depth {
if self.program_ids.len() >= self.compute_budget.max_invoke_depth {
return Err(InstructionError::CallDepth);
}
if self.program_ids.contains(key) && self.program_ids.last() != Some(key) {
@@ -260,6 +256,9 @@ impl InvokeContext for ThisInvokeContext {
fn is_cross_program_supported(&self) -> bool {
self.is_cross_program_supported
}
fn get_compute_budget(&self) -> ComputeBudget {
self.compute_budget
}
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
self.compute_meter.clone()
}
@@ -290,9 +289,7 @@ pub struct MessageProcessor {
#[serde(skip)]
is_cross_program_supported: bool,
#[serde(skip)]
max_invoke_depth: usize,
#[serde(skip)]
compute_budget: u64,
compute_budget: ComputeBudget,
}
impl std::fmt::Debug for MessageProcessor {
@@ -339,11 +336,7 @@ 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,
compute_budget: ComputeBudget::default(),
}
}
}
@@ -391,11 +384,7 @@ 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) {
pub fn set_compute_budget(&mut self, compute_budget: ComputeBudget) {
self.compute_budget = compute_budget;
}
@@ -651,7 +640,6 @@ impl MessageProcessor {
self.programs.clone(), // get rid of clone
log_collector,
self.is_cross_program_supported,
self.max_invoke_depth,
self.compute_budget,
);
let keyed_accounts =
@@ -742,8 +730,7 @@ mod tests {
vec![],
None,
true,
DEFAULT_MAX_INVOKE_DEPTH,
DEFAULT_COMPUTE_BUDGET,
ComputeBudget::default(),
);
// Check call depth increases and has a limit
@@ -1514,8 +1501,7 @@ mod tests {
vec![],
None,
true,
DEFAULT_MAX_INVOKE_DEPTH,
DEFAULT_COMPUTE_BUDGET,
ComputeBudget::default(),
);
let metas = vec![
AccountMeta::new(owned_key, false),