Skip to content

Commit 90e899e

Browse files
committed
Fix CBOR instances for AlonzoPlutusPurpose.
By using quantified constraints and deriving via the fixed CBORGroup instances. Remove all TODOs pertaining to issue #4110 from related arbitrary instances. Fix tests by removing the exceptional handling of previous eras for roundtripping.
1 parent d71d792 commit 90e899e

File tree

6 files changed

+72
-101
lines changed

6 files changed

+72
-101
lines changed

eras/alonzo/impl/CHANGELOG.md

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

33
## 1.14.0.0
44

5+
* Fix `AlonzoPlutusPurpose` CBOR(Group) instances. #5135
6+
* Reset shuffled serialization indexes for `AlonzoPlutusPurpose AsItem`.
57
* Deprecated `toAlonzoTransitionConfigPairs`
68
* Fixed `FromJSON` instance for `TransitionConfig AlonzoEra`
79
* Added `COMPLETE` pragma for `TxCert AlonzoEra`

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import Cardano.Ledger.Alonzo.TxCert ()
6666
import Cardano.Ledger.BaseTypes (ProtVer (..), kindObject)
6767
import Cardano.Ledger.Binary (
6868
Annotator,
69+
CBORGroup (..),
6970
DecCBOR (decCBOR),
7071
DecCBORGroup (..),
7172
Decoder,
@@ -83,7 +84,6 @@ import Cardano.Ledger.Binary.Coders (
8384
decode,
8485
encode,
8586
(!>),
86-
(<!),
8787
(<*!),
8888
)
8989
import Cardano.Ledger.Binary.Plain (serializeAsHexText)
@@ -323,36 +323,62 @@ instance
323323
AlonzoCertifying x -> rnf x
324324
AlonzoRewarding x -> rnf x
325325

326-
instance Era era => EncCBORGroup (AlonzoPlutusPurpose AsIx era) where
326+
instance
327+
( forall a b. (EncCBOR a, EncCBOR b) => EncCBOR (f a b)
328+
, Era era
329+
, Typeable f
330+
, EncCBOR (TxCert era)
331+
) =>
332+
EncCBORGroup (AlonzoPlutusPurpose f era)
333+
where
327334
listLen _ = 2
328335
listLenBound _ = 2
329336
encCBORGroup = \case
330-
AlonzoSpending (AsIx redeemerIx) -> encodeWord8 0 <> encCBOR redeemerIx
331-
AlonzoMinting (AsIx redeemerIx) -> encodeWord8 1 <> encCBOR redeemerIx
332-
AlonzoCertifying (AsIx redeemerIx) -> encodeWord8 2 <> encCBOR redeemerIx
333-
AlonzoRewarding (AsIx redeemerIx) -> encodeWord8 3 <> encCBOR redeemerIx
337+
AlonzoSpending p -> encodeWord8 0 <> encCBOR p
338+
AlonzoMinting p -> encodeWord8 1 <> encCBOR p
339+
AlonzoCertifying p -> encodeWord8 2 <> encCBOR p
340+
AlonzoRewarding p -> encodeWord8 3 <> encCBOR p
334341
encodedGroupSizeExpr size_ _proxy =
335342
encodedSizeExpr size_ (Proxy :: Proxy Word8)
336343
+ encodedSizeExpr size_ (Proxy :: Proxy Word16)
337344

338-
instance Era era => DecCBORGroup (AlonzoPlutusPurpose AsIx era) where
345+
instance
346+
( forall a b. (DecCBOR a, DecCBOR b) => DecCBOR (f a b)
347+
, Era era
348+
, Typeable f
349+
, DecCBOR (TxCert era)
350+
) =>
351+
DecCBORGroup (AlonzoPlutusPurpose f era)
352+
where
339353
decCBORGroup =
340354
decodeWord8 >>= \case
341-
0 -> AlonzoSpending . AsIx <$> decCBOR
342-
1 -> AlonzoMinting . AsIx <$> decCBOR
343-
2 -> AlonzoCertifying . AsIx <$> decCBOR
344-
3 -> AlonzoRewarding . AsIx <$> decCBOR
355+
0 -> AlonzoSpending <$> decCBOR
356+
1 -> AlonzoMinting <$> decCBOR
357+
2 -> AlonzoCertifying <$> decCBOR
358+
3 -> AlonzoRewarding <$> decCBOR
345359
n -> fail $ "Unexpected tag for AlonzoPlutusPurpose: " <> show n
346360

347-
-- | Incorrect CBOR implementation. Missing length encoding. Must keep it for backwards
348-
-- compatibility
349-
instance Era era => EncCBOR (AlonzoPlutusPurpose AsIx era) where
350-
encCBOR = encCBORGroup
351-
352-
-- | Incorrect CBOR implementation. Missing length encoding. Must keep it for backwards
353-
-- compatibility
354-
instance Era era => DecCBOR (AlonzoPlutusPurpose AsIx era) where
355-
decCBOR = decCBORGroup
361+
deriving via
362+
(CBORGroup (AlonzoPlutusPurpose f era))
363+
instance
364+
( forall a b. (EncCBOR a, EncCBOR b) => EncCBOR (f a b)
365+
, Era era
366+
, Typeable f
367+
, EncCBOR (TxCert era)
368+
) =>
369+
EncCBOR (AlonzoPlutusPurpose f era)
370+
371+
deriving via
372+
(CBORGroup (AlonzoPlutusPurpose f era))
373+
instance
374+
( forall a b. (EncCBOR a, EncCBOR b) => EncCBOR (f a b)
375+
, forall a b. (DecCBOR a, DecCBOR b) => DecCBOR (f a b)
376+
, Era era
377+
, Typeable f
378+
, EncCBOR (TxCert era)
379+
, DecCBOR (TxCert era)
380+
) =>
381+
DecCBOR (AlonzoPlutusPurpose f era)
356382

357383
instance
358384
( forall a b. (ToJSON a, ToJSON b) => ToJSON (f a b)
@@ -369,29 +395,6 @@ instance
369395
where
370396
kindObjectWithValue name n = kindObject name ["value" .= n]
371397

372-
-- | /Note/ - serialization of `AlonzoPlutusPurpose` `AsItem`
373-
--
374-
-- * Tags do not match the `AlonzoPlutusPurpose` `AsIx`. Unfortunate inconsistency
375-
--
376-
-- * It is only used for predicate failures. Thus we can change it after Conway to be
377-
-- consistent with `AlonzoPlutusPurpose` `AsIx`
378-
instance (Era era, EncCBOR (TxCert era)) => EncCBOR (AlonzoPlutusPurpose AsItem era) where
379-
encCBOR = \case
380-
AlonzoSpending (AsItem x) -> encode (Sum (AlonzoSpending @_ @era . AsItem) 1 !> To x)
381-
AlonzoMinting (AsItem x) -> encode (Sum (AlonzoMinting @_ @era . AsItem) 0 !> To x)
382-
AlonzoCertifying (AsItem x) -> encode (Sum (AlonzoCertifying . AsItem) 3 !> To x)
383-
AlonzoRewarding (AsItem x) -> encode (Sum (AlonzoRewarding @_ @era . AsItem) 2 !> To x)
384-
385-
-- | See note on the `EncCBOR` instace.
386-
instance (Era era, DecCBOR (TxCert era)) => DecCBOR (AlonzoPlutusPurpose AsItem era) where
387-
decCBOR = decode (Summands "AlonzoPlutusPurpose" dec)
388-
where
389-
dec 1 = SumD (AlonzoSpending . AsItem) <! From
390-
dec 0 = SumD (AlonzoMinting . AsItem) <! From
391-
dec 3 = SumD (AlonzoCertifying . AsItem) <! From
392-
dec 2 = SumD (AlonzoRewarding . AsItem) <! From
393-
dec n = Invalid n
394-
395398
pattern SpendingPurpose ::
396399
AlonzoEraScript era => f Word32 TxIn -> PlutusPurpose f era
397400
pattern SpendingPurpose c <- (toSpendingPurpose -> Just c)

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,7 @@ instance
349349
) =>
350350
Arbitrary (AlonzoUtxowPredFailure era)
351351
where
352-
-- Switch to this implementation once #4110 is taken care of
353-
-- arbitrary = genericArbitraryU
354-
arbitrary =
355-
oneof
356-
[ ShelleyInAlonzoUtxowPredFailure <$> arbitrary
357-
, -- MissingRedeemers <$> arbitrary -- see #4110
358-
MissingRequiredDatums <$> arbitrary <*> arbitrary
359-
, NotAllowedSupplementalDatums <$> arbitrary <*> arbitrary
360-
, PPViewHashesDontMatch <$> arbitrary
361-
, UnspendableUTxONoDatumHash <$> arbitrary
362-
-- , ExtraRedeemers <$> arbitrary -- see #4110
363-
]
352+
arbitrary = genericArbitraryU
364353

365354
deriving instance Arbitrary ix => Arbitrary (AsIx ix it)
366355

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxowSpec/Invalid.hs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Cardano.Ledger.Alonzo.Rules (
1919
)
2020
import Cardano.Ledger.Alonzo.Scripts (eraLanguages)
2121
import Cardano.Ledger.Alonzo.TxWits (TxDats (..), unRedeemersL)
22-
import Cardano.Ledger.BaseTypes (Mismatch (..), StrictMaybe (..), natVersion)
22+
import Cardano.Ledger.BaseTypes (Mismatch (..), StrictMaybe (..))
2323
import Cardano.Ledger.Coin (Coin (..))
2424
import Cardano.Ledger.Credential (Credential (..), StakeReference (..))
2525
import Cardano.Ledger.Keys (asWitness, witVKeyHash)
@@ -67,11 +67,6 @@ spec = describe "Invalid transactions" $ do
6767

6868
let resetAddrWits tx = updateAddrTxWits $ tx & witsTxL . addrTxWitsL .~ []
6969
fixupResetAddrWits = fixupPPHash >=> resetAddrWits
70-
-- PlutusPurpose serialization wasn't fixed until Conway
71-
withPlutusPurposeRoundTripFailures =
72-
if eraProtVerLow @era < natVersion @9
73-
then withCborRoundTripFailures
74-
else id
7570

7671
forM_ (eraLanguages @era) $ \lang ->
7772
withSLanguage lang $ \slang ->
@@ -205,16 +200,15 @@ spec = describe "Invalid transactions" $ do
205200
removeSpenders = Map.filterWithKey (const . not . isSpender)
206201
dropSpendingRedeemers = pure . (witsTxL . rdmrsTxWitsL . unRedeemersL %~ removeSpenders)
207202
withPostFixup (dropSpendingRedeemers >=> fixupPPHash >=> resetAddrWits) $
208-
withPlutusPurposeRoundTripFailures $
209-
submitFailingTx
210-
tx
211-
[ injectFailure $
212-
ExtraRedeemers [mkMintingPurpose $ AsIx 0]
213-
, injectFailure $
214-
MissingRedeemers [(mkSpendingPurpose $ AsItem txIn, scriptHash)]
215-
, injectFailure $
216-
CollectErrors [NoRedeemer $ mkSpendingPurpose $ AsItem txIn]
217-
]
203+
submitFailingTx
204+
tx
205+
[ injectFailure $
206+
ExtraRedeemers [mkMintingPurpose $ AsIx 0]
207+
, injectFailure $
208+
MissingRedeemers [(mkSpendingPurpose $ AsItem txIn, scriptHash)]
209+
, injectFailure $
210+
CollectErrors [NoRedeemer $ mkSpendingPurpose $ AsItem txIn]
211+
]
218212

219213
it "Missing witness for collateral input" $ do
220214
let scriptHash = alwaysSucceedsWithDatumHash
@@ -249,10 +243,9 @@ spec = describe "Invalid transactions" $ do
249243
let fixedRedeemers = txFixed ^. witsTxL . rdmrsTxWitsL . unRedeemersL
250244
extraRedeemers = Map.keys $ Map.filter (== redeemer) fixedRedeemers
251245
withNoFixup $
252-
withPlutusPurposeRoundTripFailures $
253-
submitFailingTx
254-
txFixed
255-
[injectFailure $ ExtraRedeemers extraRedeemers]
246+
submitFailingTx
247+
txFixed
248+
[injectFailure $ ExtraRedeemers extraRedeemers]
256249

257250
it "Minting" $
258251
testPurpose (mkMintingPurpose $ AsIx 2)
@@ -277,7 +270,6 @@ spec = describe "Invalid transactions" $ do
277270
mkBasicTx mkBasicTxBody
278271
& bodyTxL . certsTxBodyL <>~ certs
279272
& witsTxL . rdmrsTxWitsL . unRedeemersL <>~ redeemers
280-
withPlutusPurposeRoundTripFailures $
281-
submitFailingTx
282-
tx
283-
[injectFailure $ ExtraRedeemers [mkCertifyingPurpose (AsIx 2)]]
273+
submitFailingTx
274+
tx
275+
[injectFailure $ ExtraRedeemers [mkCertifyingPurpose (AsIx 2)]]

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/Arbitrary.hs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,7 @@ instance
9090
) =>
9191
Arbitrary (BabbageContextError era)
9292
where
93-
-- Switch to this implementation once #4110 is taken care of
94-
-- arbitrary = genericArbitraryU
95-
arbitrary =
96-
oneof
97-
[ AlonzoContextError <$> arbitrary
98-
, ByronTxOutInContext <$> arbitrary
99-
, -- , RedeemerPointerPointsToNothing <$> arbitrary -- see #4110
100-
InlineDatumsNotSupported <$> arbitrary
101-
, ReferenceScriptsNotSupported <$> arbitrary
102-
, ReferenceInputsNotSupported <$> arbitrary
103-
]
93+
arbitrary = genericArbitraryU
10494

10595
instance
10696
( EraTxOut era

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/Imp/UtxowSpec.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,12 @@ spec = describe "UTXOW" $ do
8181
mkBasicTx mkBasicTxBody
8282
& bodyTxL . inputsTxBodyL .~ [txIn]
8383
& witsTxL . rdmrsTxWitsL . unRedeemersL %~ Map.insert prp (dt, ExUnits 0 0)
84-
let submit =
85-
submitFailingTx
86-
tx
87-
[ injectFailure $ ExtraRedeemers [prp]
88-
, injectFailure $
89-
CollectErrors [BadTranslation (inject $ RedeemerPointerPointsToNothing prp)]
90-
]
91-
if eraProtVerLow @era < natVersion @9
92-
then -- PlutusPurpose serialization was fixed in Conway
93-
withCborRoundTripFailures submit
94-
else submit
84+
submitFailingTx
85+
tx
86+
[ injectFailure $ ExtraRedeemers [prp]
87+
, injectFailure $
88+
CollectErrors [BadTranslation (inject $ RedeemerPointerPointsToNothing prp)]
89+
]
9590

9691
it "P1 reference scripts must be witnessed" $ do
9792
(_, addr) <- freshKeyAddr

0 commit comments

Comments
 (0)