{-# LANGUAGE NamedFieldPuns #-}

module Ouroboros.Network.ConnectionManager.Test.Utils where

import Prelude hiding (read)

import Ouroboros.Network.ConnectionHandler (ConnectionHandlerTrace)
import Ouroboros.Network.ConnectionManager.Core as CM
import Ouroboros.Network.ConnectionManager.Types

import Test.QuickCheck (counterexample, property)
import Test.QuickCheck.Monoids (All (..))


verifyAbstractTransition :: AbstractTransition
                         -> Bool
verifyAbstractTransition :: Transition' AbstractState -> Bool
verifyAbstractTransition Transition { AbstractState
fromState :: AbstractState
fromState :: forall state. Transition' state -> state
fromState, AbstractState
toState :: AbstractState
toState :: forall state. Transition' state -> state
toState } =
    case (AbstractState
fromState, AbstractState
toState) of
      --
      -- Outbound
      --

      -- @Reserve@
      (AbstractState
TerminatedSt, AbstractState
ReservedOutboundSt) -> Bool
True
      (AbstractState
UnknownConnectionSt, AbstractState
ReservedOutboundSt) -> Bool
True
      -- @Connected@
      (AbstractState
ReservedOutboundSt, UnnegotiatedSt Provenance
Outbound) -> Bool
True
      -- @Negotiated^{Unidirectional}_{Outbound}@
      (UnnegotiatedSt Provenance
Outbound, AbstractState
OutboundUniSt)  -> Bool
True
      -- @Negotiated^{Duplex}_{Outbound}@
      (UnnegotiatedSt Provenance
Outbound, OutboundDupSt TimeoutExpired
Ticking) -> Bool
True
      (UnnegotiatedSt Provenance
_,        AbstractState
TerminatingSt) -> Bool
True

      -- @DemotedToCold^{Unidirectional}_{Local}@
      (AbstractState
OutboundUniSt, OutboundIdleSt DataFlow
Unidirectional) -> Bool
True
      -- @TimeoutExpired@
      (OutboundDupSt TimeoutExpired
Ticking, OutboundDupSt TimeoutExpired
Expired) -> Bool
True
      -- @DemotedToCold^{Duplex}_{Local}@
      (OutboundDupSt TimeoutExpired
Expired, OutboundIdleSt DataFlow
Duplex) -> Bool
True
      -- identity transition executed by 'demotedToColdRemote'
      (OutboundIdleSt DataFlow
dataFlow, OutboundIdleSt DataFlow
dataFlow') -> DataFlow
dataFlow DataFlow -> DataFlow -> Bool
forall a. Eq a => a -> a -> Bool
== DataFlow
dataFlow'

      --
      -- Outbound ↔ Inbound
      --

      -- @DemotedToCold^{Duplex}_{Local}@
      (OutboundDupSt TimeoutExpired
Ticking, InboundIdleSt DataFlow
Duplex) -> Bool
True
      -- @Awake^{Duplex}_{Local}
      -- or
      -- 'SelfConn⁻¹'
      (InboundIdleSt DataFlow
Duplex, OutboundDupSt TimeoutExpired
Ticking) -> Bool
True
      -- @PromotedToWarm^{Duplex}_{Remote}@
      (OutboundDupSt TimeoutExpired
Ticking, AbstractState
DuplexSt) -> Bool
True
      (OutboundDupSt TimeoutExpired
Expired, AbstractState
DuplexSt) -> Bool
True
      -- can be executed by 'demotedToColdRemote'
      (OutboundDupSt TimeoutExpired
expired, OutboundDupSt TimeoutExpired
expired')
                                        -> TimeoutExpired
expired TimeoutExpired -> TimeoutExpired -> Bool
forall a. Eq a => a -> a -> Bool
== TimeoutExpired
expired'
      -- @PromotedToWarm^{Duplex}_{Local}@
      (InboundSt DataFlow
Duplex, AbstractState
DuplexSt) -> Bool
True
      -- @DemotedToCold^{Duplex}_{Remote}@
      (AbstractState
DuplexSt, OutboundDupSt TimeoutExpired
Ticking) -> Bool
True
      -- @DemotedToCold^{Duplex}_{Local}@
      (AbstractState
DuplexSt, InboundSt DataFlow
Duplex) -> Bool
True

      --
      -- Inbound
      --

      -- @Accepted@
      (AbstractState
TerminatedSt, UnnegotiatedSt Provenance
Inbound) -> Bool
True
      (AbstractState
UnknownConnectionSt, UnnegotiatedSt Provenance
Inbound) -> Bool
True
      -- @Overwritten@
      (AbstractState
ReservedOutboundSt, UnnegotiatedSt Provenance
Inbound) -> Bool
True
      -- @Negotiated^{Duplex}_{Inbound}
      (UnnegotiatedSt Provenance
Inbound, InboundIdleSt DataFlow
Duplex) -> Bool
True
      -- @Negotiated^{Unidirectional}_{Inbound}
      (UnnegotiatedSt Provenance
Inbound, InboundIdleSt DataFlow
Unidirectional) -> Bool
True

      -- @SelfConnNegotiated^{Duplex}_{Inbound}
      (UnnegotiatedSt Provenance
Outbound, InboundIdleSt DataFlow
Duplex) -> Bool
True
      -- @SelfConnNegotiated^{Unidirectional}_{Inbound}
      (UnnegotiatedSt Provenance
Outbound, InboundIdleSt DataFlow
Unidirectional) -> Bool
True

      -- 'unregisterOutboundConnection' and 'demotedToColdRemote' might perform
      (InboundIdleSt DataFlow
Duplex, InboundIdleSt DataFlow
Duplex) -> Bool
True
      -- @Awake^{Duplex}_{Remote}
      (InboundIdleSt DataFlow
Duplex, InboundSt DataFlow
Duplex) -> Bool
True
      -- @Commit^{Duplex}
      (InboundIdleSt DataFlow
Duplex, AbstractState
TerminatingSt) -> Bool
True
      -- @DemotedToCold^{Duplex}_{Local}
      (InboundSt DataFlow
Duplex, InboundIdleSt DataFlow
Duplex) -> Bool
True

      -- @Awake^{Unidirectional}_{Remote}
      (InboundIdleSt DataFlow
Unidirectional, InboundSt DataFlow
Unidirectional) -> Bool
True
      -- @Commit^{Unidirectional}
      (InboundIdleSt DataFlow
Unidirectional, AbstractState
TerminatingSt) -> Bool
True
      -- @DemotedToCold^{Unidirectional}_{Local}
      (InboundSt DataFlow
Unidirectional, InboundIdleSt DataFlow
Unidirectional) -> Bool
True

      --
      -- OutboundIdleSt
      --

      (OutboundIdleSt DataFlow
Duplex, InboundSt DataFlow
Duplex) -> Bool
True
      (OutboundIdleSt DataFlow
_dataFlow, AbstractState
TerminatingSt) -> Bool
True

      --
      -- Connecting to oneself
      --

      -- 'SelfConn'
      (UnnegotiatedSt Provenance
Outbound, UnnegotiatedSt Provenance
Inbound) -> Bool
True
      -- 'SelfConn⁻¹'
      (UnnegotiatedSt Provenance
Inbound,  UnnegotiatedSt Provenance
Outbound) -> Bool
True
      -- 'SelfConn⁻¹'
      (InboundIdleSt DataFlow
Unidirectional, AbstractState
OutboundUniSt) -> Bool
True

      --
      -- Terminate
      --

      -- @Terminate@
      (AbstractState
TerminatingSt, AbstractState
TerminatedSt) -> Bool
True

      -- explicit prohibition of reflexive terminate transition
      (AbstractState
TerminatedSt, AbstractState
TerminatedSt) -> Bool
False
      -- implicit terminate transition
      (AbstractState
_, AbstractState
TerminatedSt) -> Bool
True

      -- explicit prohibition of reflexive unknown transition
      (AbstractState
UnknownConnectionSt, AbstractState
UnknownConnectionSt) -> Bool
False
      (AbstractState
_, AbstractState
UnknownConnectionSt) -> Bool
True

      -- We accept connection in 'TerminatingSt'
      (AbstractState
TerminatingSt, UnnegotiatedSt Provenance
Inbound) -> Bool
True

      (AbstractState, AbstractState)
_ -> Bool
False

-- | Maps each valid transition into one number. Collapses all invalid transition into a
-- single number.
--
-- NOTE: Should be in sync with 'verifyAbstractTransition'
--
validTransitionMap :: AbstractTransition
                   -> (Int, String)
validTransitionMap :: Transition' AbstractState -> (Int, String)
validTransitionMap t :: Transition' AbstractState
t@Transition { AbstractState
fromState :: forall state. Transition' state -> state
fromState :: AbstractState
fromState, AbstractState
toState :: forall state. Transition' state -> state
toState :: AbstractState
toState } =
    case (AbstractState
fromState, AbstractState
toState) of
      (AbstractState
TerminatedSt            , AbstractState
ReservedOutboundSt)                -> (Int
01, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
UnknownConnectionSt     , AbstractState
ReservedOutboundSt)                -> (Int
02, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
ReservedOutboundSt      , UnnegotiatedSt Provenance
Outbound)           -> (Int
03, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Outbound , AbstractState
OutboundUniSt)                     -> (Int
04, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Outbound , OutboundDupSt TimeoutExpired
Ticking)             -> (Int
05, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
OutboundUniSt           , OutboundIdleSt DataFlow
Unidirectional)     -> (Int
06, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
Ticking   , OutboundDupSt TimeoutExpired
Expired)             -> (Int
07, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
Expired   , OutboundIdleSt DataFlow
Duplex)             -> (Int
08, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundIdleSt DataFlow
dataFlow , OutboundIdleSt DataFlow
dataFlow')
        | DataFlow
dataFlow DataFlow -> DataFlow -> Bool
forall a. Eq a => a -> a -> Bool
== DataFlow
dataFlow'                                     -> (Int
09, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
Ticking   , InboundIdleSt DataFlow
Duplex)              -> (Int
10, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Duplex    , OutboundDupSt TimeoutExpired
Ticking)             -> (Int
11, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
Ticking   , AbstractState
DuplexSt)                          -> (Int
12, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
Expired   , AbstractState
DuplexSt)                          -> (Int
13, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundDupSt TimeoutExpired
expired   , OutboundDupSt TimeoutExpired
expired')
        | TimeoutExpired
expired TimeoutExpired -> TimeoutExpired -> Bool
forall a. Eq a => a -> a -> Bool
== TimeoutExpired
expired'                                       -> (Int
14, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundSt DataFlow
Duplex             , AbstractState
DuplexSt)                     -> (Int
15, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
DuplexSt                     , OutboundDupSt TimeoutExpired
Ticking)        -> (Int
16, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
DuplexSt                     , InboundSt DataFlow
Duplex)             -> (Int
17, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
TerminatedSt                 , UnnegotiatedSt Provenance
Inbound)       -> (Int
18, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
UnknownConnectionSt          , UnnegotiatedSt Provenance
Inbound)       -> (Int
19, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
ReservedOutboundSt           , UnnegotiatedSt Provenance
Inbound)       -> (Int
20, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Inbound       , InboundIdleSt DataFlow
Duplex)         -> (Int
21, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Inbound       , InboundIdleSt DataFlow
Unidirectional) -> (Int
22, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Duplex         , InboundIdleSt DataFlow
Duplex)         -> (Int
23, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Duplex         , InboundSt DataFlow
Duplex)             -> (Int
24, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Duplex         , AbstractState
TerminatingSt)                -> (Int
25, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundSt DataFlow
Duplex             , InboundIdleSt DataFlow
Duplex)         -> (Int
26, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Unidirectional , InboundSt DataFlow
Unidirectional)     -> (Int
27, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Unidirectional , AbstractState
TerminatingSt)                -> (Int
28, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundSt DataFlow
Unidirectional     , InboundIdleSt DataFlow
Unidirectional) -> (Int
29, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (OutboundIdleSt DataFlow
Duplex        , InboundSt DataFlow
Duplex)             -> (Int
30, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Outbound      , UnnegotiatedSt Provenance
Inbound)       -> (Int
36, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Inbound       , UnnegotiatedSt Provenance
Outbound)      -> (Int
37, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t) -- max valid transition
      (OutboundIdleSt DataFlow
_dataFlow , AbstractState
TerminatingSt)                    -> (Int
38, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
TerminatingSt            , AbstractState
TerminatedSt)                     -> (Int
39, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
_                        , AbstractState
TerminatedSt)                     -> (Int
40, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
_                        , AbstractState
UnknownConnectionSt)              -> (Int
41, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState
TerminatingSt            , UnnegotiatedSt Provenance
Inbound)           -> (Int
42, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Outbound      , InboundIdleSt DataFlow
Duplex)         -> (Int
43, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (UnnegotiatedSt Provenance
Outbound      , InboundIdleSt DataFlow
Unidirectional) -> (Int
44, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (InboundIdleSt DataFlow
Unidirectional , AbstractState
OutboundUniSt)                -> (Int
45, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)
      (AbstractState, AbstractState)
_                                                             -> (Int
99, Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
t)

-- Assuming all transitions in the transition list are valid, we only need to
-- look at the 'toState' of the current transition and the 'fromState' of the
-- next transition.
verifyAbstractTransitionOrder :: Bool -- ^ Check last transition: useful for
                                      --    distinguish Diffusion layer tests
                                      --    vs non-Diffusion ones.
                              -> [AbstractTransition]
                              -> All
verifyAbstractTransitionOrder :: Bool -> [Transition' AbstractState] -> All
verifyAbstractTransitionOrder Bool
_ [] = All
forall a. Monoid a => a
mempty
verifyAbstractTransitionOrder Bool
checkLast (Transition' AbstractState
h:[Transition' AbstractState]
t) = [Transition' AbstractState] -> Transition' AbstractState -> All
go [Transition' AbstractState]
t Transition' AbstractState
h
  where
    go :: [AbstractTransition] -> AbstractTransition -> All
    -- All transitions must end in the 'UnknownConnectionSt', and since we
    -- assume that all transitions are valid we do not have to check the
    -- 'fromState'.
    go :: [Transition' AbstractState] -> Transition' AbstractState -> All
go [] (Transition AbstractState
_ AbstractState
UnknownConnectionSt) = All
forall a. Monoid a => a
mempty
    go [] tr :: Transition' AbstractState
tr@(Transition AbstractState
_ AbstractState
_)          =
      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]
++ Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
tr)
            (Bool -> Property
forall prop. Testable prop => prop -> Property
property (Bool -> Bool
not Bool
checkLast))
    -- All transitions have to be in a correct order, which means that the
    -- current state we are looking at (current toState) needs to be equal to
    -- the next 'fromState', in order for the transition chain to be correct.
    go (next :: Transition' AbstractState
next@(Transition AbstractState
nextFromState AbstractState
_) : [Transition' AbstractState]
ts)
        curr :: Transition' AbstractState
curr@(Transition AbstractState
_ AbstractState
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]
++ Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
curr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nto: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Transition' AbstractState -> String
forall a. Show a => a -> String
show Transition' AbstractState
next)
              (Bool -> Property
forall prop. Testable prop => prop -> Property
property (AbstractState
currToState AbstractState -> AbstractState -> Bool
forall a. Eq a => a -> a -> Bool
== AbstractState
nextFromState)))
         All -> All -> All
forall a. Semigroup a => a -> a -> a
<> [Transition' AbstractState] -> Transition' AbstractState -> All
go [Transition' AbstractState]
ts Transition' AbstractState
next


-- | List of all valid transition's names.
--
-- NOTE: Should be in sync with 'verifyAbstractTransition', but due to #3516
-- abrupt terminating transitions and identity transitions are trimmed for now,
-- until we tweak the generators to include more connection errors.
--
allValidTransitionsNames :: [String]
allValidTransitionsNames :: [String]
allValidTransitionsNames =
  (Transition' AbstractState -> String)
-> [Transition' AbstractState] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Transition' AbstractState -> String
forall a. Show a => a -> String
show
  [ AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
UnknownConnectionSt             AbstractState
ReservedOutboundSt
  -- , Transition TerminatedSt                    ReservedOutboundSt
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
ReservedOutboundSt              (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)        (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       AbstractState
OutboundUniSt
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
OutboundUniSt                   (DataFlow -> AbstractState
OutboundIdleSt DataFlow
Unidirectional)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)         (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Expired)
  -- , Transition (OutboundDupSt Expired)         (OutboundIdleSt Duplex)
  -- , Transition (OutboundIdleSt Unidirectional) (OutboundIdleSt Unidirectional)
  -- , Transition (OutboundIdleSt Duplex)         (OutboundIdleSt Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)         (DataFlow -> AbstractState
InboundIdleSt DataFlow
Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (DataFlow -> AbstractState
InboundIdleSt DataFlow
Duplex)          (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)         AbstractState
DuplexSt
  -- , Transition (OutboundDupSt Expired)         DuplexSt
  -- , Transition (OutboundDupSt Ticking)         (OutboundDupSt Ticking)
  -- , Transition (OutboundDupSt Expired)         (OutboundDupSt Expired)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (DataFlow -> AbstractState
InboundSt DataFlow
Duplex)              AbstractState
DuplexSt
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
DuplexSt                        (TimeoutExpired -> AbstractState
OutboundDupSt TimeoutExpired
Ticking)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
DuplexSt                        (DataFlow -> AbstractState
InboundSt DataFlow
Duplex)
  -- , Transition TerminatedSt                    (UnnegotiatedSt Inbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
UnknownConnectionSt             (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
ReservedOutboundSt              (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)        (DataFlow -> AbstractState
InboundIdleSt DataFlow
Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)        (DataFlow -> AbstractState
InboundIdleSt DataFlow
Unidirectional)
  -- , Transition (InboundIdleSt Duplex)          (InboundIdleSt Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (DataFlow -> AbstractState
InboundIdleSt DataFlow
Duplex)          (DataFlow -> AbstractState
InboundSt DataFlow
Duplex)
  -- , Transition (InboundIdleSt Duplex)          TerminatingSt
  -- , Transition (InboundSt Duplex)              (InboundIdleSt Duplex)
  -- , Transition (InboundIdleSt Unidirectional)  (InboundSt Unidirectional)
  -- , Transition (InboundIdleSt Unidirectional)  TerminatingSt
  -- , Transition (InboundSt Unidirectional)      (InboundIdleSt Unidirectional)
  -- , Transition (OutboundIdleSt Duplex)         (InboundSt Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Inbound)        (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)
  -- , Transition (OutboundIdleSt Unidirectional) TerminatingSt
  -- , Transition (OutboundIdleSt Duplex)         TerminatingSt
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition AbstractState
TerminatingSt                   AbstractState
TerminatedSt
  -- , Transition TerminatedSt                    UnknownConnectionSt
  -- , Transition TerminatingSt                   (UnnegotiatedSt Inbound)
  -- , Transition (_)                             (TerminatedSt)
  -- , Transition (_)                             (UnknownConnectionSt)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       (DataFlow -> AbstractState
InboundIdleSt DataFlow
Unidirectional)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (Provenance -> AbstractState
UnnegotiatedSt Provenance
Outbound)       (DataFlow -> AbstractState
InboundIdleSt DataFlow
Duplex)
  , AbstractState -> AbstractState -> Transition' AbstractState
forall state. state -> state -> Transition' state
Transition (DataFlow -> AbstractState
InboundIdleSt DataFlow
Unidirectional)  AbstractState
OutboundUniSt
  ]

abstractStateIsFinalTransition :: Transition' AbstractState -> Bool
abstractStateIsFinalTransition :: Transition' AbstractState -> Bool
abstractStateIsFinalTransition (Transition AbstractState
_ AbstractState
UnknownConnectionSt) = Bool
True
abstractStateIsFinalTransition Transition' AbstractState
_                                  = Bool
False

-- | This function is necessary to check tests that only read from the
-- `traceTVar` logging output. These `traceTVar` logs do not transition to
-- 'UnknownConnectionSt'.
--
abstractStateIsFinalTransitionTVarTracing :: Transition' AbstractState -> Bool
abstractStateIsFinalTransitionTVarTracing :: Transition' AbstractState -> Bool
abstractStateIsFinalTransitionTVarTracing (Transition AbstractState
_ AbstractState
UnknownConnectionSt) = Bool
True
abstractStateIsFinalTransitionTVarTracing Transition' AbstractState
_                                  = Bool
False

connectionManagerTraceMap
  :: CM.Trace
      ntnAddr
      (ConnectionHandlerTrace ntnVersion ntnVersionData)
  -> String
connectionManagerTraceMap :: forall ntnAddr ntnVersion ntnVersionData.
Trace ntnAddr (ConnectionHandlerTrace ntnVersion ntnVersionData)
-> String
connectionManagerTraceMap (TrIncludeConnection Provenance
p ntnAddr
_)        =
  String
"TrIncludeConnection " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
connectionManagerTraceMap (TrReleaseConnection Provenance
p ntnAddr
_)     =
  String
"TrUnregisterConnection " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
connectionManagerTraceMap (TrConnect Maybe ntnAddr
_ ntnAddr
_)                  =
  String
"TrConnect"
connectionManagerTraceMap (TrConnectError Maybe ntnAddr
_ ntnAddr
_ SomeException
_)          =
  String
"TrConnectError"
connectionManagerTraceMap (TrTerminatingConnection Provenance
p ConnectionId ntnAddr
_)    =
  String
"TrTerminatingConnection " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
connectionManagerTraceMap (TrTerminatedConnection Provenance
p ntnAddr
_)     =
  String
"TrTerminatedConnection " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
connectionManagerTraceMap (TrConnectionHandler ConnectionId ntnAddr
_ ConnectionHandlerTrace ntnVersion ntnVersionData
_)        =
  String
"TrConnectionHandler"
connectionManagerTraceMap Trace ntnAddr (ConnectionHandlerTrace ntnVersion ntnVersionData)
TrShutdown                       =
  String
"TrShutdown"
connectionManagerTraceMap (TrConnectionExists Provenance
p ntnAddr
_ AbstractState
as)      =
  String
"TrConnectionExists " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ AbstractState -> String
forall a. Show a => a -> String
show AbstractState
as
connectionManagerTraceMap (TrForbiddenConnection ConnectionId ntnAddr
_)        =
  String
"TrForbiddenConnection"
connectionManagerTraceMap (TrConnectionFailure ConnectionId ntnAddr
_)          =
  String
"TrConnectionFailure"
connectionManagerTraceMap (TrConnectionNotFound Provenance
p ntnAddr
_)       =
  String
"TrConnectionNotFound " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Provenance -> String
forall a. Show a => a -> String
show Provenance
p
connectionManagerTraceMap (TrForbiddenOperation ntnAddr
_ AbstractState
as)      =
  String
"TrForbiddenOperation" String -> String -> String
forall a. [a] -> [a] -> [a]
++ AbstractState -> String
forall a. Show a => a -> String
show AbstractState
as
connectionManagerTraceMap (TrPruneConnections Set ntnAddr
_ Int
_ Set ntnAddr
_)       =
  String
"TrPruneConnections"
connectionManagerTraceMap (TrConnectionCleanup ConnectionId ntnAddr
_)          =
  String
"TrConnectionCleanup"
connectionManagerTraceMap (TrConnectionTimeWait ConnectionId ntnAddr
_)         =
  String
"TrConnectionTimeWait"
connectionManagerTraceMap (TrConnectionTimeWaitDone ConnectionId ntnAddr
_)     =
  String
"TrConnectionTimeWaitDone"
connectionManagerTraceMap (TrConnectionManagerCounters ConnectionManagerCounters
_)  =
  String
"TrConnectionManagerCounters"
connectionManagerTraceMap (TrState Map ntnAddr AbstractState
_)                      =
  String
"TrState"
connectionManagerTraceMap (TrUnexpectedlyFalseAssertion AssertionLocation ntnAddr
_) =
  String
"TrUnexpectedlyFalseAssertion"