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

Fix panic parsing seeds when account has qualified path #2268

Merged
merged 2 commits into from
Nov 22, 2022

Conversation

mcintyre94
Copy link
Contributor

This PR fixes a panic caused when Anchor parses seeds that reference an account imported from another file using its full path, eg in code like this:

    #[derive(Accounts)]
    pub struct LikePost<'info> {
        #[account(
            init,
            space = 448,
            payer = liker,
            seeds = [post.owner.as_ref() , post.id.to_le_bytes().as_ref()],
            bump
        )]
        pub like: Account<'info, dot::program::Like>,
        #[account(mut)]
        pub post: Account<'info, dot::program::Post>,
        #[account(mut)]
        pub liker: Signer<'info>,
        pub system_program: Program<'info, System>,
        pub rent: Sysvar<'info, Rent>,
    }

Here the post.owner and post.id seeds caused a panic.

The cause was:

let strct = self.ctx.structs().find(|s| s.ident == account).unwrap();

When this runs the struct idents are mapped to non-qualified type names: ["Like", "LoadedLike", "DeletePostEvent", "User", "LoadedUser", "LikeDislikePostEvent", "UpdatePostEvent", "Post", "LoadedPost", "NewPostEvent", "CreateUser", "DeletePost", "LikePost", "CreatePost", "DislikePost", "UpdatePost", "Mutable", "Empty", "ProgramsMap", "WithPrograms", "CpiAccount"]

While the account was dot :: program :: Post. Since this doesn't match any of the unqualified struct names, it panics on the unwrap.

Here account comes from a call to:

anchor/lang/syn/src/lib.rs

Lines 199 to 209 in 8ce18c3

pub fn ty_name(&self) -> Option<String> {
match self {
AccountField::Field(field) => match &field.ty {
Ty::Account(account) => Some(parser::tts_to_string(&account.account_type_path)),
Ty::ProgramAccount(account) => {
Some(parser::tts_to_string(&account.account_type_path))
}
_ => None,
},
AccountField::CompositeField(field) => Some(field.symbol.clone()),
}

This PR updates the call to ty_name to remove any import path from the account. So account is returned as Post and correctly matches the imported struct name, fixing the panic.

@vercel
Copy link

vercel bot commented Nov 21, 2022

@mcintyre94 is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@Henry-E
Copy link

Henry-E commented Nov 21, 2022

It might be nice to add this as one of the example seeds in the pda derivation example https://github.com/coral-xyz/anchor/tree/master/tests/pda-derivation , if only so that we verify in tests that this new macro code actually does solve the issue of panicking on imported seeds

@mcintyre94
Copy link
Contributor Author

mcintyre94 commented Nov 21, 2022

Thanks @Henry-E! I've updated the pda-derivation test to add a seed that triggers this error. When I run the test locally now with the current Anchor build it fails with the expected panic, with this branch it passes.
Updated in a new commit: 31baab3

@Henry-E
Copy link

Henry-E commented Nov 22, 2022

Thanks very much for adding the test, really appreciate it!

Just running the CI now and will hopefully be able to merge once that's done.

@Henry-E Henry-E merged commit 982799b into coral-xyz:master Nov 22, 2022
@mcintyre94 mcintyre94 deleted the cm-parse-seed-panic branch November 22, 2022 11:52
Henry-E pushed a commit to Henry-E/anchor that referenced this pull request Dec 6, 2022
* Fix panic parsing seeds when account has qualified path

* Add a test for seed derivation with account using type with path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants