Skip to content

Cache calculated bump seeds in context - #1367

Merged
armaniferrante merged 16 commits into
masterfrom
armani/bumps
Jan 27, 2022
Merged

Cache calculated bump seeds in context#1367
armaniferrante merged 16 commits into
masterfrom
armani/bumps

Conversation

@armaniferrante

@armaniferrante armaniferrante commented Jan 27, 2022

Copy link
Copy Markdown
Contributor

Closes #1364.

Pre-requisite for #1361. This is needed for non anchor accounts that won't have the account header and so can't store bump seeds directly in the account data (and so the bump needs to be stored in some other associated account).

One can use ctx.bumps.get("<my_account_name>") to get a bump seed already searched for in the accounts context.

@armaniferrante armaniferrante changed the title update Cache calculated bump seeds in context Jan 27, 2022
@armaniferrante
armaniferrante marked this pull request as ready for review January 27, 2022 21:36
@billythedummy

billythedummy commented Feb 9, 2022

Copy link
Copy Markdown
Contributor

Given that we know which accounts require a PDA check at compile-time, would it be more efficient to generate a struct like BumpsAccountStruct instead of using a BTreeMap? For example:

#[derive(Accounts)]
#[derive(bumps)] // hypothetical proc macro for this suggestion
pub struct Example<'info> {
    #[account(
        init,
        seeds = [...],
        bump,
    )]
    pub pda_1: AccountInfo<'info>,

    pub not_pda: AccountInfo<'info>,
}

would generate

pub struct BumpsExample {
  pub pda_1: Option<u8>
} 

With a BTreeMap with string keys, there are no compile-time checks for account name refactors as well. If i change the struct field name from pda_1 to pda_2 but forget to change the code from ctx.bumps.get("pda_1") to ctx.bumps.get("pda_2"), it becomes a runtime error instead

@armaniferrante

Copy link
Copy Markdown
Contributor Author

@billythedummy that sounds good. How would you expect to access this from the context?

@billythedummy

Copy link
Copy Markdown
Contributor

so i think ctx.bumps would be the generated struct instead of a BTreeMap (so BumpsExample in this case). So instead of ctx.bumps.get("pda_1") it will be ctx.bumps.pda_1. This will probably require adding a new generic to Context for this "bumps" type. Haven't looked closely at the lang internals yet so i'm not sure how difficult it would be to implement this.

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.

lang: Add BTreeMap to Context containing all bump seeds found during constraint execution

3 participants