Skip to content
Merged
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
6 changes: 5 additions & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,11 @@ instance AlonzoEraScript era => DecCBOR (Annotator (RedeemersRaw era)) where
( peekTokenType >>= \case
TypeMapLenIndef -> decodeMapRedeemers
TypeMapLen -> decodeMapRedeemers
_ -> decodeListRedeemers
_ ->
ifDecoderVersionAtLeast
(natVersion @12)
(fail "List encoding of redeemers not supported starting with PV 12")
decodeListRedeemers
)
( mapTraverseableDecoderA
(decodeList decodeAnnElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ instance AlonzoEraScript era => DecCBOR (RedeemersRaw era) where
( peekTokenType >>= \case
TypeMapLenIndef -> decodeMapRedeemers
TypeMapLen -> decodeMapRedeemers
_ -> decodeListRedeemers
_ ->
ifDecoderVersionAtLeast
(natVersion @12)
(fail "List encoding of redeemers not supported starting with PV 12")
decodeListRedeemers
)
(RedeemersRaw . Map.fromList <$> decodeList decodeElement)
where
Expand Down
1 change: 1 addition & 0 deletions eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ library testlib
Test.Cardano.Ledger.Conway.Arbitrary
Test.Cardano.Ledger.Conway.Binary.Annotator
Test.Cardano.Ledger.Conway.Binary.Cddl
Test.Cardano.Ledger.Conway.Binary.Golden
Test.Cardano.Ledger.Conway.Binary.Regression
Test.Cardano.Ledger.Conway.Binary.RoundTrip
Test.Cardano.Ledger.Conway.BinarySpec
Expand Down
103 changes: 103 additions & 0 deletions eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary/Golden.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-unused-foralls #-}

module Test.Cardano.Ledger.Conway.Binary.Golden (
expectDecoderResultOn,
expectDecoderFailure,
listRedeemersEnc,
goldenListRedeemers,
) where

import Cardano.Ledger.Alonzo.Core (
AsIx (..),
eraProtVerLow,
pattern SpendingPurpose,
)
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..))
import Cardano.Ledger.Alonzo.TxWits (Redeemers (..), unRedeemers)
import Cardano.Ledger.BaseTypes (Version)
import Cardano.Ledger.Binary (
Annotator (..),
DecCBOR (..),
ToCBOR (..),
decodeFullAnnotator,
toLazyByteString,
)
import Cardano.Ledger.Binary.Plain (DecoderError (..), Tokens (..))
import Cardano.Ledger.Plutus (Data (..))
import qualified Data.Map as Map
import Data.Typeable (Proxy (..), Typeable)
import PlutusLedgerApi.Common (Data (..))
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc (..))
import Test.Cardano.Ledger.Binary.RoundTrip (embedTripAnnExpectation)
import Test.Cardano.Ledger.Common (
Expectation,
HasCallStack,
Spec,
ToExpr,
expectationFailure,
it,
shouldBe,
shouldBeExpr,
showExpr,
)
import Test.Cardano.Ledger.Conway.Era (ConwayEraTest)

expectDecoderFailure ::
forall a.
(ToExpr a, DecCBOR (Annotator a), Typeable a, HasCallStack) =>
Version ->
Enc ->
DecoderError ->
Expectation
expectDecoderFailure version enc expectedErr =
case result of
Left err -> err `shouldBe` expectedErr
Right x ->
expectationFailure $
"Expected a failure, but decoder succeeded:\n"
<> showExpr x
where
bytes = toLazyByteString $ toCBOR enc
result = decodeFullAnnotator @a version (label $ Proxy @(Annotator a)) decCBOR bytes

expectDecoderResultOn ::
forall a b.
(ToExpr b, DecCBOR (Annotator a), Eq b, HasCallStack) =>
Version -> Enc -> a -> (a -> b) -> Expectation
expectDecoderResultOn version enc expected f =
embedTripAnnExpectation version version (\x _ -> f x `shouldBeExpr` f expected) enc

-- | A simple redeemer encoded as a list
listRedeemersEnc :: Enc
listRedeemersEnc =
mconcat
[ E (TkListLen 1)
, mconcat
[ E (TkListLen 4)
, E (0 :: Int)
, E (10 :: Int)
, E (20 :: Int)
, mconcat
[ E (TkListLen 2)
, E (30 :: Int)
, E (40 :: Int)
]
]
]

goldenListRedeemers :: forall era. ConwayEraTest era => Spec
goldenListRedeemers =
it "Decoding Redeemers encoded as a list succeeds" $
expectDecoderResultOn @(Redeemers era)
(eraProtVerLow @era)
listRedeemersEnc
(Redeemers $ Map.singleton (SpendingPurpose $ AsIx 10) (Data $ I 20, ExUnits 30 40))
unRedeemers
2 changes: 2 additions & 0 deletions eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import qualified Test.Cardano.Ledger.Alonzo.Binary.CostModelsSpec as CostModelsS
import qualified Test.Cardano.Ledger.Alonzo.Binary.TxWitsSpec as TxWitsSpec
import qualified Test.Cardano.Ledger.Babbage.TxInfoSpec as BabbageTxInfo
import Test.Cardano.Ledger.Common
import qualified Test.Cardano.Ledger.Conway.Binary.Golden as Golden
import qualified Test.Cardano.Ledger.Conway.Binary.Regression as Regression
import qualified Test.Cardano.Ledger.Conway.BinarySpec as Binary
import qualified Test.Cardano.Ledger.Conway.CommitteeRatifySpec as CommitteeRatify
Expand Down Expand Up @@ -60,3 +61,4 @@ spec =
BabbageTxInfo.spec @era
describe "PlutusV3" $
BabbageTxInfo.txInfoSpec @era SPlutusV3
describe "Golden" $ Golden.goldenListRedeemers @era
3 changes: 2 additions & 1 deletion eras/dijkstra/impl/cardano-ledger-dijkstra.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ library testlib
Test.Cardano.Ledger.Dijkstra.Arbitrary
Test.Cardano.Ledger.Dijkstra.Binary.Annotator
Test.Cardano.Ledger.Dijkstra.Binary.Cddl
Test.Cardano.Ledger.Dijkstra.Binary.Golden
Test.Cardano.Ledger.Dijkstra.Binary.RoundTrip
Test.Cardano.Ledger.Dijkstra.CDDL
Test.Cardano.Ledger.Dijkstra.Era
Expand All @@ -137,7 +138,7 @@ library testlib
bytestring,
cardano-data,
cardano-ledger-allegra:{cardano-ledger-allegra, testlib},
cardano-ledger-alonzo:testlib,
cardano-ledger-alonzo:{cardano-ledger-alonzo, testlib},
cardano-ledger-babbage:{cardano-ledger-babbage, testlib},
cardano-ledger-binary,
cardano-ledger-conway:{cardano-ledger-conway, testlib},
Expand Down
3 changes: 3 additions & 0 deletions eras/dijkstra/impl/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import qualified Test.Cardano.Ledger.Babbage.TxInfoSpec as BabbageTxInfo
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Dijkstra.Binary.Annotator ()
import qualified Test.Cardano.Ledger.Dijkstra.Binary.CddlSpec as Cddl
import qualified Test.Cardano.Ledger.Dijkstra.Binary.Golden as Golden
import Test.Cardano.Ledger.Dijkstra.Binary.RoundTrip ()
import qualified Test.Cardano.Ledger.Dijkstra.GoldenSpec as GoldenSpec
import qualified Test.Cardano.Ledger.Dijkstra.Imp as Imp
Expand All @@ -29,3 +30,5 @@ main =
BabbageTxInfo.spec @DijkstraEra
txInfoSpec @DijkstraEra SPlutusV3
txInfoSpec @DijkstraEra SPlutusV4
describe "Golden" $ do
Golden.goldenListRedeemersDisallowed @DijkstraEra
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Dijkstra.Binary.Golden (
goldenListRedeemersDisallowed,
) where

import Cardano.Ledger.Alonzo.TxWits (Redeemers)
import Cardano.Ledger.Binary (DecoderError (..), DeserialiseFailure (..))
import Cardano.Ledger.Dijkstra.Core (eraProtVerLow)
import Test.Cardano.Ledger.Common (Spec, it)
import Test.Cardano.Ledger.Conway.Binary.Golden (expectDecoderFailure, listRedeemersEnc)
import Test.Cardano.Ledger.Dijkstra.Era (DijkstraEraTest)

goldenListRedeemersDisallowed :: forall era. DijkstraEraTest era => Spec
goldenListRedeemersDisallowed =
it "Decoding Redeemers encoded as a list fails" $
expectDecoderFailure @(Redeemers era)
(eraProtVerLow @era)
listRedeemersEnc
( DecoderErrorDeserialiseFailure
"Annotator (MemoBytes (RedeemersRaw DijkstraEra))"
(DeserialiseFailure 0 "List encoding of redeemers not supported starting with PV 12")
)
2 changes: 2 additions & 0 deletions libs/cardano-ledger-binary/src/Cardano/Ledger/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Cardano.Ledger.Binary (
Term (..),
C.DeserialiseFailure (..),
translateViaCBORAnnotator,
toLazyByteString,
) where

import Cardano.Ledger.Binary.Decoding
Expand Down Expand Up @@ -40,6 +41,7 @@ import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Binary.Version
import qualified Codec.CBOR.Read as C (DeserialiseFailure (..))
import Codec.CBOR.Term (Term (..))
import Codec.CBOR.Write (toLazyByteString)
import Control.Monad.Except (Except, MonadError (throwError))
import Data.Text (Text)

Expand Down