Skip to content

Commit 26cb69a

Browse files
committed
Added golden tests
1 parent a64f266 commit 26cb69a

File tree

2 files changed

+118
-21
lines changed
  • eras
    • alonzo/impl/src/Cardano/Ledger/Alonzo
    • dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary

2 files changed

+118
-21
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,12 @@ instance
671671
txWitnessField 4 =
672672
fieldAA
673673
(\x wits -> wits {atwrDatsTxWits = x})
674-
From
674+
( D $
675+
ifDecoderVersionAtLeast
676+
(natVersion @12)
677+
noDuplicatesDatsDecoder
678+
decCBOR
679+
)
675680
txWitnessField 5 = fieldAA (\x wits -> wits {atwrRdmrsTxWits = x}) From
676681
txWitnessField 6 = fieldA addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV2)
677682
txWitnessField 7 = fieldA addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV3)
@@ -682,10 +687,24 @@ instance
682687
pairDecoder = fmap (asHashedScriptPair @era . fromNativeScript) <$> decCBOR
683688
{-# INLINE pairDecoder #-}
684689

690+
noDuplicatesDatsDecoder :: Decoder s (Annotator (TxDats era))
691+
noDuplicatesDatsDecoder = do
692+
allowTag setTag
693+
dats <- decodeList decCBOR
694+
pure $ TxDats <$> go Map.empty dats
695+
where
696+
go m [] = pure m
697+
go m (x:xs) = do
698+
x' <- x
699+
let dh = hashData x'
700+
if dh `Map.member` m
701+
then fail $ "Duplicate dats found: " <> show dh
702+
else go (Map.insert dh x' m) xs
703+
685704
noDuplicatesScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
686705
noDuplicatesScriptsDecoder = do
687706
allowTag setTag
688-
scripts <- decodeList $ decCBOR @(Annotator (Script era))
707+
scripts <- decodeList $ fmap (fromNativeScript @era) <$> decCBOR
689708
pure $ go Map.empty scripts
690709
where
691710
go m [] = pure m

eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary/Golden.hs

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE PatternSynonyms #-}
56
{-# LANGUAGE RankNTypes #-}
@@ -10,7 +11,8 @@ module Test.Cardano.Ledger.Dijkstra.Binary.Golden (
1011
spec,
1112
) where
1213

13-
import Cardano.Ledger.Allegra.Scripts (TimelockRaw (..))
14+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusTxInfo, SupportedLanguage (..))
15+
import Cardano.Ledger.Alonzo.Scripts (plutusScriptBinary)
1416
import Cardano.Ledger.Alonzo.TxWits (Redeemers)
1517
import Cardano.Ledger.BaseTypes (Version)
1618
import Cardano.Ledger.Binary (DecoderError (..), DeserialiseFailure (..), Tokens (..))
@@ -25,8 +27,10 @@ import Cardano.Ledger.Dijkstra.Core (
2527
eraProtVerLow,
2628
pattern DelegTxCert,
2729
)
30+
import Cardano.Ledger.Plutus (SLanguage (..))
2831
import Cardano.Ledger.TxIn (TxIn (..))
2932
import qualified Data.Set as Set
33+
import Test.Cardano.Ledger.Alonzo.Arbitrary (alwaysSucceedsLang)
3034
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc (..))
3135
import Test.Cardano.Ledger.Common (Spec, describe, it)
3236
import Test.Cardano.Ledger.Conway.Binary.Golden (expectDecoderFailureAnn, listRedeemersEnc)
@@ -41,17 +45,10 @@ spec = describe "Golden" $ do
4145
describe "TxWits" $ do
4246
goldenDuplicateVKeyWitsDisallowed @era
4347
goldenDuplicateNativeScriptsDisallowed @era
44-
45-
goldenListRedeemersDisallowed :: forall era. DijkstraEraTest era => Spec
46-
goldenListRedeemersDisallowed =
47-
it "Decoding Redeemers encoded as a list fails" $
48-
expectDecoderFailureAnn @(Redeemers era)
49-
(eraProtVerLow @era)
50-
listRedeemersEnc
51-
( DecoderErrorDeserialiseFailure
52-
"Annotator (MemoBytes (RedeemersRaw DijkstraEra))"
53-
(DeserialiseFailure 0 "List encoding of redeemers not supported starting with PV 12")
54-
)
48+
goldenDuplicatePlutusScriptsDisallowed @era SPlutusV1
49+
goldenDuplicatePlutusScriptsDisallowed @era SPlutusV2
50+
goldenDuplicatePlutusScriptsDisallowed @era SPlutusV3
51+
goldenDuplicatePlutusDataDisallowed @era
5552

5653
duplicateCertsTx :: forall era. DijkstraEraTest era => Version -> Enc
5754
duplicateCertsTx v =
@@ -88,20 +85,68 @@ witsDuplicateVKeyWits =
8885
where
8986
vkeywit = mkWitnessVKey (mkDummySafeHash 0) (mkKeyPair 0)
9087

91-
witsDuplicateNativeScripts :: forall era. DijkstraEraTest era => Version -> Enc
92-
witsDuplicateNativeScripts v =
88+
witsDuplicateNativeScripts :: Enc
89+
witsDuplicateNativeScripts =
9390
mconcat
9491
[ E $ TkMapLen 1
9592
, E @Int 1
9693
, Em
9794
[ E $ TkTag 258
9895
, E $ TkListLen 2
99-
, Ev v nativeScript
100-
, Ev v nativeScript
96+
, nativeScript
97+
, nativeScript
98+
]
99+
]
100+
where
101+
nativeScript = Em [E $ TkListLen 2, E @Int 1, E $ TkListLen 0]
102+
103+
witsDuplicatePlutus ::
104+
forall era l.
105+
EraPlutusTxInfo l era =>
106+
SLanguage l -> Enc
107+
witsDuplicatePlutus slang =
108+
mconcat
109+
[ E $ TkMapLen 1
110+
, E @Int $ case slang of
111+
SPlutusV1 -> 3
112+
SPlutusV2 -> 6
113+
SPlutusV3 -> 7
114+
l -> error $ "Unsupported plutus version: " <> show l
115+
, Em
116+
[ E $ TkTag 258
117+
, E $ TkListLen 2
118+
, plutus
119+
, plutus
120+
]
121+
]
122+
where
123+
plutus = E . plutusScriptBinary $ alwaysSucceedsLang @era (SupportedLanguage slang) 0
124+
125+
witsDuplicatePlutusData :: Enc
126+
witsDuplicatePlutusData =
127+
mconcat
128+
[ E $ TkMapLen 1
129+
, E @Int 4
130+
, Em
131+
[ E $ TkTag 258
132+
, E $ TkListLen 2
133+
, dat
134+
, dat
101135
]
102136
]
103137
where
104-
nativeScript = TimelockAllOf @era mempty
138+
dat = E @Int 0
139+
140+
goldenListRedeemersDisallowed :: forall era. DijkstraEraTest era => Spec
141+
goldenListRedeemersDisallowed =
142+
it "Decoding Redeemers encoded as a list fails" $
143+
expectDecoderFailureAnn @(Redeemers era)
144+
(eraProtVerLow @era)
145+
listRedeemersEnc
146+
( DecoderErrorDeserialiseFailure
147+
"Annotator (MemoBytes (RedeemersRaw DijkstraEra))"
148+
(DeserialiseFailure 0 "List encoding of redeemers not supported starting with PV 12")
149+
)
105150

106151
goldenDuplicateCertsDisallowed :: forall era. DijkstraEraTest era => Spec
107152
goldenDuplicateCertsDisallowed =
@@ -138,7 +183,40 @@ goldenDuplicateNativeScriptsDisallowed =
138183
it "Decoding a TxWits with duplicate native scripts fails" $
139184
expectDecoderFailureAnn @(TxWits era)
140185
version
141-
(witsDuplicateNativeScripts @era version)
142-
(DecoderErrorCustom "foo" "bar")
186+
witsDuplicateNativeScripts
187+
( DecoderErrorCustom
188+
"Annotator"
189+
"Duplicate scripts found: ScriptHash \"d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\""
190+
)
143191
where
144192
version = eraProtVerLow @era
193+
194+
goldenDuplicatePlutusScriptsDisallowed ::
195+
forall era l.
196+
( DijkstraEraTest era
197+
, EraPlutusTxInfo l era
198+
) =>
199+
SLanguage l -> Spec
200+
goldenDuplicatePlutusScriptsDisallowed slang =
201+
it ("Decoding a TxWits with duplicate " <> show slang <> " scripts fails") $
202+
expectDecoderFailureAnn @(TxWits era)
203+
(eraProtVerLow @era)
204+
(witsDuplicatePlutus @era slang)
205+
( DecoderErrorDeserialiseFailure
206+
"Annotator (MemoBytes (AlonzoTxWitsRaw DijkstraEra))"
207+
( DeserialiseFailure
208+
22
209+
"Final number of elements: 1 does not match the total count that was decoded: 2"
210+
)
211+
)
212+
213+
goldenDuplicatePlutusDataDisallowed :: forall era. DijkstraEraTest era => Spec
214+
goldenDuplicatePlutusDataDisallowed =
215+
it "Decoding a TxWits with duplicate plutus data fails" $
216+
expectDecoderFailureAnn @(TxWits era)
217+
(eraProtVerLow @era)
218+
witsDuplicatePlutusData
219+
( DecoderErrorCustom
220+
"Annotator"
221+
"Duplicate dats found: SafeHash \"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314\""
222+
)

0 commit comments

Comments
 (0)