{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving         #-}

module Ouroboros.Network.Protocol.ChainSync.Codec.CDDL where

import Codec.CBOR.Read qualified as CBOR
import Codec.Serialise (Serialise)
import Codec.Serialise.Class qualified as Serialise
import Control.DeepSeq
import Data.ByteString.Lazy qualified as BL
import GHC.Generics (Generic)
import Network.TypedProtocol.Codec
import Ouroboros.Network.Protocol.ChainSync.Codec
import Ouroboros.Network.Protocol.ChainSync.Type
import Test.Data.CDDL
import Test.QuickCheck

newtype BlockHeader = BlockHeader Any
  deriving (BlockHeader -> BlockHeader -> Bool
(BlockHeader -> BlockHeader -> Bool)
-> (BlockHeader -> BlockHeader -> Bool) -> Eq BlockHeader
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlockHeader -> BlockHeader -> Bool
== :: BlockHeader -> BlockHeader -> Bool
$c/= :: BlockHeader -> BlockHeader -> Bool
/= :: BlockHeader -> BlockHeader -> Bool
Eq, Int -> BlockHeader -> ShowS
[BlockHeader] -> ShowS
BlockHeader -> String
(Int -> BlockHeader -> ShowS)
-> (BlockHeader -> String)
-> ([BlockHeader] -> ShowS)
-> Show BlockHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlockHeader -> ShowS
showsPrec :: Int -> BlockHeader -> ShowS
$cshow :: BlockHeader -> String
show :: BlockHeader -> String
$cshowList :: [BlockHeader] -> ShowS
showList :: [BlockHeader] -> ShowS
Show, Gen BlockHeader
Gen BlockHeader
-> (BlockHeader -> [BlockHeader]) -> Arbitrary BlockHeader
BlockHeader -> [BlockHeader]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen BlockHeader
arbitrary :: Gen BlockHeader
$cshrink :: BlockHeader -> [BlockHeader]
shrink :: BlockHeader -> [BlockHeader]
Arbitrary, [BlockHeader] -> Encoding
BlockHeader -> Encoding
(BlockHeader -> Encoding)
-> (forall s. Decoder s BlockHeader)
-> ([BlockHeader] -> Encoding)
-> (forall s. Decoder s [BlockHeader])
-> Serialise BlockHeader
forall s. Decoder s [BlockHeader]
forall s. Decoder s BlockHeader
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: BlockHeader -> Encoding
encode :: BlockHeader -> Encoding
$cdecode :: forall s. Decoder s BlockHeader
decode :: forall s. Decoder s BlockHeader
$cencodeList :: [BlockHeader] -> Encoding
encodeList :: [BlockHeader] -> Encoding
$cdecodeList :: forall s. Decoder s [BlockHeader]
decodeList :: forall s. Decoder s [BlockHeader]
Serialise, (forall x. BlockHeader -> Rep BlockHeader x)
-> (forall x. Rep BlockHeader x -> BlockHeader)
-> Generic BlockHeader
forall x. Rep BlockHeader x -> BlockHeader
forall x. BlockHeader -> Rep BlockHeader x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlockHeader -> Rep BlockHeader x
from :: forall x. BlockHeader -> Rep BlockHeader x
$cto :: forall x. Rep BlockHeader x -> BlockHeader
to :: forall x. Rep BlockHeader x -> BlockHeader
Generic, BlockHeader -> ()
(BlockHeader -> ()) -> NFData BlockHeader
forall a. (a -> ()) -> NFData a
$crnf :: BlockHeader -> ()
rnf :: BlockHeader -> ()
NFData)

newtype HeaderPoint = HeaderPoint Any
  deriving (HeaderPoint -> HeaderPoint -> Bool
(HeaderPoint -> HeaderPoint -> Bool)
-> (HeaderPoint -> HeaderPoint -> Bool) -> Eq HeaderPoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HeaderPoint -> HeaderPoint -> Bool
== :: HeaderPoint -> HeaderPoint -> Bool
$c/= :: HeaderPoint -> HeaderPoint -> Bool
/= :: HeaderPoint -> HeaderPoint -> Bool
Eq, Int -> HeaderPoint -> ShowS
[HeaderPoint] -> ShowS
HeaderPoint -> String
(Int -> HeaderPoint -> ShowS)
-> (HeaderPoint -> String)
-> ([HeaderPoint] -> ShowS)
-> Show HeaderPoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HeaderPoint -> ShowS
showsPrec :: Int -> HeaderPoint -> ShowS
$cshow :: HeaderPoint -> String
show :: HeaderPoint -> String
$cshowList :: [HeaderPoint] -> ShowS
showList :: [HeaderPoint] -> ShowS
Show, Gen HeaderPoint
Gen HeaderPoint
-> (HeaderPoint -> [HeaderPoint]) -> Arbitrary HeaderPoint
HeaderPoint -> [HeaderPoint]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen HeaderPoint
arbitrary :: Gen HeaderPoint
$cshrink :: HeaderPoint -> [HeaderPoint]
shrink :: HeaderPoint -> [HeaderPoint]
Arbitrary, [HeaderPoint] -> Encoding
HeaderPoint -> Encoding
(HeaderPoint -> Encoding)
-> (forall s. Decoder s HeaderPoint)
-> ([HeaderPoint] -> Encoding)
-> (forall s. Decoder s [HeaderPoint])
-> Serialise HeaderPoint
forall s. Decoder s [HeaderPoint]
forall s. Decoder s HeaderPoint
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: HeaderPoint -> Encoding
encode :: HeaderPoint -> Encoding
$cdecode :: forall s. Decoder s HeaderPoint
decode :: forall s. Decoder s HeaderPoint
$cencodeList :: [HeaderPoint] -> Encoding
encodeList :: [HeaderPoint] -> Encoding
$cdecodeList :: forall s. Decoder s [HeaderPoint]
decodeList :: forall s. Decoder s [HeaderPoint]
Serialise, (forall x. HeaderPoint -> Rep HeaderPoint x)
-> (forall x. Rep HeaderPoint x -> HeaderPoint)
-> Generic HeaderPoint
forall x. Rep HeaderPoint x -> HeaderPoint
forall x. HeaderPoint -> Rep HeaderPoint x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HeaderPoint -> Rep HeaderPoint x
from :: forall x. HeaderPoint -> Rep HeaderPoint x
$cto :: forall x. Rep HeaderPoint x -> HeaderPoint
to :: forall x. Rep HeaderPoint x -> HeaderPoint
Generic, HeaderPoint -> ()
(HeaderPoint -> ()) -> NFData HeaderPoint
forall a. (a -> ()) -> NFData a
$crnf :: HeaderPoint -> ()
rnf :: HeaderPoint -> ()
NFData)

newtype HeaderTip = HeaderTip Any
  deriving (HeaderTip -> HeaderTip -> Bool
(HeaderTip -> HeaderTip -> Bool)
-> (HeaderTip -> HeaderTip -> Bool) -> Eq HeaderTip
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HeaderTip -> HeaderTip -> Bool
== :: HeaderTip -> HeaderTip -> Bool
$c/= :: HeaderTip -> HeaderTip -> Bool
/= :: HeaderTip -> HeaderTip -> Bool
Eq, Int -> HeaderTip -> ShowS
[HeaderTip] -> ShowS
HeaderTip -> String
(Int -> HeaderTip -> ShowS)
-> (HeaderTip -> String)
-> ([HeaderTip] -> ShowS)
-> Show HeaderTip
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HeaderTip -> ShowS
showsPrec :: Int -> HeaderTip -> ShowS
$cshow :: HeaderTip -> String
show :: HeaderTip -> String
$cshowList :: [HeaderTip] -> ShowS
showList :: [HeaderTip] -> ShowS
Show, Gen HeaderTip
Gen HeaderTip -> (HeaderTip -> [HeaderTip]) -> Arbitrary HeaderTip
HeaderTip -> [HeaderTip]
forall a. Gen a -> (a -> [a]) -> Arbitrary a
$carbitrary :: Gen HeaderTip
arbitrary :: Gen HeaderTip
$cshrink :: HeaderTip -> [HeaderTip]
shrink :: HeaderTip -> [HeaderTip]
Arbitrary, [HeaderTip] -> Encoding
HeaderTip -> Encoding
(HeaderTip -> Encoding)
-> (forall s. Decoder s HeaderTip)
-> ([HeaderTip] -> Encoding)
-> (forall s. Decoder s [HeaderTip])
-> Serialise HeaderTip
forall s. Decoder s [HeaderTip]
forall s. Decoder s HeaderTip
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: HeaderTip -> Encoding
encode :: HeaderTip -> Encoding
$cdecode :: forall s. Decoder s HeaderTip
decode :: forall s. Decoder s HeaderTip
$cencodeList :: [HeaderTip] -> Encoding
encodeList :: [HeaderTip] -> Encoding
$cdecodeList :: forall s. Decoder s [HeaderTip]
decodeList :: forall s. Decoder s [HeaderTip]
Serialise, (forall x. HeaderTip -> Rep HeaderTip x)
-> (forall x. Rep HeaderTip x -> HeaderTip) -> Generic HeaderTip
forall x. Rep HeaderTip x -> HeaderTip
forall x. HeaderTip -> Rep HeaderTip x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HeaderTip -> Rep HeaderTip x
from :: forall x. HeaderTip -> Rep HeaderTip x
$cto :: forall x. Rep HeaderTip x -> HeaderTip
to :: forall x. Rep HeaderTip x -> HeaderTip
Generic, HeaderTip -> ()
(HeaderTip -> ()) -> NFData HeaderTip
forall a. (a -> ()) -> NFData a
$crnf :: HeaderTip -> ()
rnf :: HeaderTip -> ()
NFData)

chainSyncCodec :: Codec (ChainSync BlockHeader HeaderPoint HeaderTip)
                        CBOR.DeserialiseFailure IO BL.ByteString
chainSyncCodec :: Codec
  (ChainSync BlockHeader HeaderPoint HeaderTip)
  DeserialiseFailure
  IO
  ByteString
chainSyncCodec =
    (BlockHeader -> Encoding)
-> (forall s. Decoder s BlockHeader)
-> (HeaderPoint -> Encoding)
-> (forall s. Decoder s HeaderPoint)
-> (HeaderTip -> Encoding)
-> (forall s. Decoder s HeaderTip)
-> Codec
     (ChainSync BlockHeader HeaderPoint HeaderTip)
     DeserialiseFailure
     IO
     ByteString
forall header point tip (m :: * -> *).
MonadST m =>
(header -> Encoding)
-> (forall s. Decoder s header)
-> (point -> Encoding)
-> (forall s. Decoder s point)
-> (tip -> Encoding)
-> (forall s. Decoder s tip)
-> Codec
     (ChainSync header point tip) DeserialiseFailure m ByteString
codecChainSync
      BlockHeader -> Encoding
forall a. Serialise a => a -> Encoding
Serialise.encode
      Decoder s BlockHeader
forall s. Decoder s BlockHeader
forall a s. Serialise a => Decoder s a
Serialise.decode
      HeaderPoint -> Encoding
forall a. Serialise a => a -> Encoding
Serialise.encode
      Decoder s HeaderPoint
forall s. Decoder s HeaderPoint
forall a s. Serialise a => Decoder s a
Serialise.decode
      HeaderTip -> Encoding
forall a. Serialise a => a -> Encoding
Serialise.encode
      Decoder s HeaderTip
forall s. Decoder s HeaderTip
forall a s. Serialise a => Decoder s a
Serialise.decode