{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module Ouroboros.Network.Protocol.ChainSync.Type where
import Data.Singletons
import Network.TypedProtocol.Core
import Control.DeepSeq
import Ouroboros.Network.Util.ShowProxy (ShowProxy (..))
data ChainSync header point tip where
StIdle :: ChainSync header point tip
StNext :: StNextKind -> ChainSync header point tip
StIntersect :: ChainSync header point tip
StDone :: ChainSync header point tip
instance (ShowProxy header, ShowProxy tip)
=> ShowProxy (ChainSync header point tip) where
showProxy :: Proxy (ChainSync header point tip) -> String
showProxy Proxy (ChainSync header point tip)
_ = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ String
"ChainSync ("
, Proxy header -> String
forall {k} (p :: k). ShowProxy p => Proxy p -> String
showProxy (Proxy header
forall {k} (t :: k). Proxy t
Proxy :: Proxy header)
, String
") ("
, Proxy tip -> String
forall {k} (p :: k). ShowProxy p => Proxy p -> String
showProxy (Proxy tip
forall {k} (t :: k). Proxy t
Proxy :: Proxy tip)
, String
")"
]
data SingChainSync (k :: ChainSync header point tip) where
SingIdle :: SingChainSync StIdle
SingNext :: SingNextKind k
-> SingChainSync (StNext k)
SingIntersect :: SingChainSync StIntersect
SingDone :: SingChainSync StDone
deriving instance Show (SingChainSync k)
instance StateTokenI StIdle where stateToken :: StateToken 'StIdle
stateToken = StateToken 'StIdle
SingChainSync 'StIdle
forall {k} {k} {k} {header :: k} {point :: k} {tip :: k}.
SingChainSync 'StIdle
SingIdle
instance SingI k =>
StateTokenI (StNext k) where stateToken :: StateToken ('StNext k)
stateToken = SingNextKind k -> SingChainSync ('StNext k)
forall {k} {k} {k} {header :: k} {point :: k} {tip :: k}
(k :: StNextKind).
SingNextKind k -> SingChainSync ('StNext k)
SingNext Sing k
SingNextKind k
forall {k} (a :: k). SingI a => Sing a
sing
instance StateTokenI StIntersect where stateToken :: StateToken 'StIntersect
stateToken = StateToken 'StIntersect
SingChainSync 'StIntersect
forall {k} {k} {k} {header :: k} {point :: k} {tip :: k}.
SingChainSync 'StIntersect
SingIntersect
instance StateTokenI StDone where stateToken :: StateToken 'StDone
stateToken = StateToken 'StDone
SingChainSync 'StDone
forall {k} {k} {k} {header :: k} {point :: k} {tip :: k}.
SingChainSync 'StDone
SingDone
data StNextKind where
StCanAwait :: StNextKind
StMustReply :: StNextKind
data SingNextKind (k :: StNextKind) where
SingCanAwait :: SingNextKind StCanAwait
SingMustReply :: SingNextKind StMustReply
deriving instance Show (SingNextKind k)
type instance Sing = SingNextKind
instance SingI StCanAwait where sing :: Sing 'StCanAwait
sing = Sing 'StCanAwait
SingNextKind 'StCanAwait
SingCanAwait
instance SingI StMustReply where sing :: Sing 'StMustReply
sing = Sing 'StMustReply
SingNextKind 'StMustReply
SingMustReply
instance Protocol (ChainSync header point tip) where
data Message (ChainSync header point tip) from to where
MsgRequestNext :: Message (ChainSync header point tip)
StIdle (StNext StCanAwait)
MsgAwaitReply :: Message (ChainSync header point tip)
(StNext StCanAwait) (StNext StMustReply)
MsgRollForward :: header -> tip
-> Message (ChainSync header point tip)
(StNext any) StIdle
MsgRollBackward :: point -> tip
-> Message (ChainSync header point tip)
(StNext any) StIdle
MsgFindIntersect :: [point]
-> Message (ChainSync header point tip)
StIdle StIntersect
MsgIntersectFound :: point -> tip
-> Message (ChainSync header point tip)
StIntersect StIdle
MsgIntersectNotFound :: tip
-> Message (ChainSync header point tip)
StIntersect StIdle
MsgDone :: Message (ChainSync header point tip)
StIdle StDone
type StateAgency StIdle = ClientAgency
type StateAgency (StNext _) = ServerAgency
type StateAgency StIntersect = ServerAgency
type StateAgency StDone = NobodyAgency
type StateToken = SingChainSync
instance ( NFData header
, NFData point
, NFData tip
) => NFData (Message (ChainSync header point tip) from to) where
rnf :: Message (ChainSync header point tip) from to -> ()
rnf Message (ChainSync header point tip) from to
R:MessageChainSyncfromto (*) (*) (*) header point tip from to
MsgRequestNext = ()
rnf Message (ChainSync header point tip) from to
R:MessageChainSyncfromto (*) (*) (*) header point tip from to
MsgAwaitReply = ()
rnf (MsgRollForward header
h tip
t) = header -> ()
forall a. NFData a => a -> ()
rnf header
h () -> () -> ()
forall a b. a -> b -> b
`seq` tip -> ()
forall a. NFData a => a -> ()
rnf tip
t
rnf (MsgRollBackward point
p tip
t) = point -> ()
forall a. NFData a => a -> ()
rnf point
p () -> () -> ()
forall a b. a -> b -> b
`seq` tip -> ()
forall a. NFData a => a -> ()
rnf tip
t
rnf (MsgFindIntersect [point]
ps) = [point] -> ()
forall a. NFData a => a -> ()
rnf [point]
ps
rnf (MsgIntersectFound point
p tip
t) = point -> ()
forall a. NFData a => a -> ()
rnf point
p () -> () -> ()
forall a b. a -> b -> b
`seq` tip -> ()
forall a. NFData a => a -> ()
rnf tip
t
rnf (MsgIntersectNotFound tip
t) = tip -> ()
forall a. NFData a => a -> ()
rnf tip
t
rnf Message (ChainSync header point tip) from to
R:MessageChainSyncfromto (*) (*) (*) header point tip from to
MsgDone = ()
data TokNextKind (k :: StNextKind) where
TokCanAwait :: TokNextKind StCanAwait
TokMustReply :: TokNextKind StMustReply
instance NFData (TokNextKind k) where
rnf :: TokNextKind k -> ()
rnf TokNextKind k
TokCanAwait = ()
rnf TokNextKind k
TokMustReply = ()
instance (Show header, Show point, Show tip)
=> Show (Message (ChainSync header point tip) from to) where
show :: Message (ChainSync header point tip) from to -> String
show Message (ChainSync header point tip) from to
R:MessageChainSyncfromto (*) (*) (*) header point tip from to
MsgRequestNext = String
"MsgRequestNext"
show Message (ChainSync header point tip) from to
R:MessageChainSyncfromto (*) (*) (*) header point tip from to
MsgAwaitReply = String
"MsgAwaitReply"
show (MsgRollForward header
h tip
tip) = String
"MsgRollForward "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ header -> String
forall a. Show a => a -> String
show header
h
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ tip -> String
forall a. Show a => a -> String
show tip
tip
show (MsgRollBackward point
p tip
tip) = String
"MsgRollBackward "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ point -> String
forall a. Show a => a -> String
show point
p
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ tip -> String
forall a. Show a => a -> String
show tip
tip
show (MsgFindIntersect [point]
ps) = String
"MsgFindIntersect " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [point] -> String
forall a. Show a => a -> String
show [point]
ps
show (MsgIntersectFound point
p tip
tip) = String
"MsgIntersectFound "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ point -> String
forall a. Show a => a -> String
show point
p
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ tip -> String
forall a. Show a => a -> String
show tip
tip
show (MsgIntersectNotFound tip
tip) = String
"MsgIntersectNotFound "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ tip -> String
forall a. Show a => a -> String
show tip
tip
show MsgDone{} = String
"MsgDone"