module Hydra.Node.UnsyncedPeriod where

import Hydra.Prelude

import Hydra.Tx.ContestationPeriod (ContestationPeriod, toNominalDiffTime)

-- | Period of time after which we consider the node becoming unsynced with the chain.
-- Beyond this period the node will refuse to process new transactions and signing snapshots.
newtype UnsyncedPeriod = UnsyncedPeriod {UnsyncedPeriod -> NominalDiffTime
unsyncedPeriodToNominalDiffTime :: NominalDiffTime}
  deriving stock (UnsyncedPeriod -> UnsyncedPeriod -> Bool
(UnsyncedPeriod -> UnsyncedPeriod -> Bool)
-> (UnsyncedPeriod -> UnsyncedPeriod -> Bool) -> Eq UnsyncedPeriod
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
== :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
$c/= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
/= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
Eq, Eq UnsyncedPeriod
Eq UnsyncedPeriod =>
(UnsyncedPeriod -> UnsyncedPeriod -> Ordering)
-> (UnsyncedPeriod -> UnsyncedPeriod -> Bool)
-> (UnsyncedPeriod -> UnsyncedPeriod -> Bool)
-> (UnsyncedPeriod -> UnsyncedPeriod -> Bool)
-> (UnsyncedPeriod -> UnsyncedPeriod -> Bool)
-> (UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod)
-> Ord UnsyncedPeriod
UnsyncedPeriod -> UnsyncedPeriod -> Bool
UnsyncedPeriod -> UnsyncedPeriod -> Ordering
UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
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
$ccompare :: UnsyncedPeriod -> UnsyncedPeriod -> Ordering
compare :: UnsyncedPeriod -> UnsyncedPeriod -> Ordering
$c< :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
< :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
$c<= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
<= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
$c> :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
> :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
$c>= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
>= :: UnsyncedPeriod -> UnsyncedPeriod -> Bool
$cmax :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
max :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
$cmin :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
min :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
Ord, (forall x. UnsyncedPeriod -> Rep UnsyncedPeriod x)
-> (forall x. Rep UnsyncedPeriod x -> UnsyncedPeriod)
-> Generic UnsyncedPeriod
forall x. Rep UnsyncedPeriod x -> UnsyncedPeriod
forall x. UnsyncedPeriod -> Rep UnsyncedPeriod x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UnsyncedPeriod -> Rep UnsyncedPeriod x
from :: forall x. UnsyncedPeriod -> Rep UnsyncedPeriod x
$cto :: forall x. Rep UnsyncedPeriod x -> UnsyncedPeriod
to :: forall x. Rep UnsyncedPeriod x -> UnsyncedPeriod
Generic)
  deriving newtype (Int -> UnsyncedPeriod -> ShowS
[UnsyncedPeriod] -> ShowS
UnsyncedPeriod -> String
(Int -> UnsyncedPeriod -> ShowS)
-> (UnsyncedPeriod -> String)
-> ([UnsyncedPeriod] -> ShowS)
-> Show UnsyncedPeriod
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnsyncedPeriod -> ShowS
showsPrec :: Int -> UnsyncedPeriod -> ShowS
$cshow :: UnsyncedPeriod -> String
show :: UnsyncedPeriod -> String
$cshowList :: [UnsyncedPeriod] -> ShowS
showList :: [UnsyncedPeriod] -> ShowS
Show, ReadPrec [UnsyncedPeriod]
ReadPrec UnsyncedPeriod
Int -> ReadS UnsyncedPeriod
ReadS [UnsyncedPeriod]
(Int -> ReadS UnsyncedPeriod)
-> ReadS [UnsyncedPeriod]
-> ReadPrec UnsyncedPeriod
-> ReadPrec [UnsyncedPeriod]
-> Read UnsyncedPeriod
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS UnsyncedPeriod
readsPrec :: Int -> ReadS UnsyncedPeriod
$creadList :: ReadS [UnsyncedPeriod]
readList :: ReadS [UnsyncedPeriod]
$creadPrec :: ReadPrec UnsyncedPeriod
readPrec :: ReadPrec UnsyncedPeriod
$creadListPrec :: ReadPrec [UnsyncedPeriod]
readListPrec :: ReadPrec [UnsyncedPeriod]
Read, Integer -> UnsyncedPeriod
UnsyncedPeriod -> UnsyncedPeriod
UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
(UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod)
-> (UnsyncedPeriod -> UnsyncedPeriod)
-> (Integer -> UnsyncedPeriod)
-> Num UnsyncedPeriod
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
+ :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
$c- :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
- :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
$c* :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
* :: UnsyncedPeriod -> UnsyncedPeriod -> UnsyncedPeriod
$cnegate :: UnsyncedPeriod -> UnsyncedPeriod
negate :: UnsyncedPeriod -> UnsyncedPeriod
$cabs :: UnsyncedPeriod -> UnsyncedPeriod
abs :: UnsyncedPeriod -> UnsyncedPeriod
$csignum :: UnsyncedPeriod -> UnsyncedPeriod
signum :: UnsyncedPeriod -> UnsyncedPeriod
$cfromInteger :: Integer -> UnsyncedPeriod
fromInteger :: Integer -> UnsyncedPeriod
Num, Num UnsyncedPeriod
Ord UnsyncedPeriod
(Num UnsyncedPeriod, Ord UnsyncedPeriod) =>
(UnsyncedPeriod -> Rational) -> Real UnsyncedPeriod
UnsyncedPeriod -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: UnsyncedPeriod -> Rational
toRational :: UnsyncedPeriod -> Rational
Real, [UnsyncedPeriod] -> Value
[UnsyncedPeriod] -> Encoding
UnsyncedPeriod -> Bool
UnsyncedPeriod -> Value
UnsyncedPeriod -> Encoding
(UnsyncedPeriod -> Value)
-> (UnsyncedPeriod -> Encoding)
-> ([UnsyncedPeriod] -> Value)
-> ([UnsyncedPeriod] -> Encoding)
-> (UnsyncedPeriod -> Bool)
-> ToJSON UnsyncedPeriod
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: UnsyncedPeriod -> Value
toJSON :: UnsyncedPeriod -> Value
$ctoEncoding :: UnsyncedPeriod -> Encoding
toEncoding :: UnsyncedPeriod -> Encoding
$ctoJSONList :: [UnsyncedPeriod] -> Value
toJSONList :: [UnsyncedPeriod] -> Value
$ctoEncodingList :: [UnsyncedPeriod] -> Encoding
toEncodingList :: [UnsyncedPeriod] -> Encoding
$comitField :: UnsyncedPeriod -> Bool
omitField :: UnsyncedPeriod -> Bool
ToJSON, Maybe UnsyncedPeriod
Value -> Parser [UnsyncedPeriod]
Value -> Parser UnsyncedPeriod
(Value -> Parser UnsyncedPeriod)
-> (Value -> Parser [UnsyncedPeriod])
-> Maybe UnsyncedPeriod
-> FromJSON UnsyncedPeriod
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser UnsyncedPeriod
parseJSON :: Value -> Parser UnsyncedPeriod
$cparseJSONList :: Value -> Parser [UnsyncedPeriod]
parseJSONList :: Value -> Parser [UnsyncedPeriod]
$comittedField :: Maybe UnsyncedPeriod
omittedField :: Maybe UnsyncedPeriod
FromJSON)

instance ToCBOR UnsyncedPeriod where
  toCBOR :: UnsyncedPeriod -> Encoding
toCBOR = UnsyncedPeriod -> Encoding
forall a. (Generic a, GToCBOR (Rep a)) => a -> Encoding
genericToCBOR

instance FromCBOR UnsyncedPeriod where
  fromCBOR :: forall s. Decoder s UnsyncedPeriod
fromCBOR = Decoder s UnsyncedPeriod
forall a s. (Generic a, GFromCBOR (Rep a)) => Decoder s a
genericFromCBOR

-- | Compute a default 'UnsyncedPeriod' based on the 'ContestationPeriod'.
-- This is the legacy behavior: half of the contestation period.
defaultUnsyncedPeriodFor :: ContestationPeriod -> UnsyncedPeriod
defaultUnsyncedPeriodFor :: ContestationPeriod -> UnsyncedPeriod
defaultUnsyncedPeriodFor ContestationPeriod
cp = NominalDiffTime -> UnsyncedPeriod
UnsyncedPeriod (NominalDiffTime -> UnsyncedPeriod)
-> NominalDiffTime -> UnsyncedPeriod
forall a b. (a -> b) -> a -> b
$ ContestationPeriod -> NominalDiffTime
toNominalDiffTime ContestationPeriod
cp NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* NominalDiffTime
0.5