{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Chain.Update.ProtocolVersion (
  ProtocolVersion (..),
)
where

import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  encodeListLen,
  enforceSize,
  fromByronCBOR,
  toByronCBOR,
 )
import Cardano.Prelude
import Data.Aeson (ToJSON)
import Formatting (bprint, shown)
import Formatting.Buildable (Buildable (..))
import NoThunks.Class (NoThunks (..))
import qualified Prelude

-- | Communication protocol version
data ProtocolVersion = ProtocolVersion
  { ProtocolVersion -> Word16
pvMajor :: !Word16
  , ProtocolVersion -> Word16
pvMinor :: !Word16
  , ProtocolVersion -> Word8
pvAlt :: !Word8
  }
  deriving (ProtocolVersion -> ProtocolVersion -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ProtocolVersion -> ProtocolVersion -> Bool
$c/= :: ProtocolVersion -> ProtocolVersion -> Bool
== :: ProtocolVersion -> ProtocolVersion -> Bool
$c== :: ProtocolVersion -> ProtocolVersion -> Bool
Eq, forall x. Rep ProtocolVersion x -> ProtocolVersion
forall x. ProtocolVersion -> Rep ProtocolVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ProtocolVersion x -> ProtocolVersion
$cfrom :: forall x. ProtocolVersion -> Rep ProtocolVersion x
Generic, Eq ProtocolVersion
ProtocolVersion -> ProtocolVersion -> Bool
ProtocolVersion -> ProtocolVersion -> Ordering
ProtocolVersion -> ProtocolVersion -> ProtocolVersion
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
$cmin :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
max :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
$cmax :: ProtocolVersion -> ProtocolVersion -> ProtocolVersion
>= :: ProtocolVersion -> ProtocolVersion -> Bool
$c>= :: ProtocolVersion -> ProtocolVersion -> Bool
> :: ProtocolVersion -> ProtocolVersion -> Bool
$c> :: ProtocolVersion -> ProtocolVersion -> Bool
<= :: ProtocolVersion -> ProtocolVersion -> Bool
$c<= :: ProtocolVersion -> ProtocolVersion -> Bool
< :: ProtocolVersion -> ProtocolVersion -> Bool
$c< :: ProtocolVersion -> ProtocolVersion -> Bool
compare :: ProtocolVersion -> ProtocolVersion -> Ordering
$ccompare :: ProtocolVersion -> ProtocolVersion -> Ordering
Ord)
  deriving anyclass (ProtocolVersion -> ()
forall a. (a -> ()) -> NFData a
rnf :: ProtocolVersion -> ()
$crnf :: ProtocolVersion -> ()
NFData, Context -> ProtocolVersion -> IO (Maybe ThunkInfo)
Proxy ProtocolVersion -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy ProtocolVersion -> String
$cshowTypeOf :: Proxy ProtocolVersion -> String
wNoThunks :: Context -> ProtocolVersion -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> ProtocolVersion -> IO (Maybe ThunkInfo)
noThunks :: Context -> ProtocolVersion -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> ProtocolVersion -> IO (Maybe ThunkInfo)
NoThunks)

instance Show ProtocolVersion where
  show :: ProtocolVersion -> String
show ProtocolVersion
pv =
    forall a. [a] -> [[a]] -> [a]
intercalate String
"." [forall a b. (Show a, ConvertText String b) => a -> b
show (ProtocolVersion -> Word16
pvMajor ProtocolVersion
pv), forall a b. (Show a, ConvertText String b) => a -> b
show (ProtocolVersion -> Word16
pvMinor ProtocolVersion
pv), forall a b. (Show a, ConvertText String b) => a -> b
show (ProtocolVersion -> Word8
pvAlt ProtocolVersion
pv)]

instance Buildable ProtocolVersion where
  build :: ProtocolVersion -> Builder
build = forall a. Format Builder a -> a
bprint forall a r. Show a => Format r (a -> r)
shown

-- Used for debugging purposes only
instance ToJSON ProtocolVersion

instance ToCBOR ProtocolVersion where
  toCBOR :: ProtocolVersion -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR

instance FromCBOR ProtocolVersion where
  fromCBOR :: forall s. Decoder s ProtocolVersion
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR ProtocolVersion where
  encCBOR :: ProtocolVersion -> Encoding
encCBOR ProtocolVersion
pv =
    Word -> Encoding
encodeListLen Word
3
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (ProtocolVersion -> Word16
pvMajor ProtocolVersion
pv)
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (ProtocolVersion -> Word16
pvMinor ProtocolVersion
pv)
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (ProtocolVersion -> Word8
pvAlt ProtocolVersion
pv)

  encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy ProtocolVersion -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
f Proxy ProtocolVersion
pv =
    Size
1
      forall a. Num a => a -> a -> a
+ forall a.
EncCBOR a =>
(forall t. EncCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
f (ProtocolVersion -> Word16
pvMajor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ProtocolVersion
pv)
      forall a. Num a => a -> a -> a
+ forall a.
EncCBOR a =>
(forall t. EncCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
f (ProtocolVersion -> Word16
pvMinor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ProtocolVersion
pv)
      forall a. Num a => a -> a -> a
+ forall a.
EncCBOR a =>
(forall t. EncCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
f (ProtocolVersion -> Word8
pvAlt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ProtocolVersion
pv)

instance DecCBOR ProtocolVersion where
  decCBOR :: forall s. Decoder s ProtocolVersion
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"ProtocolVersion" Int
3
    Word16 -> Word16 -> Word8 -> ProtocolVersion
ProtocolVersion forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR