@@ -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
219243deriving newtype instance
0 commit comments