{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Block.SupportsDiffusionPipelining (
BlockSupportsDiffusionPipelining (..)
, updateTentativeHeaderState
, DisableDiffusionPipelining (..)
, SelectViewDiffusionPipelining (..)
, SelectViewTentativeState (..)
, BlockConfig (..)
, Header (..)
) where
import Control.Monad (guard)
import Data.Coerce
import Data.Kind
import Data.Proxy
import GHC.Generics (Generic)
import NoThunks.Class
import Ouroboros.Consensus.Block.Abstract
import Ouroboros.Consensus.Block.SupportsProtocol
import Ouroboros.Consensus.Protocol.Abstract
class
( Show (TentativeHeaderState blk)
, NoThunks (TentativeHeaderState blk)
, Show (TentativeHeaderView blk)
) => BlockSupportsDiffusionPipelining blk where
type blk :: Type
type blk :: Type
:: Proxy blk -> TentativeHeaderState blk
::
BlockConfig blk
-> Header blk
-> TentativeHeaderView blk
::
Proxy blk
-> TentativeHeaderView blk
-> TentativeHeaderState blk
-> Maybe (TentativeHeaderState blk)
updateTentativeHeaderState ::
forall blk. BlockSupportsDiffusionPipelining blk
=> BlockConfig blk
-> Header blk
-> TentativeHeaderState blk
-> Maybe (TentativeHeaderState blk)
BlockConfig blk
bcfg Header blk
hdr =
Proxy blk
-> TentativeHeaderView blk
-> TentativeHeaderState blk
-> Maybe (TentativeHeaderState blk)
forall blk.
BlockSupportsDiffusionPipelining blk =>
Proxy blk
-> TentativeHeaderView blk
-> TentativeHeaderState blk
-> Maybe (TentativeHeaderState blk)
applyTentativeHeaderView (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @blk) (BlockConfig blk -> Header blk -> TentativeHeaderView blk
forall blk.
BlockSupportsDiffusionPipelining blk =>
BlockConfig blk -> Header blk -> TentativeHeaderView blk
tentativeHeaderView BlockConfig blk
bcfg Header blk
hdr)
newtype DisableDiffusionPipelining blk = DisableDiffusionPipelining blk
newtype instance (DisableDiffusionPipelining blk) =
(Header blk)
newtype instance BlockConfig (DisableDiffusionPipelining blk) =
DisableDiffusionPipeliningBlockConfig (BlockConfig blk)
instance BlockSupportsDiffusionPipelining (DisableDiffusionPipelining blk) where
type _ = ()
type _ = ()
initialTentativeHeaderState :: Proxy (DisableDiffusionPipelining blk)
-> TentativeHeaderState (DisableDiffusionPipelining blk)
initialTentativeHeaderState Proxy (DisableDiffusionPipelining blk)
_ = ()
tentativeHeaderView :: BlockConfig (DisableDiffusionPipelining blk)
-> Header (DisableDiffusionPipelining blk)
-> TentativeHeaderView (DisableDiffusionPipelining blk)
tentativeHeaderView BlockConfig (DisableDiffusionPipelining blk)
_ Header (DisableDiffusionPipelining blk)
_ = ()
applyTentativeHeaderView :: Proxy (DisableDiffusionPipelining blk)
-> TentativeHeaderView (DisableDiffusionPipelining blk)
-> TentativeHeaderState (DisableDiffusionPipelining blk)
-> Maybe (TentativeHeaderState (DisableDiffusionPipelining blk))
applyTentativeHeaderView Proxy (DisableDiffusionPipelining blk)
_ () () = Maybe ()
Maybe (TentativeHeaderState (DisableDiffusionPipelining blk))
forall a. Maybe a
Nothing
newtype SelectViewDiffusionPipelining blk = SelectViewDiffusionPipelining blk
newtype instance (SelectViewDiffusionPipelining blk) =
(Header blk)
newtype instance BlockConfig (SelectViewDiffusionPipelining blk) =
SelectViewDiffusionPipeliningBlockConfig (BlockConfig blk)
data SelectViewTentativeState proto =
LastInvalidSelectView !(SelectView proto)
| NoLastInvalidSelectView
deriving stock ((forall x.
SelectViewTentativeState proto
-> Rep (SelectViewTentativeState proto) x)
-> (forall x.
Rep (SelectViewTentativeState proto) x
-> SelectViewTentativeState proto)
-> Generic (SelectViewTentativeState proto)
forall x.
Rep (SelectViewTentativeState proto) x
-> SelectViewTentativeState proto
forall x.
SelectViewTentativeState proto
-> Rep (SelectViewTentativeState proto) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall proto x.
Rep (SelectViewTentativeState proto) x
-> SelectViewTentativeState proto
forall proto x.
SelectViewTentativeState proto
-> Rep (SelectViewTentativeState proto) x
$cfrom :: forall proto x.
SelectViewTentativeState proto
-> Rep (SelectViewTentativeState proto) x
from :: forall x.
SelectViewTentativeState proto
-> Rep (SelectViewTentativeState proto) x
$cto :: forall proto x.
Rep (SelectViewTentativeState proto) x
-> SelectViewTentativeState proto
to :: forall x.
Rep (SelectViewTentativeState proto) x
-> SelectViewTentativeState proto
Generic)
deriving stock instance ConsensusProtocol proto => Show (SelectViewTentativeState proto)
deriving stock instance ConsensusProtocol proto => Eq (SelectViewTentativeState proto)
deriving anyclass instance ConsensusProtocol proto => NoThunks (SelectViewTentativeState proto)
instance
( BlockSupportsProtocol blk
, Show (SelectView (BlockProtocol blk))
) => BlockSupportsDiffusionPipelining (SelectViewDiffusionPipelining blk) where
type (SelectViewDiffusionPipelining blk) =
SelectViewTentativeState (BlockProtocol blk)
type (SelectViewDiffusionPipelining blk) =
SelectView (BlockProtocol blk)
initialTentativeHeaderState :: Proxy (SelectViewDiffusionPipelining blk)
-> TentativeHeaderState (SelectViewDiffusionPipelining blk)
initialTentativeHeaderState Proxy (SelectViewDiffusionPipelining blk)
_ = SelectViewTentativeState (BlockProtocol blk)
TentativeHeaderState (SelectViewDiffusionPipelining blk)
forall proto. SelectViewTentativeState proto
NoLastInvalidSelectView
tentativeHeaderView :: BlockConfig (SelectViewDiffusionPipelining blk)
-> Header (SelectViewDiffusionPipelining blk)
-> TentativeHeaderView (SelectViewDiffusionPipelining blk)
tentativeHeaderView = (BlockConfig blk -> Header blk -> SelectView (BlockProtocol blk))
-> BlockConfig (SelectViewDiffusionPipelining blk)
-> Header (SelectViewDiffusionPipelining blk)
-> SelectView (BlockProtocol blk)
forall a b. Coercible a b => a -> b
coerce BlockConfig blk -> Header blk -> SelectView (BlockProtocol blk)
forall blk.
BlockSupportsProtocol blk =>
BlockConfig blk -> Header blk -> SelectView (BlockProtocol blk)
selectView
applyTentativeHeaderView :: Proxy (SelectViewDiffusionPipelining blk)
-> TentativeHeaderView (SelectViewDiffusionPipelining blk)
-> TentativeHeaderState (SelectViewDiffusionPipelining blk)
-> Maybe (TentativeHeaderState (SelectViewDiffusionPipelining blk))
applyTentativeHeaderView Proxy (SelectViewDiffusionPipelining blk)
_ TentativeHeaderView (SelectViewDiffusionPipelining blk)
sv' TentativeHeaderState (SelectViewDiffusionPipelining blk)
st = do
case TentativeHeaderState (SelectViewDiffusionPipelining blk)
st of
SelectViewTentativeState (BlockProtocol blk)
TentativeHeaderState (SelectViewDiffusionPipelining blk)
NoLastInvalidSelectView -> () -> Maybe ()
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
LastInvalidSelectView SelectView (BlockProtocol blk)
sv -> Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ SelectView (BlockProtocol blk)
sv SelectView (BlockProtocol blk)
-> SelectView (BlockProtocol blk) -> Bool
forall a. Ord a => a -> a -> Bool
< SelectView (BlockProtocol blk)
TentativeHeaderView (SelectViewDiffusionPipelining blk)
sv'
SelectViewTentativeState (BlockProtocol blk)
-> Maybe (SelectViewTentativeState (BlockProtocol blk))
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SelectViewTentativeState (BlockProtocol blk)
-> Maybe (SelectViewTentativeState (BlockProtocol blk)))
-> SelectViewTentativeState (BlockProtocol blk)
-> Maybe (SelectViewTentativeState (BlockProtocol blk))
forall a b. (a -> b) -> a -> b
$ SelectView (BlockProtocol blk)
-> SelectViewTentativeState (BlockProtocol blk)
forall proto. SelectView proto -> SelectViewTentativeState proto
LastInvalidSelectView SelectView (BlockProtocol blk)
TentativeHeaderView (SelectViewDiffusionPipelining blk)
sv'