Skip to content

Commit

Permalink
refactor: move statically defined builtin default costs map from sola…
Browse files Browse the repository at this point in the history
…na-cost-model to its own crate. Makes sharing builtin costs between crates easier, and provide space to future refactoring; Issue #2351
  • Loading branch information
tao-stones committed Jul 31, 2024
1 parent 2fce82b commit 76d99c8
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 52 deletions.
29 changes: 23 additions & 6 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 @@ -19,6 +19,7 @@ members = [
"bench-tps",
"bloom",
"bucket_map",
"builtins-default-costs",
"cargo-registry",
"clap-utils",
"clap-v3-utils",
Expand Down Expand Up @@ -343,6 +344,7 @@ solana-bloom = { path = "bloom", version = "=2.1.0" }
solana-bn254 = { path = "curves/bn254", version = "=2.1.0" }
solana-bpf-loader-program = { path = "programs/bpf_loader", version = "=2.1.0" }
solana-bucket-map = { path = "bucket_map", version = "=2.1.0" }
solana-builtins-default-costs = { path = "builtins-default-costs", version = "=2.1.0" }
agave-cargo-registry = { path = "cargo-registry", version = "=2.1.0" }
solana-clap-utils = { path = "clap-utils", version = "=2.1.0" }
solana-clap-v3-utils = { path = "clap-v3-utils", version = "=2.1.0" }
Expand Down
45 changes: 45 additions & 0 deletions builtins-default-costs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "solana-builtins-default-costs"
description = "Solana builtins default costs"
documentation = "https://docs.rs/solana-builtins-default-costs"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
ahash = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
solana-address-lookup-table-program = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-compute-budget-program = { workspace = true }
solana-config-program = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-loader-v4-program = { workspace = true }
solana-sdk = { workspace = true }
solana-stake-program = { workspace = true }
solana-system-program = { workspace = true }
solana-vote-program = { workspace = true }
# Add additional builtin programs here

[lib]
crate-type = ["lib"]
name = "solana_builtins_default_costs"

[dev-dependencies]
rand = "0.8.5"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[build-dependencies]
rustc_version = { workspace = true }

[features]
frozen-abi = [
"dep:solana-frozen-abi",
"solana-vote-program/frozen-abi",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate test;
use {
rand::Rng,
solana_cost_model::block_cost_limits::BUILTIN_INSTRUCTION_COSTS,
solana_builtins_default_costs::BUILTIN_INSTRUCTION_COSTS,
solana_sdk::{
address_lookup_table, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
compute_budget, ed25519_program, loader_v4, pubkey::Pubkey, secp256k1_program,
Expand Down
1 change: 1 addition & 0 deletions builtins-default-costs/build.rs
33 changes: 33 additions & 0 deletions builtins-default-costs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(min_specialization))]
#![allow(clippy::arithmetic_side_effects)]
use {
ahash::AHashMap,
lazy_static::lazy_static,
solana_sdk::{
address_lookup_table, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
compute_budget, ed25519_program, loader_v4, pubkey::Pubkey, secp256k1_program,
},
};

// Number of compute units for each built-in programs
lazy_static! {
/// Number of compute units for each built-in programs
pub static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, u64> = [
(solana_stake_program::id(), solana_stake_program::stake_instruction::DEFAULT_COMPUTE_UNITS),
(solana_config_program::id(), solana_config_program::config_processor::DEFAULT_COMPUTE_UNITS),
(solana_vote_program::id(), solana_vote_program::vote_processor::DEFAULT_COMPUTE_UNITS),
(solana_system_program::id(), solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS),
(compute_budget::id(), solana_compute_budget_program::DEFAULT_COMPUTE_UNITS),
(address_lookup_table::program::id(), solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS),
(bpf_loader_upgradeable::id(), solana_bpf_loader_program::UPGRADEABLE_LOADER_COMPUTE_UNITS),
(bpf_loader_deprecated::id(), solana_bpf_loader_program::DEPRECATED_LOADER_COMPUTE_UNITS),
(bpf_loader::id(), solana_bpf_loader_program::DEFAULT_LOADER_COMPUTE_UNITS),
(loader_v4::id(), solana_loader_v4_program::DEFAULT_COMPUTE_UNITS),
// Note: These are precompile, run directly in bank during sanitizing;
(secp256k1_program::id(), 0),
(ed25519_program::id(), 0),
]
.iter()
.cloned()
.collect();
}
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ serde_bytes = { workspace = true }
serde_derive = { workspace = true }
solana-accounts-db = { workspace = true }
solana-bloom = { workspace = true }
solana-builtins-default-costs = { workspace = true }
solana-client = { workspace = true }
solana-compute-budget = { workspace = true }
solana-connection-cache = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion core/src/banking_stage/packet_filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::immutable_deserialized_packet::ImmutableDeserializedPacket,
solana_cost_model::block_cost_limits::BUILTIN_INSTRUCTION_COSTS,
solana_builtins_default_costs::BUILTIN_INSTRUCTION_COSTS,
solana_sdk::{ed25519_program, saturating_add_assign, secp256k1_program},
thiserror::Error,
};
Expand Down
9 changes: 2 additions & 7 deletions cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ edition = { workspace = true }
ahash = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
solana-address-lookup-table-program = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-builtins-default-costs = { 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-sdk = { workspace = true }
solana-stake-program = { workspace = true }
solana-system-program = { workspace = true }
solana-vote-program = { workspace = true }

[lib]
Expand All @@ -36,6 +30,7 @@ itertools = { workspace = true }
rand = "0.8.5"
solana-logger = { workspace = true }
solana-sdk = { workspace = true, features = ["dev-context-only-utils"] }
solana-system-program = { workspace = true }
static_assertions = { workspace = true }
test-case = { workspace = true }

Expand Down
30 changes: 0 additions & 30 deletions cost-model/src/block_cost_limits.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
//! defines block cost related limits
//!
use {
ahash::AHashMap,
lazy_static::lazy_static,
solana_sdk::{
address_lookup_table, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
compute_budget, ed25519_program, loader_v4, pubkey::Pubkey, secp256k1_program,
},
};
/// Static configurations:
///
Expand All @@ -34,28 +26,6 @@ pub const ED25519_VERIFY_STRICT_COST: u64 = COMPUTE_UNIT_TO_US_RATIO * 80;
pub const WRITE_LOCK_UNITS: u64 = COMPUTE_UNIT_TO_US_RATIO * 10;
/// Number of data bytes per compute units
pub const INSTRUCTION_DATA_BYTES_COST: u64 = 140 /*bytes per us*/ / COMPUTE_UNIT_TO_US_RATIO;
// Number of compute units for each built-in programs
lazy_static! {
/// Number of compute units for each built-in programs
pub static ref BUILTIN_INSTRUCTION_COSTS: AHashMap<Pubkey, u64> = [
(solana_stake_program::id(), solana_stake_program::stake_instruction::DEFAULT_COMPUTE_UNITS),
(solana_config_program::id(), solana_config_program::config_processor::DEFAULT_COMPUTE_UNITS),
(solana_vote_program::id(), solana_vote_program::vote_processor::DEFAULT_COMPUTE_UNITS),
(solana_system_program::id(), solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS),
(compute_budget::id(), solana_compute_budget_program::DEFAULT_COMPUTE_UNITS),
(address_lookup_table::program::id(), solana_address_lookup_table_program::processor::DEFAULT_COMPUTE_UNITS),
(bpf_loader_upgradeable::id(), solana_bpf_loader_program::UPGRADEABLE_LOADER_COMPUTE_UNITS),
(bpf_loader_deprecated::id(), solana_bpf_loader_program::DEPRECATED_LOADER_COMPUTE_UNITS),
(bpf_loader::id(), solana_bpf_loader_program::DEFAULT_LOADER_COMPUTE_UNITS),
(loader_v4::id(), solana_loader_v4_program::DEFAULT_COMPUTE_UNITS),
// Note: These are precompile, run directly in bank during sanitizing;
(secp256k1_program::id(), 0),
(ed25519_program::id(), 0),
]
.iter()
.cloned()
.collect();
}

/// Statically computed data:
///
Expand Down
1 change: 1 addition & 0 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use {
crate::{block_cost_limits::*, transaction_cost::*},
log::*,
solana_builtins_default_costs::BUILTIN_INSTRUCTION_COSTS,
solana_compute_budget::compute_budget_processor::{
process_compute_budget_instructions, DEFAULT_HEAP_COST,
DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT,
Expand Down
28 changes: 21 additions & 7 deletions programs/sbf/Cargo.lock

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

0 comments on commit 76d99c8

Please sign in to comment.