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

enum reprs are missing in idl #904

Closed
dovahcrow opened this issue Oct 20, 2021 · 2 comments
Closed

enum reprs are missing in idl #904

dovahcrow opened this issue Oct 20, 2021 · 2 comments
Labels
idl related to the IDL, either program or client side

Comments

@dovahcrow
Copy link

dovahcrow commented Oct 20, 2021

Enums with different reprs are all treated as u8 repr when the frontend loads the account data using anchor-ts, due to the repr information are not included in the IDL file.

The error message is also very confusing as it contains minimal information about the root cause of the bug: Anchor is trying to parse the enum as u8 but due to the size difference of different reprs, Anchor reads data from wrong places and gets an unexpected variant.
image

P.S. the stack trace is inaccurate as well, which makes debugging anchor errors very unpleasant.

@porkbrain
Copy link

I encountered this issue when storing an array of enums with zero copy account. In my case, even though the program writes for example Bar::A to the second index, the anchor-ts can only understand first index, everything else is parsed as Bar::None (in another words, the first variant with discriminant of 0).

#[derive(Accounts)]
struct Endpoint<'info> {
  foo: AccountLoader<'info, Foo>
}

#[account(zero_copy)]
struct Foo {
  // .. other fields
  bar: [10; Bar],
  // ... other fields
}

enum Bar {
  None,
  A { inner: .. }, // if inner is not repr(packed), then the anchor-ts incorrectly calculates layout size
  B { inner: .. },
  Never { padding: [u8; N] } // is this safe to do with zero copy?
}

Also, the representation for my enum has layout with a span of -1 if that is relevant:

  WrappedLayout {
    span: -1,
    property: 'bar',
    layout: Structure {
      span: -1,
      property: undefined,
      fields: [Array],
      decodePrefixes: false
    },
    decoder: [Function (anonymous)],
    encoder: [Function (anonymous)]
  }

@acheroncrypto
Copy link
Collaborator

Added in #2824.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idl related to the IDL, either program or client side
Projects
None yet
Development

No branches or pull requests

3 participants