{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module Ouroboros.Network.InboundGovernor.Test.Utils where
import Test.QuickCheck
import Test.QuickCheck.Monoids
import Ouroboros.Network.ConnectionManager.Types
import Ouroboros.Network.InboundGovernor (RemoteSt (..))
import Ouroboros.Network.InboundGovernor qualified as IG
import Ouroboros.Network.Server2 (RemoteTransition)
import Ouroboros.Network.Server2 qualified as Server
pattern RemoteEstSt :: RemoteSt
pattern $mRemoteEstSt :: forall {r}. RemoteSt -> ((# #) -> r) -> ((# #) -> r) -> r
RemoteEstSt <- (\ case
RemoteSt
RemoteHotSt -> Bool
True
RemoteSt
RemoteWarmSt -> Bool
True
RemoteSt
_ -> Bool
False -> True
)
{-# COMPLETE RemoteEstSt, RemoteIdleSt, RemoteColdSt #-}
verifyRemoteTransition :: RemoteTransition -> Bool
verifyRemoteTransition :: RemoteTransition -> Bool
verifyRemoteTransition Transition {Maybe RemoteSt
fromState :: Maybe RemoteSt
fromState :: forall state. Transition' state -> state
fromState, Maybe RemoteSt
toState :: Maybe RemoteSt
toState :: forall state. Transition' state -> state
toState} =
case (Maybe RemoteSt
fromState, Maybe RemoteSt
toState) of
(Maybe RemoteSt
Nothing, Just RemoteSt
RemoteIdleSt) -> Bool
True
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteEstSt) -> Bool
True
(Just RemoteSt
RemoteColdSt, Just RemoteSt
RemoteEstSt) -> Bool
True
(Just RemoteSt
RemoteWarmSt, Just RemoteSt
RemoteHotSt) -> Bool
True
(Just RemoteSt
RemoteHotSt, Just RemoteSt
RemoteWarmSt) -> Bool
True
(Just RemoteSt
RemoteEstSt, Just RemoteSt
RemoteIdleSt) -> Bool
True
(Just RemoteSt
RemoteEstSt, Just RemoteSt
RemoteColdSt) -> Bool
False
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteColdSt) -> Bool
True
(Just RemoteSt
RemoteIdleSt, Maybe RemoteSt
Nothing) -> Bool
True
(Just RemoteSt
RemoteColdSt, Maybe RemoteSt
Nothing) -> Bool
True
(Just RemoteSt
RemoteEstSt, Maybe RemoteSt
Nothing) -> Bool
True
(Maybe RemoteSt
Nothing, Maybe RemoteSt
Nothing) -> Bool
True
(Just RemoteSt
RemoteWarmSt, Just RemoteSt
RemoteWarmSt) -> Bool
True
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteIdleSt) -> Bool
True
(Just RemoteSt
RemoteColdSt, Just RemoteSt
RemoteColdSt) -> Bool
True
(Maybe RemoteSt
_, Maybe RemoteSt
_) -> Bool
False
validRemoteTransitionMap :: RemoteTransition -> (Int, String)
validRemoteTransitionMap :: RemoteTransition -> (Int, String)
validRemoteTransitionMap t :: RemoteTransition
t@Transition { Maybe RemoteSt
fromState :: forall state. Transition' state -> state
fromState :: Maybe RemoteSt
fromState, Maybe RemoteSt
toState :: forall state. Transition' state -> state
toState :: Maybe RemoteSt
toState } =
case (Maybe RemoteSt
fromState, Maybe RemoteSt
toState) of
(Maybe RemoteSt
Nothing , Just RemoteSt
RemoteIdleSt) -> (Int
00, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteEstSt) -> (Int
01, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteColdSt, Just RemoteSt
RemoteEstSt) -> (Int
02, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteWarmSt, Just RemoteSt
RemoteHotSt) -> (Int
03, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteHotSt , Just RemoteSt
RemoteWarmSt) -> (Int
04, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteEstSt , Just RemoteSt
RemoteIdleSt) -> (Int
05, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteColdSt) -> (Int
06, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteIdleSt, Maybe RemoteSt
Nothing) -> (Int
07, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteColdSt, Maybe RemoteSt
Nothing) -> (Int
08, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteEstSt , Maybe RemoteSt
Nothing) -> (Int
09, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Maybe RemoteSt
Nothing , Maybe RemoteSt
Nothing) -> (Int
10, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteWarmSt, Just RemoteSt
RemoteWarmSt) -> (Int
11, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteIdleSt, Just RemoteSt
RemoteIdleSt) -> (Int
12, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Just RemoteSt
RemoteColdSt, Just RemoteSt
RemoteColdSt) -> (Int
13, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
(Maybe RemoteSt
_ , Maybe RemoteSt
_) -> (Int
99, RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
t)
allValidRemoteTransitionsNames :: [String]
allValidRemoteTransitionsNames :: [String]
allValidRemoteTransitionsNames =
(RemoteTransition -> String) -> [RemoteTransition] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map RemoteTransition -> String
forall a. Show a => a -> String
show
[ Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition Maybe RemoteSt
forall a. Maybe a
Nothing (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteIdleSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteIdleSt) (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteWarmSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteWarmSt) (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteHotSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteHotSt ) (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteWarmSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteWarmSt) (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteIdleSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteIdleSt) (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteColdSt)
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteIdleSt) Maybe RemoteSt
forall a. Maybe a
Nothing
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteColdSt) Maybe RemoteSt
forall a. Maybe a
Nothing
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteWarmSt) Maybe RemoteSt
forall a. Maybe a
Nothing
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition (RemoteSt -> Maybe RemoteSt
forall a. a -> Maybe a
Just RemoteSt
RemoteHotSt) Maybe RemoteSt
forall a. Maybe a
Nothing
, Maybe RemoteSt -> Maybe RemoteSt -> RemoteTransition
forall state. state -> state -> Transition' state
Transition Maybe RemoteSt
forall a. Maybe a
Nothing Maybe RemoteSt
forall a. Maybe a
Nothing
]
verifyRemoteTransitionOrder :: Bool
-> [RemoteTransition]
-> All
verifyRemoteTransitionOrder :: Bool -> [RemoteTransition] -> All
verifyRemoteTransitionOrder Bool
_ [] = All
forall a. Monoid a => a
mempty
verifyRemoteTransitionOrder Bool
checkLast (RemoteTransition
h:[RemoteTransition]
t) = [RemoteTransition] -> RemoteTransition -> All
go [RemoteTransition]
t RemoteTransition
h
where
go :: [RemoteTransition] -> RemoteTransition -> All
go :: [RemoteTransition] -> RemoteTransition -> All
go [] (Transition Maybe RemoteSt
_ Maybe RemoteSt
Nothing) = All
forall a. Monoid a => a
mempty
go [] tr :: RemoteTransition
tr@(Transition Maybe RemoteSt
_ Maybe RemoteSt
_) =
Property -> All
forall p. Testable p => p -> All
All
(Property -> All) -> Property -> All
forall a b. (a -> b) -> a -> b
$ String -> Property -> Property
forall prop. Testable prop => String -> prop -> Property
counterexample
(String
"\nUnexpected last transition: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
tr)
(Bool -> Property
forall prop. Testable prop => prop -> Property
property (Bool -> Bool
not Bool
checkLast))
go (next :: RemoteTransition
next@(Transition Maybe RemoteSt
nextFromState Maybe RemoteSt
_) : [RemoteTransition]
ts)
curr :: RemoteTransition
curr@(Transition Maybe RemoteSt
_ Maybe RemoteSt
currToState) =
Property -> All
forall p. Testable p => p -> All
All
(String -> Property -> Property
forall prop. Testable prop => String -> prop -> Property
counterexample
(String
"\nUnexpected transition order!\nWent from: "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
curr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nto: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ RemoteTransition -> String
forall a. Show a => a -> String
show RemoteTransition
next)
(Bool -> Property
forall prop. Testable prop => prop -> Property
property (Maybe RemoteSt
currToState Maybe RemoteSt -> Maybe RemoteSt -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe RemoteSt
nextFromState)))
All -> All -> All
forall a. Semigroup a => a -> a -> a
<> [RemoteTransition] -> RemoteTransition -> All
go [RemoteTransition]
ts RemoteTransition
next
remoteStrIsFinalTransition :: Transition' (Maybe RemoteSt) -> Bool
remoteStrIsFinalTransition :: RemoteTransition -> Bool
remoteStrIsFinalTransition (Transition Maybe RemoteSt
_ Maybe RemoteSt
Nothing) = Bool
True
remoteStrIsFinalTransition RemoteTransition
_ = Bool
False
inboundGovernorTraceMap :: IG.Trace ntnAddr -> String
inboundGovernorTraceMap :: forall ntnAddr. Trace ntnAddr -> String
inboundGovernorTraceMap (IG.TrNewConnection Provenance
p ConnectionId ntnAddr
_) =
String
"TrNewConnection " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
inboundGovernorTraceMap (IG.TrResponderRestarted ConnectionId ntnAddr
_ MiniProtocolNum
mpn) =
String
"TrResponderRestarted " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MiniProtocolNum -> String
forall a. Show a => a -> String
show MiniProtocolNum
mpn
inboundGovernorTraceMap (IG.TrResponderStartFailure ConnectionId ntnAddr
_ MiniProtocolNum
mpn SomeException
se) =
String
"TrResponderStartFailure " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MiniProtocolNum -> String
forall a. Show a => a -> String
show MiniProtocolNum
mpn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SomeException -> String
forall a. Show a => a -> String
show SomeException
se
inboundGovernorTraceMap (IG.TrResponderErrored ConnectionId ntnAddr
_ MiniProtocolNum
mpn SomeException
se) =
String
"TrResponderErrored " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MiniProtocolNum -> String
forall a. Show a => a -> String
show MiniProtocolNum
mpn String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SomeException -> String
forall a. Show a => a -> String
show SomeException
se
inboundGovernorTraceMap (IG.TrResponderStarted ConnectionId ntnAddr
_ MiniProtocolNum
mpn) =
String
"TrResponderStarted " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MiniProtocolNum -> String
forall a. Show a => a -> String
show MiniProtocolNum
mpn
inboundGovernorTraceMap (IG.TrResponderTerminated ConnectionId ntnAddr
_ MiniProtocolNum
mpn) =
String
"TrResponderTerminated " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MiniProtocolNum -> String
forall a. Show a => a -> String
show MiniProtocolNum
mpn
inboundGovernorTraceMap (IG.TrPromotedToWarmRemote ConnectionId ntnAddr
_ OperationResult AbstractState
ora) =
String
"TrPromotedToWarmRemote " String -> String -> String
forall a. [a] -> [a] -> [a]
++ OperationResult AbstractState -> String
forall a. Show a => a -> String
show OperationResult AbstractState
ora
inboundGovernorTraceMap (IG.TrPromotedToHotRemote ConnectionId ntnAddr
_) =
String
"TrPromotedToHotRemote"
inboundGovernorTraceMap (IG.TrDemotedToWarmRemote ConnectionId ntnAddr
_) =
String
"TrDemotedToWarmRemote"
inboundGovernorTraceMap (IG.TrDemotedToColdRemote ConnectionId ntnAddr
_ OperationResult DemotedToColdRemoteTr
ora) =
String
"TrDemotedToColdRemote " String -> String -> String
forall a. [a] -> [a] -> [a]
++ OperationResult DemotedToColdRemoteTr -> String
forall a. Show a => a -> String
show OperationResult DemotedToColdRemoteTr
ora
inboundGovernorTraceMap (IG.TrWaitIdleRemote ConnectionId ntnAddr
_ OperationResult AbstractState
ora) =
String
"TrWaitIdleRemote " String -> String -> String
forall a. [a] -> [a] -> [a]
++ OperationResult AbstractState -> String
forall a. Show a => a -> String
show OperationResult AbstractState
ora
inboundGovernorTraceMap (IG.TrMuxCleanExit ConnectionId ntnAddr
_) =
String
"TrMuxCleanExit"
inboundGovernorTraceMap (IG.TrMuxErrored ConnectionId ntnAddr
_ SomeException
se) =
String
"TrMuxErrored " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SomeException -> String
forall a. Show a => a -> String
show SomeException
se
inboundGovernorTraceMap (IG.TrInboundGovernorCounters Counters
_) =
String
"TrInboundGovernorCounters"
inboundGovernorTraceMap (IG.TrRemoteState Map (ConnectionId ntnAddr) RemoteSt
_) =
String
"TrRemoteState"
inboundGovernorTraceMap (IG.TrUnexpectedlyFalseAssertion IGAssertionLocation ntnAddr
_) =
String
"TrUnexpectedlyFalseAssertion"
inboundGovernorTraceMap (IG.TrInboundGovernorError SomeException
se) =
String
"TrInboundGovernorError " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SomeException -> String
forall a. Show a => a -> String
show SomeException
se
inboundGovernorTraceMap IG.TrMaturedConnections {} =
String
"TrMaturedConnections"
inboundGovernorTraceMap IG.TrInactive {} =
String
"TrMaturedConnections"
serverTraceMap :: Show ntnAddr => Server.Trace ntnAddr -> String
serverTraceMap :: forall ntnAddr. Show ntnAddr => Trace ntnAddr -> String
serverTraceMap (Server.TrAcceptConnection ConnectionId ntnAddr
_) = String
"TrAcceptConnection"
serverTraceMap st :: Trace ntnAddr
st@(Server.TrAcceptError SomeException
_) = Trace ntnAddr -> String
forall a. Show a => a -> String
show Trace ntnAddr
st
serverTraceMap st :: Trace ntnAddr
st@(Server.TrAcceptPolicyTrace AcceptConnectionsPolicyTrace
_) = Trace ntnAddr -> String
forall a. Show a => a -> String
show Trace ntnAddr
st
serverTraceMap (Server.TrServerStarted [ntnAddr]
_) = String
"TrServerStarted"
serverTraceMap st :: Trace ntnAddr
st@Trace ntnAddr
Server.TrServerStopped = Trace ntnAddr -> String
forall a. Show a => a -> String
show Trace ntnAddr
st
serverTraceMap st :: Trace ntnAddr
st@(Server.TrServerError SomeException
_) = Trace ntnAddr -> String
forall a. Show a => a -> String
show Trace ntnAddr
st