Skip to content

Commit

Permalink
Extract compute-budget-interface crate (solana-labs#3393)
Browse files Browse the repository at this point in the history
* minimize borsh usage in solana_sdk::compute_budget

* extract compute-budget-instruction crate

* fix deprecated since

* update lock file

* fix missing feature activation

* post-rebase fix

* remove unused deps

* rename to solana-compute-budget-interface

* rename directory

* fix feature activation
  • Loading branch information
kevinheavey authored Nov 21, 2024
1 parent 86a7062 commit eeb52d2
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 21 deletions.
14 changes: 14 additions & 0 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 @@ -108,6 +108,7 @@ members = [
"sdk/clock",
"sdk/cluster-type",
"sdk/commitment-config",
"sdk/compute-budget-interface",
"sdk/cpi",
"sdk/decode-error",
"sdk/define-syscall",
Expand Down Expand Up @@ -436,6 +437,7 @@ solana-clock = { path = "sdk/clock", version = "=2.2.0" }
solana-cluster-type = { path = "sdk/cluster-type", version = "=2.2.0" }
solana-commitment-config = { path = "sdk/commitment-config", version = "=2.2.0" }
solana-compute-budget = { path = "compute-budget", version = "=2.2.0" }
solana-compute-budget-interface = { path = "sdk/compute-budget-interface", version = "=2.2.0" }
solana-compute-budget-program = { path = "programs/compute-budget", version = "=2.2.0" }
solana-config-program = { path = "programs/config", version = "=2.2.0" }
solana-connection-cache = { path = "connection-cache", version = "=2.2.0", default-features = false }
Expand Down
12 changes: 12 additions & 0 deletions programs/sbf/Cargo.lock

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

12 changes: 11 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ full = [
"solana-pubkey/rand",
"dep:solana-cluster-type",
"dep:solana-ed25519-program",
"dep:solana-compute-budget-interface",
"dep:solana-keypair",
"dep:solana-precompile-error",
"dep:solana-presigner",
Expand All @@ -47,10 +48,16 @@ full = [
"dep:solana-signer",
"dep:solana-transaction-error"
]
borsh = ["dep:borsh", "solana-program/borsh", "solana-secp256k1-recover/borsh"]
borsh = [
"dep:borsh",
"solana-compute-budget-interface/borsh",
"solana-program/borsh",
"solana-secp256k1-recover/borsh"
]
dev-context-only-utils = [
"qualifier_attr",
"solana-account/dev-context-only-utils",
"solana-compute-budget-interface/dev-context-only-utils",
"solana-transaction-context/dev-context-only-utils",
]
frozen-abi = [
Expand Down Expand Up @@ -107,6 +114,9 @@ solana-cluster-type = { workspace = true, features = [
"serde",
], optional = true }
solana-commitment-config = { workspace = true, optional = true, features = ["serde"] }
solana-compute-budget-interface = { workspace = true, optional = true, features = [
"serde",
] }
solana-decode-error = { workspace = true }
solana-derivation-path = { workspace = true }
solana-ed25519-program = { workspace = true, optional = true }
Expand Down
37 changes: 37 additions & 0 deletions sdk/compute-budget-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "solana-compute-budget-interface"
description = "Solana compute budget interface."
documentation = "https://docs.rs/solana-compute-budget-interface"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
borsh = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, features = [
"frozen-abi",
], optional = true }
solana-frozen-abi-macro = { workspace = true, features = [
"frozen-abi",
], optional = true }
solana-instruction = { workspace = true, features = ["std"] }
solana-sdk-ids = { workspace = true }

[features]
borsh = ["dep:borsh"]
dev-context-only-utils = ["borsh"]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro", "serde"]
serde = ["dep:serde", "dep:serde_derive"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
//! The compute budget native program.
//! Instructions for the compute budget native program.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]

#![cfg(feature = "full")]

pub use solana_sdk_ids::compute_budget::{check_id, id, ID};
#[cfg(feature = "borsh")]
use {
crate::instruction::Instruction,
borsh::{BorshDeserialize, BorshSerialize},
};
use borsh::{BorshDeserialize, BorshSerialize};
use solana_instruction::Instruction;
pub use solana_sdk_ids::compute_budget::{check_id, id, ID};

/// Compute Budget Instructions
#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
#[cfg_attr(
feature = "frozen-abi",
derive(
solana_frozen_abi_macro::AbiExample,
solana_frozen_abi_macro::AbiEnumVisitor
)
)]
#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ComputeBudgetInstruction {
Unused, // deprecated variant, reserved value.
/// Request a specific transaction-wide program heap region size in bytes.
Expand All @@ -29,35 +37,57 @@ pub enum ComputeBudgetInstruction {
SetLoadedAccountsDataSizeLimit(u32),
}

macro_rules! to_instruction {
($discriminator: expr, $num: expr, $num_type: ty) => {{
let mut data = [0u8; size_of::<$num_type>() + 1];
data[0] = $discriminator;
data[1..].copy_from_slice(&$num.to_le_bytes());
Instruction {
program_id: id(),
data: data.to_vec(),
accounts: vec![],
}
}};
}

impl ComputeBudgetInstruction {
#[cfg(feature = "borsh")]
/// Create a `ComputeBudgetInstruction::RequestHeapFrame` `Instruction`
pub fn request_heap_frame(bytes: u32) -> Instruction {
Instruction::new_with_borsh(id(), &Self::RequestHeapFrame(bytes), vec![])
to_instruction!(1, bytes, u32)
}

#[cfg(feature = "borsh")]
/// Create a `ComputeBudgetInstruction::SetComputeUnitLimit` `Instruction`
pub fn set_compute_unit_limit(units: u32) -> Instruction {
Instruction::new_with_borsh(id(), &Self::SetComputeUnitLimit(units), vec![])
to_instruction!(2, units, u32)
}

#[cfg(feature = "borsh")]
/// Create a `ComputeBudgetInstruction::SetComputeUnitPrice` `Instruction`
pub fn set_compute_unit_price(micro_lamports: u64) -> Instruction {
Instruction::new_with_borsh(id(), &Self::SetComputeUnitPrice(micro_lamports), vec![])
to_instruction!(3, micro_lamports, u64)
}

/// Serialize Instruction using borsh, this is only used in runtime::cost_model::tests but compilation
/// can't be restricted as it's used across packages
#[cfg(all(feature = "dev-context-only-utils", feature = "borsh"))]
#[cfg(feature = "dev-context-only-utils")]
pub fn pack(self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(&self)
}

#[cfg(feature = "borsh")]
/// Create a `ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit` `Instruction`
pub fn set_loaded_accounts_data_size_limit(bytes: u32) -> Instruction {
Instruction::new_with_borsh(id(), &Self::SetLoadedAccountsDataSizeLimit(bytes), vec![])
to_instruction!(4, bytes, u32)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_to_instruction() {
let ix = ComputeBudgetInstruction::set_compute_unit_limit(257);
assert_eq!(ix.data, vec![2, 1, 1, 0, 0]);
let ix = ComputeBudgetInstruction::set_compute_unit_price(u64::MAX);
assert_eq!(ix.data, vec![3, 255, 255, 255, 255, 255, 255, 255, 255]);
}
}
7 changes: 6 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub use solana_program::{borsh, borsh0_10, borsh1};
#[deprecated(since = "2.2.0", note = "Use `solana-signer` crate instead")]
pub use solana_signer::signers;
pub mod client;
pub mod compute_budget;
pub mod entrypoint;
pub mod entrypoint_deprecated;
pub mod epoch_rewards_hasher;
Expand Down Expand Up @@ -111,6 +110,12 @@ pub use solana_account as account;
pub use solana_account::state_traits as account_utils;
#[deprecated(since = "2.1.0", note = "Use `solana-bn254` crate instead")]
pub use solana_bn254 as alt_bn128;
#[deprecated(
since = "2.2.0",
note = "Use `solana-compute-budget-interface` crate instead"
)]
#[cfg(feature = "full")]
pub use solana_compute_budget_interface as compute_budget;
#[deprecated(since = "2.1.0", note = "Use `solana-decode-error` crate instead")]
pub use solana_decode_error as decode_error;
#[deprecated(since = "2.1.0", note = "Use `solana-derivation-path` crate instead")]
Expand Down
12 changes: 12 additions & 0 deletions svm/examples/Cargo.lock

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

0 comments on commit eeb52d2

Please sign in to comment.