Skip to content

Commit

Permalink
Add Compute Budget Crate (anza-xyz#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec authored Jun 1, 2024
1 parent 99e2aba commit 62eb4cc
Show file tree
Hide file tree
Showing 49 changed files with 133 additions and 72 deletions.
26 changes: 22 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"cli-output",
"client",
"client-test",
"compute-budget",
"connection-cache",
"core",
"cost-model",
Expand Down Expand Up @@ -328,6 +329,7 @@ solana-cli = { path = "cli", version = "=2.0.0" }
solana-cli-config = { path = "cli-config", version = "=2.0.0" }
solana-cli-output = { path = "cli-output", version = "=2.0.0" }
solana-client = { path = "client", version = "=2.0.0" }
solana-compute-budget = { path = "compute-budget", version = "=2.0.0" }
solana-compute-budget-program = { path = "programs/compute-budget", version = "=2.0.0" }
solana-config-program = { path = "programs/config", version = "=2.0.0" }
solana-connection-cache = { path = "connection-cache", version = "=2.0.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ solana-clap-utils = { workspace = true }
solana-cli-config = { workspace = true }
solana-cli-output = { workspace = true }
solana-client = { workspace = true }
solana-compute-budget = { workspace = true }
solana-config-program = { workspace = true }
solana-faucet = { workspace = true }
solana-loader-v4-program = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion cli/src/compute_budget.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
solana_client::rpc_config::RpcSimulateTransactionConfig,
solana_program_runtime::compute_budget_processor::MAX_COMPUTE_UNIT_LIMIT,
solana_compute_budget::compute_budget_processor::MAX_COMPUTE_UNIT_LIMIT,
solana_rpc_client::rpc_client::RpcClient,
solana_sdk::{
borsh1::try_from_slice_unchecked,
Expand Down
3 changes: 2 additions & 1 deletion cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ use {
},
tpu_client::{TpuClient, TpuClientConfig},
},
solana_program_runtime::{compute_budget::ComputeBudget, invoke_context::InvokeContext},
solana_compute_budget::compute_budget::ComputeBudget,
solana_program_runtime::invoke_context::InvokeContext,
solana_rbpf::{elf::Executable, verifier::RequisiteVerifier},
solana_remote_wallet::remote_wallet::RemoteWalletManager,
solana_rpc_client::rpc_client::RpcClient,
Expand Down
3 changes: 2 additions & 1 deletion cli/src/program_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use {
},
tpu_client::{TpuClient, TpuClientConfig},
},
solana_program_runtime::{compute_budget::ComputeBudget, invoke_context::InvokeContext},
solana_compute_budget::compute_budget::ComputeBudget,
solana_program_runtime::invoke_context::InvokeContext,
solana_rbpf::{elf::Executable, verifier::RequisiteVerifier},
solana_remote_wallet::remote_wallet::RemoteWalletManager,
solana_rpc_client::rpc_client::RpcClient,
Expand Down
13 changes: 13 additions & 0 deletions compute-budget/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "solana-compute-budget"
description = "Solana compute budget"
documentation = "https://docs.rs/solana-compute-budget"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-sdk = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use {
crate::compute_budget_processor::{self, process_compute_budget_instructions},
crate::compute_budget_processor::{
self, process_compute_budget_instructions, DEFAULT_HEAP_COST,
},
solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey, transaction::Result},
};

Expand All @@ -11,10 +13,6 @@ impl ::solana_frozen_abi::abi_example::AbiExample for ComputeBudget {
}
}

/// Roughly 0.5us/page, where page is 32K; given roughly 15CU/us, the
/// default heap page cost = 0.5 * 15 ~= 8CU/page
pub const DEFAULT_HEAP_COST: u64 = 8;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ComputeBudget {
/// Number of compute units that a transaction or individual instruction is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
compute_budget::DEFAULT_HEAP_COST,
prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
},
crate::prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
solana_sdk::{
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
Expand All @@ -15,6 +12,9 @@ use {
};

const MAX_HEAP_FRAME_BYTES: u32 = 256 * 1024;
/// Roughly 0.5us/page, where page is 32K; given roughly 15CU/us, the
/// default heap page cost = 0.5 * 15 ~= 8CU/page
pub const DEFAULT_HEAP_COST: u64 = 8;
pub const DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT: u32 = 200_000;
pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;

Expand Down
5 changes: 5 additions & 0 deletions compute-budget/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Solana compute budget types and default configurations.
pub mod compute_budget;
pub mod compute_budget_processor;
pub mod prioritization_fee;
File renamed without changes.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ serde_derive = { workspace = true }
solana-accounts-db = { workspace = true }
solana-bloom = { workspace = true }
solana-client = { workspace = true }
solana-compute-budget = { workspace = true }
solana-cost-model = { workspace = true }
solana-entry = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true }
Expand Down
5 changes: 2 additions & 3 deletions core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use {
BankingStageStats,
},
itertools::Itertools,
solana_compute_budget::compute_budget_processor::process_compute_budget_instructions,
solana_ledger::token_balances::collect_token_balances,
solana_measure::{measure::Measure, measure_us},
solana_poh::poh_recorder::{
BankStart, PohRecorderError, RecordTransactionsSummary, RecordTransactionsTimings,
TransactionRecorder,
},
solana_program_runtime::{
compute_budget_processor::process_compute_budget_instructions, timings::ExecuteTimings,
},
solana_program_runtime::timings::ExecuteTimings,
solana_runtime::{
bank::{Bank, LoadAndExecuteTransactionsOutput},
compute_budget_details::GetComputeBudgetDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use {
ForwardOption, TOTAL_BUFFERED_PACKETS,
},
crossbeam_channel::RecvTimeoutError,
solana_compute_budget::compute_budget_processor::process_compute_budget_instructions,
solana_cost_model::cost_model::CostModel,
solana_measure::measure_us,
solana_program_runtime::compute_budget_processor::process_compute_budget_instructions,
solana_runtime::{bank::Bank, bank_forks::BankForks},
solana_sdk::{
self,
Expand Down
3 changes: 1 addition & 2 deletions cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ lazy_static = { workspace = true }
log = { workspace = true }
solana-address-lookup-table-program = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-compute-budget = { workspace = true }
solana-compute-budget-program = { workspace = true }
solana-config-program = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-loader-v4-program = { workspace = true }
solana-metrics = { workspace = true }
solana-program-runtime = { workspace = true }
solana-sdk = { workspace = true }
solana-stake-program = { workspace = true }
solana-system-program = { workspace = true }
Expand All @@ -48,7 +48,6 @@ rustc_version = { workspace = true }
frozen-abi = [
"dep:solana-frozen-abi",
"dep:solana-frozen-abi-macro",
"solana-program-runtime/frozen-abi",
"solana-sdk/frozen-abi",
"solana-vote-program/frozen-abi",
]
Expand Down
11 changes: 4 additions & 7 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
use {
crate::{block_cost_limits::*, transaction_cost::*},
log::*,
solana_program_runtime::{
compute_budget::DEFAULT_HEAP_COST,
compute_budget_processor::{
process_compute_budget_instructions, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
MAX_COMPUTE_UNIT_LIMIT,
},
solana_compute_budget::compute_budget_processor::{
process_compute_budget_instructions, DEFAULT_HEAP_COST,
DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT,
},
solana_sdk::{
borsh1::try_from_slice_unchecked,
Expand Down Expand Up @@ -571,7 +568,7 @@ mod tests {
// default loaded_accounts_data_size_limit
const DEFAULT_PAGE_COST: u64 = 8;
let expected_loaded_accounts_data_size_cost =
solana_program_runtime::compute_budget_processor::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES
solana_compute_budget::compute_budget_processor::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES
as u64
/ ACCOUNT_DATA_COST_PAGE_SIZE
* DEFAULT_PAGE_COST;
Expand Down
1 change: 1 addition & 0 deletions ledger-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ solana-accounts-db = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-clap-utils = { workspace = true }
solana-cli-output = { workspace = true }
solana-compute-budget = { workspace = true }
solana-core = { workspace = true }
solana-cost-model = { workspace = true }
solana-entry = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions program-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ num-traits = { workspace = true }
percentage = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
solana-compute-budget = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-measure = { workspace = true }
Expand Down
Loading

0 comments on commit 62eb4cc

Please sign in to comment.