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

Exception: deserialiseLazyOrPanic ... DeserialiseFailure 1 "Expected index < 2" #13

Open
haroldcarr opened this issue Aug 31, 2023 · 4 comments

Comments

@haroldcarr
Copy link

haroldcarr commented Aug 31, 2023

Do you have any ideas off-the-top-of-your-head.

I know from looking around that the "real" error is coming from Codec.Borsch.Decoding decodeEnum

Also, note the size - it is BIG.

groth16ProvingKeyJsonNew rng 8 eg
-- *** Exception: deserialiseLazyOrPanic for Proxy * (Either Text Groth16ProvingKeyJSON): DeserialiseFailure 1 "Expected index < 2"
.....
44,49,53,53,44,49,57,52,44,50,50,54,44,49,52,57,44,49,56,57,44,49,55,49,44,49,56,51,93,125] (14839025)
CallStack (from HasCallStack):
  error, called at src/Foreign/Rust/Marshall/Util.hs:39:19 in frgn-rst-0.1.0-bce6ec5d:Foreign.Rust.Marshall.Util
  deserialiseLazyOrPanic, called at src/Foreign/Rust/Marshall/Util.hs:31:28 in frgn-rst-0.1.0-bce6ec5d:Foreign.Rust.Marshall.Util
  deserialiseStrictOrPanic, called at src/Foreign/Rust/Marshall/Variable.hs:96:17 in frgn-rst-0.1.0-bce6ec5d:Foreign.Rust.Marshall.Variable

For serialization in Rust, I use this macro:

macro_rules! to_from_haskell_via_ark_compressed {
    ($impl_ty:ty) => {

        impl ToHaskell<DNC> for $impl_ty {
            fn to_haskell<W: Write>(&self, writer: &mut W, tag: PhantomData<DNC>) -> Result<()> {
                let mut serz = vec![];
                match CanonicalSerialize::serialize_compressed(self, &mut serz)
                {
                    Ok(()) => serz.to_haskell(writer, tag),
                    Err(e) =>
                        Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            format!("to_haskell serialization error: {:?}", e)
                        )))

                }
            }
        }

        impl FromHaskell<DNC> for $impl_ty {
            fn from_haskell(buf: &mut &[u8], tag: PhantomData<DNC>) -> Result<Self> {
                let bytes = <Vec<u8>>::from_haskell(buf, tag)?;
                match CanonicalDeserialize::deserialize_compressed(&bytes[..])
                {
                    Ok(deserz) => Ok(deserz),
                    Err(e)     =>
                        Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            format!("from_haskell deserialization error: {:?}", e)
                        )))
                }
            }
        }
    }
}

I have used that macro for other types with with smaller instances successfully.

@edsko
Copy link
Collaborator

edsko commented Nov 8, 2023

Sorry for the very slow response, I've been inundated with stuff. One thought that comes to mind: in the Haskell side you need to make a choice between AsEnum versus AsStruct; the latter adds a tag for the constructor, the former does not. Could it be related to that perhaps?

@haroldcarr
Copy link
Author

Thank you for the response. I can't try the AsEnum/Struct thing at the moment, but I will sometime and report back.

Question: in your purgatory article you only use via As* in ONE place : via AsStruct Color
The rest just do deriving newtype (BorshSize, ToBorsh, FromBorsh) --- so that is what I have done everwhere.
Is there a reason you only explicitly leave via As* out in other places?

@edsko
Copy link
Collaborator

edsko commented Nov 9, 2023

The blog post refers to an earlier version of the library, where the default was AsEnum and you could use AsStruct if you really wanted to skip the tag for the constructor. However, that was error prone, and was leading to precisely the kinds of decoding errors that you are seeing, so we changed the library to force people to make an explicit choice between enums and structs.

@haroldcarr
Copy link
Author

Got it. I will try being explicit and report back. Thank you.

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

No branches or pull requests

2 participants