{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Byron.Spec.Chain.STS.Rule.BBody where

import Byron.Spec.Chain.STS.Block
import Byron.Spec.Chain.STS.Rule.Bupi
import Byron.Spec.Ledger.Core (BlockCount, Epoch, hash)
import Byron.Spec.Ledger.Delegation (
  DELEG,
  DIState,
  DSEnv (DSEnv),
  _dIStateDelegationMap,
  _dSEnvAllowedDelegators,
  _dSEnvEpoch,
  _dSEnvK,
  _dSEnvSlot,
 )
import Byron.Spec.Ledger.STS.UTXO (UTxOEnv (UTxOEnv, pps, utxo0), UTxOState)
import Byron.Spec.Ledger.STS.UTXOWS (UTXOWS)
import Byron.Spec.Ledger.UTxO (UTxO)
import Byron.Spec.Ledger.Update (PParams, UPIState, maxBkSz)
import Control.State.Transition (
  Embed,
  Environment,
  STS (..),
  Signal,
  State,
  TRC (TRC),
  initialRules,
  judgmentContext,
  trans,
  transitionRules,
  wrapFailed,
  (?!),
 )
import Data.Bimap (keys)
import Data.Data (Data, Typeable)
import Data.Set (fromList)
import Data.Word (Word8)
import Lens.Micro ((^.))

data BBODY deriving (Typeable BBODY
BBODY -> DataType
BBODY -> Constr
(forall b. Data b => b -> b) -> BBODY -> BBODY
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) -> BBODY -> u
forall u. (forall d. Data d => d -> u) -> BBODY -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BBODY
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BBODY -> c BBODY
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BBODY)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c BBODY)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> BBODY -> m BBODY
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> BBODY -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> BBODY -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> BBODY -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> BBODY -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> BBODY -> r
gmapT :: (forall b. Data b => b -> b) -> BBODY -> BBODY
$cgmapT :: (forall b. Data b => b -> b) -> BBODY -> BBODY
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c BBODY)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c BBODY)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BBODY)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BBODY)
dataTypeOf :: BBODY -> DataType
$cdataTypeOf :: BBODY -> DataType
toConstr :: BBODY -> Constr
$ctoConstr :: BBODY -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BBODY
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BBODY
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BBODY -> c BBODY
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> BBODY -> c BBODY
Data, Typeable)

-- | These `PredicateFailure`s are all throwable.
data BbodyPredicateFailure
  = InvalidBlockSize
  | InvalidUtxoHash
  | InvalidDelegationHash
  | InvalidUpdateProposalHash
  | BUPIFailure (PredicateFailure BUPI)
  | DelegationFailure (PredicateFailure DELEG)
  | UTXOWSFailure (PredicateFailure UTXOWS)
  deriving (BbodyPredicateFailure -> BbodyPredicateFailure -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BbodyPredicateFailure -> BbodyPredicateFailure -> Bool
$c/= :: BbodyPredicateFailure -> BbodyPredicateFailure -> Bool
== :: BbodyPredicateFailure -> BbodyPredicateFailure -> Bool
$c== :: BbodyPredicateFailure -> BbodyPredicateFailure -> Bool
Eq, Int -> BbodyPredicateFailure -> ShowS
[BbodyPredicateFailure] -> ShowS
BbodyPredicateFailure -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BbodyPredicateFailure] -> ShowS
$cshowList :: [BbodyPredicateFailure] -> ShowS
show :: BbodyPredicateFailure -> String
$cshow :: BbodyPredicateFailure -> String
showsPrec :: Int -> BbodyPredicateFailure -> ShowS
$cshowsPrec :: Int -> BbodyPredicateFailure -> ShowS
Show, Typeable BbodyPredicateFailure
BbodyPredicateFailure -> DataType
BbodyPredicateFailure -> Constr
(forall b. Data b => b -> b)
-> BbodyPredicateFailure -> BbodyPredicateFailure
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) -> BbodyPredicateFailure -> u
forall u.
(forall d. Data d => d -> u) -> BbodyPredicateFailure -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BbodyPredicateFailure
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BbodyPredicateFailure
-> c BbodyPredicateFailure
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BbodyPredicateFailure)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BbodyPredicateFailure)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BbodyPredicateFailure -> m BbodyPredicateFailure
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BbodyPredicateFailure -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> BbodyPredicateFailure -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> BbodyPredicateFailure -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> BbodyPredicateFailure -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> BbodyPredicateFailure -> r
gmapT :: (forall b. Data b => b -> b)
-> BbodyPredicateFailure -> BbodyPredicateFailure
$cgmapT :: (forall b. Data b => b -> b)
-> BbodyPredicateFailure -> BbodyPredicateFailure
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BbodyPredicateFailure)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BbodyPredicateFailure)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BbodyPredicateFailure)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c BbodyPredicateFailure)
dataTypeOf :: BbodyPredicateFailure -> DataType
$cdataTypeOf :: BbodyPredicateFailure -> DataType
toConstr :: BbodyPredicateFailure -> Constr
$ctoConstr :: BbodyPredicateFailure -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BbodyPredicateFailure
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c BbodyPredicateFailure
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BbodyPredicateFailure
-> c BbodyPredicateFailure
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BbodyPredicateFailure
-> c BbodyPredicateFailure
Data, Typeable)

instance STS BBODY where
  type
    Environment BBODY =
      ( PParams
      , Epoch
      , UTxO
      , Word8
      , BlockCount -- Chain stability parameter
      )

  type
    State BBODY =
      ( UTxOState
      , DIState
      , UPIState
      )

  type Signal BBODY = Block

  type PredicateFailure BBODY = BbodyPredicateFailure

  initialRules :: [InitialRule BBODY]
initialRules = []

  transitionRules :: [TransitionRule BBODY]
transitionRules =
    [ do
        TRC ((PParams
ppsVal, Epoch
e_n, UTxO
utxoGenesis, Word8
ngk, BlockCount
k), (UTxOState
utxoSt, DIState
ds, UPIState
us), Signal BBODY
b) <- forall sts (rtype :: RuleType).
Rule sts rtype (RuleContext rtype sts)
judgmentContext
        let bMax :: Natural
bMax = PParams
ppsVal forall s a. s -> Getting a s a -> a
^. Lens' PParams Natural
maxBkSz
        Block -> Natural
bSize Signal BBODY
b forall a. Ord a => a -> a -> Bool
<= Natural
bMax forall sts (ctx :: RuleType).
Bool -> PredicateFailure sts -> Rule sts ctx ()
?! BbodyPredicateFailure
InvalidBlockSize
        let bh :: BlockHeader
bh = Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockHeader
bHeader
        forall a. HasHash a => a -> Hash
hash (Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody [Tx]
bUtxo) forall a. Eq a => a -> a -> Bool
== BlockHeader
bh forall s a. s -> Getting a s a -> a
^. Lens' BlockHeader Hash
bhUtxoHash forall sts (ctx :: RuleType).
Bool -> PredicateFailure sts -> Rule sts ctx ()
?! BbodyPredicateFailure
InvalidUtxoHash
        forall a. HasHash a => a -> Hash
hash (Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody [DCert]
bDCerts) forall a. Eq a => a -> a -> Bool
== BlockHeader
bh forall s a. s -> Getting a s a -> a
^. Lens' BlockHeader Hash
bhDlgHash forall sts (ctx :: RuleType).
Bool -> PredicateFailure sts -> Rule sts ctx ()
?! BbodyPredicateFailure
InvalidDelegationHash
        forall a. HasHash a => a -> Hash
hash (Block -> (Maybe UProp, [Vote])
bUpdPayload Signal BBODY
b) forall a. Eq a => a -> a -> Bool
== BlockHeader
bh forall s a. s -> Getting a s a -> a
^. Lens' BlockHeader Hash
bhUpdHash forall sts (ctx :: RuleType).
Bool -> PredicateFailure sts -> Rule sts ctx ()
?! BbodyPredicateFailure
InvalidUpdateProposalHash

        UPIState
us' <-
          forall sub super (rtype :: RuleType).
Embed sub super =>
RuleContext rtype sub -> Rule super rtype (State sub)
trans @BUPI forall a b. (a -> b) -> a -> b
$
            forall sts. (Environment sts, State sts, Signal sts) -> TRC sts
TRC
              ( (BlockHeader
bh forall s a. s -> Getting a s a -> a
^. Lens' BlockHeader Slot
bhSlot, DIState -> Bimap VKeyGenesis VKey
_dIStateDelegationMap DIState
ds, BlockCount
k, Word8
ngk)
              , UPIState
us
              , (Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody (Maybe UProp)
bUpdProp, Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody [Vote]
bUpdVotes, Block -> (ProtVer, VKey)
bEndorsment Signal BBODY
b)
              )
        DIState
ds' <-
          forall sub super (rtype :: RuleType).
Embed sub super =>
RuleContext rtype sub -> Rule super rtype (State sub)
trans @DELEG forall a b. (a -> b) -> a -> b
$
            forall sts. (Environment sts, State sts, Signal sts) -> TRC sts
TRC
              ( ( DSEnv
                    { _dSEnvAllowedDelegators :: Set VKeyGenesis
_dSEnvAllowedDelegators =
                        (forall a. Ord a => [a] -> Set a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Bimap a b -> [a]
keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. DIState -> Bimap VKeyGenesis VKey
_dIStateDelegationMap) DIState
ds
                    , _dSEnvEpoch :: Epoch
_dSEnvEpoch = Epoch
e_n
                    , _dSEnvSlot :: Slot
_dSEnvSlot = BlockHeader
bh forall s a. s -> Getting a s a -> a
^. Lens' BlockHeader Slot
bhSlot
                    , _dSEnvK :: BlockCount
_dSEnvK = BlockCount
k
                    }
                )
              , DIState
ds
              , Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody [DCert]
bDCerts
              )
        UTxOState
utxoSt' <-
          forall sub super (rtype :: RuleType).
Embed sub super =>
RuleContext rtype sub -> Rule super rtype (State sub)
trans @UTXOWS forall a b. (a -> b) -> a -> b
$
            forall sts. (Environment sts, State sts, Signal sts) -> TRC sts
TRC
              (UTxOEnv {utxo0 :: UTxO
utxo0 = UTxO
utxoGenesis, pps :: PParams
pps = PParams
ppsVal}, UTxOState
utxoSt, Signal BBODY
b forall s a. s -> Getting a s a -> a
^. Lens' Block BlockBody
bBody forall s a. s -> Getting a s a -> a
^. Lens' BlockBody [Tx]
bUtxo)

        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (UTxOState
utxoSt', DIState
ds', UPIState
us')
    ]

instance Embed BUPI BBODY where
  wrapFailed :: PredicateFailure BUPI -> PredicateFailure BBODY
wrapFailed = PredicateFailure BUPI -> BbodyPredicateFailure
BUPIFailure

instance Embed DELEG BBODY where
  wrapFailed :: PredicateFailure DELEG -> PredicateFailure BBODY
wrapFailed = PredicateFailure DELEG -> BbodyPredicateFailure
DelegationFailure

instance Embed UTXOWS BBODY where
  wrapFailed :: PredicateFailure UTXOWS -> PredicateFailure BBODY
wrapFailed = PredicateFailure UTXOWS -> BbodyPredicateFailure
UTXOWSFailure