Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 57 additions & 20 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import Cardano.Ledger.Binary (
decodeMapLenOrIndef,
decodeMapLikeEnforceNoDuplicates,
decodeNonEmptyList,
decodeSetLikeEnforceNoDuplicates,
encodeFoldableEncoder,
encodeListLen,
encodeTag,
Expand Down Expand Up @@ -636,55 +637,91 @@ instance
txWitnessField
[]
where
setDecoder :: (Ord a, DecCBOR a) => Decoder s (Annotator (Set a))
setDecoder =
pure
<$> ifDecoderVersionAtLeast
(natVersion @12)
(decodeSetLikeEnforceNoDuplicates Set.insert (\s -> (length s, s)) decCBOR)
(allowTag setTag >> Set.fromList . NE.toList <$> decodeNonEmptyList decCBOR)
{-# INLINE setDecoder #-}

txWitnessField :: Word -> Field (Annotator (AlonzoTxWitsRaw era))
txWitnessField 0 =
fieldAA
(\x wits -> wits {atwrAddrTxWits = x})
( D $
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> mapTraverseableDecoderA (decodeNonEmptyList decCBOR) (Set.fromList . NE.toList)
)
setDecoder
(mapTraverseableDecoderA (decodeList decCBOR) Set.fromList)
)
txWitnessField 1 =
fieldAA
addScriptsTxWitsRaw
(D nativeScriptsDecoder)
fieldAA addScriptsTxWitsRaw (D scriptsDecoder)
txWitnessField 2 =
fieldAA
(\x wits -> wits {atwrBootAddrTxWits = x})
( D $
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> mapTraverseableDecoderA (decodeNonEmptyList decCBOR) (Set.fromList . NE.toList)
)
setDecoder
(mapTraverseableDecoderA (decodeList decCBOR) Set.fromList)
)
txWitnessField 3 = fieldA addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV1)
txWitnessField 4 =
fieldAA
(\x wits -> wits {atwrDatsTxWits = x})
From
( D $
ifDecoderVersionAtLeast
(natVersion @12)
noDuplicatesDatsDecoder
decCBOR
)
txWitnessField 5 = fieldAA (\x wits -> wits {atwrRdmrsTxWits = x}) From
txWitnessField 6 = fieldA addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV2)
txWitnessField 7 = fieldA addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV3)
txWitnessField n = invalidField n
{-# INLINE txWitnessField #-}

nativeScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
nativeScriptsDecoder =
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> mapTraverseableDecoderA (decodeNonEmptyList pairDecoder) (Map.fromList . NE.toList)
)
(mapTraverseableDecoderA (decodeList pairDecoder) Map.fromList)
pairDecoder :: Decoder s (Annotator (ScriptHash, Script era))
pairDecoder = fmap (asHashedScriptPair @era . fromNativeScript) <$> decCBOR
{-# INLINE pairDecoder #-}

noDuplicatesDatsDecoder :: Decoder s (Annotator (TxDats era))
noDuplicatesDatsDecoder = do
allowTag setTag
dats <- decodeList decCBOR
pure $ TxDats <$> go Map.empty dats
where
pairDecoder :: Decoder s (Annotator (ScriptHash, Script era))
pairDecoder = fmap (asHashedScriptPair . fromNativeScript) <$> decCBOR
go m [] = pure m
go m (x : xs) = do
x' <- x
let dh = hashData x'
if dh `Map.member` m
then fail $ "Duplicate dats found: " <> show dh
else go (Map.insert dh x' m) xs

noDuplicatesScriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
noDuplicatesScriptsDecoder = do
allowTag setTag
scripts <- decodeList $ fmap (fromNativeScript @era) <$> decCBOR
pure $ go Map.empty scripts
where
go m [] = pure m
go m (x : xs) = do
x' <- x
let sh = hashScript x'
if sh `Map.member` m
then fail $ "Duplicate scripts found: " <> show sh
else go (Map.insert sh x' m) xs

scriptsDecoder :: Decoder s (Annotator (Map ScriptHash (Script era)))
scriptsDecoder =
ifDecoderVersionAtLeast
(natVersion @12)
noDuplicatesScriptsDecoder
(allowTag setTag >> mapTraverseableDecoderA (decodeList pairDecoder) Map.fromList)
{-# INLINE scriptsDecoder #-}
{-# INLINE decCBOR #-}

deriving via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Data.MapExtras as Map (fromElems)
import Data.Maybe.Strict (maybeToStrictMaybe)
import qualified Data.Sequence as Seq
import qualified Data.Sequence.Strict as StrictSeq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Lens.Micro
Expand Down Expand Up @@ -152,16 +153,23 @@ instance (AlonzoEraScript era, DecCBOR (NativeScript era)) => DecCBOR (AlonzoTxW
txWitnessField
[]
where
setDecoder :: (Ord a, DecCBOR a) => Decoder s (Set a)
setDecoder =
allowTag setTag
>> ifDecoderVersionAtLeast
(natVersion @12)
(decodeSetLikeEnforceNoDuplicates Set.insert (\s -> (length s, s)) decCBOR)
(Set.fromList . NE.toList <$> decodeNonEmptyList decCBOR)
{-# INLINE setDecoder #-}

txWitnessField :: Word -> Field (AlonzoTxWitsRaw era)
txWitnessField 0 =
field
(\x wits -> wits {atwrAddrTxWits = x})
( D $
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> Set.fromList . NE.toList <$> decodeNonEmptyList decCBOR
)
setDecoder
(Set.fromList <$> decodeList decCBOR)
)
txWitnessField 1 = field addScriptsTxWitsRaw (D nativeScriptsDecoder)
Expand All @@ -171,31 +179,49 @@ instance (AlonzoEraScript era, DecCBOR (NativeScript era)) => DecCBOR (AlonzoTxW
( D $
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> Set.fromList . NE.toList <$> decodeNonEmptyList decCBOR
)
setDecoder
(Set.fromList <$> decodeList decCBOR)
)
txWitnessField 3 = field addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV1)
txWitnessField 4 = field (\x wits -> wits {atwrDatsTxWits = x}) From
txWitnessField 4 =
field
(\x wits -> wits {atwrDatsTxWits = x})
( D $
ifDecoderVersionAtLeast
(natVersion @12)
( TxDats
<$> decodeSetLikeEnforceNoDuplicates
(\x -> Map.insert (hashData x) x)
(\m -> (length m, m))
decCBOR
)
decCBOR
)
txWitnessField 5 = field (\x wits -> wits {atwrRdmrsTxWits = x}) From
txWitnessField 6 = field addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV2)
txWitnessField 7 = field addScriptsTxWitsRaw (decodeAlonzoPlutusScript SPlutusV3)
txWitnessField n = invalidField n
{-# INLINE txWitnessField #-}

pairDecoder :: Decoder s (ScriptHash, Script era)
pairDecoder = asHashedScriptPair @era . fromNativeScript <$> decCBOR
{-# INLINE pairDecoder #-}

mapDecoder :: Decoder s (Map ScriptHash (Script era))
mapDecoder =
allowTag setTag
>> ifDecoderVersionAtLeast
(natVersion @12)
(decodeSetLikeEnforceNoDuplicates (uncurry Map.insert) (\m -> (length m, m)) pairDecoder)
(Map.fromList . NE.toList <$> decodeNonEmptyList pairDecoder)
{-# INLINE mapDecoder #-}

nativeScriptsDecoder :: Decoder s (Map ScriptHash (Script era))
nativeScriptsDecoder =
ifDecoderVersionAtLeast
(natVersion @9)
( allowTag setTag
>> Map.fromList . NE.toList <$> decodeNonEmptyList pairDecoder
)
mapDecoder
(Map.fromList <$> decodeList pairDecoder)
where
pairDecoder :: Decoder s (ScriptHash, Script era)
pairDecoder = asHashedScriptPair @era . fromNativeScript <$> decCBOR
{-# INLINE pairDecoder #-}
{-# INLINE nativeScriptsDecoder #-}

deriving newtype instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
Expand All @@ -10,7 +9,7 @@

module Test.Cardano.Ledger.Conway.Binary.Golden (
expectDecoderResultOn,
expectDecoderFailure,
expectDecoderFailureAnn,
listRedeemersEnc,
goldenListRedeemers,
) where
Expand Down Expand Up @@ -50,14 +49,14 @@ import Test.Cardano.Ledger.Common (
)
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)

expectDecoderFailure ::
expectDecoderFailureAnn ::
forall a.
(ToExpr a, DecCBOR (Annotator a), Typeable a, HasCallStack) =>
Version ->
Enc ->
DecoderError ->
Expectation
expectDecoderFailure version enc expectedErr =
expectDecoderFailureAnn version enc expectedErr =
case result of
Left err -> err `shouldBe` expectedErr
Right x ->
Expand Down
2 changes: 1 addition & 1 deletion eras/dijkstra/impl/cardano-ledger-dijkstra.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ library testlib
cardano-ledger-allegra:{cardano-ledger-allegra, testlib},
cardano-ledger-alonzo:{cardano-ledger-alonzo, testlib},
cardano-ledger-babbage:{cardano-ledger-babbage, testlib},
cardano-ledger-binary,
cardano-ledger-binary:{cardano-ledger-binary, testlib},
cardano-ledger-conway:{cardano-ledger-conway, testlib},
cardano-ledger-core:{cardano-ledger-core, testlib},
cardano-ledger-dijkstra,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion eras/dijkstra/impl/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ main =
txInfoSpec @DijkstraEra SPlutusV3
txInfoSpec @DijkstraEra SPlutusV4
describe "Golden" $ do
Golden.goldenListRedeemersDisallowed @DijkstraEra
Golden.spec @DijkstraEra
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

Expand Down
Loading
Loading