Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package-metadata: Add macro to define program id from Cargo.toml #1806

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 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 @@ -99,6 +99,7 @@ members = [
"sdk/cargo-test-sbf",
"sdk/gen-headers",
"sdk/macro",
"sdk/package-metadata",
"sdk/package-metadata-macro",
"sdk/program",
"send-transaction-service",
Expand Down Expand Up @@ -365,6 +366,7 @@ solana-metrics = { path = "metrics", version = "=2.0.0" }
solana-net-utils = { path = "net-utils", version = "=2.0.0" }
solana-nohash-hasher = "0.2.1"
solana-notifier = { path = "notifier", version = "=2.0.0" }
solana-package-metadata = { path = "sdk/package-metadata", version = "=2.0.0" }
solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version = "=2.0.0" }
solana-perf = { path = "perf", version = "=2.0.0" }
solana-poh = { path = "poh", version = "=2.0.0" }
Expand Down
70 changes: 67 additions & 3 deletions programs/sbf/Cargo.lock

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

1 change: 1 addition & 0 deletions programs/sbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ solana-curve25519 = { path = "../../curves/curve25519", version = "=2.0.0" }
solana-ledger = { path = "../../ledger", version = "=2.0.0" }
solana-logger = { path = "../../logger", version = "=2.0.0" }
solana-measure = { path = "../../measure", version = "=2.0.0" }
solana-package-metadata = { path = "../../sdk/package-metadata", version = "=2.0.0" }
solana-poseidon = { path = "../../poseidon/", version = "=2.0.0" }
solana-program = { path = "../../sdk/program", version = "=2.0.0" }
solana-program-runtime = { path = "../../program-runtime", version = "=2.0.0" }
Expand Down
4 changes: 4 additions & 0 deletions programs/sbf/rust/simulation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-package-metadata = { workspace = true }
solana-program = { workspace = true }

[lib]
crate-type = ["cdylib"]

[package.metadata.solana]
program-id = "Sim1jD5C35odT8mzctm8BWnjic8xW5xgeb5MbcbErTo"
3 changes: 1 addition & 2 deletions programs/sbf/rust/simulation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use {
solana_program::{
account_info::{next_account_info, AccountInfo},
clock::Clock,
declare_id,
entrypoint::ProgramResult,
msg,
pubkey::Pubkey,
Expand All @@ -13,7 +12,7 @@ use {
std::convert::TryInto,
};

declare_id!("Sim1jD5C35odT8mzctm8BWnjic8xW5xgeb5MbcbErTo");
solana_package_metadata::declare_id_with_package_metadata!("solana.program-id");

solana_program::entrypoint!(process_instruction);

Expand Down
19 changes: 19 additions & 0 deletions sdk/package-metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "solana-package-metadata"
description = "Solana Package Metadata"
documentation = "https://docs.rs/solana-package-metadata"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-package-metadata-macro = { workspace = true }

[dev-dependencies]
solana-program = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
90 changes: 90 additions & 0 deletions sdk/package-metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/// Macro for accessing data from the `package.metadata` section of the Cargo manifest
///
/// # Arguments
/// * `key` - A string slice of a dot-separated path to the TOML key of interest
///
/// # Example
/// Given the following `Cargo.toml`:
/// ```ignore
/// [package]
/// name = "MyApp"
/// version = "0.1.0"
///
/// [package.metadata]
/// copyright = "Copyright (c) 2024 ACME Inc."
/// ```
///
/// You can fetch the copyright with the following:
/// ```ignore
/// use solana_sdk_macro::package_metadata;
///
/// pub fn main() {
/// let copyright = package_metadata!("copyright");
/// assert_eq!(copyright, "Copyright (c) 2024 ACME Inc.");
/// }
/// ```
///
/// ## TOML Support
/// This macro only supports static data:
/// * Strings
/// * Integers
/// * Floating-point numbers
/// * Booleans
/// * Datetimes
/// * Arrays
///
/// ## Array Example
/// Given the following Cargo manifest:
/// ```ignore
/// [package.metadata.arrays]
/// some_array = [ 1, 2, 3 ]
/// ```
///
/// This is legal:
/// ```ignore
/// static ARR: [i64; 3] = package_metadata!("arrays.some_array");
/// ```
///
/// It does *not* currently support accessing TOML array elements directly.
/// TOML tables are not supported.
pub use solana_package_metadata_macro::package_metadata;

/// Convenience macro for declaring a program id from Cargo.toml package metadata.
///
/// # Arguments
/// * `key` - A string slice of a dot-separated path to the TOML key of interest
///
/// # Example
/// Given the following `Cargo.toml`:
/// ```ignore
/// [package]
/// name = "my-solana-program"
/// version = "0.1.0"
///
/// [package.metadata.solana]
/// program-id = "MyProgram1111111111111111111111111111111111"
/// ```
///
/// A program can use the program id declared in its `Cargo.toml` as the program
/// id in code:
///
/// ```ignore
/// declare_id_with_package_metadata!("solana.program-id");
/// ```
///
/// This program id behaves exactly as if the developer had written:
///
/// ```
/// solana_program::declare_id!("MyProgram1111111111111111111111111111111111");
/// ```
///
/// Meaning that it's possible to refer to the program id using `crate::id()`,
/// without needing to specify the program id in multiple places.
#[macro_export]
macro_rules! declare_id_with_package_metadata {
($key:literal) => {
solana_program::declare_id!(solana_program::pubkey::Pubkey::from_str_const(
$crate::package_metadata!($key)
));
};
}
10 changes: 10 additions & 0 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ impl Pubkey {
Self(pubkey_array)
}

/// Decode a string into a Pubkey, usable in a const context
///
/// Note: Until https://github.com/Nullus157/bs58-rs/pull/120 lands, this
/// function does not include a check that the output decodes to exactly 32
/// bytes.
pub const fn from_str_const(s: &str) -> Self {
let id_array = bs58::decode(s.as_bytes()).into_array_const_unwrap::<PUBKEY_BYTES>();
Pubkey::new_from_array(id_array)
}

#[deprecated(since = "1.3.9", note = "Please use 'Pubkey::new_unique' instead")]
#[cfg(not(target_os = "solana"))]
pub fn new_rand() -> Self {
Expand Down
Loading