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

Use Cuddle to define CDDL in Shelley #4552

Closed
wants to merge 7 commits into from
Closed

Use Cuddle to define CDDL in Shelley #4552

wants to merge 7 commits into from

Conversation

nc6
Copy link
Contributor

@nc6 nc6 commented Aug 15, 2024

Description

This addresses issue #4462

  • Moves some core type CDDL definitions to cardano-ledger-core
  • Defines Shelley CDDL types through Huddle
  • Adds huddle-based testing in Shelley
  • Moves the Shelley CDDL to being auto-generated via the Huddle def
  • Updates Conway to use some definitions from Shelley where they were unchanged
  • Updates the gen-cddl.sh script to check Shelley definitions

One unexpected change: the huddle definition for protocol_param_updates has a size limit on the max block header size. This is consistent with the Haskell serialisers and data structure.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • All visible changes are prepended to the latest section of a CHANGELOG.md for the affected packages.
    New section is never added with the code changes. (See RELEASING.md)
  • When applicable, versions are updated in .cabal and CHANGELOG.md files according to the
    versioning process.
  • The version bounds in .cabal files for all affected packages are updated.
    If you change the bounds in a cabal file, that package itself must have a version increase. (See RELEASING.md)
  • Code is formatted with fourmolu (use scripts/fourmolize.sh)
  • Cabal files are formatted (use scripts/cabal-format.sh)
  • hie.yaml has been updated (use scripts/gen-hie.sh)
  • Self-reviewed the diff

@nc6 nc6 requested a review from lehins August 15, 2024 11:11
@nc6 nc6 force-pushed the nc/4462 branch 2 times, most recently from b890871 to c788d10 Compare August 16, 2024 08:11
nc6 and others added 7 commits August 19, 2024 10:11
Now that the CDDL is defined with Cuddle, we can
begin to properly modularise it. As a first step,
we move the common crypto and utility types into
the core package.
- Switch existing tests to using the generated CDDL
- Add additional Huddle based tests for Shelley types
- Add a tool to regenerate the Shelley CDDL from Huddle
- Move additional core types to the core CDDL
- Make a few fixes in the Shelley Huddle spec

There is one unusual thing here: the size bound on
the max block header size in the protocol param
update. This does not reflect the original CDDL,
but it is consistent with the FromCBOR instance
and the underlying data type in PParams. I can
only assume that the CDDL generator wasn't
exploring the whole range and thus never found
this error.
Now that Shelley is defined using Huddle, we can
rely on the relevant parts from Conway.
Copy link
Collaborator

@lehins lehins left a comment

Choose a reason for hiding this comment

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

This looks awesome. I am loving seeing CDDL specified in Haskell 😄

Comment on lines +130 to +144
protocol_param_update = {? 0 : uint
, ? 1 : uint
, ? 2 : uint
, ? 3 : uint
, ? 4 : uint .size 2
, ? 5 : coin
, ? 6 : coin
, ? 7 : epoch
, ? 8 : uint
, ? 9 : nonnegative_interval
, ? 10 : unit_interval
, ? 11 : unit_interval
, ? 12 : unit_interval
, ? 13 : nonce
, ? 14 : [protocol_version]
Copy link
Collaborator

Choose a reason for hiding this comment

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

All of the comments have disappeared, which were very useful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most of the comments will come back with #4553. But yes, at the moment, comments are only supported on rules, not on arbitrary CDDL sections. I will have a think about how best to bring this back - input-output-hk/cuddle#36

Comment on lines +179 to +190
-- the real bounded_bytes does not have this limit. it instead has a different
-- limit which cannot be expressed in CDDL.
-- The limit is as follows:
-- - bytes with a definite-length encoding are limited to size 0..64
-- - for bytes with an indefinite-length CBOR encoding, each chunk is
-- limited to size 0..64
-- ( reminder: in CBOR, the indefinite-length encoding of bytestrings
-- consists of a token #2.31 followed by a sequence of definite-length
-- encoded bytestrings and a stop code )

-- a type for distinct values.
-- The type parameter must support .size, for example: bytes or uint
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert this into CDDL comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in #4553

Comment on lines +155 to +170
unit_interval :: Rule
unit_interval = "unit_interval" =:= tag 30 (arr [1, 2])

-- unit_interval = tag 0 [uint, uint]
--
-- Comment above depicts the actual definition for `unit_interval`.
--
-- Unit interval is a number in the range between 0 and 1, which
-- means there are two extra constraints:
-- \* numerator <= denominator
-- \* denominator > 0
--
-- Relation between numerator and denominator cannot be expressed in CDDL, which
-- poses a problem for testing. We need to be able to generate random valid data
-- for testing implementation of our encoders/decoders. Which means we cannot use
-- the actual definition here and we hard code the value to 1/2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert it to CDDL comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in #4553

Comment on lines +143 to +144
set :: IsType0 t0 => t0 -> GRuleCall
set = binding $ \x -> "set" =:= tag 258 (arr [0 <+ a x]) / sarr [0 <+ a x]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This definition should live in cardano-ledger-conway, since it is not valid for all previous eras.

More over it will also change in the next era, because tag 258 will become mandatory

Comment on lines +139 to +141
-- Conway era introduces an optional 258 tag for sets, which will become mandatory in the
-- second era after Conway. We recommend all the tooling to account for this future breaking
-- change sooner rather than later, in order to provide a smooth transition for their users.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Convert to CDDL comment


pool_metadata = [url, metadata_hash]

pool_params = (pool_keyhash
Copy link
Collaborator

Choose a reason for hiding this comment

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

Am I right in assuming that key+value syntax is not supported by huddle?

Suggested change
pool_params = (pool_keyhash
pool_params = (operator: pool_keyhash

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is supported, but not for named groups (only for maps). input-output-hk/cuddle#32

, ? 10 : unit_interval
, ? 11 : unit_interval
, ? 12 : unit_interval
, ? 13 : nonce
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't remember the distinction between the two, but this has changed from previous spec. Was this desired?

Suggested change
, ? 13 : nonce
, ? 13 : $nonce


vkeywitness = [ $vkey, $signature ]
multi_host_name = (2, dns_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lost comment


transaction_index = uint .size 2
$hash32 = bytes .size 32
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the difference in syntax between = and /=?

Suggested change
$hash32 = bytes .size 32
$hash32 /= bytes .size 32

Copy link
Contributor Author

Choose a reason for hiding this comment

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

   For both type choices and group choices, additional alternatives can
   be added to a rule later in separate rules by using "/=" and "//=",
   respectively, instead of "=":

                 attire /= "swimwear"

                 delivery //= (
                 lat: float, long: float, drone-type: tstr
                 )

   It is not an error if a name is first used with a "/=" or "//="
   (there is no need to "create it" with "=").

So basically CDDL supports ad-hoc extending choices. Huddle does not support this. In any case, these definitions are the only definitions, so the use of /= is meaningless.

import Data.Word (Word64)
import GHC.Num (Integer)
import GHC.Show (Show (show))
import Test.Cardano.Ledger.Core.Binary.CDDL
Copy link
Collaborator

Choose a reason for hiding this comment

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

We've just made a release, so addition of a new module will require a minor version bump for:

  • cardano-ledger-shelley-1.13.1.0
  • cardano-ledger-conway-1.16.2.0
  • cardano-ledger-core-1.14.1.0

Also, the lower bounds will need to be adjusted accordingly

@nc6
Copy link
Contributor Author

nc6 commented Aug 28, 2024

Closing this in favour of #4553

@nc6 nc6 closed this Aug 28, 2024
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.

2 participants