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

Eq for MemoBytes should compare the bytes. #2565

Merged
merged 2 commits into from
Nov 25, 2021
Merged

Eq for MemoBytes should compare the bytes. #2565

merged 2 commits into from
Nov 25, 2021

Conversation

nc6
Copy link
Contributor

@nc6 nc6 commented Nov 25, 2021

Two instances of a memoised type are equal iff they have the same bytes.
Justification:

  • We require that deserialisation is left inverse to serialisation. So
    if we have
    serialise x == serialise y
    deserialise (serialise x) == deserialise (serialise y)
    x == y
  • Eq should be compatible with hashing (e.g. if a == b, then
    hash a == hash b). Currently, this is broken. If I serialise a
    TxBodyRaw in two different valid ways (as is perfectly allowed),
    then the resulting TxBody will currently compare as equal but hash
    to a different value.
  • In choosing not to require a canonical serialisation, the serialised
    bytes are an integral part of the type, and must be considered in
    comparing for equality.

Two instances of a memoised type are equal iff they have the same bytes.
Justification:
- We require that deserialisation is left inverse to serialisation. So
  if we have
  `serialise x == serialise y`
  `deserialise (serialise x) == deserialise (serialise y)`
  `x == y`
- `Eq` should be compatible with hashing (e.g. if `a == b`, then
  `hash a == hash b`). Currently, this is broken. If I serialise a
  `TxBodyRaw` in two different valid ways (as is perfectly allowed),
  then the resulting `TxBody` will currently compare as equal but hash
  to a different value.
- In choosing not to require a canonical serialisation, the serialised
  bytes are an integral part of the type, and must be considered in
  comparing for equality.
@@ -69,7 +69,7 @@ instance (Typeable t, FromCBOR (Annotator t)) => FromCBOR (Annotator (MemoBytes
(Annotator getT, Annotator getBytes) <- withSlice fromCBOR
pure (Annotator (\fullbytes -> Memo (getT fullbytes) (toShort (toStrict (getBytes fullbytes)))))

instance Eq t => Eq (MemoBytes t) where (Memo x _) == (Memo y _) = x == y
instance Eq (MemoBytes t) where (Memo _ x) == (Memo _ y) = x == y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not comparing both constituents of Memo implicitly supposes that if two binary representations are equal, then their Haskell representation must be equal a well; is there anything truly enforcing that? While it sounds plausible in my head, is it really the case 🤔 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was what I was justifying above:

serialise x == serialise y
deserialise (serialise x) == deserialise (serialise y)
x == y

The property that deserialise is left-inverse to serialise is a required property for any serialisable type (as opposed to the other way around!). And we have tests verifying this property for serialised types.

These are not needed now that `MemoBytes` is always comparable.
@nc6 nc6 merged commit ecdb4d1 into master Nov 25, 2021
@iohk-bors iohk-bors bot deleted the nc/memo-eq branch November 25, 2021 15:08
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

Successfully merging this pull request may close these issues.

3 participants