coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

Add a constant for ANCHOR_DISCRIMINATOR_SIZE? #3005

Open mikemaccana opened 4 weeks ago

mikemaccana commented 4 weeks ago

Heya! @nickfrosty and I were talking and wondering what you think here.

Right now when teaching Anchor we avoid magic numbers by getting people to add pub const ANCHOR_DISCRIMINATOR: usize = 8; to almost every new project, and get people to write...

space = ANCHOR_DISCRIMINATOR + SomeStruct::INIT_SPACE,

...when they need to initialize new accounts. I'm wondering what a better solution would be:

acheroncrypto commented 4 weeks ago
  • Include an ANCHOR_DISCRIMINATOR const out of the box?

There are plans to support dynamic discriminator length which would make this redundant. This is also one of the main reasons why the new IDL spec have required discriminator field.

  • Have something like INIT_SPACE that included the discriminator size, eg TOTAL_SPACE or similar?

Yeah, tbh I think InitSpace should have included the discriminator by default since it's pretty much only being used in init contexts anyway. That being said, making that change now would be a breaking change, and it's likely many people will keep adding the discriminator size if we were to make changes to InitSpace. Instead, we can deprecate InitSpace, and have a new one TotalSpace (or Space for simplicity) like you've suggested.

I also think it's possible to completely hide the discriminator logic from the user's POV.