Skip to content

Commit 184d8d2

Browse files
committed
Allow empty lists pre PV9 again
1 parent 1c0334d commit 184d8d2

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,7 @@ instance
622622
s <- decodeSetLikeEnforceNoDuplicates Set.insert (\s -> (length s, s)) decCBOR
623623
when (Set.null s) $ fail "Set cannot be empty"
624624
pure s
625-
ifDecoderVersionAtLeast
626-
(natVersion @9)
627-
(ifDecoderVersionAtLeast (natVersion @12) nonEmptyNoDuplicatesDecoder nonEmptyDecoder)
628-
(Set.fromList <$> decodeList decCBOR)
625+
ifDecoderVersionAtLeast (natVersion @12) nonEmptyNoDuplicatesDecoder nonEmptyDecoder
629626
{-# INLINE addrWitsSetDecoder #-}
630627

631628
txWitnessField :: Word -> Field (Annotator (AlonzoTxWitsRaw era))
@@ -638,8 +635,7 @@ instance
638635
addrWitsSetDecoder
639636
(mapTraverseableDecoderA (decodeList decCBOR) Set.fromList)
640637
)
641-
txWitnessField 1 =
642-
fieldAA addScriptsTxWitsRaw (D nativeScriptsDecoder)
638+
txWitnessField 1 = fieldAA addScriptsTxWitsRaw (D nativeScriptsDecoder)
643639
txWitnessField 2 =
644640
fieldAA
645641
(\x wits -> wits {atwrBootAddrTxWits = x})
@@ -662,20 +658,25 @@ instance
662658
{-# INLINE hashedNativeSciptDecoder #-}
663659

664660
noDuplicateNativeScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
665-
noDuplicateNativeScriptsDecoder =
666-
noDuplicateNonEmptySetAsMapDecoderAnn
667-
(\ns -> (hashScript $ fromNativeScript ns, fromNativeScript ns))
661+
noDuplicateNativeScriptsDecoder = noDuplicateNonEmptySetAsMapDecoderAnn (asHashedScriptPair . fromNativeScript)
668662
{-# INLINE noDuplicateNativeScriptsDecoder #-}
669663

670664
nativeScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
671665
nativeScriptsDecoder =
672666
ifDecoderVersionAtLeast
673-
(natVersion @12)
674-
noDuplicateNativeScriptsDecoder
675-
( do
676-
allowTag setTag
677-
mapTraverseableDecoderA (decodeNonEmptyList hashedNativeSciptDecoder) (Map.fromList . NE.toList)
667+
(natVersion @9)
668+
( ifDecoderVersionAtLeast
669+
(natVersion @12)
670+
noDuplicateNativeScriptsDecoder
671+
( do
672+
allowTag setTag
673+
mapTraverseableDecoderA (decodeNonEmptyList hashedNativeSciptDecoder) (Map.fromList . NE.toList)
674+
)
678675
)
676+
(mapTraverseableDecoderA (decodeList pairDecoder) Map.fromList)
677+
where
678+
pairDecoder :: Decoder s (Annotator (ScriptHash, Script era))
679+
pairDecoder = fmap (asHashedScriptPair . fromNativeScript) <$> decCBOR
679680
{-# INLINE nativeScriptsDecoder #-}
680681
{-# INLINE decCBOR #-}
681682

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary/Annotator.hs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,31 @@ instance (AlonzoEraScript era, DecCBOR (NativeScript era)) => DecCBOR (AlonzoTxW
167167
(Set.fromList <$> decodeList decCBOR)
168168
{-# INLINE setDecoder #-}
169169

170+
addrWitsSetDecoder :: (Ord a, DecCBOR a) => Decoder s (Set a)
171+
addrWitsSetDecoder =
172+
do
173+
let
174+
nonEmptyDecoder = do
175+
allowTag setTag
176+
Set.fromList . NE.toList <$> decodeNonEmptyList decCBOR
177+
nonEmptyNoDuplicatesDecoder = do
178+
s <- decodeSetLikeEnforceNoDuplicates Set.insert (\s -> (length s, s)) decCBOR
179+
when (Set.null s) $ fail "Set cannot be empty"
180+
pure s
181+
ifDecoderVersionAtLeast
182+
(natVersion @9)
183+
(ifDecoderVersionAtLeast (natVersion @12) nonEmptyNoDuplicatesDecoder nonEmptyDecoder)
184+
(Set.fromList <$> decodeList decCBOR)
185+
{-# INLINE addrWitsSetDecoder #-}
186+
170187
txWitnessField :: Word -> Field (AlonzoTxWitsRaw era)
171188
txWitnessField 0 =
172189
field
173190
(\x wits -> wits {atwrAddrTxWits = x})
174191
( D $
175192
ifDecoderVersionAtLeast
176193
(natVersion @9)
177-
setDecoder
194+
addrWitsSetDecoder
178195
(Set.fromList <$> decodeList decCBOR)
179196
)
180197
txWitnessField 1 = field addScriptsTxWitsRaw (D nativeScriptsDecoder)
@@ -195,25 +212,32 @@ instance (AlonzoEraScript era, DecCBOR (NativeScript era)) => DecCBOR (AlonzoTxW
195212
txWitnessField n = invalidField n
196213
{-# INLINE txWitnessField #-}
197214

198-
pairDecoder :: Decoder s (ScriptHash, Script era)
199-
pairDecoder = asHashedScriptPair @era . fromNativeScript <$> decCBOR
200-
{-# INLINE pairDecoder #-}
215+
noDuplicateNativeScriptsDecoder :: Decoder s (Map ScriptHash (Script era))
216+
noDuplicateNativeScriptsDecoder =
217+
noDuplicateNonEmptySetAsMapDecoder
218+
(\ns -> (hashScript $ fromNativeScript ns, fromNativeScript ns))
219+
{-# INLINE noDuplicateNativeScriptsDecoder #-}
201220

202-
mapDecoder :: Decoder s (Map ScriptHash (Script era))
203-
mapDecoder =
204-
allowTag setTag
205-
>> ifDecoderVersionAtLeast
206-
(natVersion @12)
207-
(decodeSetLikeEnforceNoDuplicates (uncurry Map.insert) (\m -> (length m, m)) pairDecoder)
208-
(Map.fromList . NE.toList <$> decodeNonEmptyList pairDecoder)
209-
{-# INLINE mapDecoder #-}
221+
hashedNativeSciptDecoder :: Decoder s (ScriptHash, Script era)
222+
hashedNativeSciptDecoder = (\script -> (hashScript script, script)) . fromNativeScript @era <$> decCBOR
223+
{-# INLINE hashedNativeSciptDecoder #-}
210224

211225
nativeScriptsDecoder :: Decoder s (Map ScriptHash (Script era))
212226
nativeScriptsDecoder =
213227
ifDecoderVersionAtLeast
214228
(natVersion @9)
215-
mapDecoder
229+
( ifDecoderVersionAtLeast
230+
(natVersion @12)
231+
noDuplicateNativeScriptsDecoder
232+
( do
233+
allowTag setTag
234+
Map.fromList . NE.toList <$> decodeNonEmptyList hashedNativeSciptDecoder
235+
)
236+
)
216237
(Map.fromList <$> decodeList pairDecoder)
238+
where
239+
pairDecoder :: Decoder s (ScriptHash, Script era)
240+
pairDecoder = asHashedScriptPair . fromNativeScript <$> decCBOR
217241
{-# INLINE nativeScriptsDecoder #-}
218242

219243
deriving newtype instance

0 commit comments

Comments
 (0)