{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.Shelley.Binary.RoundTrip (
  roundTripShelleyCommonSpec,
  roundTripStateEraTypesSpec,
) where

import Cardano.Ledger.Binary
import Cardano.Ledger.Core
import Cardano.Ledger.Metadata (Metadatum)
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.Shelley.State
import qualified Data.Text as T
import Test.Cardano.Base.Bytes (genByteString)
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Binary.RoundTrip
import Test.Cardano.Ledger.Shelley.Arbitrary ()
import Test.Cardano.Ledger.Shelley.Binary.Annotator ()
import Test.Cardano.Ledger.Shelley.Era (ShelleyEraTest)

roundTripShelleyCommonSpec ::
  forall era.
  ( ShelleyEraTest era
  , RuleListEra era
  ) =>
  Spec
roundTripShelleyCommonSpec :: forall era. (ShelleyEraTest era, RuleListEra era) => Spec
roundTripShelleyCommonSpec = do
  forall era.
(EraTx era, EraCertState era, Arbitrary (Tx TopTx era),
 Arbitrary (TxBody TopTx era), Arbitrary (TxOut era),
 Arbitrary (TxCert era), Arbitrary (TxWits era),
 Arbitrary (TxAuxData era), Arbitrary (Value era),
 Arbitrary (CompactForm (Value era)), Arbitrary (Script era),
 Arbitrary (PParams era), Arbitrary (PParamsUpdate era),
 Arbitrary (CertState era), Arbitrary (Accounts era),
 DecCBOR (Script era), DecCBOR (TxAuxData era),
 DecCBOR (TxWits era), DecCBOR (TxBody TopTx era),
 DecCBOR (Tx TopTx era), Typeable (CertState era), HasCallStack) =>
Spec
roundTripCoreEraTypesSpec @era
  forall era.
(EraTxOut era, EraGov era, EraStake era, EraCertState era,
 Eq (StashedAVVMAddresses era), Show (StashedAVVMAddresses era),
 EncCBOR (StashedAVVMAddresses era),
 DecCBOR (StashedAVVMAddresses era),
 Arbitrary (StashedAVVMAddresses era), Arbitrary (TxOut era),
 Arbitrary (Value era), Arbitrary (PParams era),
 Arbitrary (GovState era), Arbitrary (CertState era),
 Arbitrary (InstantStake era)) =>
Spec
roundTripStateEraTypesSpec @era
  forall era. (RuleListEra era, Era era, HasCallStack) => Spec
roundTripAllPredicateFailures @era
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"Metadatum size limits" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
    forall era. (Era era, HasCallStack) => (Version -> Spec) -> Spec
forEachEraVersion @era Version -> Spec
metadatumSizeLimitSpec

roundTripStateEraTypesSpec ::
  forall era.
  ( EraTxOut era
  , EraGov era
  , EraStake era
  , EraCertState era
  , Eq (StashedAVVMAddresses era)
  , Show (StashedAVVMAddresses era)
  , EncCBOR (StashedAVVMAddresses era)
  , DecCBOR (StashedAVVMAddresses era)
  , Arbitrary (StashedAVVMAddresses era)
  , Arbitrary (TxOut era)
  , Arbitrary (Value era)
  , Arbitrary (PParams era)
  , Arbitrary (GovState era)
  , Arbitrary (CertState era)
  , Arbitrary (InstantStake era)
  ) =>
  Spec
roundTripStateEraTypesSpec :: forall era.
(EraTxOut era, EraGov era, EraStake era, EraCertState era,
 Eq (StashedAVVMAddresses era), Show (StashedAVVMAddresses era),
 EncCBOR (StashedAVVMAddresses era),
 DecCBOR (StashedAVVMAddresses era),
 Arbitrary (StashedAVVMAddresses era), Arbitrary (TxOut era),
 Arbitrary (Value era), Arbitrary (PParams era),
 Arbitrary (GovState era), Arbitrary (CertState era),
 Arbitrary (InstantStake era)) =>
Spec
roundTripStateEraTypesSpec = do
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"State Types Families" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    forall era t.
(Era era, Typeable t, Show t, Eq t, EncCBOR t, DecShareCBOR t,
 Arbitrary t, HasCallStack) =>
Spec
roundTripShareEraSpec @era @(GovState era)
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"State Types" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    forall era (t :: * -> *).
(Era era, Typeable t, Show (t era), Eq (t era), EncCBOR (t era),
 DecShareCBOR (t era), Arbitrary (t era), HasCallStack) =>
Spec
roundTripShareEraTypeSpec @era @UTxOState
    forall era (t :: * -> *).
(Era era, Show (t era), Eq (t era), EncCBOR (t era),
 DecCBOR (t era), Arbitrary (t era), HasCallStack) =>
Spec
roundTripEraTypeSpec @era @EpochState
    forall era (t :: * -> *).
(Era era, Show (t era), Eq (t era), EncCBOR (t era),
 DecCBOR (t era), Arbitrary (t era), HasCallStack) =>
Spec
roundTripEraTypeSpec @era @NewEpochState

metadatumSizeLimitSpec :: Version -> Spec
metadatumSizeLimitSpec :: Version -> Spec
metadatumSizeLimitSpec Version
v = do
  let
    genAsciiText :: Int -> Gen Text
genAsciiText Int
n = String -> Text
T.pack (String -> Text) -> Gen String -> Gen Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen Char -> Gen String
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
n ((Char, Char) -> Gen Char
forall a. Random a => (a, a) -> Gen a
choose (Char
'a', Char
'z'))
    dec :: ToCBOR a => a -> Either DecoderError Metadatum
    dec :: forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec = Version -> ByteString -> Either DecoderError Metadatum
forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
v (ByteString -> Either DecoderError Metadatum)
-> (a -> ByteString) -> a -> Either DecoderError Metadatum
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> ByteString
toLazyByteString (Encoding -> ByteString) -> (a -> Encoding) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR
  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Accepts bytes up to 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
    Gen ByteString -> (ByteString -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
0, Int
64) Gen Int -> (Int -> Gen ByteString) -> Gen ByteString
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen ByteString
genByteString) ((ByteString -> IO ()) -> Property)
-> (ByteString -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \ByteString
bs ->
      Either DecoderError Metadatum -> IO ()
forall a b.
(HasCallStack, ToExpr a, NFData b) =>
Either a b -> IO ()
expectRightDeepExpr_ (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec ByteString
bs
  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Accepts text up to 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
    Gen Text -> (Text -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
0, Int
64) Gen Int -> (Int -> Gen Text) -> Gen Text
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen Text
genAsciiText) ((Text -> IO ()) -> Property) -> (Text -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \Text
txt ->
      Either DecoderError Metadatum -> IO ()
forall a b.
(HasCallStack, ToExpr a, NFData b) =>
Either a b -> IO ()
expectRightDeepExpr_ (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec Text
txt
  if Version
v Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
> forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2
    then do
      String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Rejects bytes exceeding 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
        Gen ByteString -> (ByteString -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
65, Int
1000) Gen Int -> (Int -> Gen ByteString) -> Gen ByteString
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen ByteString
genByteString) ((ByteString -> IO ()) -> Property)
-> (ByteString -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \ByteString
bs ->
          IO DecoderError -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO DecoderError -> IO ())
-> (Either DecoderError Metadatum -> IO DecoderError)
-> Either DecoderError Metadatum
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either DecoderError Metadatum -> IO DecoderError
forall b a. (HasCallStack, ToExpr b) => Either a b -> IO a
expectLeftExpr (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec ByteString
bs
      String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Rejects text exceeding 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
        Gen Text -> (Text -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
65, Int
1000) Gen Int -> (Int -> Gen Text) -> Gen Text
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen Text
genAsciiText) ((Text -> IO ()) -> Property) -> (Text -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \Text
txt ->
          IO DecoderError -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO DecoderError -> IO ())
-> (Either DecoderError Metadatum -> IO DecoderError)
-> Either DecoderError Metadatum
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either DecoderError Metadatum -> IO DecoderError
forall b a. (HasCallStack, ToExpr b) => Either a b -> IO a
expectLeftExpr (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec Text
txt
    else do
      String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Accepts bytes exceeding 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
        Gen ByteString -> (ByteString -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
65, Int
1000) Gen Int -> (Int -> Gen ByteString) -> Gen ByteString
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen ByteString
genByteString) ((ByteString -> IO ()) -> Property)
-> (ByteString -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \ByteString
bs ->
          Either DecoderError Metadatum -> IO ()
forall a b.
(HasCallStack, ToExpr a, NFData b) =>
Either a b -> IO ()
expectRightDeepExpr_ (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec ByteString
bs
      String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"Accepts text exceeding 64 bytes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
        Gen Text -> (Text -> IO ()) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
65, Int
1000) Gen Int -> (Int -> Gen Text) -> Gen Text
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Gen Text
genAsciiText) ((Text -> IO ()) -> Property) -> (Text -> IO ()) -> Property
forall a b. (a -> b) -> a -> b
$ \Text
txt ->
          Either DecoderError Metadatum -> IO ()
forall a b.
(HasCallStack, ToExpr a, NFData b) =>
Either a b -> IO ()
expectRightDeepExpr_ (Either DecoderError Metadatum -> IO ())
-> Either DecoderError Metadatum -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> Either DecoderError Metadatum
forall a. ToCBOR a => a -> Either DecoderError Metadatum
dec Text
txt

instance RuleListEra ShelleyEra where
  type
    EraRules ShelleyEra =
      '[ "DELEG"
       , "DELEGS"
       , "DELPL"
       , "LEDGER"
       , "LEDGERS"
       , "POOL"
       , "PPUP"
       , "UTXO"
       , "UTXOW"
       ]