Skip to content

RFC: add discriminator to idl#2138

Closed
seanyoung wants to merge 1 commit intosolana-foundation:masterfrom
seanyoung:discriminator
Closed

RFC: add discriminator to idl#2138
seanyoung wants to merge 1 commit intosolana-foundation:masterfrom
seanyoung:discriminator

Conversation

@seanyoung
Copy link
Copy Markdown
Contributor

This adds the following syntax:

#[discriminator(0xffefabcd)]
pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
      Ok(())
}

This PR is not complete yet - the code generation does not use the discriminator yet, and the client should also use the new value.

Signed-off-by: Sean Young <[email protected]>
@vercel
Copy link
Copy Markdown

vercel bot commented Aug 17, 2022

@seanyoung is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.


/// A marker attribute used to override the discriminator value that should be used.
#[proc_macro_attribute]
pub fn discriminator(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on the use case for this macro? When would an anchor program use it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So the plan was that #[discriminator(x)] would you allow you to override the discriminator, so that we can test an idl with different discriminator than the standard discriminator created with hashing.

As for a use case,I did imagine a usecase where you can specify a single byte as a discriminator, but this syntax does not allow this yet. This would allow you to just use a single byte rather than 8 bytes for discriminator.

What do you think of:

#[discriminator([42])]
fn foo(..)

or for two byte discriminators:

#[discriminator([42,12])]
fn foo(..)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you don't think the attribute is useful, I can create a test using a non-anchor rust contract and a hand-written idl, which is then used from rust and ts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this could be interesting. How would you handle potential collisions and/or ambiguity with discriminators of different lengths?

@jacobcreech
Copy link
Copy Markdown
Collaborator

Closing as stale + implemented in 0.31

@jacobcreech jacobcreech closed this Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants