Skip to content
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: Make bumps of optional account Option<u8> rather than u8 #2730

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 account Option<u8\> rather than u8 ([#2730](https://github.com/coral-xyz/anchor/pull/2730)).
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved

## [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
Loading