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

Add const pubkey support and make declare_id a declarative macro #2348

Merged
merged 18 commits into from
Oct 14, 2024

Conversation

kevinheavey
Copy link

@kevinheavey kevinheavey commented Jul 30, 2024

Problem

  • Proc macros are slow to compile
  • Proc macros are harder to move around when they need to import other crates (since Rust doesn't have macro-dependencies yet). In particular this matters for making types available without solana_program.
  • const base58 decoding can be done with the five8_const crate, which has more useful error messages than the bs58 crate (that repo has a PR to improve this but it's been open for months)

Summary of Changes

  • Add a from_str_const associated function to Pubkey. I just lifted this from package-metadata: Add macro to define program id from Cargo.toml #1806 which is a draft PR at the time of writing
  • Replace the declare_id alias in solana_program with a declarative macro of the same name that does the same thing in solana_pubkey (re-exported in solana_program)
  • Deprecate the program_declare_id! macro in solana_sdk_macro
  • Convert the pubkey! macro to a declarative macro and deprecate solana_sdk_macro::program_pubkey

@kevinheavey kevinheavey requested a review from joncinque July 30, 2024 16:20
@kevinheavey kevinheavey force-pushed the new-declare-id branch 2 times, most recently from e799f9b to 4461538 Compare September 10, 2024 18:38
@kevinheavey kevinheavey force-pushed the new-declare-id branch 2 times, most recently from 946d41c to f0ec89b Compare September 25, 2024 11:42
@@ -304,6 +304,12 @@ impl Pubkey {
Self(pubkey_array)
}

/// Decode a string into a Pubkey, usable in a const context
pub const fn from_str_const(s: &str) -> Self {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm just passing by, I think this could use a &'static str input and renamed to from_static, similar pattern as HeaderValue::from_static. Using &'static will prevent people from accidentally using this function for faillible inputs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you're really looking for is the thing a macro does which is to check that the input is a (string) literal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's not a perfect solution, but the function isn't intented to be called with a live 'a, so there is no reason to allow it in the function signature. Using &' static prevent common misuse, if someone leaks a string, they should already know what they are doing. I think, as a library design principle, function signature should clearly communicate what is it use case, using &str tell users that it works with a live 'a.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime I've undone the deprecation of the pubkey macro and converted it to a declarative macro in solana-pubkey, since this discussion revealed that Pubkey::from_str_const does not make it completely obsolete

sdk/src/lib.rs Outdated
@@ -144,6 +144,10 @@ pub use solana_sdk_macro::declare_deprecated_id;
/// assert_eq!(id(), my_id);
/// ```
pub use solana_sdk_macro::declare_id;
#[deprecated(
since = "2.1.0",
note = "Use `solana_program::pubkey::Pubkey::from_str_const` instead"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it should say "Use solana_pubkey::pubkey ... ", right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, I've also updated it to re-export solana_pubkey::pubkey instead of the one from solana_sdk_macro

@kevinheavey kevinheavey force-pushed the new-declare-id branch 2 times, most recently from 87c3252 to 296f002 Compare October 11, 2024 20:17
Copy link

@joncinque joncinque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for your patience, I wanted to do due diligence and read over the five8_const code, which also required reading firedancer code. Everything is good to go on my side

@@ -204,12 +206,17 @@ pub fn declare_deprecated_id(input: TokenStream) -> TokenStream {
TokenStream::from(quote! {#id})
}

#[deprecated(since = "2.1.0", note = "Use `solana_pubkey::declare_id` instead")]
#[proc_macro]
pub fn program_declare_id(input: TokenStream) -> TokenStream {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, until this PR, I hadn't realized there were different versions of these macros for solana_sdk and solana_program. This PR is good as is, but we should definitely consider removing a lot of the extra cruft in favor of solana_pubkey's macros in future work.

@joncinque joncinque merged commit 2ea7fd7 into anza-xyz:master Oct 14, 2024
50 checks passed
@cavemanloverboy
Copy link

good work

@kevinheavey kevinheavey deleted the new-declare-id branch October 14, 2024 13:13
ray-kast pushed a commit to abklabs/agave that referenced this pull request Nov 27, 2024
…a-xyz#2348)

* make declare_id a declarative macro

* remove old comment

* deprecate program_declare_id

* deprecate pubkey macro

* put deprecation on the re-export of the pubkey macro

* replace pubkey! with from_str_const in this repo

* fmt

* remove unused import

* Revert "remove unused rustc_version dep from wen-restart (wrong branch)"

This reverts commit 60dbddd.

* avoid wen-restart changes again

* fmt

* fix deprecation text

* make declare_deprecated_id a declarative macro

* put back the deprecation on the re-export of the pubkey macro

* fmt

* don't deprecate the pubkey macro, but make it a declarative macro

* update deprecation note

* re-export the new pubkey macro in solana-sdk (with deprecation) instead of the old one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants