{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Chain.Update.SoftwareVersion (
  SoftwareVersion (..),
  SoftwareVersionError (..),
  NumSoftwareVersion,
  checkSoftwareVersion,
)
where

import Cardano.Chain.Update.ApplicationName
import Cardano.Ledger.Binary (
  DecCBOR (..),
  DecoderError (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  cborError,
  decodeWord8,
  encodeListLen,
  enforceSize,
  fromByronCBOR,
  toByronCBOR,
 )
import Cardano.Prelude hiding (cborError)
import Data.Aeson (ToJSON)
import Data.Data (Data)
import Formatting (bprint, build, formatToString, int, stext)
import qualified Formatting.Buildable as B (Buildable (..))
import NoThunks.Class (NoThunks (..))
import qualified Prelude

-- | Numeric software version associated with 'ApplicationName'
type NumSoftwareVersion = Word32

-- | Software version
data SoftwareVersion = SoftwareVersion
  { SoftwareVersion -> ApplicationName
svAppName :: !ApplicationName
  , SoftwareVersion -> NumSoftwareVersion
svNumber :: !NumSoftwareVersion
  }
  deriving (SoftwareVersion -> SoftwareVersion -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SoftwareVersion -> SoftwareVersion -> Bool
$c/= :: SoftwareVersion -> SoftwareVersion -> Bool
== :: SoftwareVersion -> SoftwareVersion -> Bool
$c== :: SoftwareVersion -> SoftwareVersion -> Bool
Eq, forall x. Rep SoftwareVersion x -> SoftwareVersion
forall x. SoftwareVersion -> Rep SoftwareVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SoftwareVersion x -> SoftwareVersion
$cfrom :: forall x. SoftwareVersion -> Rep SoftwareVersion x
Generic, Eq SoftwareVersion
SoftwareVersion -> SoftwareVersion -> Bool
SoftwareVersion -> SoftwareVersion -> Ordering
SoftwareVersion -> SoftwareVersion -> SoftwareVersion
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 :: SoftwareVersion -> SoftwareVersion -> SoftwareVersion
$cmin :: SoftwareVersion -> SoftwareVersion -> SoftwareVersion
max :: SoftwareVersion -> SoftwareVersion -> SoftwareVersion
$cmax :: SoftwareVersion -> SoftwareVersion -> SoftwareVersion
>= :: SoftwareVersion -> SoftwareVersion -> Bool
$c>= :: SoftwareVersion -> SoftwareVersion -> Bool
> :: SoftwareVersion -> SoftwareVersion -> Bool
$c> :: SoftwareVersion -> SoftwareVersion -> Bool
<= :: SoftwareVersion -> SoftwareVersion -> Bool
$c<= :: SoftwareVersion -> SoftwareVersion -> Bool
< :: SoftwareVersion -> SoftwareVersion -> Bool
$c< :: SoftwareVersion -> SoftwareVersion -> Bool
compare :: SoftwareVersion -> SoftwareVersion -> Ordering
$ccompare :: SoftwareVersion -> SoftwareVersion -> Ordering
Ord)
  deriving anyclass (SoftwareVersion -> ()
forall a. (a -> ()) -> NFData a
rnf :: SoftwareVersion -> ()
$crnf :: SoftwareVersion -> ()
NFData, Context -> SoftwareVersion -> IO (Maybe ThunkInfo)
Proxy SoftwareVersion -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy SoftwareVersion -> String
$cshowTypeOf :: Proxy SoftwareVersion -> String
wNoThunks :: Context -> SoftwareVersion -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> SoftwareVersion -> IO (Maybe ThunkInfo)
noThunks :: Context -> SoftwareVersion -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> SoftwareVersion -> IO (Maybe ThunkInfo)
NoThunks)

instance B.Buildable SoftwareVersion where
  build :: SoftwareVersion -> Builder
build SoftwareVersion
sv =
    forall a. Format Builder a -> a
bprint (forall r. Format r (Text -> r)
stext forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (NumSoftwareVersion -> Builder) (NumSoftwareVersion -> Builder)
":" forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Integral a => Format r (a -> r)
int) (ApplicationName -> Text
unApplicationName forall a b. (a -> b) -> a -> b
$ SoftwareVersion -> ApplicationName
svAppName SoftwareVersion
sv) (SoftwareVersion -> NumSoftwareVersion
svNumber SoftwareVersion
sv)

instance Show SoftwareVersion where
  show :: SoftwareVersion -> String
show = forall a. Format String a -> a
formatToString forall a r. Buildable a => Format r (a -> r)
build

-- Used for debugging purposes only
instance ToJSON SoftwareVersion

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

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

instance EncCBOR SoftwareVersion where
  encCBOR :: SoftwareVersion -> Encoding
encCBOR SoftwareVersion
sv = Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (SoftwareVersion -> ApplicationName
svAppName SoftwareVersion
sv) forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (SoftwareVersion -> NumSoftwareVersion
svNumber SoftwareVersion
sv)

  encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy SoftwareVersion -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
f Proxy SoftwareVersion
sv =
    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 (SoftwareVersion -> ApplicationName
svAppName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy SoftwareVersion
sv)
      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 (SoftwareVersion -> NumSoftwareVersion
svNumber forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy SoftwareVersion
sv)

instance DecCBOR SoftwareVersion where
  decCBOR :: forall s. Decoder s SoftwareVersion
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"SoftwareVersion" Int
2
    ApplicationName -> NumSoftwareVersion -> SoftwareVersion
SoftwareVersion 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

data SoftwareVersionError
  = SoftwareVersionApplicationNameError ApplicationNameError
  deriving (Typeable SoftwareVersionError
SoftwareVersionError -> DataType
SoftwareVersionError -> Constr
(forall b. Data b => b -> b)
-> SoftwareVersionError -> SoftwareVersionError
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> SoftwareVersionError -> u
forall u.
(forall d. Data d => d -> u) -> SoftwareVersionError -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SoftwareVersionError
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> SoftwareVersionError
-> c SoftwareVersionError
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SoftwareVersionError)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SoftwareVersionError)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> SoftwareVersionError -> m SoftwareVersionError
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> SoftwareVersionError -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> SoftwareVersionError -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> SoftwareVersionError -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> SoftwareVersionError -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SoftwareVersionError -> r
gmapT :: (forall b. Data b => b -> b)
-> SoftwareVersionError -> SoftwareVersionError
$cgmapT :: (forall b. Data b => b -> b)
-> SoftwareVersionError -> SoftwareVersionError
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SoftwareVersionError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SoftwareVersionError)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SoftwareVersionError)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SoftwareVersionError)
dataTypeOf :: SoftwareVersionError -> DataType
$cdataTypeOf :: SoftwareVersionError -> DataType
toConstr :: SoftwareVersionError -> Constr
$ctoConstr :: SoftwareVersionError -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SoftwareVersionError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SoftwareVersionError
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> SoftwareVersionError
-> c SoftwareVersionError
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> SoftwareVersionError
-> c SoftwareVersionError
Data, SoftwareVersionError -> SoftwareVersionError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SoftwareVersionError -> SoftwareVersionError -> Bool
$c/= :: SoftwareVersionError -> SoftwareVersionError -> Bool
== :: SoftwareVersionError -> SoftwareVersionError -> Bool
$c== :: SoftwareVersionError -> SoftwareVersionError -> Bool
Eq, Int -> SoftwareVersionError -> ShowS
[SoftwareVersionError] -> ShowS
SoftwareVersionError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SoftwareVersionError] -> ShowS
$cshowList :: [SoftwareVersionError] -> ShowS
show :: SoftwareVersionError -> String
$cshow :: SoftwareVersionError -> String
showsPrec :: Int -> SoftwareVersionError -> ShowS
$cshowsPrec :: Int -> SoftwareVersionError -> ShowS
Show)

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

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

instance EncCBOR SoftwareVersionError where
  encCBOR :: SoftwareVersionError -> Encoding
encCBOR (SoftwareVersionApplicationNameError ApplicationNameError
applicationNameError) =
    Word -> Encoding
encodeListLen Word
2
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR (Word8
0 :: Word8)
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR ApplicationNameError
applicationNameError

instance DecCBOR SoftwareVersionError where
  decCBOR :: forall s. Decoder s SoftwareVersionError
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"SoftwareVersionError" Int
2
    Word8
tag <- forall s. Decoder s Word8
decodeWord8
    case Word8
tag of
      Word8
0 -> ApplicationNameError -> SoftwareVersionError
SoftwareVersionApplicationNameError forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR
      Word8
_ -> forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Word8 -> DecoderError
DecoderErrorUnknownTag Text
"SoftwareVersionError" Word8
tag

instance B.Buildable SoftwareVersionError where
  build :: SoftwareVersionError -> Builder
build = \case
    SoftwareVersionApplicationNameError ApplicationNameError
err ->
      forall a. Format Builder a -> a
bprint
        ( Format
  (ApplicationNameError -> Builder) (ApplicationNameError -> Builder)
"ApplicationName was invalid when checking SoftwareVersion\n Error:"
            forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Buildable a => Format r (a -> r)
build
        )
        ApplicationNameError
err

-- | A software version is valid iff its application name is valid
checkSoftwareVersion ::
  MonadError SoftwareVersionError m => SoftwareVersion -> m ()
checkSoftwareVersion :: forall (m :: * -> *).
MonadError SoftwareVersionError m =>
SoftwareVersion -> m ()
checkSoftwareVersion SoftwareVersion
sv =
  forall (m :: * -> *).
MonadError ApplicationNameError m =>
ApplicationName -> m ()
checkApplicationName (SoftwareVersion -> ApplicationName
svAppName SoftwareVersion
sv)
    forall e' (m :: * -> *) e a.
MonadError e' m =>
Either e a -> (e -> e') -> m a
`wrapError` ApplicationNameError -> SoftwareVersionError
SoftwareVersionApplicationNameError