{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Dijkstra.Binary.RoundTrip (
  roundTripDijkstraCommonSpec,
  roundTripDijkstraEraTypesSpec,
  roundTripDijkstraTxSpec,
) where

import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..), IsPhase2Valid (..))
import Cardano.Ledger.Binary (Tokens (..), decodeFull, serialize')
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Core (
  EraTx (..),
  EraTxBody (mkBasicTxBody),
  SafeToHash (..),
  TxLevel (..),
  eraName,
  eraProtVerHigh,
 )
import Cardano.Ledger.Dijkstra (DijkstraEra)
import Cardano.Ledger.Dijkstra.Block.Endorser (EraEndorserBlock (EbReferences))
import Cardano.Ledger.Hashes (hashAnnotated)
import Cardano.Ledger.MempoolTx.Internal (MempoolTx (..), unwrapMempoolTx)
import Data.Either (isLeft)
import Lens.Micro ((&), (.~), (^.))
import Test.Cardano.Ledger.Binary.Golden (decodeEnc)
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc (..))
import Test.Cardano.Ledger.Binary.RoundTrip (embedTripAnn)
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Conway.Binary.RoundTrip (roundTripConwayCommonSpec)
import Test.Cardano.Ledger.Core.Binary.RoundTrip (
  roundTripAnnEraExpectation,
  roundTripAnnEraSpec,
  roundTripEraExpectation,
  roundTripEraSpec,
 )
import Test.Cardano.Ledger.Dijkstra.Arbitrary ()
import Test.Cardano.Ledger.Dijkstra.Binary.Annotator ()
import Test.Cardano.Ledger.Dijkstra.Era (DijkstraEraTest)

roundTripDijkstraCommonSpec :: forall era. DijkstraEraTest era => Spec
roundTripDijkstraCommonSpec :: forall era. DijkstraEraTest era => Spec
roundTripDijkstraCommonSpec = do
  forall era. DijkstraEraTest era => Spec
roundTripDijkstraEraTypesSpec @era
  forall era. ConwayEraTest era => Spec
roundTripConwayCommonSpec @era

roundTripDijkstraEraTypesSpec :: forall era. DijkstraEraTest era => Spec
roundTripDijkstraEraTypesSpec :: forall era. DijkstraEraTest era => Spec
roundTripDijkstraEraTypesSpec =
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe (forall era. Era era => String
eraName @era String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" Endorser Block Types") (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    forall era t.
(Era era, Show t, Eq t, EncCBOR t, DecCBOR t, Arbitrary t,
 HasCallStack) =>
Spec
roundTripEraSpec @era @(EbReferences era)
    forall era t.
(Era era, Show t, Eq t, ToCBOR t, DecCBOR (Annotator t),
 Arbitrary t, HasCallStack) =>
Spec
roundTripAnnEraSpec @era @(EbReferences era)

-- | Round-trip tests for the two serializations of a top-level transaction: the block format
-- of `Tx`, which carries the `IsPhase2Valid` flag, and the mempool format of `MempoolTx`.
roundTripDijkstraTxSpec :: Spec
roundTripDijkstraTxSpec :: Spec
roundTripDijkstraTxSpec =
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"Tx TopTx" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    String -> (Tx TopTx DijkstraEra -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Block format with any IsPhase2Valid flag" ((Tx TopTx DijkstraEra -> Property) -> Spec)
-> (Tx TopTx DijkstraEra -> Property) -> Spec
forall a b. (a -> b) -> a -> b
$
      \(Tx TopTx DijkstraEra
tx :: Tx TopTx DijkstraEra) ->
        [Expectation] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
          [ forall era t.
(Era era, Show t, Eq t, EncCBOR t, DecCBOR t, HasCallStack) =>
t -> Expectation
roundTripEraExpectation @DijkstraEra Tx TopTx DijkstraEra
tx
          , forall era t.
(Era era, Show t, Eq t, ToCBOR t, DecCBOR (Annotator t),
 HasCallStack) =>
t -> Expectation
roundTripAnnEraExpectation @DijkstraEra Tx TopTx DijkstraEra
tx
          ]
    String -> (MempoolTx DijkstraEra -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Mempool round trip preserves the reconstructed canonical block hash" ((MempoolTx DijkstraEra -> Expectation) -> Spec)
-> (MempoolTx DijkstraEra -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$
      \(MempoolTx DijkstraEra
mempoolTx :: MempoolTx DijkstraEra) ->
        case Version
-> Version
-> MempoolTx DijkstraEra
-> Either RoundTripFailure (MempoolTx DijkstraEra)
forall a b.
(ToCBOR a, DecCBOR (Annotator b)) =>
Version -> Version -> a -> Either RoundTripFailure b
embedTripAnn (forall era. Era era => Version
eraProtVerHigh @DijkstraEra) (forall era. Era era => Version
eraProtVerHigh @DijkstraEra) MempoolTx DijkstraEra
mempoolTx of
          Left RoundTripFailure
err -> HasCallStack => String -> Expectation
String -> Expectation
expectationFailure (String -> Expectation) -> String -> Expectation
forall a b. (a -> b) -> a -> b
$ RoundTripFailure -> String
forall a. Show a => a -> String
show RoundTripFailure
err
          Right (MempoolTx DijkstraEra
decodedMempoolTx :: MempoolTx DijkstraEra) -> do
            let decodedTx :: Tx TopTx DijkstraEra
decodedTx = MempoolTx DijkstraEra -> Tx TopTx DijkstraEra
forall era. MempoolTx era -> Tx TopTx era
unwrapMempoolTx MempoolTx DijkstraEra
decodedMempoolTx
            Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated Tx TopTx DijkstraEra
decodedTx SafeHash EraIndependentTx
-> SafeHash EraIndependentTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated (MempoolTx DijkstraEra -> Tx TopTx DijkstraEra
forall era. MempoolTx era -> Tx TopTx era
unwrapMempoolTx MempoolTx DijkstraEra
mempoolTx)
            forall era t.
(Era era, Show t, Eq t, ToCBOR t, DecCBOR (Annotator t),
 HasCallStack) =>
t -> Expectation
roundTripAnnEraExpectation @DijkstraEra Tx TopTx DijkstraEra
decodedTx
    String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"Wire formats" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
      forall era. (Era era, HasCallStack) => (Version -> Spec) -> Spec
forEachEraVersion @DijkstraEra ((Version -> Spec) -> Spec) -> (Version -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \Version
version -> do
        let tx :: Tx TopTx DijkstraEra
tx = forall era (l :: TxLevel). EraTx era => TxBody l era -> Tx l era
mkBasicTx @DijkstraEra @TopTx (forall era (l :: TxLevel).
(EraTxBody era, Typeable l) =>
TxBody l era
mkBasicTxBody @DijkstraEra @TopTx)
            authorFields :: Enc
authorFields =
              [Enc] -> Enc
forall a. Monoid a => [a] -> a
mconcat
                [ Version -> TxBody TopTx DijkstraEra -> Enc
forall a. EncCBOR a => Version -> a -> Enc
Ev Version
version (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (TxBody TopTx DijkstraEra)
     (Tx TopTx DijkstraEra)
     (TxBody TopTx DijkstraEra)
-> TxBody TopTx DijkstraEra
forall s a. s -> Getting a s a -> a
^. Getting
  (TxBody TopTx DijkstraEra)
  (Tx TopTx DijkstraEra)
  (TxBody TopTx DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxBody l DijkstraEra)
bodyTxL)
                , Version -> AlonzoTxWits DijkstraEra -> Enc
forall a. EncCBOR a => Version -> a -> Enc
Ev Version
version (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (AlonzoTxWits DijkstraEra)
     (Tx TopTx DijkstraEra)
     (AlonzoTxWits DijkstraEra)
-> AlonzoTxWits DijkstraEra
forall s a. s -> Getting a s a -> a
^. (TxWits DijkstraEra
 -> Const (AlonzoTxWits DijkstraEra) (TxWits DijkstraEra))
-> Tx TopTx DijkstraEra
-> Const (AlonzoTxWits DijkstraEra) (Tx TopTx DijkstraEra)
Getting
  (AlonzoTxWits DijkstraEra)
  (Tx TopTx DijkstraEra)
  (AlonzoTxWits DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxWits era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxWits DijkstraEra)
witsTxL)
                , (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkNull
                ]
            mempoolEnc :: Enc
mempoolEnc = (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Word -> Tokens -> Tokens
TkListLen Word
3) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
authorFields
            legacyMempoolEnc :: Bool -> Enc
legacyMempoolEnc Bool
isValid =
              [Enc] -> Enc
forall a. Monoid a => [a] -> a
mconcat
                [ (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Word -> Tokens -> Tokens
TkListLen Word
4)
                , Version -> TxBody TopTx DijkstraEra -> Enc
forall a. EncCBOR a => Version -> a -> Enc
Ev Version
version (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (TxBody TopTx DijkstraEra)
     (Tx TopTx DijkstraEra)
     (TxBody TopTx DijkstraEra)
-> TxBody TopTx DijkstraEra
forall s a. s -> Getting a s a -> a
^. Getting
  (TxBody TopTx DijkstraEra)
  (Tx TopTx DijkstraEra)
  (TxBody TopTx DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxBody l DijkstraEra)
bodyTxL)
                , Version -> AlonzoTxWits DijkstraEra -> Enc
forall a. EncCBOR a => Version -> a -> Enc
Ev Version
version (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (AlonzoTxWits DijkstraEra)
     (Tx TopTx DijkstraEra)
     (AlonzoTxWits DijkstraEra)
-> AlonzoTxWits DijkstraEra
forall s a. s -> Getting a s a -> a
^. (TxWits DijkstraEra
 -> Const (AlonzoTxWits DijkstraEra) (TxWits DijkstraEra))
-> Tx TopTx DijkstraEra
-> Const (AlonzoTxWits DijkstraEra) (Tx TopTx DijkstraEra)
Getting
  (AlonzoTxWits DijkstraEra)
  (Tx TopTx DijkstraEra)
  (AlonzoTxWits DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxWits era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxWits DijkstraEra)
witsTxL)
                , (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Bool -> Tokens -> Tokens
TkBool Bool
isValid)
                , (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkNull
                ]
            blockEnc :: Bool -> Enc
blockEnc Bool
isValid = (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Word -> Tokens -> Tokens
TkListLen Word
4) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
authorFields Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Bool -> Tokens -> Tokens
TkBool Bool
isValid)
            indefiniteBlockEnc :: Bool -> Enc
indefiniteBlockEnc Bool
isValid =
              (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkListBegin Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
authorFields Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Bool -> Tokens -> Tokens
TkBool Bool
isValid) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkBreak

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Encodes the mempool and block layouts with their distinct field order" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
          [(IsPhase2Valid, Bool)]
-> ((IsPhase2Valid, Bool) -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(IsPhase2Valid
Phase2Valid, Bool
True), (IsPhase2Valid
Phase2Invalid, Bool
False)] (((IsPhase2Valid, Bool) -> Expectation) -> Expectation)
-> ((IsPhase2Valid, Bool) -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \(IsPhase2Valid
flag, Bool
isValid) -> do
            let txWithFlag :: Tx TopTx DijkstraEra
txWithFlag = Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> (Tx TopTx DijkstraEra -> Tx TopTx DijkstraEra)
-> Tx TopTx DijkstraEra
forall a b. a -> (a -> b) -> b
& (IsPhase2Valid -> Identity IsPhase2Valid)
-> Tx TopTx DijkstraEra -> Identity (Tx TopTx DijkstraEra)
forall era. AlonzoEraTx era => Lens' (Tx TopTx era) IsPhase2Valid
Lens' (Tx TopTx DijkstraEra) IsPhase2Valid
isPhase2ValidTxL ((IsPhase2Valid -> Identity IsPhase2Valid)
 -> Tx TopTx DijkstraEra -> Identity (Tx TopTx DijkstraEra))
-> IsPhase2Valid -> Tx TopTx DijkstraEra -> Tx TopTx DijkstraEra
forall s t a b. ASetter s t a b -> b -> s -> t
.~ IsPhase2Valid
flag
            Version -> Tx TopTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version Tx TopTx DijkstraEra
txWithFlag ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' (Bool -> Enc
blockEnc Bool
isValid)
            Version -> MempoolTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version (Tx TopTx DijkstraEra -> MempoolTx DijkstraEra
forall era. Tx TopTx era -> MempoolTx era
UnsafeMkMempoolTx Tx TopTx DijkstraEra
txWithFlag) ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
mempoolEnc

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Rejects block transactions in both mempool decoders" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
          [Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
True, Bool
False] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
isValid -> do
            let enc :: Enc
enc = Bool -> Enc
blockEnc Bool
isValid
            forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(MempoolTx DijkstraEra) Version
version Enc
enc Either DecoderError (MempoolTx DijkstraEra)
-> (Either DecoderError (MempoolTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (MempoolTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft
            forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull @(MempoolTx DijkstraEra) Version
version (Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize Enc
enc) Either DecoderError (MempoolTx DijkstraEra)
-> (Either DecoderError (MempoolTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (MempoolTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Rejects both mempool layouts in both block transaction decoders" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
          [Enc] -> (Enc -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Enc
mempoolEnc, Bool -> Enc
legacyMempoolEnc Bool
True] ((Enc -> Expectation) -> Expectation)
-> (Enc -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Enc
enc -> do
            forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(Tx TopTx DijkstraEra) Version
version Enc
enc Either DecoderError (Tx TopTx DijkstraEra)
-> (Either DecoderError (Tx TopTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (Tx TopTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft
            forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull @(Tx TopTx DijkstraEra) Version
version (Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize Enc
enc) Either DecoderError (Tx TopTx DijkstraEra)
-> (Either DecoderError (Tx TopTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (Tx TopTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Rejects a legacy false validity flag in both mempool decoders" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
          let enc :: Enc
enc = Bool -> Enc
legacyMempoolEnc Bool
False
          forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(MempoolTx DijkstraEra) Version
version Enc
enc Either DecoderError (MempoolTx DijkstraEra)
-> (Either DecoderError (MempoolTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (MempoolTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft
          forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull @(MempoolTx DijkstraEra) Version
version (Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize Enc
enc) Either DecoderError (MempoolTx DijkstraEra)
-> (Either DecoderError (MempoolTx DijkstraEra) -> Bool)
-> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either DecoderError (MempoolTx DijkstraEra) -> Bool
forall a b. Either a b -> Bool
isLeft

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Normalizes legacy and indefinite mempool envelopes into the canonical layouts"
          (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ [Enc] -> (Enc -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_
            [ Enc
mempoolEnc
            , Bool -> Enc
legacyMempoolEnc Bool
True
            , (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkListBegin Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
authorFields Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkBreak
            ]
          ((Enc -> Expectation) -> Expectation)
-> (Enc -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Enc
enc -> do
            decodedAnn <- Either DecoderError (MempoolTx DijkstraEra)
-> IO (MempoolTx DijkstraEra)
forall a b. (HasCallStack, Show a) => Either a b -> IO b
expectRight (Either DecoderError (MempoolTx DijkstraEra)
 -> IO (MempoolTx DijkstraEra))
-> Either DecoderError (MempoolTx DijkstraEra)
-> IO (MempoolTx DijkstraEra)
forall a b. (a -> b) -> a -> b
$ forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(MempoolTx DijkstraEra) Version
version Enc
enc
            decodedPlain <-
              expectRight $ decodeFull @(MempoolTx DijkstraEra) version (Plain.serialize enc)
            forM_ [decodedAnn, decodedPlain] $ \MempoolTx DijkstraEra
decoded -> do
              let blockTx :: Tx TopTx DijkstraEra
blockTx = MempoolTx DijkstraEra -> Tx TopTx DijkstraEra
forall era. MempoolTx era -> Tx TopTx era
unwrapMempoolTx MempoolTx DijkstraEra
decoded
              Version -> MempoolTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version MempoolTx DijkstraEra
decoded ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
mempoolEnc
              Version -> Tx TopTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version Tx TopTx DijkstraEra
blockTx ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' (Bool -> Enc
blockEnc Bool
True)
              Tx TopTx DijkstraEra -> ByteString
forall t. SafeToHash t => t -> ByteString
originalBytes Tx TopTx DijkstraEra
blockTx ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' (Bool -> Enc
blockEnc Bool
True)
              Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated Tx TopTx DijkstraEra
blockTx SafeHash EraIndependentTx
-> SafeHash EraIndependentTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated Tx TopTx DijkstraEra
tx

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Preserves noncanonical witness bytes through both mempool decoders" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
          let witsEnc :: Enc
witsEnc = (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkMapBegin Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkBreak
              fields :: Enc
fields = Version -> TxBody TopTx DijkstraEra -> Enc
forall a. EncCBOR a => Version -> a -> Enc
Ev Version
version (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (TxBody TopTx DijkstraEra)
     (Tx TopTx DijkstraEra)
     (TxBody TopTx DijkstraEra)
-> TxBody TopTx DijkstraEra
forall s a. s -> Getting a s a -> a
^. Getting
  (TxBody TopTx DijkstraEra)
  (Tx TopTx DijkstraEra)
  (TxBody TopTx DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxBody l DijkstraEra)
bodyTxL) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
witsEnc Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E Tokens -> Tokens
TkNull
              mempoolWithWits :: Enc
mempoolWithWits = (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Word -> Tokens -> Tokens
TkListLen Word
3) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
fields
              blockWithWits :: Enc
blockWithWits = (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Word -> Tokens -> Tokens
TkListLen Word
4) Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> Enc
fields Enc -> Enc -> Enc
forall a. Semigroup a => a -> a -> a
<> (Tokens -> Tokens) -> Enc
forall a. ToCBOR a => a -> Enc
E (Bool -> Tokens -> Tokens
TkBool Bool
True)
          decodedAnn <- Either DecoderError (MempoolTx DijkstraEra)
-> IO (MempoolTx DijkstraEra)
forall a b. (HasCallStack, Show a) => Either a b -> IO b
expectRight (Either DecoderError (MempoolTx DijkstraEra)
 -> IO (MempoolTx DijkstraEra))
-> Either DecoderError (MempoolTx DijkstraEra)
-> IO (MempoolTx DijkstraEra)
forall a b. (a -> b) -> a -> b
$ forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(MempoolTx DijkstraEra) Version
version Enc
mempoolWithWits
          decodedPlain <-
            expectRight $
              decodeFull @(MempoolTx DijkstraEra) version (Plain.serialize mempoolWithWits)
          forM_ [decodedAnn, decodedPlain] $ \MempoolTx DijkstraEra
decoded -> do
            let blockTx :: Tx TopTx DijkstraEra
blockTx = MempoolTx DijkstraEra -> Tx TopTx DijkstraEra
forall era. MempoolTx era -> Tx TopTx era
unwrapMempoolTx MempoolTx DijkstraEra
decoded
            AlonzoTxWits DijkstraEra -> ByteString
forall t. SafeToHash t => t -> ByteString
originalBytes (Tx TopTx DijkstraEra
blockTx Tx TopTx DijkstraEra
-> Getting
     (AlonzoTxWits DijkstraEra)
     (Tx TopTx DijkstraEra)
     (AlonzoTxWits DijkstraEra)
-> AlonzoTxWits DijkstraEra
forall s a. s -> Getting a s a -> a
^. (TxWits DijkstraEra
 -> Const (AlonzoTxWits DijkstraEra) (TxWits DijkstraEra))
-> Tx TopTx DijkstraEra
-> Const (AlonzoTxWits DijkstraEra) (Tx TopTx DijkstraEra)
Getting
  (AlonzoTxWits DijkstraEra)
  (Tx TopTx DijkstraEra)
  (AlonzoTxWits DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxWits era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxWits DijkstraEra)
witsTxL) ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
witsEnc
            Version -> MempoolTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version MempoolTx DijkstraEra
decoded ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
mempoolWithWits
            Tx TopTx DijkstraEra -> ByteString
forall t. SafeToHash t => t -> ByteString
originalBytes Tx TopTx DijkstraEra
blockTx ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
blockWithWits
            TxBody TopTx DijkstraEra -> SafeHash EraIndependentTxBody
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated (Tx TopTx DijkstraEra
blockTx Tx TopTx DijkstraEra
-> Getting
     (TxBody TopTx DijkstraEra)
     (Tx TopTx DijkstraEra)
     (TxBody TopTx DijkstraEra)
-> TxBody TopTx DijkstraEra
forall s a. s -> Getting a s a -> a
^. Getting
  (TxBody TopTx DijkstraEra)
  (Tx TopTx DijkstraEra)
  (TxBody TopTx DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxBody l DijkstraEra)
bodyTxL) SafeHash EraIndependentTxBody
-> SafeHash EraIndependentTxBody -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` TxBody TopTx DijkstraEra -> SafeHash EraIndependentTxBody
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated (Tx TopTx DijkstraEra
tx Tx TopTx DijkstraEra
-> Getting
     (TxBody TopTx DijkstraEra)
     (Tx TopTx DijkstraEra)
     (TxBody TopTx DijkstraEra)
-> TxBody TopTx DijkstraEra
forall s a. s -> Getting a s a -> a
^. Getting
  (TxBody TopTx DijkstraEra)
  (Tx TopTx DijkstraEra)
  (TxBody TopTx DijkstraEra)
forall era (l :: TxLevel).
EraTx era =>
Lens' (Tx l era) (TxBody l era)
forall (l :: TxLevel).
Lens' (Tx l DijkstraEra) (TxBody l DijkstraEra)
bodyTxL)
            Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated Tx TopTx DijkstraEra
blockTx SafeHash EraIndependentTx
-> SafeHash EraIndependentTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldNotBe` Tx TopTx DijkstraEra -> SafeHash EraIndependentTx
forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated Tx TopTx DijkstraEra
tx

        String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Preserves indefinite block bytes through both public Tx decoders" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
          [Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
True, Bool
False] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
isValid -> do
            let enc :: Enc
enc = Bool -> Enc
indefiniteBlockEnc Bool
isValid
                bytes :: ByteString
bytes = Enc -> ByteString
forall a. ToCBOR a => a -> ByteString
Plain.serialize' Enc
enc
            decodedAnn <- Either DecoderError (Tx TopTx DijkstraEra)
-> IO (Tx TopTx DijkstraEra)
forall a b. (HasCallStack, Show a) => Either a b -> IO b
expectRight (Either DecoderError (Tx TopTx DijkstraEra)
 -> IO (Tx TopTx DijkstraEra))
-> Either DecoderError (Tx TopTx DijkstraEra)
-> IO (Tx TopTx DijkstraEra)
forall a b. (a -> b) -> a -> b
$ forall a.
DecCBOR (Annotator a) =>
Version -> Enc -> Either DecoderError a
decodeEnc @(Tx TopTx DijkstraEra) Version
version Enc
enc
            decodedPlain <-
              expectRight $ decodeFull @(Tx TopTx DijkstraEra) version (Plain.serialize enc)
            forM_ [decodedAnn, decodedPlain] $ \Tx TopTx DijkstraEra
decoded -> do
              Tx TopTx DijkstraEra -> ByteString
forall t. SafeToHash t => t -> ByteString
originalBytes Tx TopTx DijkstraEra
decoded ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` ByteString
bytes
              Version -> Tx TopTx DijkstraEra -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version Tx TopTx DijkstraEra
decoded ByteString -> ByteString -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` ByteString
bytes