Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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