Skip to content

Commit

Permalink
lang: Make bumps of optional accounts Option<u8> rather than u8 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen authored Dec 14, 2023
1 parent 4c0af6d commit c402972
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Require explicit `overflow-checks` flag ([#2716](https://github.com/coral-xyz/anchor/pull/2716)).
- ts: Remove `anchor-deprecated-state` feature ([#2717](https://github.com/coral-xyz/anchor/pull/2717)).
- lang: Remove `CLOSED_ACCOUNT_DISCRIMINATOR` ([#2726](https://github.com/coral-xyz/anchor/pull/2726)).
- lang: Make bumps of optional accounts `Option<u8>` rather than `u8` ([#2730](https://github.com/coral-xyz/anchor/pull/2730)).

## [0.29.0] - 2023-10-16

Expand Down
7 changes: 5 additions & 2 deletions lang/syn/src/codegen/accounts/bumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
match af {
AccountField::Field(f) => {
let constraints = constraints::linearize(&f.constraints);
let bump_field = quote!(pub #ident: u8);
let bump_default_field = quote!(#ident: u8::MAX);
let (bump_field, bump_default_field) = if f.is_optional {
(quote!(pub #ident: Option<u8>), quote!(#ident: None))
} else {
(quote!(pub #ident: u8), quote!(#ident: u8::MAX))
};

for c in constraints.iter() {
// Verify this in super::constraints
Expand Down
14 changes: 12 additions & 2 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,19 @@ fn generate_constraint_init_group(
}
}
};
let bump = if f.is_optional {
quote!(Some(__bump))
} else {
quote!(__bump)
};

(
quote! {
let (__pda_address, __bump) = Pubkey::find_program_address(
&[#maybe_seeds_plus_comma],
__program_id,
);
__bumps.#field = __bump;
__bumps.#field = #bump;
#validate_pda
},
quote! {
Expand Down Expand Up @@ -871,6 +876,11 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
let maybe_seeds_plus_comma = (!s.is_empty()).then(|| {
quote! { #s, }
});
let bump = if f.is_optional {
quote!(Some(__bump))
} else {
quote!(__bump)
};

// Not init here, so do all the checks.
let define_pda = match c.bump.as_ref() {
Expand All @@ -880,7 +890,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
&[#maybe_seeds_plus_comma],
&#deriving_program_id,
);
__bumps.#name = __bump;
__bumps.#name = #bump;
},
// Bump target given. Use it.
Some(b) => quote! {
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/programs/misc-optional/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub mod misc_optional {
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>) -> Result<()> {
let mut acc = ctx.accounts.my_pda.as_ref().unwrap().load_init()?;
acc.data = 9;
acc.bump = ctx.bumps.my_pda;
acc.bump = ctx.bumps.my_pda.unwrap();
Ok(())
}

Expand Down

1 comment on commit c402972

@vercel
Copy link

@vercel vercel bot commented on c402972 Dec 14, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-lang.com
www.anchor-lang.com
anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app

Please sign in to comment.