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: Fix account try_from usage #2770

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

acheroncrypto
Copy link
Collaborator

@acheroncrypto acheroncrypto commented Jan 12, 2024

Problem

Using try_from in the instruction handler:

pub fn try_from(ctx: Context<TryFrom>) -> Result<()> {
    Account::<MyAccount>::try_from(&ctx.accounts.my_account)?;
    Ok(())
}

results in:

error: lifetime may not live long enough
  --> programs/try-from/src/lib.rs:15:9
   |
14 |     pub fn try_from(ctx: Context<TryFrom>) -> Result<()> {
   |                     ---
   |                     |
   |                     has type `anchor_lang::context::Context<'_, '1, '_, '_, TryFrom<'_>>`
   |                     has type `anchor_lang::context::Context<'_, '_, '_, '_, TryFrom<'2>>`
15 |         Account::<MyAccount>::try_from(&ctx.accounts.my_account)?;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

Details

Ideally, the fix would be limited to Anchor internals without needing a breaking change to the users. However, this is difficult to achieve because #2656 added the same lifetime requirement for the AccountInfo references:

pub struct Account<'info, T: AccountSerialize + AccountDeserialize + Clone> {
account: T,
info: &'info AccountInfo<'info>,
}

The info: &'info AccountInfo<'info> part is incompatible with Anchor's codegen because accounts passed to the context has a shorter lifetime than the AccountInfo's internal lifetime. While the lifetime annotations are incorrect based on Anchor's codegen, it's also not accidental. To properly annotate these lifetimes, we'd have to add another lifetime to the Account type(and all other account types) which would significantly hinder the developer experience since it's a public API.

In short, this problem is caused by annoting the same lifetimes in places where it's technically incorrect but we're doing so in order to not have multiple lifetimes in account types.

One possible solution for this problem is to elide the lifetimes by using core::mem::transmute. Using transmute is heavily discouraged but my understanding is, this should be safe to do in this case because both accounts lifetime and the AccountInfo's internal lifetime live longer than the user's instruction handler.

Usage

try_from!(Account::<MyAccount>, ctx.accounts.my_account)

Summary of changes

  • Add a test program for try_from usage
  • Add try_from! macro

Copy link

vercel bot commented Jan 12, 2024

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

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant