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 plutus double CBOR encoding bug #720

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Jimbo4350
Copy link
Contributor

@Jimbo4350 Jimbo4350 commented Jan 9, 2025

Resolves: #685

Changelog

- description: |
    Introduce new type `PlutusScriptInEra` and fix the double cbor encoding plutus script bug
    Resolves: https://github.com/IntersectMBO/cardano-api/issues/685
# uncomment types applicable to the change:
  type:
  # - feature        # introduces a new feature
  # - breaking       # the API has changed in a breaking way
  # - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  # - refactoring    # QoL changes
  - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

Additional context for the PR goes here. If the PR fixes a particular issue please provide a link to the issue.

How to trust this PR

Highlight important bits of the PR that will make the review faster. If there are commands the reviewer can run to observe the new behavior, describe them.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@Jimbo4350 Jimbo4350 changed the title Jordan/plutus script in era Fix plutus double CBOR encoding bug Jan 9, 2025
@Jimbo4350 Jimbo4350 force-pushed the jordan/plutus-script-in-era branch 3 times, most recently from 36cb43d to b2d6cf8 Compare January 9, 2025 20:31
@Jimbo4350 Jimbo4350 marked this pull request as ready for review January 9, 2025 20:31
@carbolymer carbolymer linked an issue Jan 10, 2025 that may be closed by this pull request
cardano-api/gen/Test/Hedgehog/Roundtrip/CBOR.hs Outdated Show resolved Hide resolved
Right (_, needToEncode) ->
case CBOR.deserialiseFromBytes CBOR.decodeBytes $ LBS.fromStrict needToEncode of
Left _ -> NotDoubleEncoded
Right (_, final) -> IsDoubleEncoded $ CBOR.toStrictByteString $ CBOR.encodeBytes final
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the script getting encoded here again? Are you expecting needToEncode to be different than CBOR.encodeBytes final

You're encoding final before returning. Why is that? Doesn't that make it double encoded again?

cardano-api/internal/Cardano/Api/Script.hs Outdated Show resolved Hide resolved
cardano-api/internal/Cardano/Api/Script.hs Outdated Show resolved Hide resolved
cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs Outdated Show resolved Hide resolved
cardano-api/internal/Cardano/Api/Script.hs Outdated Show resolved Hide resolved
Comment on lines 468 to 474
data IsDoubleEncoded
= -- | Original plutus script bytes
IsDoubleEncoded
Crypto.ByteString
| NotDoubleEncoded

isPlutusScriptDoubleEncoded :: LBS.ByteString -> IsDoubleEncoded
Copy link
Contributor

Choose a reason for hiding this comment

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

data IsDoubleEncoded
  = -- | Original plutus script bytes
    IsDoubleEncoded
      Crypto.ByteString
  | NotDoubleEncoded

isPlutusScriptDoubleEncoded :: LBS.ByteString -> IsDoubleEncoded

I think those types are a little bit unwieldy. It forces the user to pattern match on the type to extract the bytes. I know it's only used internally in the tests and in the deserialisation instance, but I think it could be improved.

What do you think about something like that:

data IsPlutusScriptDoubleEncoded 
 = DoubleEncodedPlutusScript
 | NotDoubleEncodedPlutusScript

normalisePlutusScriptCborEncoding
  :: LBS.ByteString -- ^ original plutus script, possibly doubly encoded
  -> (IsPlutusScriptDoubleEncoded, LBS.ByteString) 
    -- ^ singly encoded plutus script - if the original script was singly encoded, 
    -- returns original bytes

this way if the user is not interested in the encoding problem, they can ignore the first element of the tuple and just use the resulting bytes

Copy link
Contributor Author

@Jimbo4350 Jimbo4350 Jan 10, 2025

Choose a reason for hiding this comment

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

It forces the user to pattern match on the type to extract the bytes

I think we should get rid of the IsPlutusScriptDoubleEncoded type. I'll push some changes.

@Jimbo4350 Jimbo4350 force-pushed the jordan/plutus-script-in-era branch 3 times, most recently from 8eaa6a5 to f9fd11d Compare January 13, 2025 21:19
@Jimbo4350 Jimbo4350 force-pushed the jordan/plutus-script-in-era branch 3 times, most recently from b77b45f to 3b0a1ac Compare January 16, 2025 19:29
(Script lang) instance
Implement removePlutusScriptDoubleEncoding function
Utilize removePlutusScriptDoubleEncoding in the SerialiseAsCBOR (PlutusScript
lang)
When decoding plutus script bytes to this type the validity of those
bytes are checked and the decoder will fail if they are invalid
This type is used in testing to double check we can still decode double
encoded plutus script bytes and that the resulting bytes are valid
This function decodes a double encoded plutus script and checks if the
bytes are valid.

It essentially tests if the existing SerialiseAsCBOR instance for
PlutusScript lang can decode a double encoded plutus script correctly.
@Jimbo4350 Jimbo4350 force-pushed the jordan/plutus-script-in-era branch from 3b0a1ac to ed0a681 Compare January 16, 2025 19:30
@Jimbo4350 Jimbo4350 enabled auto-merge January 16, 2025 19:30
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.

[BUG] - Remove plutus scripts double serialisation
2 participants