Skip to content

Commit 0504345

Browse files
authored
Merge pull request #4846 from IntersectMBO/td/nonannotator-deccbor-instances
Non-Annotator DecCBOR instances
2 parents 9f98a9e + 06db334 commit 0504345

File tree

77 files changed

+1445
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1445
-226
lines changed

eras/allegra/impl/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 1.7.0.0
44

5+
* Add `DecCBOR` instances for:
6+
* `Timelock`
7+
* `AllegraTxAuxData`
8+
* `AllegraTxBody`
59
* Converted `CertState` to a type family
610
* Made the fields of predicate failures and environments lazy
711
* Add `Era era` constraint to `NoThunks` instance for `TimeLock`

eras/allegra/impl/src/Cardano/Ledger/Allegra/Scripts.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import Cardano.Ledger.MemoBytes (
7676
MemoBytes (Memo),
7777
Memoized (..),
7878
byteCountMemoBytes,
79+
decodeMemoized,
7980
getMemoRawType,
8081
mkMemoBytes,
8182
mkMemoizedEra,
@@ -189,9 +190,8 @@ instance Era era => EncCBOR (TimelockRaw era) where
189190
TimeStart m -> Sum TimeStart 4 !> To m
190191
TimeExpire m -> Sum TimeExpire 5 !> To m
191192

192-
-- This instance allows us to derive instance DecCBOR (Annotator (Timelock crypto)).
193-
-- Since Timelock is a newtype around (Memo (Timelock crypto)).
194-
193+
-- This instance allows us to derive instance DecCBOR (Annotator (Timelock era)).
194+
-- Since Timelock is a newtype around (Memo (Timelock era)).
195195
instance Era era => DecCBOR (Annotator (TimelockRaw era)) where
196196
decCBOR = decode (Summands "TimelockRaw" decRaw)
197197
where
@@ -204,6 +204,16 @@ instance Era era => DecCBOR (Annotator (TimelockRaw era)) where
204204
decRaw 5 = Ann (SumD TimeExpire <! From)
205205
decRaw n = Invalid n
206206

207+
instance Era era => DecCBOR (TimelockRaw era) where
208+
decCBOR = decode $ Summands "TimelockRaw" $ \case
209+
0 -> SumD Signature <! From
210+
1 -> SumD AllOf <! From
211+
2 -> SumD AnyOf <! From
212+
3 -> SumD MOfN <! From <! From
213+
4 -> SumD TimeStart <! From
214+
5 -> SumD TimeExpire <! From
215+
n -> Invalid n
216+
207217
-- =================================================================
208218
-- Native Scripts are Memoized TimelockRaw.
209219
-- The patterns give the appearence that the mutual recursion is not present.
@@ -222,6 +232,9 @@ instance Era era => MemPack (Timelock era) where
222232
instance Era era => NoThunks (Timelock era)
223233
instance Era era => EncCBOR (Timelock era)
224234

235+
instance Era era => DecCBOR (Timelock era) where
236+
decCBOR = TimelockConstr <$> decodeMemoized decCBOR
237+
225238
instance Memoized (Timelock era) where
226239
type RawType (Timelock era) = TimelockRaw era
227240

eras/allegra/impl/src/Cardano/Ledger/Allegra/TxAuxData.hs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ instance NFData (AllegraTxAuxDataRaw era)
122122

123123
newtype AllegraTxAuxData era = AuxiliaryDataWithBytes (MemoBytes (AllegraTxAuxDataRaw era))
124124
deriving (Generic)
125-
deriving newtype (Eq, ToCBOR, SafeToHash)
125+
deriving newtype (Eq, ToCBOR, SafeToHash, DecCBOR)
126126

127127
instance Memoized (AllegraTxAuxData era) where
128128
type RawType (AllegraTxAuxData era) = AllegraTxAuxDataRaw era
@@ -172,7 +172,7 @@ instance Era era => DecCBOR (Annotator (AllegraTxAuxDataRaw era)) where
172172
TypeListLen -> decodeFromList
173173
TypeListLen64 -> decodeFromList
174174
TypeListLenIndef -> decodeFromList
175-
_ -> error "Failed to decode AuxiliaryData"
175+
_ -> fail "Failed to decode AuxiliaryDataRaw"
176176
where
177177
decodeFromMap =
178178
decode
@@ -187,6 +187,30 @@ instance Era era => DecCBOR (Annotator (AllegraTxAuxDataRaw era)) where
187187
<*! D (sequence <$> decCBOR)
188188
)
189189

190+
instance Era era => DecCBOR (AllegraTxAuxDataRaw era) where
191+
decCBOR =
192+
peekTokenType >>= \case
193+
TypeMapLen -> decodeFromMap
194+
TypeMapLen64 -> decodeFromMap
195+
TypeMapLenIndef -> decodeFromMap
196+
TypeListLen -> decodeFromList
197+
TypeListLen64 -> decodeFromList
198+
TypeListLenIndef -> decodeFromList
199+
_ -> fail "Failed to decode AuxiliaryDataRaw"
200+
where
201+
decodeFromMap =
202+
decode
203+
( Emit AllegraTxAuxDataRaw
204+
<! From
205+
<! Emit StrictSeq.empty
206+
)
207+
decodeFromList =
208+
decode
209+
( RecD AllegraTxAuxDataRaw
210+
<! From
211+
<! From
212+
)
213+
190214
deriving via
191215
(Mem (AllegraTxAuxDataRaw era))
192216
instance

eras/allegra/impl/src/Cardano/Ledger/Allegra/TxBody/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ emptyAllegraTxBodyRaw =
210210
-- Wrap it all up in a newtype, hiding the insides with a pattern construtor.
211211

212212
newtype AllegraTxBody e = TxBodyConstr (MemoBytes (AllegraTxBodyRaw () e))
213-
deriving newtype (SafeToHash, ToCBOR)
213+
deriving newtype (SafeToHash, ToCBOR, DecCBOR)
214214

215215
instance Memoized (AllegraTxBody era) where
216216
type RawType (AllegraTxBody era) = AllegraTxBodyRaw () era

eras/allegra/impl/test/Test/Cardano/Ledger/Allegra/Binary/CddlSpec.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Test.Cardano.Ledger.Allegra.Binary.Cddl (readAllegraCddlFiles)
99
import Test.Cardano.Ledger.Allegra.CDDL (allegraCDDL)
1010
import Test.Cardano.Ledger.Binary.Cddl (
1111
beforeAllCddlFile,
12+
cddlDecoderEquivalenceSpec,
1213
cddlRoundTripAnnCborSpec,
1314
cddlRoundTripCborSpec,
1415
)
@@ -22,11 +23,21 @@ spec =
2223
describe "Ruby-based" $ beforeAllCddlFile 3 readAllegraCddlFiles $ do
2324
cddlRoundTripCborSpec @(Value AllegraEra) v "coin"
2425
cddlRoundTripAnnCborSpec @(TxBody AllegraEra) v "transaction_body"
26+
cddlRoundTripCborSpec @(TxBody AllegraEra) v "transaction_body"
2527
cddlRoundTripAnnCborSpec @(Script AllegraEra) v "native_script"
28+
cddlRoundTripCborSpec @(Script AllegraEra) v "native_script"
2629
cddlRoundTripAnnCborSpec @(TxAuxData AllegraEra) v "auxiliary_data"
30+
cddlRoundTripCborSpec @(TxAuxData AllegraEra) v "auxiliary_data"
31+
describe "DecCBOR instances equivalence via CDDL" $ do
32+
cddlDecoderEquivalenceSpec @(TxBody AllegraEra) v "transaction_body"
33+
cddlDecoderEquivalenceSpec @(Script AllegraEra) v "native_script"
34+
cddlDecoderEquivalenceSpec @(TxAuxData AllegraEra) v "auxiliary_data"
2735

2836
describe "Huddle" $ specWithHuddle allegraCDDL 100 $ do
2937
huddleRoundTripCborSpec @(Value AllegraEra) v "coin"
3038
huddleRoundTripAnnCborSpec @(TxBody AllegraEra) v "transaction_body"
39+
huddleRoundTripCborSpec @(TxBody AllegraEra) v "transaction_body"
3140
huddleRoundTripAnnCborSpec @(TxAuxData AllegraEra) v "auxiliary_data"
41+
huddleRoundTripCborSpec @(TxAuxData AllegraEra) v "auxiliary_data"
3242
huddleRoundTripAnnCborSpec @(Script AllegraEra) v "native_script"
43+
huddleRoundTripCborSpec @(Script AllegraEra) v "native_script"

eras/allegra/impl/test/Test/Cardano/Ledger/Allegra/BinarySpec.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Test.Cardano.Ledger.Allegra.Arbitrary ()
1111
import Test.Cardano.Ledger.Allegra.TreeDiff ()
1212
import Test.Cardano.Ledger.Common
1313
import Test.Cardano.Ledger.Core.Binary (specUpgrade)
14+
import Test.Cardano.Ledger.Core.Binary as Binary (decoderEquivalenceCoreEraTypesSpec)
1415
import Test.Cardano.Ledger.Core.Binary.RoundTrip (RuleListEra (..))
1516
import Test.Cardano.Ledger.Shelley.Binary.RoundTrip (roundTripShelleyCommonSpec)
1617

@@ -19,6 +20,8 @@ spec = do
1920
specUpgrade @AllegraEra def
2021
describe "RoundTrip" $ do
2122
roundTripShelleyCommonSpec @AllegraEra
23+
describe "DecCBOR instances equivalence" $ do
24+
Binary.decoderEquivalenceCoreEraTypesSpec @AllegraEra
2225

2326
instance RuleListEra AllegraEra where
2427
type

eras/alonzo/impl/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
## 1.13.0.0
44

5+
* Add `DecCBOR` instances for:
6+
* `MaryTxBody`
7+
* `TxDats`
8+
* `AlonzoTxAuxData`
9+
* `AlonzoScript`
10+
* `Redeemers`
11+
* `AlonzoTxWits`
12+
* `AlonozTxBody`
13+
* `AlonzoTx`
14+
* `AlonzoTxSeq`
15+
* Remove redundant `EncCBOR (Data era)` constraint from `DecCBOR` instance for `Annotator (AlonzoTxWits era)`
516
* Converted `CertState` to a type family
617
* Remove `reapplyAlonzoTx` as no longer needed.
718
* Add `TxInfoResult` data family, `mkTxInfoResult` and `lookupTxInfoResult` to `EraPlutusContext`
@@ -16,8 +27,11 @@
1627

1728
### `testlib`
1829

30+
* Add `DecCBOR` instances for `TranslationInstance`
1931
* Converted `CertState` to a type family
2032
* Expose `alonzoFixupFees`
33+
* Move `Arbitrary` instances for `Data`, `BinaryData` and `Datum` to `cardano-ledger-core`
34+
* Move `Arbitrary` instance for `PV1.Data` to `cardano-ledger-core`
2135

2236
## 1.12.0.0
2337

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,18 @@ instance AlonzoEraScript era => DecCBOR (Annotator (AlonzoScript era)) where
610610
{-# INLINE decodeScript #-}
611611
{-# INLINE decCBOR #-}
612612

613+
instance AlonzoEraScript era => DecCBOR (AlonzoScript era) where
614+
decCBOR = decode (Summands "AlonzoScript" decodeScript)
615+
where
616+
decodeScript = \case
617+
0 -> SumD TimelockScript <! From
618+
1 -> decodePlutus SPlutusV1
619+
2 -> decodePlutus SPlutusV2
620+
3 -> decodePlutus SPlutusV3
621+
n -> Invalid n
622+
decodePlutus slang =
623+
SumD PlutusScript <! D (decodePlutusScript slang)
624+
613625
-- | Verify that every `Script` represents a valid script. Force native scripts to Normal
614626
-- Form, to ensure that there are no bottoms and deserialize `Plutus` scripts into a
615627
-- `Cardano.Ledger.Plutus.Language.PlutusRunnable`.

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import Cardano.Ledger.Binary (
105105
Encoding,
106106
ToCBOR (..),
107107
decodeNullMaybe,
108+
decodeNullStrictMaybe,
108109
encodeListLen,
109110
encodeNullMaybe,
110111
serialize,
@@ -449,6 +450,23 @@ instance
449450
)
450451
{-# INLINE decCBOR #-}
451452

453+
instance
454+
( Typeable era
455+
, DecCBOR (TxBody era)
456+
, DecCBOR (TxWits era)
457+
, DecCBOR (TxAuxData era)
458+
) =>
459+
DecCBOR (AlonzoTx era)
460+
where
461+
decCBOR =
462+
decode $
463+
RecD AlonzoTx
464+
<! From
465+
<! From
466+
<! From
467+
<! D (decodeNullStrictMaybe decCBOR)
468+
{-# INLINE decCBOR #-}
469+
452470
alonzoEqTxRaw :: AlonzoEraTx era => Tx era -> Tx era -> Bool
453471
alonzoEqTxRaw tx1 tx2 =
454472
shelleyEqTxRaw tx1 tx2 && (tx1 ^. isValidTxL == tx2 ^. isValidTxL)

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxAuxData.hs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import Cardano.Ledger.BaseTypes (ProtVer)
5959
import Cardano.Ledger.Binary (
6060
Annotator (..),
6161
DecCBOR (..),
62+
Decoder,
6263
EncCBOR (..),
6364
ToCBOR,
6465
TokenType (..),
@@ -183,16 +184,10 @@ getAlonzoTxAuxDataScripts AlonzoTxAuxData {atadTimelock = timelocks, atadPlutus
183184

184185
instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
185186
decCBOR =
186-
peekTokenType >>= \case
187-
TypeMapLen -> decodeShelley
188-
TypeMapLen64 -> decodeShelley
189-
TypeMapLenIndef -> decodeShelley
190-
TypeListLen -> decodeShelleyMA
191-
TypeListLen64 -> decodeShelleyMA
192-
TypeListLenIndef -> decodeShelleyMA
193-
TypeTag -> decodeAlonzo
194-
TypeTag64 -> decodeAlonzo
195-
_ -> fail "Failed to decode AlonzoTxAuxData"
187+
decodeTxAuxDataByTokenType @(Annotator (AlonzoTxAuxDataRaw era))
188+
decodeShelley
189+
decodeAllegra
190+
decodeAlonzo
196191
where
197192
decodeShelley =
198193
decode
@@ -201,7 +196,7 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
201196
<*! Ann (Emit StrictSeq.empty)
202197
<*! Ann (Emit Map.empty)
203198
)
204-
decodeShelleyMA =
199+
decodeAllegra =
205200
decode
206201
( Ann (RecD AlonzoTxAuxDataRaw)
207202
<*! Ann From
@@ -214,13 +209,6 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
214209
TagD 259 $
215210
SparseKeyed "AlonzoTxAuxData" (pure emptyAuxData) auxDataField []
216211

217-
addPlutusScripts lang scripts ad =
218-
case NE.nonEmpty scripts of
219-
Nothing -> ad
220-
Just neScripts ->
221-
-- Avoid leaks by deepseq, since non empty list is lazy.
222-
neScripts `deepseq` ad {atadrPlutus = Map.insert lang neScripts $ atadrPlutus ad}
223-
224212
auxDataField :: Word -> Field (Annotator (AlonzoTxAuxDataRaw era))
225213
auxDataField 0 = fieldA (\x ad -> ad {atadrMetadata = x}) From
226214
auxDataField 1 =
@@ -232,6 +220,53 @@ instance Era era => DecCBOR (Annotator (AlonzoTxAuxDataRaw era)) where
232220
auxDataField 4 = fieldA (addPlutusScripts PlutusV3) (D (guardPlutus PlutusV3 >> decCBOR))
233221
auxDataField n = field (\_ t -> t) (Invalid n)
234222

223+
instance Era era => DecCBOR (AlonzoTxAuxDataRaw era) where
224+
decCBOR =
225+
decodeTxAuxDataByTokenType @(AlonzoTxAuxDataRaw era)
226+
decodeShelley
227+
decodeAllegra
228+
decodeAlonzo
229+
where
230+
decodeShelley =
231+
decode
232+
(Emit AlonzoTxAuxDataRaw <! From <! Emit StrictSeq.empty <! Emit Map.empty)
233+
decodeAllegra =
234+
decode
235+
(RecD AlonzoTxAuxDataRaw <! From <! From <! Emit Map.empty)
236+
decodeAlonzo =
237+
decode $
238+
TagD 259 $
239+
SparseKeyed "AlonzoTxAuxData" emptyAuxData auxDataField []
240+
241+
auxDataField :: Word -> Field (AlonzoTxAuxDataRaw era)
242+
auxDataField 0 = field (\x ad -> ad {atadrMetadata = x}) From
243+
auxDataField 1 = field (\x ad -> ad {atadrTimelock = atadrTimelock ad <> x}) From
244+
auxDataField 2 = field (addPlutusScripts PlutusV1) (D (guardPlutus PlutusV1 >> decCBOR))
245+
auxDataField 3 = field (addPlutusScripts PlutusV2) (D (guardPlutus PlutusV2 >> decCBOR))
246+
auxDataField 4 = field (addPlutusScripts PlutusV3) (D (guardPlutus PlutusV3 >> decCBOR))
247+
auxDataField n = field (\_ t -> t) (Invalid n)
248+
249+
decodeTxAuxDataByTokenType :: forall t s. Decoder s t -> Decoder s t -> Decoder s t -> Decoder s t
250+
decodeTxAuxDataByTokenType decodeShelley decodeAllegra decodeAlonzo =
251+
peekTokenType >>= \case
252+
TypeMapLen -> decodeShelley
253+
TypeMapLen64 -> decodeShelley
254+
TypeMapLenIndef -> decodeShelley
255+
TypeListLen -> decodeAllegra
256+
TypeListLen64 -> decodeAllegra
257+
TypeListLenIndef -> decodeAllegra
258+
TypeTag -> decodeAlonzo
259+
TypeTag64 -> decodeAlonzo
260+
_ -> fail "Failed to decode AlonzoTxAuxData"
261+
262+
addPlutusScripts :: Language -> [PlutusBinary] -> AlonzoTxAuxDataRaw era -> AlonzoTxAuxDataRaw era
263+
addPlutusScripts lang scripts ad =
264+
case NE.nonEmpty scripts of
265+
Nothing -> ad
266+
Just neScripts ->
267+
-- Avoid leaks by deepseq, since non empty list is lazy.
268+
neScripts `deepseq` ad {atadrPlutus = Map.insert lang neScripts $ atadrPlutus ad}
269+
235270
emptyAuxData :: AlonzoTxAuxDataRaw era
236271
emptyAuxData = AlonzoTxAuxDataRaw mempty mempty mempty
237272

@@ -240,7 +275,7 @@ emptyAuxData = AlonzoTxAuxDataRaw mempty mempty mempty
240275

241276
newtype AlonzoTxAuxData era = AlonzoTxAuxDataConstr (MemoBytes (AlonzoTxAuxDataRaw era))
242277
deriving (Generic)
243-
deriving newtype (ToCBOR, SafeToHash)
278+
deriving newtype (ToCBOR, SafeToHash, DecCBOR)
244279

245280
instance Memoized (AlonzoTxAuxData era) where
246281
type RawType (AlonzoTxAuxData era) = AlonzoTxAuxDataRaw era

0 commit comments

Comments
 (0)