tx wide compute budget (#18631)
This commit is contained in:
@ -37,6 +37,7 @@ regex = "1.5.4"
|
||||
serde = { version = "1.0.126", features = ["rc"] }
|
||||
serde_derive = "1.0.103"
|
||||
solana-config-program = { path = "../programs/config", version = "=1.8.0" }
|
||||
solana-compute-budget-program = { path = "../programs/compute-budget", version = "=1.8.0" }
|
||||
solana-frozen-abi = { path = "../frozen-abi", version = "=1.8.0" }
|
||||
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.8.0" }
|
||||
solana-logger = { path = "../logger", version = "=1.8.0" }
|
||||
|
@ -76,23 +76,26 @@ use solana_sdk::{
|
||||
INITIAL_RENT_EPOCH, MAX_PROCESSING_AGE, MAX_RECENT_BLOCKHASHES,
|
||||
MAX_TRANSACTION_FORWARDING_DELAY, SECONDS_PER_DAY,
|
||||
},
|
||||
compute_budget,
|
||||
epoch_info::EpochInfo,
|
||||
epoch_schedule::EpochSchedule,
|
||||
feature,
|
||||
feature_set::{self, FeatureSet},
|
||||
feature_set::{self, tx_wide_compute_cap, FeatureSet},
|
||||
fee_calculator::{FeeCalculator, FeeRateGovernor},
|
||||
genesis_config::{ClusterType, GenesisConfig},
|
||||
hard_forks::HardForks,
|
||||
hash::{extend_and_hash, hashv, Hash},
|
||||
incinerator,
|
||||
inflation::Inflation,
|
||||
instruction::CompiledInstruction,
|
||||
instruction::{CompiledInstruction, InstructionError},
|
||||
lamports::LamportsError,
|
||||
message::Message,
|
||||
native_loader,
|
||||
native_token::sol_to_lamports,
|
||||
nonce, nonce_account,
|
||||
process_instruction::{BpfComputeBudget, Executor, ProcessInstructionWithContext},
|
||||
process_instruction::{
|
||||
BpfComputeBudget, ComputeMeter, Executor, ProcessInstructionWithContext,
|
||||
},
|
||||
program_utils::limited_deserialize,
|
||||
pubkey::Pubkey,
|
||||
recent_blockhashes_account,
|
||||
@ -412,6 +415,28 @@ impl CachedExecutors {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TxComputeMeter {
|
||||
remaining: u64,
|
||||
}
|
||||
impl TxComputeMeter {
|
||||
pub fn new(cap: u64) -> Self {
|
||||
Self { remaining: cap }
|
||||
}
|
||||
}
|
||||
impl ComputeMeter for TxComputeMeter {
|
||||
fn consume(&mut self, amount: u64) -> std::result::Result<(), InstructionError> {
|
||||
let exceeded = self.remaining < amount;
|
||||
self.remaining = self.remaining.saturating_sub(amount);
|
||||
if exceeded {
|
||||
return Err(InstructionError::ComputationalBudgetExceeded);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn get_remaining(&self) -> u64 {
|
||||
self.remaining
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct BankRc {
|
||||
/// where all the Accounts are stored
|
||||
@ -3172,76 +3197,96 @@ impl Bank {
|
||||
Vec::with_capacity(sanitized_txs.len());
|
||||
let mut transaction_log_messages: Vec<Option<Vec<String>>> =
|
||||
Vec::with_capacity(sanitized_txs.len());
|
||||
let bpf_compute_budget = self
|
||||
.bpf_compute_budget
|
||||
.unwrap_or_else(BpfComputeBudget::new);
|
||||
|
||||
let executed: Vec<TransactionExecutionResult> = loaded_txs
|
||||
.iter_mut()
|
||||
.zip(sanitized_txs.as_transactions_iter())
|
||||
.map(|(accs, tx)| match accs {
|
||||
(Err(e), _nonce_rollback) => {
|
||||
inner_instructions.push(None);
|
||||
transaction_log_messages.push(None);
|
||||
inner_instructions.push(None);
|
||||
(Err(e.clone()), None)
|
||||
}
|
||||
(Ok(loaded_transaction), nonce_rollback) => {
|
||||
let feature_set = self.feature_set.clone();
|
||||
signature_count += u64::from(tx.message().header.num_required_signatures);
|
||||
let executors = self.get_executors(&tx.message, &loaded_transaction.loaders);
|
||||
|
||||
let (account_refcells, loader_refcells) = Self::accounts_to_refcells(
|
||||
&mut loaded_transaction.accounts,
|
||||
&mut loaded_transaction.loaders,
|
||||
);
|
||||
let mut bpf_compute_budget = self
|
||||
.bpf_compute_budget
|
||||
.unwrap_or_else(BpfComputeBudget::new);
|
||||
|
||||
let instruction_recorders = if enable_cpi_recording {
|
||||
let ix_count = tx.message.instructions.len();
|
||||
let mut recorders = Vec::with_capacity(ix_count);
|
||||
recorders.resize_with(ix_count, InstructionRecorder::default);
|
||||
Some(recorders)
|
||||
let mut process_result = if feature_set.is_active(&tx_wide_compute_cap::id()) {
|
||||
compute_budget::process_request(&mut bpf_compute_budget, tx)
|
||||
} else {
|
||||
None
|
||||
Ok(())
|
||||
};
|
||||
|
||||
let log_collector = if enable_log_recording {
|
||||
Some(Rc::new(LogCollector::default()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut process_result = self.message_processor.process_message(
|
||||
tx.message(),
|
||||
&loader_refcells,
|
||||
&account_refcells,
|
||||
&self.rent_collector,
|
||||
log_collector.clone(),
|
||||
executors.clone(),
|
||||
instruction_recorders.as_deref(),
|
||||
self.feature_set.clone(),
|
||||
bpf_compute_budget,
|
||||
&mut timings.details,
|
||||
self.rc.accounts.clone(),
|
||||
&self.ancestors,
|
||||
);
|
||||
|
||||
transaction_log_messages.push(Self::collect_log_messages(log_collector));
|
||||
inner_instructions.push(Self::compile_recorded_instructions(
|
||||
instruction_recorders,
|
||||
&tx.message,
|
||||
));
|
||||
|
||||
if let Err(e) = Self::refcells_to_accounts(
|
||||
&mut loaded_transaction.accounts,
|
||||
&mut loaded_transaction.loaders,
|
||||
account_refcells,
|
||||
loader_refcells,
|
||||
) {
|
||||
warn!("Account lifetime mismanagement");
|
||||
process_result = Err(e);
|
||||
}
|
||||
|
||||
if process_result.is_ok() {
|
||||
self.update_executors(executors);
|
||||
let executors =
|
||||
self.get_executors(&tx.message, &loaded_transaction.loaders);
|
||||
|
||||
let (account_refcells, loader_refcells) = Self::accounts_to_refcells(
|
||||
&mut loaded_transaction.accounts,
|
||||
&mut loaded_transaction.loaders,
|
||||
);
|
||||
|
||||
let instruction_recorders = if enable_cpi_recording {
|
||||
let ix_count = tx.message.instructions.len();
|
||||
let mut recorders = Vec::with_capacity(ix_count);
|
||||
recorders.resize_with(ix_count, InstructionRecorder::default);
|
||||
Some(recorders)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let log_collector = if enable_log_recording {
|
||||
Some(Rc::new(LogCollector::default()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let compute_meter = Rc::new(RefCell::new(TxComputeMeter::new(
|
||||
bpf_compute_budget.max_units,
|
||||
)));
|
||||
|
||||
process_result = self.message_processor.process_message(
|
||||
tx.message(),
|
||||
&loader_refcells,
|
||||
&account_refcells,
|
||||
&self.rent_collector,
|
||||
log_collector.clone(),
|
||||
executors.clone(),
|
||||
instruction_recorders.as_deref(),
|
||||
feature_set,
|
||||
bpf_compute_budget,
|
||||
compute_meter,
|
||||
&mut timings.details,
|
||||
self.rc.accounts.clone(),
|
||||
&self.ancestors,
|
||||
);
|
||||
|
||||
transaction_log_messages.push(Self::collect_log_messages(log_collector));
|
||||
inner_instructions.push(Self::compile_recorded_instructions(
|
||||
instruction_recorders,
|
||||
&tx.message,
|
||||
));
|
||||
|
||||
if let Err(e) = Self::refcells_to_accounts(
|
||||
&mut loaded_transaction.accounts,
|
||||
&mut loaded_transaction.loaders,
|
||||
account_refcells,
|
||||
loader_refcells,
|
||||
) {
|
||||
warn!("Account lifetime mismanagement");
|
||||
process_result = Err(e);
|
||||
}
|
||||
|
||||
if process_result.is_ok() {
|
||||
self.update_executors(executors);
|
||||
}
|
||||
} else {
|
||||
transaction_log_messages.push(None);
|
||||
inner_instructions.push(None);
|
||||
}
|
||||
|
||||
let nonce_rollback =
|
||||
@ -5490,6 +5535,7 @@ pub(crate) mod tests {
|
||||
use solana_sdk::{
|
||||
account::Account,
|
||||
clock::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT},
|
||||
compute_budget,
|
||||
epoch_schedule::MINIMUM_SLOTS_PER_EPOCH,
|
||||
feature::Feature,
|
||||
genesis_config::create_genesis_config,
|
||||
@ -13708,4 +13754,47 @@ pub(crate) mod tests {
|
||||
rent_debits.push(&Pubkey::default(), i64::MAX as u64, 0);
|
||||
assert_eq!(rent_debits.0.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compute_request_instruction() {
|
||||
solana_logger::setup();
|
||||
let GenesisConfigInfo {
|
||||
genesis_config,
|
||||
mint_keypair,
|
||||
..
|
||||
} = create_genesis_config_with_leader(
|
||||
1_000_000_000_000_000,
|
||||
&Pubkey::new_unique(),
|
||||
bootstrap_validator_stake_lamports(),
|
||||
);
|
||||
let mut bank = Bank::new(&genesis_config);
|
||||
|
||||
fn mock_ix_processor(
|
||||
_pubkey: &Pubkey,
|
||||
_data: &[u8],
|
||||
invoke_context: &mut dyn InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
let compute_budget = invoke_context.get_bpf_compute_budget();
|
||||
assert_eq!(
|
||||
*compute_budget,
|
||||
BpfComputeBudget {
|
||||
max_units: 1,
|
||||
..BpfComputeBudget::default()
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
let program_id = solana_sdk::pubkey::new_rand();
|
||||
bank.add_builtin("mock_program", program_id, mock_ix_processor);
|
||||
|
||||
let message = Message::new(
|
||||
&[
|
||||
compute_budget::request_units(1),
|
||||
Instruction::new_with_bincode(program_id, &0, vec![]),
|
||||
],
|
||||
Some(&mint_keypair.pubkey()),
|
||||
);
|
||||
let tx = Transaction::new(&[&mint_keypair], message, bank.last_blockhash());
|
||||
bank.process_transaction(&tx).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ use crate::{
|
||||
system_instruction_processor,
|
||||
};
|
||||
use solana_sdk::{
|
||||
feature_set,
|
||||
instruction::InstructionError,
|
||||
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
|
||||
pubkey::Pubkey,
|
||||
@ -86,7 +87,15 @@ pub enum ActivationType {
|
||||
/// normal child Bank creation.
|
||||
/// https://github.com/solana-labs/solana/blob/84b139cc94b5be7c9e0c18c2ad91743231b85a0d/runtime/src/bank.rs#L1723
|
||||
fn feature_builtins() -> Vec<(Builtin, Pubkey, ActivationType)> {
|
||||
vec![]
|
||||
vec![(
|
||||
Builtin::new(
|
||||
"compute_budget_program",
|
||||
solana_sdk::compute_budget::id(),
|
||||
solana_compute_budget_program::process_instruction,
|
||||
),
|
||||
feature_set::tx_wide_compute_cap::id(),
|
||||
ActivationType::NewProgram,
|
||||
)]
|
||||
}
|
||||
|
||||
pub(crate) fn get() -> Builtins {
|
||||
|
@ -10,7 +10,8 @@ use solana_sdk::{
|
||||
account_utils::StateMut,
|
||||
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
|
||||
feature_set::{
|
||||
instructions_sysvar_enabled, neon_evm_compute_budget, updated_verify_policy, FeatureSet,
|
||||
instructions_sysvar_enabled, neon_evm_compute_budget, tx_wide_compute_cap,
|
||||
updated_verify_policy, FeatureSet,
|
||||
},
|
||||
ic_logger_msg, ic_msg,
|
||||
instruction::{CompiledInstruction, Instruction, InstructionError},
|
||||
@ -301,6 +302,7 @@ impl<'a> ThisInvokeContext<'a> {
|
||||
programs: &'a [(Pubkey, ProcessInstructionWithContext)],
|
||||
log_collector: Option<Rc<LogCollector>>,
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
executors: Rc<RefCell<Executors>>,
|
||||
instruction_recorder: Option<InstructionRecorder>,
|
||||
feature_set: Arc<FeatureSet>,
|
||||
@ -314,6 +316,13 @@ impl<'a> ThisInvokeContext<'a> {
|
||||
executable_accounts,
|
||||
accounts,
|
||||
);
|
||||
let compute_meter = if feature_set.is_active(&tx_wide_compute_cap::id()) {
|
||||
compute_meter
|
||||
} else {
|
||||
Rc::new(RefCell::new(ThisComputeMeter {
|
||||
remaining: bpf_compute_budget.max_units,
|
||||
}))
|
||||
};
|
||||
let mut invoke_context = Self {
|
||||
invoke_stack: Vec::with_capacity(bpf_compute_budget.max_invoke_depth),
|
||||
rent,
|
||||
@ -322,9 +331,7 @@ impl<'a> ThisInvokeContext<'a> {
|
||||
programs,
|
||||
logger: Rc::new(RefCell::new(ThisLogger { log_collector })),
|
||||
bpf_compute_budget,
|
||||
compute_meter: Rc::new(RefCell::new(ThisComputeMeter {
|
||||
remaining: bpf_compute_budget.max_units,
|
||||
})),
|
||||
compute_meter,
|
||||
executors,
|
||||
instruction_recorder,
|
||||
feature_set,
|
||||
@ -1138,6 +1145,7 @@ impl MessageProcessor {
|
||||
instruction_index: usize,
|
||||
feature_set: Arc<FeatureSet>,
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
timings: &mut ExecuteDetailsTimings,
|
||||
account_db: Arc<Accounts>,
|
||||
ancestors: &Ancestors,
|
||||
@ -1164,7 +1172,7 @@ impl MessageProcessor {
|
||||
&& *program_id == crate::neon_evm_program::id()
|
||||
{
|
||||
// Bump the compute budget for neon_evm
|
||||
bpf_compute_budget.max_units = 500_000;
|
||||
bpf_compute_budget.max_units = bpf_compute_budget.max_units.max(500_000);
|
||||
bpf_compute_budget.heap_size = Some(256 * 1024);
|
||||
}
|
||||
|
||||
@ -1178,6 +1186,7 @@ impl MessageProcessor {
|
||||
&self.programs,
|
||||
log_collector,
|
||||
bpf_compute_budget,
|
||||
compute_meter,
|
||||
executors,
|
||||
instruction_recorder,
|
||||
feature_set,
|
||||
@ -1218,6 +1227,7 @@ impl MessageProcessor {
|
||||
instruction_recorders: Option<&[InstructionRecorder]>,
|
||||
feature_set: Arc<FeatureSet>,
|
||||
bpf_compute_budget: BpfComputeBudget,
|
||||
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
|
||||
timings: &mut ExecuteDetailsTimings,
|
||||
account_db: Arc<Accounts>,
|
||||
ancestors: &Ancestors,
|
||||
@ -1240,6 +1250,7 @@ impl MessageProcessor {
|
||||
instruction_index,
|
||||
feature_set.clone(),
|
||||
bpf_compute_budget,
|
||||
compute_meter.clone(),
|
||||
timings,
|
||||
account_db.clone(),
|
||||
ancestors,
|
||||
@ -1266,6 +1277,7 @@ mod tests {
|
||||
instruction::{AccountMeta, Instruction, InstructionError},
|
||||
message::Message,
|
||||
native_loader::create_loadable_account_for_test,
|
||||
process_instruction::MockComputeMeter,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -1313,6 +1325,7 @@ mod tests {
|
||||
&[],
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
Rc::new(RefCell::new(Executors::default())),
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
@ -1926,6 +1939,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -1953,6 +1967,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -1984,6 +1999,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -2107,6 +2123,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -2138,6 +2155,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -2167,6 +2185,7 @@ mod tests {
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
BpfComputeBudget::new(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
&mut ExecuteDetailsTimings::default(),
|
||||
Arc::new(Accounts::default()),
|
||||
&ancestors,
|
||||
@ -2268,6 +2287,7 @@ mod tests {
|
||||
programs.as_slice(),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
Rc::new(RefCell::new(Executors::default())),
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
@ -2324,6 +2344,7 @@ mod tests {
|
||||
programs.as_slice(),
|
||||
None,
|
||||
BpfComputeBudget::default(),
|
||||
Rc::new(RefCell::new(MockComputeMeter::default())),
|
||||
Rc::new(RefCell::new(Executors::default())),
|
||||
None,
|
||||
Arc::new(FeatureSet::all_enabled()),
|
||||
|
Reference in New Issue
Block a user