-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
lang: proposal: Type safe bumps
in context
#1824
Comments
bumps
in contextbumps
in context
The non-typesafe bump seeds are one of the largest blockers that is keeping our projects on version 0.20.1. |
Is there merit to this issue? I understand the concerns but I am not understanding why it's not getting more attention |
The proposed solutions sounds good to me! |
The easiest and probably the most developer friendly solution would probably be to generate a struct to hold the bump seeds. The generated struct would have a field for each account that has a Library changes: /// Associated bump seeds for `Accounts`.
pub trait Bumps {
/// Struct to hold account bump seeds.
type Bumps: Sized;
}
pub struct Context<'a, 'b, 'c, 'info, T: Bumps> {
/// Currently executing program id.
pub program_id: &'a Pubkey,
/// Deserialized accounts.
pub accounts: &'b mut T,
/// Remaining accounts given but not deserialized or validated.
/// Be very careful when using this directly.
pub remaining_accounts: &'c [AccountInfo<'info>],
/// Bump seeds found during constraint validation. This is provided as a
/// convenience so that handlers don't have to recalculate bump seeds or
/// pass them in as arguments.
pub bumps: T::Bumps,
} Code generation: #[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, seeds = [b"foo"], bump)]
foo: AccountInfo<'info>,
bar: AccountInfo<'info>,
#[account(seeds = [b"baz"], bump)]
baz: AccountInfo<'info>,
...
}
// Auto-generated for `Initialize`.
pub struct InitializeBumps {
foo: u8,
baz: u8,
}
// Auto-generated for `Initialize`.
impl<'info> Bumps for Initialize<'info> {
type Bumps = InitializeBumps;
} Usage: #[program]
pub mod example {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
msg!("foo: {}", ctx.bumps.foo);
msg!("baz: {}", ctx.bumps.baz);
Ok(())
}
} |
Added in #2542. |
Current design:
Proposed:
Under the hood implement it like this:
The main idea of course is not to query accounts by name from an arbitrary map, this is very unsafe and difficult to check. A changed account or a typo will not be caught by the compiler and should be covered by tests. At the same time, there is room for maneuver, because. generating a enum and requesting by it from an array (and without an option on the output) shouldn't be very difficult.
The text was updated successfully, but these errors were encountered: