Skip to content

Add CDDLs #1422

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,5 @@ cabal.project.local
cabal.project.local~
.HTF/
.ghc.environment.*

ouroboros-consensus-cardano/cddl/out/
Empty file added .gitmodules
Empty file.
46 changes: 45 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ index-state:
-- Bump this if you need newer packages from Hackage
, hackage.haskell.org 2025-05-15T07:59:00Z
-- Bump this if you need newer packages from CHaP
, cardano-haskell-packages 2025-05-15T08:36:14Z
, cardano-haskell-packages 2025-05-15T10:30:42Z

packages:
ouroboros-consensus
Expand Down Expand Up @@ -64,3 +64,47 @@ if impl (ghc >= 9.12)

-- https://github.com/kapralVV/Unique/issues/11
, Unique:hashable

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger
tag: 3028f02583b22cd1d1039a42acfad94626bfa39c
--sha256: sha256-UFIQ1qinge1TMtqU2e0Nyjdpj42yj+lLKrpnXeqo1mI=
subdir:
eras/allegra/impl
eras/alonzo/impl
eras/alonzo/test-suite
eras/babbage/impl
eras/babbage/test-suite
eras/conway/impl
eras/conway/test-suite
eras/mary/impl
eras/shelley/impl
eras/shelley/test-suite
eras/shelley-ma/test-suite
libs/cardano-ledger-api
libs/cardano-ledger-core
libs/cardano-ledger-binary
libs/cardano-protocol-tpraos
libs/non-integral
libs/small-steps
libs/cardano-data
libs/set-algebra
libs/vector-map
eras/byron/chain/executable-spec
eras/byron/ledger/executable-spec
eras/byron/ledger/impl
eras/byron/crypto

source-repository-package
type: git
location: https://github.com/input-output-hk/cuddle
tag: 12667de12c911f94f0c40433bb02e46e74fad02d

allow-newer:
ouroboros-consensus:cardano-ledger-core,
ouroboros-consensus-cardano:cardano-ledger-byron,
ouroboros-consensus-cardano:cardano-ledger-conway,
ouroboros-consensus-cardano:cardano-ledger-core,
ouroboros-consensus-cardano:cardano-ledger-shelley,
ouroboros-consensus-protocol:cardano-ledger-core,
53 changes: 53 additions & 0 deletions ouroboros-consensus-cardano/cddl/base.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
telescope7<byron, shelley, allegra, mary, alonzo, babbage, conway>
= [pastEra, pastEra, pastEra, pastEra, pastEra, pastEra, currentEra<conway>] /
[pastEra, pastEra, pastEra, pastEra, pastEra, currentEra<babbage>] /
[pastEra, pastEra, pastEra, pastEra, currentEra<alonzo>] /
[pastEra, pastEra, pastEra, currentEra<mary>] /
[pastEra, pastEra, currentEra<allegra>] /
[pastEra, currentEra<shelley>] /
[currentEra<byron>]

ns7<byron, shelley, allegra, mary, alonzo, babbage, conway>
= [6, conway] /
[5, babbage] /
[4, alonzo] /
[3, mary] /
[2, allegra] /
[1, shelley] /
[0, byron]

;; Blockchain types
pastEra = [bound, bound]
currentEra<st> = [bound, st]
bound = [relativeTime, slotno, epochno]
eraIdx = word8
individualPoolStake = [stake, hash]
nonce = [0] / [1, hash]
point = [] / [ slotno, hash ]
poolDistr = map<keyhash, individualPoolStake>
slotno = word64
stake = rational

withOrigin<v> = [] / [v]
withOriginTH<v> = [0] / [1, v]

;; Collections
either<x, y> = [0, x] / [1, y]
map<x, y> = { * x => y }
maybe<x> = [] / [x]
seq<x> = [*23 x] / [24* x] ; encoded with indefinite-length encoding
set<x> = #6.258([* x])

;; Types from other packages
blockno = word64
epochno = word64
coin = word64
rational = [int, int]
keyhash = bstr .size 28
hash = bstr .size 32
relativeTime = int

;; Base word types
word8 = uint .size 1
word32 = uint .size 4
word64 = uint .size 8
44 changes: 44 additions & 0 deletions ouroboros-consensus-cardano/cddl/cddlc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
mkdir -p out

gen () {
echo -n -e "\t- $1"
mkdir -p out/$(echo $1 | rev | cut -d'/' -f2- | rev)
cddlc -u2tcddl $1.cddl > "out/$1.compiled.cddl"
echo " ok"
}

CDDL_INCLUDE_PATH=""
for f in $(fd -t d); do
CDDL_INCLUDE_PATH+="$f:"
done

export CDDL_INCLUDE_PATH=$CDDL_INCLUDE_PATH.:

IN=$(fd -e cddl \
-E ledger-cddls \
-E out \
-E base.cddl \
-E node-to-client/localstatequery/shelley \
-E node-to-client/localstatequery/byron \
-E node-to-client/localstatequery/consensus \
-E disk/snapshot
)

echo "Generating complete CDDLs:"
for f in $IN; do
gen $(echo $f | cut -d'.' -f1)
done

UNDEFINEDS=$(grep -R undefined out)

if [ ! -z "$UNDEFINEDS" ]; then
echo -e "\033[0;31mThere were undefined references!\n$UNDEFINEDS\033[0m"

while IFS= read -r line || [[ -n $line ]]; do
fileName=$(echo $line | cut -d':' -f1)
ref=$(echo $line | cut -d':' -f3 | tr -d ' ')
echo "$ref = any" >> $fileName
done < <(printf '%s' "$UNDEFINEDS")
fi

echo "Done"
15 changes: 15 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/block.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cardanoBlock = byron.block
/ [2, shelley.block]
/ [3, allegra.block]
/ [4, mary.block]
/ [5, alonzo.block]
/ [6, babbage.block]
/ [7, conway.block]

;# import byron as byron
;# import shelley as shelley
;# import allegra as allegra
;# import mary as mary
;# import alonzo as alonzo
;# import babbage as babbage
;# import conway as conway
9 changes: 9 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/snapshot.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ledgerStateSnapshot =
[snapshotEncodingVersion1, extLedgerState]

snapshotEncodingVersion1 = 1

extLedgerState = [ledgerState, headerState]

;# import ledgerstate
;# import headerstate
23 changes: 23 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/snapshot/headerstate.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
headerState =
[withOrigin<headerStateTip>, headerStateChainDep]

headerStateTip =
ns7<byronAnnTip, annTip, annTip, annTip, annTip, annTip, annTip>

byronAnnTip = [slotno, hash, blockno, bool]
annTip = [slotno, hash, blockno]

headerStateChainDep =
telescope7<versionedPbftState,
versionedTPraosState,
versionedTPraosState,
versionedTPraosState,
versionedTPraosState,
versionedPraosState,
versionedPraosState>

versionedPbftState = [serializationFormat1, {* keyhash => [* slotno]}]

;# import base
;# import praos
;# import tpraos
27 changes: 27 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/snapshot/ledgerstate.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ledgerState =
telescope7<byron.ledgerstate,
versionedShelleyLedgerState<shelley.ledgerstate>,
versionedShelleyLedgerState<allegra.ledgerstate>,
versionedShelleyLedgerState<mary.ledgerstate>,
versionedShelleyLedgerState<alonzo.ledgerstate>,
versionedShelleyLedgerState<babbage.ledgerstate>,
versionedShelleyLedgerState<conway.ledgerstate>>

versionedShelleyLedgerState<era> = [ shelleyVersion2, shelleyLedgerState<era> ]

shelleyVersion2 = 2

shelleyLedgerState<era> = [ withOrigin<shelleyTip>, era, shelleyTransition ]

shelleyTip = [slotno, blockno, hash]

shelleyTransition = word32

;# import base
;# import byron as byron
;# import shelley as shelley
;# import allegra as allegra
;# import mary as mary
;# import alonzo as alonzo
;# import babbage as babbage
;# import conway as conway
13 changes: 13 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/snapshot/praos.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
versionedPraosState = [praosVersion, praosState]

praosVersion = 0

praosState = [withOrigin<slotno>,
{* keyhash => word64},
nonce,
nonce,
nonce,
nonce,
nonce]

;# import base
12 changes: 12 additions & 0 deletions ouroboros-consensus-cardano/cddl/disk/snapshot/tpraos.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
versionedTPraosState =
[serializationFormat1, [withOriginTH<slotno>, tpraosState]]

tpraosState = [prtclState, ticknState, nonce]

prtclState = [{* keyhash => word64}, nonce, nonce]

ticknState = [nonce, nonce]

serializationFormat1 = 1

;# import base
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query = 0
result = byron.upistate

;# import byron as byron
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query = 2
result = blockno

;# import base
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query = 3
result = point

;# import base
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query = [1]
result = eraIdx

;# import base
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query = [0]
result = maybe<bound>

;# import base
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query = [0]
result = interpreter

interpreter = [* eraSummary]
eraSummary = [eraStart, eraEnd, eraParams]
eraStart = bound
eraEnd = null / bound
eraParams = [epochSize, slotLength, safeZone, genesisWindow]
epochSize = word64
slotLength = int ; millisec
safeZone = standardSafeZone / unsafeIndefiniteSafeZone
standardSafeZone = [0, safeFromTip, safeBeforeEpoch]
safeFromTip = word64
safeBeforeEpoch = [0]
unsafeIndefiniteSafeZone = [1]
genesisWindow = word64

;# import base
Copy link

Choose a reason for hiding this comment

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

Can we use qualified or explicit imports? Then it would be more clear which rules come from this library. For example we could have eraStart = base.bound

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query = 1
result = utctime

utctime = [year, dayOfYear, timeOfDayPico]
year = bigint
dayOfYear = int
timeOfDayPico = bigint
Loading
Loading