{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE EmptyDataDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Protocol.TPraos.Rules.Updn (
  UPDN,
  UpdnEnv (..),
  UpdnState (..),
  PredicateFailure,
  UpdnPredicateFailure,
)
where

import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Crypto
import Cardano.Ledger.Slot
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))

data UPDN c

newtype UpdnEnv
  = -- | New nonce
    UpdnEnv
      Nonce

data UpdnState = UpdnState Nonce Nonce
  deriving (Int -> UpdnState -> ShowS
[UpdnState] -> ShowS
UpdnState -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpdnState] -> ShowS
$cshowList :: [UpdnState] -> ShowS
show :: UpdnState -> String
$cshow :: UpdnState -> String
showsPrec :: Int -> UpdnState -> ShowS
$cshowsPrec :: Int -> UpdnState -> ShowS
Show, UpdnState -> UpdnState -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UpdnState -> UpdnState -> Bool
$c/= :: UpdnState -> UpdnState -> Bool
== :: UpdnState -> UpdnState -> Bool
$c== :: UpdnState -> UpdnState -> Bool
Eq)

data UpdnPredicateFailure c -- No predicate failures
  deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x.
Rep (UpdnPredicateFailure c) x -> UpdnPredicateFailure c
forall c x.
UpdnPredicateFailure c -> Rep (UpdnPredicateFailure c) x
$cto :: forall c x.
Rep (UpdnPredicateFailure c) x -> UpdnPredicateFailure c
$cfrom :: forall c x.
UpdnPredicateFailure c -> Rep (UpdnPredicateFailure c) x
Generic, Int -> UpdnPredicateFailure c -> ShowS
forall c. Int -> UpdnPredicateFailure c -> ShowS
forall c. [UpdnPredicateFailure c] -> ShowS
forall c. UpdnPredicateFailure c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpdnPredicateFailure c] -> ShowS
$cshowList :: forall c. [UpdnPredicateFailure c] -> ShowS
show :: UpdnPredicateFailure c -> String
$cshow :: forall c. UpdnPredicateFailure c -> String
showsPrec :: Int -> UpdnPredicateFailure c -> ShowS
$cshowsPrec :: forall c. Int -> UpdnPredicateFailure c -> ShowS
Show, UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
forall c. UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
$c/= :: forall c. UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
== :: UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
$c== :: forall c. UpdnPredicateFailure c -> UpdnPredicateFailure c -> Bool
Eq)

instance NoThunks (UpdnPredicateFailure c)

newtype UpdnEvent c = NewEpoch EpochNo

instance
  Crypto c =>
  STS (UPDN c)
  where
  type State (UPDN c) = UpdnState
  type Signal (UPDN c) = SlotNo
  type Environment (UPDN c) = UpdnEnv
  type BaseM (UPDN c) = ShelleyBase
  type PredicateFailure (UPDN c) = UpdnPredicateFailure c
  type Event (UPDN c) = UpdnEvent c
  initialRules :: [InitialRule (UPDN c)]
initialRules =
    [ forall (f :: * -> *) a. Applicative f => a -> f a
pure
        ( Nonce -> Nonce -> UpdnState
UpdnState
            Nonce
initialNonce
            Nonce
initialNonce
        )
    ]
    where
      initialNonce :: Nonce
initialNonce = Word64 -> Nonce
mkNonceFromNumber Word64
0
  transitionRules :: [TransitionRule (UPDN c)]
transitionRules = [forall c. Crypto c => TransitionRule (UPDN c)
updTransition]

updTransition :: Crypto c => TransitionRule (UPDN c)
updTransition :: forall c. Crypto c => TransitionRule (UPDN c)
updTransition = do
  TRC (UpdnEnv Nonce
eta, UpdnState Nonce
eta_v Nonce
eta_c, Signal (UPDN c)
s) <- forall sts (rtype :: RuleType).
Rule sts rtype (RuleContext rtype sts)
judgmentContext
  EpochInfo Identity
ei <- forall sts a (ctx :: RuleType).
STS sts =>
BaseM sts a -> Rule sts ctx a
liftSTS forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks Globals -> EpochInfo Identity
epochInfoPure
  Word64
sp <- forall sts a (ctx :: RuleType).
STS sts =>
BaseM sts a -> Rule sts ctx a
liftSTS forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks Globals -> Word64
stabilityWindow
  EpochNo Word64
e <- forall sts a (ctx :: RuleType).
STS sts =>
BaseM sts a -> Rule sts ctx a
liftSTS forall a b. (a -> b) -> a -> b
$ HasCallStack => EpochInfo Identity -> SlotNo -> ShelleyBase EpochNo
epochInfoEpoch EpochInfo Identity
ei Signal (UPDN c)
s
  let newEpochNo :: EpochNo
newEpochNo = Word64 -> EpochNo
EpochNo (Word64
e forall a. Num a => a -> a -> a
+ Word64
1)
  SlotNo
firstSlotNextEpoch <- forall sts a (ctx :: RuleType).
STS sts =>
BaseM sts a -> Rule sts ctx a
liftSTS forall a b. (a -> b) -> a -> b
$ HasCallStack => EpochInfo Identity -> EpochNo -> ShelleyBase SlotNo
epochInfoFirst EpochInfo Identity
ei EpochNo
newEpochNo
  forall sts (ctx :: RuleType). Event sts -> Rule sts ctx ()
tellEvent forall a b. (a -> b) -> a -> b
$ forall c. EpochNo -> UpdnEvent c
NewEpoch EpochNo
newEpochNo
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
    Nonce -> Nonce -> UpdnState
UpdnState
      (Nonce
eta_v Nonce -> Nonce -> Nonce
 Nonce
eta)
      ( if Signal (UPDN c)
s SlotNo -> Duration -> SlotNo
+* Word64 -> Duration
Duration Word64
sp forall a. Ord a => a -> a -> Bool
< SlotNo
firstSlotNextEpoch
          then Nonce
eta_v Nonce -> Nonce -> Nonce
 Nonce
eta
          else Nonce
eta_c
      )