From c9782de6f952461ee73bf57de1be259fedf7228c Mon Sep 17 00:00:00 2001 From: Nicholas Clarke Date: Fri, 5 Jul 2024 10:24:19 +0200 Subject: [PATCH] Add utility to generate cardano CDDL files. This addresses issue #4461 We add two things: - A haskell tool to generate the CDDL files - A script which verifies if any Cuddle specs have changed, and invokes the tool if so. Both of these would need to be updated in case additional Cuddle specs were added. The tool can be invoked with `cabal run cardano-cddl`. --- cabal.project | 1 + .../conway/impl/cddl-files/conway-merged.cddl | 501 ++++++++++++++++++ hie.yaml | 3 + libs/cardano-cddl/CHANGELOG.md | 5 + libs/cardano-cddl/LICENSE | 202 +++++++ libs/cardano-cddl/app/Main.hs | 28 + libs/cardano-cddl/cardano-cddl.cabal | 34 ++ scripts/gen-cddl.sh | 9 + 8 files changed, 783 insertions(+) create mode 100644 eras/conway/impl/cddl-files/conway-merged.cddl create mode 100644 libs/cardano-cddl/CHANGELOG.md create mode 100644 libs/cardano-cddl/LICENSE create mode 100644 libs/cardano-cddl/app/Main.hs create mode 100644 libs/cardano-cddl/cardano-cddl.cabal create mode 100755 scripts/gen-cddl.sh diff --git a/cabal.project b/cabal.project index 451e0552896..ed59599d68b 100644 --- a/cabal.project +++ b/cabal.project @@ -38,6 +38,7 @@ packages: eras/shelley/impl eras/shelley/test-suite eras/shelley-ma/test-suite + libs/cardano-cddl libs/cardano-ledger-api libs/cardano-ledger-core libs/cardano-ledger-binary diff --git a/eras/conway/impl/cddl-files/conway-merged.cddl b/eras/conway/impl/cddl-files/conway-merged.cddl new file mode 100644 index 00000000000..38dd74b86bc --- /dev/null +++ b/eras/conway/impl/cddl-files/conway-merged.cddl @@ -0,0 +1,501 @@ +$hash28 = bytes .size 28 + +$hash32 = bytes .size 32 + +$kes_signature = bytes .size 448 + +$kes_vkey = bytes .size 32 + +$signature = bytes .size 64 + +$vkey = bytes .size 32 + +$vrf_cert = [bytes, bytes .size 80] + +$vrf_vkey = bytes .size 32 + +addr_keyhash = $hash28 + +address = h'001000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000' + / h'102000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000' + / h'203000000000000000000000000000000000000000000000000000000033000000000000000000000000000000000000000000000000000000' + / h'304000000000000000000000000000000000000000000000000000000044000000000000000000000000000000000000000000000000000000' + / h'405000000000000000000000000000000000000000000000000000000087680203' + / h'506000000000000000000000000000000000000000000000000000000087680203' + / h'6070000000000000000000000000000000000000000000000000000000' + / h'7080000000000000000000000000000000000000000000000000000000' + +anchor = [anchor_url : url, anchor_data_hash : $hash32] + +asset_name = bytes .size (0 .. 32) + +auth_committee_hot_cert = (14 + , committee_cold_credential + , committee_hot_credential) + +auxiliary_data = metadata + / [transaction_metadata : metadata + , auxiliary_scripts : [* native_script]] + / #6.259({? 0 : metadata + , ? 1 : [* native_script] + , ? 2 : [* plutus_v1_script] + , ? 3 : [* plutus_v2_script] + , ? 4 : [* plutus_v3_script]}) + +auxiliary_data_hash = $hash32 + +big_int = int / big_uint / big_nint + +big_nint = #6.3(bounded_bytes) + +big_uint = #6.2(bounded_bytes) + +block = [header + , transaction_bodies : [* transaction_body] + , transaction_witness_sets : [* transaction_witness_set] + , auxiliary_data_set : {* transaction_index => auxiliary_data} + , invalid_transactions : [* transaction_index]] + +block_no = uint .size 8 + +bootstrap_witness = [public_key : $vkey + , signature : $signature + , chain_code : bytes .size 32 + , attributes : bytes] + +bounded_bytes = bytes .size (0 .. 64) + +certificate = [stake_registration + / stake_deregistration + / stake_delegation + / pool_registration + / pool_retirement + / reg_cert + / unreg_cert + / vote_deleg_cert + / stake_vote_deleg_cert + / stake_reg_deleg_cert + / vote_reg_deleg_cert + / stake_vote_reg_deleg_cert + / auth_committee_hot_cert + / resign_committee_cold_cert + / reg_drep_cert + / unreg_drep_cert + / update_drep_cert] + +certificates = nonempty_set + +coin = uint + +committee_cold_credential = credential + +committee_hot_credential = credential + +constitution = [anchor, scripthash / nil] + +constr = #6.121([* a0]) + / #6.122([* a0]) + / #6.123([* a0]) + / #6.124([* a0]) + / #6.125([* a0]) + / #6.126([* a0]) + / #6.127([* a0]) + / #6.102([uint, [* a0]]) + +; The format for costmdls is flexible enough to allow adding Plutus +; built-ins and language versions in the future. +costmdls = {? 0 : [* int64] + , ? 1 : [* int64] + , ? 2 : [* int64] + , * 3 .. 255 => [* int64]} + +credential = [0, addr_keyhash // 1, scripthash] + +data = #6.24(bytes .cbor plutus_data) + +datum_hash = $hash32 + +datum_option = [0, $hash32 // 1, data] + +distinct_VBytes = bytes .size 8 + / bytes .size 16 + / bytes .size 20 + / bytes .size 24 + / bytes .size 30 + / bytes .size 32 + +dns_name = text .size (0 .. 128) + +drep = [0, addr_keyhash // 1, scripthash // 2 // 3] + +drep_credential = credential + +drep_voting_thresholds = [unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval] + +epoch_interval = uint .size 4 + +epoch_no = uint .size 8 + +ex_unit_prices = [mem_price : nonnegative_interval + , step_price : nonnegative_interval] + +ex_units = [mem : uint, steps : uint] + +gov_action = [parameter_change_action + / hard_fork_initiation_action + / treasury_withdrawals_action + / no_confidence + / update_committee + / new_constitution + / info_action] + +gov_action_id = [transaction_id : $hash32, gov_action_index : uint .size 2] + +hard_fork_initiation_action = (1, gov_action_id / nil, protocol_version) + +header = [header_body, body_signature : $kes_signature] + +header_body = [block_number : block_no + , slot : slot_no + , prev_hash : $hash32 / nil + , issuer_vkey : $vkey + , vrf_vkey : $vrf_vkey + , vrf_result : $vrf_cert + , block_body_size : uint .size 4 + , block_body_hash : $hash32 + , operational_cert + , protocol_version] + +info_action = 6 + +int64 = -9223372036854775808 .. 9223372036854775807 + +invalid_before = (4, slot_no) + +invalid_hereafter = (5, slot_no) + +ipv4 = bytes .size 4 + +ipv6 = bytes .size 16 + +language = 0 / 1 / 2 + +major_protocol_version = 1 .. 10 + +metadata = {* transaction_metadatum_label => transaction_metadatum} + +mint = multiasset + +multi_host_name = (2, dns_name) + +multiasset = {+ policy_id => {+ asset_name => a0}} + +native_script = [script_pubkey + / script_all + / script_any + / script_n_of_k + / invalid_before + / invalid_hereafter] + +negInt64 = -9223372036854775808 .. -1 + +network_id = 0 / 1 + +new_constitution = (5, gov_action_id / nil, constitution) + +no_confidence = (3, gov_action_id / nil) + +nonZeroInt64 = negInt64 / posInt64 + +nonempty_set = #6.258([+ a0]) / [+ a0] + +nonnegative_interval = #6.30([uint, positive_int]) + +operational_cert = [hot_vkey : $kes_vkey + , sequence_number : uint .size 8 + , kes_period : uint + , sigma : $signature] + +parameter_change_action = (0 + , gov_action_id / nil + , protocol_param_update + , policy_hash / nil) + +plutus_data = constr + / {* plutus_data => plutus_data} + / [* plutus_data] + / big_int + / bounded_bytes + +; The real type of plutus_v1_script, plutus_v2_script and plutus_v3_script is bytes. +; However, because we enforce uniqueness when many scripts are supplied, +; we need to hack around for tests in order to avoid generating duplicates, +; since the cddl tool we use for roundtrip testing doesn't generate distinct collections. +; +plutus_v1_script = distinct_VBytes + +plutus_v2_script = distinct_VBytes + +plutus_v3_script = distinct_VBytes + +policy_hash = scripthash + +policy_id = scripthash + +pool_keyhash = $hash28 + +pool_metadata = [url, pool_metadata_hash] + +pool_metadata_hash = $hash32 + +pool_params = (pool_keyhash + , vrf_keyhash + , coin + , coin + , unit_interval + , reward_account + , set + , [* relay] + , pool_metadata / nil) + +pool_registration = (3, pool_params) + +pool_retirement = (4, pool_keyhash, epoch_no) + +pool_voting_thresholds = [unit_interval + , unit_interval + , unit_interval + , unit_interval + , unit_interval] + +port = uint .le 65535 + +posInt64 = 1 .. 9223372036854775807 + +positive_coin = 1 .. 18446744073709551615 + +positive_int = 1 .. 18446744073709551615 + +post_alonzo_transaction_output = {0 : address + , 1 : value + , ? 2 : datum_option + , ? 3 : script_ref} + +potential_languages = 0 .. 255 + +pre_babbage_transaction_output = [address + , amount : value + , ? datum_hash : datum_hash] + +proposal_procedure = [deposit : coin, reward_account, gov_action, anchor] + +proposal_procedures = nonempty_set + +protocol_param_update = {? 0 : coin + , ? 1 : coin + , ? 2 : uint .size 4 + , ? 3 : uint .size 4 + , ? 4 : uint .size 2 + , ? 5 : coin + , ? 6 : coin + , ? 7 : epoch_interval + , ? 8 : uint .size 2 + , ? 9 : nonnegative_interval + , ? 10 : unit_interval + , ? 11 : unit_interval + , ? 16 : coin + , ? 17 : coin + , ? 18 : costmdls + , ? 19 : ex_unit_prices + , ? 20 : ex_units + , ? 21 : ex_units + , ? 22 : uint .size 4 + , ? 23 : uint .size 2 + , ? 24 : uint .size 2 + , ? 25 : pool_voting_thresholds + , ? 26 : drep_voting_thresholds + , ? 27 : uint .size 2 + , ? 28 : epoch_interval + , ? 29 : epoch_interval + , ? 30 : coin + , ? 31 : coin + , ? 32 : epoch_interval + , ? 33 : nonnegative_interval} + +protocol_version = [major_protocol_version, uint] + +redeemer_tag = 0 / 1 / 2 / 3 / 4 / 5 + +redeemers = [+ [tag : redeemer_tag + , index : uint .size 4 + , data : plutus_data + , ex_units : ex_units]] + / {+ [tag : redeemer_tag + , index : uint .size 4] => [data : plutus_data + , ex_units : ex_units]} + +reg_cert = (7, stake_credential, coin) + +reg_drep_cert = (16, drep_credential, coin, anchor / nil) + +relay = [single_host_addr / single_host_name / multi_host_name] + +required_signers = nonempty_set + +resign_committee_cold_cert = (15, committee_cold_credential, anchor / nil) + +reward_account = h'E090000000000000000000000000000000000000000000000000000000' + / h'F0A0000000000000000000000000000000000000000000000000000000' + +script = [0, native_script + // 1, plutus_v1_script + // 2, plutus_v2_script + // 3, plutus_v3_script] + +script_all = (1, [* native_script]) + +script_any = (2, [* native_script]) + +script_data_hash = $hash32 + +script_n_of_k = (3, uint, [* native_script]) + +script_pubkey = (0, addr_keyhash) + +script_ref = #6.24(bytes .cbor script) + +; To compute a script hash, note that you must prepend +; a tag to the bytes of the script before hashing. +; The tag is determined by the language. +; The tags in the Conway era are: +; "" for multisig scripts +; "" for Plutus V1 scripts +; "" for Plutus V2 scripts +; "" for Plutus V3 scripts +; +scripthash = $hash28 + +set = #6.258([* a0]) / [* a0] + +signkeyKES = bytes .size 64 + +single_host_addr = (0, port / nil, ipv4 / nil, ipv6 / nil) + +single_host_name = (1, port / nil, dns_name) + +slot_no = uint .size 8 + +stake_credential = credential + +stake_delegation = (2, stake_credential, pool_keyhash) + +stake_deregistration = (1, stake_credential) + +stake_reg_deleg_cert = (11, stake_credential, pool_keyhash, coin) + +stake_registration = (0, stake_credential) + +stake_vote_deleg_cert = (10, stake_credential, pool_keyhash, drep) + +stake_vote_reg_deleg_cert = (13, stake_credential, pool_keyhash, drep, coin) + +transaction = [transaction_body + , transaction_witness_set + , bool + , auxiliary_data / nil] + +transaction_body = {0 : set + , 1 : [* transaction_output] + , 2 : coin + , ? 3 : slot_no + , ? 4 : certificates + , ? 5 : withdrawals + , ? 7 : auxiliary_data_hash + , ? 8 : slot_no + , ? 9 : mint + , ? 11 : script_data_hash + , ? 13 : nonempty_set + , ? 14 : required_signers + , ? 15 : network_id + , ? 16 : transaction_output + , ? 17 : coin + , ? 18 : nonempty_set + , ? 19 : voting_procedures + , ? 20 : proposal_procedures + , ? 21 : coin + , ? 22 : positive_coin} + +transaction_index = uint .size 2 + +transaction_input = [transaction_id : $hash32, index : uint .size 2] + +transaction_metadatum = {* transaction_metadatum => transaction_metadatum} + / [* transaction_metadatum] + / int + / bytes .size (0 .. 64) + / text .size (0 .. 64) + +transaction_metadatum_label = uint .size 8 + +transaction_output = pre_babbage_transaction_output + / post_alonzo_transaction_output + +transaction_witness_set = {? 0 : nonempty_set + , ? 1 : nonempty_set + , ? 2 : nonempty_set + , ? 3 : nonempty_set + , ? 4 : nonempty_set + , ? 5 : redeemers + , ? 6 : nonempty_set + , ? 7 : nonempty_set} + +treasury_withdrawals_action = (2, {reward_account => coin}, policy_hash / nil) + +unit_interval = #6.30([1, 2]) + +unreg_cert = (8, stake_credential, coin) + +unreg_drep_cert = (17, drep_credential, coin) + +update_committee = (4 + , gov_action_id / nil + , set + , {committee_cold_credential => epoch_no} + , unit_interval) + +update_drep_cert = (18, drep_credential, anchor / nil) + +url = text .size (0 .. 128) + +value = coin / [coin, multiasset] + +vkeywitness = [$vkey, $signature] + +vote = 0 .. 2 + +vote_deleg_cert = (9, stake_credential, drep) + +vote_reg_deleg_cert = (12, stake_credential, drep, coin) + +voter = [0, addr_keyhash + // 1, scripthash + // 2, addr_keyhash + // 3, scripthash + // 4, addr_keyhash] + +voting_procedure = [vote, anchor / nil] + +voting_procedures = {+ voter => {+ gov_action_id => voting_procedure}} + +vrf_keyhash = $hash32 + +withdrawals = {+ reward_account => coin} \ No newline at end of file diff --git a/hie.yaml b/hie.yaml index 19f3d97d222..83559507bf8 100644 --- a/hie.yaml +++ b/hie.yaml @@ -314,3 +314,6 @@ cradle: - path: "libs/vector-map/bench/Bench.hs" component: "vector-map:bench:bench" + + - path: "libs/cardano-cddl/app" + component: "cardano-cddl:exe:cardano-cddl" diff --git a/libs/cardano-cddl/CHANGELOG.md b/libs/cardano-cddl/CHANGELOG.md new file mode 100644 index 00000000000..3e176d5e3b4 --- /dev/null +++ b/libs/cardano-cddl/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for cardano-cddl + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/libs/cardano-cddl/LICENSE b/libs/cardano-cddl/LICENSE new file mode 100644 index 00000000000..d6456956733 --- /dev/null +++ b/libs/cardano-cddl/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/libs/cardano-cddl/app/Main.hs b/libs/cardano-cddl/app/Main.hs new file mode 100644 index 00000000000..4bdae2afb38 --- /dev/null +++ b/libs/cardano-cddl/app/Main.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE ImportQualifiedPost #-} + +module Main where + +import Codec.CBOR.Cuddle.CDDL (sortCDDL) +import Codec.CBOR.Cuddle.Huddle (Huddle, toCDDL) +import Codec.CBOR.Cuddle.Pretty () +import Data.Foldable (for_) +import Prettyprinter (Pretty (pretty)) +import Prettyprinter.Render.Text (hPutDoc) +import System.IO (IOMode (..), withFile) +import Test.Cardano.Ledger.Conway.CDDL qualified as Conway + +-- | Map from spec to the CDDL file location +cuddleSpecs :: [(Huddle, String)] +cuddleSpecs = + [ (Conway.conway, "./eras/conway/impl/cddl-files/conway-merged.cddl") + ] + +writeSpec :: Huddle -> String -> IO () +writeSpec hddl path = + let cddl = toCDDL hddl + in withFile path WriteMode $ \h -> + hPutDoc h (pretty $ sortCDDL cddl) + +-- Generate cddl files for all relevant specifications +main :: IO () +main = for_ cuddleSpecs (uncurry writeSpec) diff --git a/libs/cardano-cddl/cardano-cddl.cabal b/libs/cardano-cddl/cardano-cddl.cabal new file mode 100644 index 00000000000..447db2cbf5f --- /dev/null +++ b/libs/cardano-cddl/cardano-cddl.cabal @@ -0,0 +1,34 @@ +cabal-version: 3.4 +name: cardano-cddl +version: 0.1.0.0 +synopsis: Tool to generate CDDL specifications for the ledger +-- description: +homepage: https://github.com/intersectmbo/cardano-ledger +license: Apache-2.0 +license-file: LICENSE +author: IOG +maintainer: operations@iohk.io +-- copyright: +category: Codec +build-type: Simple +extra-doc-files: CHANGELOG.md +-- extra-source-files: + +common warnings + ghc-options: -Wall + +executable cardano-cddl + import: warnings + main-is: Main.hs + -- other-modules: + -- other-extensions: + build-depends: + base >=4.14.0.0, + cardano-ledger-conway:testlib, + containers, + cuddle >= 0.2.1, + prettyprinter, + text + + hs-source-dirs: app + default-language: Haskell2010 diff --git a/scripts/gen-cddl.sh b/scripts/gen-cddl.sh new file mode 100755 index 00000000000..9a72700cbab --- /dev/null +++ b/scripts/gen-cddl.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -euo pipefail + +changed=$(git diff --name-only origin/master -- 'eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/CDDL.hs') + +if [[ -n "$changed" ]]; then + cabal run cardano-cddl +fi