-
Couldn't load subscription status.
- Fork 140
address: Add bpf target #389
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ pub mod syscalls; | |
| use crate::error::AddressError; | ||
| #[cfg(feature = "decode")] | ||
| use crate::error::ParseAddressError; | ||
| #[cfg(all(feature = "rand", not(target_os = "solana")))] | ||
| #[cfg(all(feature = "rand", not(any(target_os = "solana", target_arch = "bpf"))))] | ||
| pub use crate::hasher::{AddressHasher, AddressHasherBuilder}; | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
|
|
@@ -57,7 +57,12 @@ pub const MAX_SEEDS: usize = 16; | |
| #[cfg(feature = "decode")] | ||
| /// Maximum string length of a base58 encoded address. | ||
| const MAX_BASE58_LEN: usize = 44; | ||
|
|
||
| /// Marker used to find program derived addresses (PDAs). | ||
| #[cfg(target_arch = "bpf")] | ||
| pub static PDA_MARKER: &[u8; 21] = b"ProgramDerivedAddress"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| /// Marker used to find program derived addresses (PDAs). | ||
| #[cfg(not(target_arch = "bpf"))] | ||
| pub const PDA_MARKER: &[u8; 21] = b"ProgramDerivedAddress"; | ||
|
|
||
| /// The address of a [Solana account][acc]. | ||
|
|
@@ -164,13 +169,13 @@ impl TryFrom<&str> for Address { | |
| } | ||
| } | ||
|
|
||
| // If target_os = "solana", then this panics so there are no dependencies. | ||
| // When target_os != "solana", this should be opt-in so users | ||
| // don't need the curve25519 dependency. | ||
| #[cfg(any(target_os = "solana", feature = "curve25519"))] | ||
| // If target_os = "solana" or target_arch = "bpf", then this panics so there | ||
| // are no dependencies; otherwise this should be opt-in so users don't need the | ||
| // curve25519 dependency. | ||
| #[cfg(any(target_os = "solana", target_arch = "bpf", feature = "curve25519"))] | ||
| #[allow(clippy::used_underscore_binding)] | ||
| pub fn bytes_are_curve_point<T: AsRef<[u8]>>(_bytes: T) -> bool { | ||
| #[cfg(not(target_os = "solana"))] | ||
| #[cfg(not(any(target_os = "solana", target_arch = "bpf")))] | ||
| { | ||
| let Ok(compressed_edwards_y) = | ||
| curve25519_dalek::edwards::CompressedEdwardsY::from_slice(_bytes.as_ref()) | ||
|
|
@@ -179,7 +184,7 @@ pub fn bytes_are_curve_point<T: AsRef<[u8]>>(_bytes: T) -> bool { | |
| }; | ||
| compressed_edwards_y.decompress().is_some() | ||
| } | ||
| #[cfg(target_os = "solana")] | ||
| #[cfg(any(target_os = "solana", target_arch = "bpf"))] | ||
| unimplemented!(); | ||
| } | ||
|
|
||
|
|
@@ -232,10 +237,10 @@ impl Address { | |
| Self::from(b) | ||
| } | ||
|
|
||
| // If target_os = "solana", then the solana_sha256_hasher crate will use | ||
| // syscalls which bring no dependencies. | ||
| // When target_os != "solana", this should be opt-in so users | ||
| // don't need the sha2 dependency. | ||
| // If target_os = "solana" or target_arch = "bpf", then the | ||
| // `solana_sha256_hasher` crate will use syscalls which bring no | ||
| // dependencies; otherwise, this should be opt-in so users don't | ||
| // need the sha2 dependency. | ||
| #[cfg(feature = "sha2")] | ||
| pub fn create_with_seed( | ||
| base: &Address, | ||
|
|
@@ -267,16 +272,16 @@ impl Address { | |
| &self.0 | ||
| } | ||
|
|
||
| // If target_os = "solana", then this panics so there are no dependencies. | ||
| // When target_os != "solana", this should be opt-in so users | ||
| // don't need the curve25519 dependency. | ||
| #[cfg(any(target_os = "solana", feature = "curve25519"))] | ||
| // If target_os = "solana" or target_arch = "bpf", then this panics so there | ||
| // are no dependencies; otherwise, this should be opt-in so users don't need | ||
| // the curve25519 dependency. | ||
| #[cfg(any(target_os = "solana", target_arch = "bpf", feature = "curve25519"))] | ||
| pub fn is_on_curve(&self) -> bool { | ||
| bytes_are_curve_point(self) | ||
| } | ||
|
|
||
| #[cfg(all(not(target_os = "solana"), feature = "std"))] | ||
| /// Log a `Address` from a program | ||
| /// Log an `Address` value. | ||
| #[cfg(all(not(any(target_os = "solana", target_arch = "bpf")), feature = "std"))] | ||
| pub fn log(&self) { | ||
| std::println!("{}", std::string::ToString::to_string(&self)); | ||
| } | ||
|
|
@@ -384,7 +389,7 @@ macro_rules! address { | |
| /// let my_id = Address::from_str("My11111111111111111111111111111111111111111").unwrap(); | ||
| /// assert_eq!(id(), my_id); | ||
| /// ``` | ||
| #[cfg(feature = "decode")] | ||
| #[cfg(all(feature = "decode", not(target_arch = "bpf")))] | ||
| #[macro_export] | ||
| macro_rules! declare_id { | ||
| ($address:expr) => { | ||
|
|
@@ -411,8 +416,56 @@ macro_rules! declare_id { | |
| }; | ||
| } | ||
|
|
||
| /// Convenience macro to declare a static address and functions to interact with it. | ||
| /// | ||
| /// Input: a single literal base58 string representation of a program's ID. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// # // wrapper is used so that the macro invocation occurs in the item position | ||
| /// # // rather than in the statement position which isn't allowed. | ||
| /// use std::str::FromStr; | ||
| /// use solana_address::{declare_id, Address}; | ||
| /// | ||
| /// # mod item_wrapper { | ||
| /// # use solana_address::declare_id; | ||
| /// declare_id!("My11111111111111111111111111111111111111111"); | ||
| /// # } | ||
| /// # use item_wrapper::id; | ||
| /// | ||
| /// let my_id = Address::from_str("My11111111111111111111111111111111111111111").unwrap(); | ||
| /// assert_eq!(id(), my_id); | ||
| /// ``` | ||
| #[cfg(all(feature = "decode", target_arch = "bpf"))] | ||
| #[macro_export] | ||
| macro_rules! declare_id { | ||
| ($address:expr) => { | ||
| /// The const program ID. | ||
| pub static ID: $crate::Address = $crate::Address::from_str_const($address); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, needs to be |
||
|
|
||
| /// Returns `true` if given address is the ID. | ||
| // TODO make this const once `derive_const` makes it out of nightly | ||
| // and we can `derive_const(PartialEq)` on `Address`. | ||
| pub fn check_id(id: &$crate::Address) -> bool { | ||
| id == &ID | ||
| } | ||
|
|
||
| /// Returns the ID. | ||
| pub const fn id() -> $crate::Address { | ||
| $crate::Address::from_str_const($address) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[test] | ||
| fn test_id() { | ||
| assert!(check_id(&id())); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// Same as [`declare_id`] except that it reports that this ID has been deprecated. | ||
| #[cfg(feature = "decode")] | ||
| #[cfg(all(feature = "decode", not(target_arch = "bpf")))] | ||
| #[macro_export] | ||
| macro_rules! declare_deprecated_id { | ||
| ($address:expr) => { | ||
|
|
@@ -442,6 +495,37 @@ macro_rules! declare_deprecated_id { | |
| }; | ||
| } | ||
|
|
||
| /// Same as [`declare_id`] except that it reports that this ID has been deprecated. | ||
| #[cfg(all(feature = "decode", target_arch = "bpf"))] | ||
| #[macro_export] | ||
| macro_rules! declare_deprecated_id { | ||
| ($address:expr) => { | ||
| /// The const ID. | ||
| pub static ID: $crate::Address = $crate::Address::from_str_const($address); | ||
|
|
||
| /// Returns `true` if given address is the ID. | ||
| // TODO make this const once `derive_const` makes it out of nightly | ||
| // and we can `derive_const(PartialEq)` on `Address`. | ||
| #[deprecated()] | ||
| pub fn check_id(id: &$crate::Address) -> bool { | ||
| id == &ID | ||
| } | ||
|
|
||
| /// Returns the ID. | ||
| #[deprecated()] | ||
| pub const fn id() -> $crate::Address { | ||
| $crate::Address::from_str_const($address) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[test] | ||
| #[allow(deprecated)] | ||
| fn test_id() { | ||
| assert!(check_id(&id())); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, core::str::from_utf8, std::string::String}; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabling feature "curve25519" will enable the
syscallsmodule, which will try to use solana-define-syscall dependency whentarget_os = "solana"ortarget_arch = "bpf", breaking the build. The fix here enables the "syscalls" feature so the dependency is found; it has no effect on other targets, it will only bring the required "error" feature.Alternatively, we could improve the separation between the "curve25519" and "syscalls" features, but this does not seem necessary at the moment.