Skip to content

Commit 35f959a

Browse files
committed
CDDL: Consolidate scripts across all eras.
* Make native_script fields era-prefixed as and when they are introduced * Add smart constructors for script types in core * Fix script_n_of_k for eras after allegra
1 parent fa8a687 commit 35f959a

File tree

21 files changed

+427
-283
lines changed

21 files changed

+427
-283
lines changed

eras/allegra/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### `testlib`
1515

16+
* Rename `native_script` -> `allegra_native_script` in CDDL
1617
* Add `auxiliary_data_array` to CDDL for simplification
1718
* Remove `metadata` redefinition in CDDL
1819
* Use fixed-sized `uint .size 8` for `slot` in CDDL for timelock validity intervals

eras/allegra/impl/cddl-files/allegra.cddl

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ hash28 = bytes .size 28
145145
; To compute a script hash, note that you must prepend
146146
; a tag to the bytes of the script before hashing.
147147
; The tag is determined by the language.
148-
; The tags in the Conway era are:
149-
; "\x00" for multisig scripts
148+
; The tags are:
149+
; "\x00" for multisig/native scripts
150150
; "\x01" for Plutus V1 scripts
151151
; "\x02" for Plutus V2 scripts
152152
; "\x03" for Plutus V3 scripts
153+
; "\x04" for Plutus V4 scripts
153154
script_hash = hash28
154155

155156
; This will be deprecated in a future era
@@ -286,20 +287,22 @@ transaction_witness_set =
286287

287288
vkeywitness = [vkey, signature]
288289

289-
; Timelock validity intervals are half-open intervals [a, b).
290+
; Allegra introduces timelock support for native scripts.
291+
; This is the 6-variant native script format used by
292+
; Allegra, Mary, Alonzo, Babbage, and Conway.
290293
;
291-
; invalid_before:
292-
; specifies the left (included) endpoint a.
294+
; Timelock validity intervals are half-open intervals [a, b).
295+
; script_invalid_before: specifies the left (included) endpoint a.
296+
; script_invalid_hereafter: specifies the right (excluded) endpoint b.
293297
;
294-
; invalid_hereafter:
295-
; specifies the right (excluded) endpoint b.
298+
; Note: Allegra switched to int64 for script_n_of_k thresholds.
296299
native_script =
297300
[ script_pubkey
298301
// script_all
299302
// script_any
300303
// script_n_of_k
301-
// invalid_before
302-
// invalid_hereafter
304+
// script_invalid_before
305+
// script_invalid_hereafter
303306
]
304307

305308

@@ -317,9 +320,13 @@ min_int64 = -9223372036854775808
317320

318321
max_int64 = 9223372036854775807
319322

320-
invalid_before = (4, slot)
323+
; Timelock validity intervals are half-open intervals [a, b).
324+
; This field specifies the left (included) endpoint a.
325+
script_invalid_before = (4, slot)
321326

322-
invalid_hereafter = (5, slot)
327+
; Timelock validity intervals are half-open intervals [a, b).
328+
; This field specifies the right (excluded) endpoint b.
329+
script_invalid_hereafter = (5, slot)
323330

324331
bootstrap_witness =
325332
[ public_key : vkey

eras/allegra/impl/testlib/Test/Cardano/Ledger/Allegra/CDDL.hs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ module Test.Cardano.Ledger.Allegra.CDDL (
1717
auxiliary_data,
1818
auxiliary_data_array,
1919
auxiliary_scripts,
20-
script_pubkey,
21-
script_all,
22-
script_any,
23-
invalid_before,
24-
invalid_hereafter,
20+
allegra_native_script,
2521
) where
2622

2723
import Cardano.Ledger.Allegra (AllegraEra)
@@ -38,45 +34,47 @@ allegraCDDL =
3834
, HIRule $ transaction @AllegraEra
3935
]
4036

41-
native_script :: Rule
42-
native_script =
37+
allegra_native_script :: Rule
38+
allegra_native_script =
4339
comment
44-
[str|Timelock validity intervals are half-open intervals [a, b).
40+
[str|Allegra introduces timelock support for native scripts.
41+
|This is the 6-variant native script format used by
42+
|Allegra, Mary, Alonzo, Babbage, and Conway.
4543
|
46-
| invalid_before:
47-
| specifies the left (included) endpoint a.
44+
|Timelock validity intervals are half-open intervals [a, b).
45+
| script_invalid_before: specifies the left (included) endpoint a.
46+
| script_invalid_hereafter: specifies the right (excluded) endpoint b.
4847
|
49-
| invalid_hereafter:
50-
| specifies the right (excluded) endpoint b.
48+
|Note: Allegra switched to int64 for script_n_of_k thresholds.
5149
|]
5250
$ "native_script"
5351
=:= arr [a script_pubkey]
5452
/ arr [a script_all]
5553
/ arr [a script_any]
5654
/ arr [a script_n_of_k]
57-
/ arr [a invalid_before]
58-
/ arr [a invalid_hereafter]
55+
/ arr [a script_invalid_before]
56+
/ arr [a script_invalid_hereafter]
5957

6058
script_pubkey :: Named Group
61-
script_pubkey = "script_pubkey" =:~ grp [0, a addr_keyhash]
59+
script_pubkey = mkScriptPubkey
6260

6361
script_all :: Named Group
64-
script_all = "script_all" =:~ grp [1, a (arr [0 <+ a native_script])]
62+
script_all = mkScriptAll allegra_native_script
6563

6664
script_any :: Named Group
67-
script_any = "script_any" =:~ grp [2, a (arr [0 <+ a native_script])]
65+
script_any = mkScriptAny allegra_native_script
6866

6967
script_n_of_k :: Named Group
70-
script_n_of_k = "script_n_of_k" =:~ grp [3, "n" ==> int64, a (arr [0 <+ a native_script])]
68+
script_n_of_k = mkScriptNOfK int64 allegra_native_script
7169

72-
invalid_before :: Named Group
73-
invalid_before = "invalid_before" =:~ grp [4, a slot]
70+
script_invalid_before :: Named Group
71+
script_invalid_before = mkScriptInvalidBefore
7472

75-
invalid_hereafter :: Named Group
76-
invalid_hereafter = "invalid_hereafter" =:~ grp [5, a slot]
73+
script_invalid_hereafter :: Named Group
74+
script_invalid_hereafter = mkScriptInvalidHereafter
7775

7876
auxiliary_scripts :: Rule
79-
auxiliary_scripts = "auxiliary_scripts" =:= arr [0 <+ a native_script]
77+
auxiliary_scripts = "auxiliary_scripts" =:= arr [0 <+ a allegra_native_script]
8078

8179
auxiliary_data_array :: Rule
8280
auxiliary_data_array =
@@ -134,6 +132,6 @@ transaction_witness_set =
134132
"transaction_witness_set"
135133
=:= mp
136134
[ opt $ idx 0 ==> arr [0 <+ a vkeywitness]
137-
, opt $ idx 1 ==> arr [0 <+ a native_script]
135+
, opt $ idx 1 ==> arr [0 <+ a allegra_native_script]
138136
, opt $ idx 2 ==> arr [0 <+ a bootstrap_witness]
139137
]

eras/alonzo/impl/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
### `testlib`
4040

41+
* Rename `plutus_script` -> `plutus_v1_script` in CDDL
42+
* Add `plutus_v1_script` to CDDL exports
4143
* Add `auxiliary_data_map` to CDDL for simplification
4244
* Remove redefinition of `auxiliary_data_hash` from CDDL
4345
* Remove deprecated function `mkPlutusScript'`

eras/alonzo/impl/cddl-files/alonzo.cddl

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ policy_id = script_hash
153153
; To compute a script hash, note that you must prepend
154154
; a tag to the bytes of the script before hashing.
155155
; The tag is determined by the language.
156-
; The tags in the Conway era are:
157-
; "\x00" for multisig scripts
156+
; The tags are:
157+
; "\x00" for multisig/native scripts
158158
; "\x01" for Plutus V1 scripts
159159
; "\x02" for Plutus V2 scripts
160160
; "\x03" for Plutus V3 scripts
161+
; "\x04" for Plutus V4 scripts
161162
script_hash = hash28
162163

163164
hash28 = bytes .size 28
@@ -436,35 +437,37 @@ network_id = 0/ 1
436437

437438
;
438439
; NEW:
439-
; 3: [* plutus_script ]
440+
; 3: [* plutus_v1_script ]
440441
; 4: [* plutus_data ]
441442
; 5: redeemers
442443
transaction_witness_set =
443444
{ ? 0 : [* vkeywitness]
444445
, ? 1 : [* native_script]
445446
, ? 2 : [* bootstrap_witness]
446-
, ? 3 : [* plutus_script]
447+
, ? 3 : [* plutus_v1_script]
447448
, ? 4 : [* plutus_data]
448449
, ? 5 : redeemers
449450
}
450451

451452

452453
vkeywitness = [vkey, signature]
453454

454-
; Timelock validity intervals are half-open intervals [a, b).
455+
; Allegra introduces timelock support for native scripts.
456+
; This is the 6-variant native script format used by
457+
; Allegra, Mary, Alonzo, Babbage, and Conway.
455458
;
456-
; invalid_before:
457-
; specifies the left (included) endpoint a.
459+
; Timelock validity intervals are half-open intervals [a, b).
460+
; script_invalid_before: specifies the left (included) endpoint a.
461+
; script_invalid_hereafter: specifies the right (excluded) endpoint b.
458462
;
459-
; invalid_hereafter:
460-
; specifies the right (excluded) endpoint b.
463+
; Note: Allegra switched to int64 for script_n_of_k thresholds.
461464
native_script =
462465
[ script_pubkey
463466
// script_all
464467
// script_any
465468
// script_n_of_k
466-
// invalid_before
467-
// invalid_hereafter
469+
// script_invalid_before
470+
// script_invalid_hereafter
468471
]
469472

470473

@@ -474,11 +477,15 @@ script_all = (1, [* native_script])
474477

475478
script_any = (2, [* native_script])
476479

477-
script_n_of_k = (3, n : uint, [* native_script])
480+
script_n_of_k = (3, n : int64, [* native_script])
478481

479-
invalid_before = (4, slot)
482+
; Timelock validity intervals are half-open intervals [a, b).
483+
; This field specifies the left (included) endpoint a.
484+
script_invalid_before = (4, slot)
480485

481-
invalid_hereafter = (5, slot)
486+
; Timelock validity intervals are half-open intervals [a, b).
487+
; This field specifies the right (excluded) endpoint b.
488+
script_invalid_hereafter = (5, slot)
482489

483490
bootstrap_witness =
484491
[ public_key : vkey
@@ -488,7 +495,19 @@ bootstrap_witness =
488495
]
489496

490497

491-
plutus_script = bytes
498+
; Alonzo introduces Plutus smart contracts.
499+
; Plutus V1 scripts are opaque bytestrings.
500+
plutus_v1_script = distinct_bytes
501+
502+
; A type for distinct values.
503+
; The type parameter must support .size, for example: bytes or uint
504+
distinct_bytes =
505+
bytes .size 8
506+
/ bytes .size 16
507+
/ bytes .size 20
508+
/ bytes .size 24
509+
/ bytes .size 30
510+
/ bytes .size 32
492511

493512
; NEW
494513
plutus_data =
@@ -565,7 +584,7 @@ auxiliary_data_array =
565584
auxiliary_scripts = [* native_script]
566585

567586
auxiliary_data_map =
568-
#6.259({? 0 : metadata, ? 1 : [* native_script], ? 2 : [* plutus_script]})
587+
#6.259({? 0 : metadata, ? 1 : [* native_script], ? 2 : [* plutus_v1_script]})
569588

570589
transaction =
571590
[transaction_body, transaction_witness_set, bool, auxiliary_data/ nil]

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/CDDL.hs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Test.Cardano.Ledger.Alonzo.CDDL (
1414
certificates,
1515
required_signers,
1616
network_id,
17-
native_script,
17+
plutus_v1_script,
1818
redeemers,
1919
constr,
2020
ex_unit_prices,
@@ -252,25 +252,30 @@ transaction_witness_set =
252252
comment
253253
[str|
254254
|NEW:
255-
| 3: [* plutus_script ]
255+
| 3: [* plutus_v1_script ]
256256
| 4: [* plutus_data ]
257257
| 5: redeemers
258258
|]
259259
$ "transaction_witness_set"
260260
=:= mp
261261
[ opt $ idx 0 ==> arr [0 <+ a vkeywitness]
262-
, opt $ idx 1 ==> arr [0 <+ a native_script]
262+
, opt $ idx 1 ==> arr [0 <+ a allegra_native_script]
263263
, opt $ idx 2 ==> arr [0 <+ a bootstrap_witness]
264-
, opt $ idx 3 ==> arr [0 <+ a plutus_script]
264+
, opt $ idx 3 ==> arr [0 <+ a plutus_v1_script]
265265
, opt $ idx 4 ==> arr [0 <+ a plutus_data]
266266
, opt $ idx 5 ==> redeemers
267267
]
268268

269269
redeemers :: Rule
270270
redeemers = "redeemers" =:= arr [0 <+ a redeemer]
271271

272-
plutus_script :: Rule
273-
plutus_script = "plutus_script" =:= VBytes
272+
plutus_v1_script :: Rule
273+
plutus_v1_script =
274+
comment
275+
[str|Alonzo introduces Plutus smart contracts.
276+
|Plutus V1 scripts are opaque bytestrings.
277+
|]
278+
$ "plutus_v1_script" =:= distinct VBytes
274279

275280
plutus_data :: Rule
276281
plutus_data =
@@ -360,8 +365,8 @@ auxiliary_data_map =
360365
259
361366
( mp
362367
[ opt (idx 0 ==> metadata)
363-
, opt (idx 1 ==> arr [0 <+ a native_script])
364-
, opt (idx 2 ==> arr [0 <+ a plutus_script])
368+
, opt (idx 1 ==> arr [0 <+ a allegra_native_script])
369+
, opt (idx 2 ==> arr [0 <+ a plutus_v1_script])
365370
]
366371
)
367372

@@ -401,28 +406,6 @@ header_body =
401406
, a (protocol_version @AlonzoEra)
402407
]
403408

404-
native_script :: Rule
405-
native_script =
406-
comment
407-
[str|Timelock validity intervals are half-open intervals [a, b).
408-
|
409-
| invalid_before:
410-
| specifies the left (included) endpoint a.
411-
|
412-
| invalid_hereafter:
413-
| specifies the right (excluded) endpoint b.
414-
|]
415-
$ "native_script"
416-
=:= arr [a script_pubkey]
417-
/ arr [a script_all]
418-
/ arr [a script_any]
419-
/ arr [a script_n_of_k]
420-
/ arr [a invalid_before]
421-
/ arr [a invalid_hereafter]
422-
423-
script_n_of_k :: Named Group
424-
script_n_of_k = "script_n_of_k" =:~ grp [3, "n" ==> VUInt, a (arr [0 <+ a native_script])]
425-
426409
positive_interval :: Rule
427410
positive_interval = "positive_interval" =:= tag 30 (arr [a positive_int, a positive_int])
428411

eras/babbage/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
### `testlib`
3030

31+
* Add `plutus_v2_script` to CDDL exports
3132
* Hide Shelley CDDL `protocol_version` and re-export a new one for Babbage
3233
* Use fixed-sized `uint .size 8` for `slot` and `block_number` in CDDL for header
3334
* Add `BabbageEraImp`

0 commit comments

Comments
 (0)