{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

-- | Tests of the node's pipelined LeiosFetch client, against a scripted server,
-- in simulated time.
module Test.Consensus.Leios.LeiosFetch.Client (tests) where

import Control.Concurrent.Class.MonadSTM.Strict
import Control.Monad.Class.MonadAsync (withAsync)
import Control.Monad.Class.MonadTime.SI (DiffTime, Time (..), getMonotonicTime)
import Control.Monad.Class.MonadTimer.SI (threadDelay)
import Control.Monad.IOSim (IOSim, runSimOrThrow)
import Control.Tracer (mkTracer)
import qualified Data.Vector.Strict as V
import Network.TypedProtocol.Codec (AnyMessage (..), PeerRole (..))
import Network.TypedProtocol.Core (IsPipelined (..), ReflRelativeAgency (..))
import Network.TypedProtocol.Peer (Peer (..))
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Client
  ( SomeLeiosFetchJob (..)
  , leiosFetchClientPeerPipelined
  , toLeiosFetchClientPeerPipelined
  )
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Type
  ( LeiosFetch (..)
  , Message (..)
  , codecLeiosFetchId
  )
import Ouroboros.Network.Channel (createConnectedBufferedChannels)
import Ouroboros.Network.Driver.Simple
  ( Role (..)
  , TraceSendRecv (..)
  , runConnectedPeersPipelined
  )
import Ouroboros.Network.Util.ShowProxy (ShowProxy (..))
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=))

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"pipelined client"
    [ TestName -> Assertion -> TestTree
testCase TestName
"the client sends a request as soon as it has one, even while a reply is outstanding" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        Run -> [(Time, Int)]
sentRequests ((forall s. IOSim s Run) -> Run
forall a. (forall s. IOSim s a) -> a
runSimOrThrow (Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
forall s.
Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
runClient Int
10 (\Int
i -> if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then DiffTime
10 else DiffTime
0) StrictTVar (IOSim s) [Int]
-> StrictTVar (IOSim s) Bool -> IOSim s ()
forall {m :: * -> *} {a}.
(MonadSTM m, Num a, MonadDelay m) =>
StrictTVar m [a] -> StrictTVar m Bool -> m ()
sendSecondLater))
          [(Time, Int)] -> [(Time, Int)] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [(DiffTime -> Time
Time DiffTime
0, Int
1), (DiffTime -> Time
Time DiffTime
1, Int
2)]
    , TestName -> Assertion -> TestTree
testCase TestName
"the client keeps at most its depth of requests outstanding" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        Run -> [(Time, Int)]
sentRequests ((forall s. IOSim s Run) -> Run
forall a. (forall s. IOSim s a) -> a
runSimOrThrow (Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
forall s.
Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
runClient Int
2 (DiffTime -> Int -> DiffTime
forall a b. a -> b -> a
const DiffTime
10) StrictTVar (IOSim s) [Int]
-> StrictTVar (IOSim s) Bool -> IOSim s ()
forall {m :: * -> *} {a}.
(MonadSTM m, Num a, MonadDelay m) =>
StrictTVar m [a] -> StrictTVar m Bool -> m ()
sendThree))
          [(Time, Int)] -> [(Time, Int)] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [(DiffTime -> Time
Time DiffTime
0, Int
1), (DiffTime -> Time
Time DiffTime
0, Int
2), (DiffTime -> Time
Time DiffTime
10, Int
3)]
    , TestName -> Assertion -> TestTree
testCase
        TestName
"the client, once told to stop, ignores the replies still outstanding, then sends MsgDone"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ let run :: Run
run = (forall s. IOSim s Run) -> Run
forall a. (forall s. IOSim s a) -> a
runSimOrThrow (Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
forall s.
Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
runClient Int
10 (DiffTime -> Int -> DiffTime
forall a b. a -> b -> a
const DiffTime
10) StrictTVar (IOSim s) [Int]
-> StrictTVar (IOSim s) Bool -> IOSim s ()
forall {m :: * -> *} {a}.
(MonadSTM m, Num a, MonadDelay m) =>
StrictTVar m [a] -> StrictTVar m Bool -> m ()
stopWhileTwoOutstanding)
           in (Run -> [(Time, Int)]
sentRequests Run
run, Run -> [Int]
handled Run
run, Run -> [Time]
sentDone Run
run)
                ([(Time, Int)], [Int], [Time])
-> ([(Time, Int)], [Int], [Time]) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ([(DiffTime -> Time
Time DiffTime
0, Int
1), (DiffTime -> Time
Time DiffTime
0, Int
2)], [], [DiffTime -> Time
Time DiffTime
20])
    ]
 where
  sendSecondLater :: StrictTVar m [a] -> StrictTVar m Bool -> m ()
sendSecondLater StrictTVar m [a]
queue StrictTVar m Bool
stop = do
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m [a] -> [a] -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m [a]
queue [a
1]
    DiffTime -> m ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m [a] -> ([a] -> [a]) -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar StrictTVar m [a]
queue ([a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a
2])
    DiffTime -> m ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
29
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m Bool -> Bool -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m Bool
stop Bool
True
  sendThree :: StrictTVar m [a] -> StrictTVar m Bool -> m ()
sendThree StrictTVar m [a]
queue StrictTVar m Bool
stop = do
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m [a] -> [a] -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m [a]
queue [a
1, a
2, a
3]
    DiffTime -> m ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
100
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m Bool -> Bool -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m Bool
stop Bool
True
  stopWhileTwoOutstanding :: StrictTVar m [a] -> StrictTVar m Bool -> m ()
stopWhileTwoOutstanding StrictTVar m [a]
queue StrictTVar m Bool
stop = do
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m [a] -> [a] -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m [a]
queue [a
1, a
2]
    DiffTime -> m ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
5
    STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar m Bool -> Bool -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m Bool
stop Bool
True

-- | What the protocol carries in these tests: the number of a request, which
-- its reply echoes.
newtype N = N Int
  deriving (N -> N -> Bool
(N -> N -> Bool) -> (N -> N -> Bool) -> Eq N
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: N -> N -> Bool
== :: N -> N -> Bool
$c/= :: N -> N -> Bool
/= :: N -> N -> Bool
Eq, Int -> N -> ShowS
[N] -> ShowS
N -> TestName
(Int -> N -> ShowS) -> (N -> TestName) -> ([N] -> ShowS) -> Show N
forall a.
(Int -> a -> ShowS) -> (a -> TestName) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> N -> ShowS
showsPrec :: Int -> N -> ShowS
$cshow :: N -> TestName
show :: N -> TestName
$cshowList :: [N] -> ShowS
showList :: [N] -> ShowS
Show)

instance ShowProxy N where showProxy :: Proxy N -> TestName
showProxy Proxy N
_ = TestName
"N"

-- | What the client did over a run.
data Run = MkRun
  { Run -> [(Time, Int)]
sentRequests :: [(Time, Int)]
  -- ^ when it sent each block request
  , Run -> [Time]
sentDone :: [Time]
  -- ^ when it sent 'MsgDone'
  , Run -> [Int]
handled :: [Int]
  -- ^ the replies it handed to their jobs
  }

-- | Run the client, leaving at most this many requests outstanding, against a
-- server that delays its reply to each block request by the given time, while
-- the script feeds the client's request queue and finally tells it to stop.
runClient ::
  Int ->
  (Int -> DiffTime) ->
  (StrictTVar (IOSim s) [Int] -> StrictTVar (IOSim s) Bool -> IOSim s ()) ->
  IOSim s Run
runClient :: forall s.
Int
-> (Int -> DiffTime)
-> (StrictTVar (IOSim s) [Int]
    -> StrictTVar (IOSim s) Bool -> IOSim s ())
-> IOSim s Run
runClient Int
depth Int -> DiffTime
delayFor StrictTVar (IOSim s) [Int]
-> StrictTVar (IOSim s) Bool -> IOSim s ()
script = do
  queue <- [Int] -> IOSim s (StrictTVar (IOSim s) [Int])
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
newTVarIO []
  stop <- newTVarIO False
  handledVar <- newTVarIO []
  events <- newTVarIO []
  let tracer = ((Role, TraceSendRecv (LeiosFetch N N N)) -> IOSim s ())
-> Tracer (IOSim s) (Role, TraceSendRecv (LeiosFetch N N N))
forall (m :: * -> *) a. Applicative m => (a -> m ()) -> Tracer m a
mkTracer (((Role, TraceSendRecv (LeiosFetch N N N)) -> IOSim s ())
 -> Tracer (IOSim s) (Role, TraceSendRecv (LeiosFetch N N N)))
-> ((Role, TraceSendRecv (LeiosFetch N N N)) -> IOSim s ())
-> Tracer (IOSim s) (Role, TraceSendRecv (LeiosFetch N N N))
forall a b. (a -> b) -> a -> b
$ \(Role
role, TraceSendRecv (LeiosFetch N N N)
event) -> do
        now <- IOSim s Time
forall (m :: * -> *). MonadMonotonicTime m => m Time
getMonotonicTime
        atomically $ modifyTVar events ((now, role, event) :)
      job Int
i =
        Message (LeiosFetch N N N) 'StIdle ('StBusy 'StBlock)
-> (Message (LeiosFetch N N N) ('StBusy 'StBlock) 'StIdle
    -> IOSim s ())
-> SomeLeiosFetchJob N N N (IOSim s)
forall point eb tx (m :: * -> *)
       (st' :: LeiosFetchBusy point eb tx).
StateTokenI ('StBusy st') =>
Message (LeiosFetch point eb tx) 'StIdle ('StBusy st')
-> (Message (LeiosFetch point eb tx) ('StBusy st') 'StIdle -> m ())
-> SomeLeiosFetchJob point eb tx m
MkSomeLeiosFetchJob
          (N -> Message (LeiosFetch N N N) 'StIdle ('StBusy 'StBlock)
forall point eb tx.
point
-> Message (LeiosFetch point eb tx) 'StIdle ('StBusy 'StBlock)
MsgLeiosBlockRequest (Int -> N
N Int
i))
          (\(MsgLeiosBlock (N Int
j)) -> STM (IOSim s) () -> IOSim s ()
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM (IOSim s) () -> IOSim s ()) -> STM (IOSim s) () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ StrictTVar (IOSim s) [Int] -> ([Int] -> [Int]) -> STM (IOSim s) ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar StrictTVar (IOSim s) [Int]
handledVar (Int
j Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:))
      awaitStop = StrictTVar (IOSim s) Bool -> STM (IOSim s) Bool
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar (IOSim s) Bool
stop STM s Bool -> (Bool -> STM s ()) -> STM s ()
forall a b. STM s a -> (a -> STM s b) -> STM s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> STM (IOSim s) ()
Bool -> STM s ()
forall (m :: * -> *). MonadSTM m => Bool -> STM m ()
check
      awaitJob =
        StrictTVar (IOSim s) [Int] -> STM (IOSim s) [Int]
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar (IOSim s) [Int]
queue STM s [Int]
-> ([Int] -> STM s (SomeLeiosFetchJob N N N (IOSim s)))
-> STM s (SomeLeiosFetchJob N N N (IOSim s))
forall a b. STM s a -> (a -> STM s b) -> STM s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          [] -> STM (IOSim s) (SomeLeiosFetchJob N N N (IOSim s))
STM s (SomeLeiosFetchJob N N N (IOSim s))
forall a. STM (IOSim s) a
forall (m :: * -> *) a. MonadSTM m => STM m a
retry
          Int
i : [Int]
is -> Int -> SomeLeiosFetchJob N N N (IOSim s)
job Int
i SomeLeiosFetchJob N N N (IOSim s)
-> STM s () -> STM s (SomeLeiosFetchJob N N N (IOSim s))
forall a b. a -> STM s b -> STM s a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ StrictTVar (IOSim s) [Int] -> [Int] -> STM (IOSim s) ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar (IOSim s) [Int]
queue [Int]
is
  _ <-
    withAsync (script queue stop) $ \Async (IOSim s) ()
_ ->
      IOSim
  s
  (Channel (IOSim s) (AnyMessage (LeiosFetch N N N)),
   Channel (IOSim s) (AnyMessage (LeiosFetch N N N)))
-> Tracer (IOSim s) (Role, TraceSendRecv (LeiosFetch N N N))
-> Codec
     (LeiosFetch N N N)
     CodecFailure
     (IOSim s)
     (AnyMessage (LeiosFetch N N N))
-> PeerPipelined (LeiosFetch N N N) 'AsClient 'StIdle (IOSim s) ()
-> Peer
     (LeiosFetch N N N)
     (FlipAgency 'AsClient)
     'NonPipelined
     'StIdle
     (IOSim s)
     ()
-> IOSim s ((), ())
forall (m :: * -> *) ps a b failure bytes (pr :: PeerRole)
       (st :: ps).
(MonadAsync m, MonadCatch m, MonadEvaluate m, ShowProxy ps,
 forall (st' :: ps) stok. (stok ~ StateToken st') => Show stok,
 NFData a, NFData b, NFData failure, Show failure) =>
m (Channel m bytes, Channel m bytes)
-> Tracer m (Role, TraceSendRecv ps)
-> Codec ps failure m bytes
-> PeerPipelined ps pr st m a
-> Peer ps (FlipAgency pr) 'NonPipelined st m b
-> m (a, b)
runConnectedPeersPipelined
        (Natural
-> IOSim
     s
     (Channel (IOSim s) (AnyMessage (LeiosFetch N N N)),
      Channel (IOSim s) (AnyMessage (LeiosFetch N N N)))
forall (m :: * -> *) a.
MonadLabelledSTM m =>
Natural -> m (Channel m a, Channel m a)
createConnectedBufferedChannels Natural
100)
        Tracer (IOSim s) (Role, TraceSendRecv (LeiosFetch N N N))
tracer
        Codec
  (LeiosFetch N N N)
  CodecFailure
  (IOSim s)
  (AnyMessage (LeiosFetch N N N))
forall point eb tx (m :: * -> *).
Monad m =>
Codec
  (LeiosFetch point eb tx)
  CodecFailure
  m
  (AnyMessage (LeiosFetch point eb tx))
codecLeiosFetchId
        (Peer
  (LeiosFetch N N N) 'AsClient ('Pipelined 'Z C) 'StIdle (IOSim s) ()
-> PeerPipelined (LeiosFetch N N N) 'AsClient 'StIdle (IOSim s) ()
forall point eb tx (m :: * -> *) a.
Peer
  (LeiosFetch point eb tx) 'AsClient ('Pipelined 'Z C) 'StIdle m a
-> LeiosFetchClientPeerPipelined point eb tx m a
toLeiosFetchClientPeerPipelined (Int
-> STM (IOSim s) ()
-> STM (IOSim s) (SomeLeiosFetchJob N N N (IOSim s))
-> Peer
     (LeiosFetch N N N) 'AsClient ('Pipelined 'Z C) 'StIdle (IOSim s) ()
forall (m :: * -> *) point eb tx a.
MonadSTM m =>
Int
-> STM m a
-> STM m (SomeLeiosFetchJob point eb tx m)
-> Peer
     (LeiosFetch point eb tx) 'AsClient ('Pipelined 'Z C) 'StIdle m a
leiosFetchClientPeerPipelined Int
depth STM (IOSim s) ()
STM s ()
awaitStop STM (IOSim s) (SomeLeiosFetchJob N N N (IOSim s))
STM s (SomeLeiosFetchJob N N N (IOSim s))
awaitJob))
        ((Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
forall s.
(Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
server Int -> DiffTime
delayFor)
  traced <- reverse <$> readTVarIO events
  replies <- reverse <$> readTVarIO handledVar
  pure
    MkRun
      { sentRequests =
          [(t, i) | (t, Client, TraceSendMsg (AnyMessage (MsgLeiosBlockRequest (N i)))) <- traced]
      , sentDone = [t | (t, Client, TraceSendMsg (AnyMessage MsgDone)) <- traced]
      , handled = replies
      }

-- | Replies to each block request after the given delay, one at a time.
server :: (Int -> DiffTime) -> Peer (LeiosFetch N N N) AsServer NonPipelined StIdle (IOSim s) ()
server :: forall s.
(Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
server Int -> DiffTime
delayFor = TheyHaveAgencyProof 'AsServer 'StIdle
-> (forall (st' :: LeiosFetch N N N).
    Message (LeiosFetch N N N) 'StIdle st'
    -> Peer
         (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
forall ps (pr :: PeerRole) (pl :: IsPipelined ps) (st :: ps)
       (m :: * -> *) a.
(StateTokenI st, ActiveState st, Outstanding pl ~ 'Z,
 OutstandingSenders pl ~ 'Z) =>
TheyHaveAgencyProof pr st
-> (forall (st' :: ps). Message ps st st' -> Peer ps pr pl st' m a)
-> Peer ps pr pl st m a
Await ReflRelativeAgency 'ClientAgency 'TheyHaveAgency 'TheyHaveAgency
TheyHaveAgencyProof 'AsServer 'StIdle
forall (r :: RelativeAgency). ReflRelativeAgency 'ClientAgency r r
ReflClientAgency ((forall (st' :: LeiosFetch N N N).
  Message (LeiosFetch N N N) 'StIdle st'
  -> Peer
       (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
 -> Peer
      (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ())
-> (forall (st' :: LeiosFetch N N N).
    Message (LeiosFetch N N N) 'StIdle st'
    -> Peer
         (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
forall a b. (a -> b) -> a -> b
$ \case
  MsgLeiosBlockRequest (N Int
i) -> IOSim
  s
  (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
forall ps (pr :: PeerRole) (pl :: IsPipelined ps) (st :: ps)
       (m :: * -> *) a.
m (Peer ps pr pl st m a) -> Peer ps pr pl st m a
Effect (IOSim
   s
   (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
 -> Peer
      (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
-> IOSim
     s
     (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
forall a b. (a -> b) -> a -> b
$ do
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay (Int -> DiffTime
delayFor Int
i)
    Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
-> IOSim
     s
     (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
 -> IOSim
      s
      (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()))
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
-> IOSim
     s
     (Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ())
forall a b. (a -> b) -> a -> b
$ WeHaveAgencyProof 'AsServer st'
-> Message (LeiosFetch N N N) st' 'StIdle
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
forall ps (pr :: PeerRole) (pl :: IsPipelined ps) (st :: ps)
       (st' :: ps) (m :: * -> *) a.
(StateTokenI st, StateTokenI st', ActiveState st,
 Outstanding pl ~ 'Z, OutstandingSenders pl ~ 'Z) =>
WeHaveAgencyProof pr st
-> Message ps st st'
-> Peer ps pr pl st' m a
-> Peer ps pr pl st m a
Yield ReflRelativeAgency 'ServerAgency 'WeHaveAgency 'WeHaveAgency
WeHaveAgencyProof 'AsServer st'
forall (r :: RelativeAgency). ReflRelativeAgency 'ServerAgency r r
ReflServerAgency (N -> Message (LeiosFetch N N N) ('StBusy 'StBlock) 'StIdle
forall eb point tx.
eb -> Message (LeiosFetch point eb tx) ('StBusy 'StBlock) 'StIdle
MsgLeiosBlock (Int -> N
N Int
i)) ((Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
forall s.
(Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
server Int -> DiffTime
delayFor)
  MsgLeiosBlockTxsRequest N
_point TxBitmaps
_bitmaps ->
    WeHaveAgencyProof 'AsServer st'
-> Message (LeiosFetch N N N) st' 'StIdle
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
forall ps (pr :: PeerRole) (pl :: IsPipelined ps) (st :: ps)
       (st' :: ps) (m :: * -> *) a.
(StateTokenI st, StateTokenI st', ActiveState st,
 Outstanding pl ~ 'Z, OutstandingSenders pl ~ 'Z) =>
WeHaveAgencyProof pr st
-> Message ps st st'
-> Peer ps pr pl st' m a
-> Peer ps pr pl st m a
Yield ReflRelativeAgency 'ServerAgency 'WeHaveAgency 'WeHaveAgency
WeHaveAgencyProof 'AsServer st'
forall (r :: RelativeAgency). ReflRelativeAgency 'ServerAgency r r
ReflServerAgency (Vector N
-> Message (LeiosFetch N N N) ('StBusy 'StBlockTxs) 'StIdle
forall tx point eb.
Vector tx
-> Message (LeiosFetch point eb tx) ('StBusy 'StBlockTxs) 'StIdle
MsgLeiosBlockTxs Vector N
forall a. Vector a
V.empty) ((Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
forall s.
(Int -> DiffTime)
-> Peer
     (LeiosFetch N N N) 'AsServer 'NonPipelined 'StIdle (IOSim s) ()
server Int -> DiffTime
delayFor)
  Message (LeiosFetch N N N) 'StIdle st'
R:MessageLeiosFetchfromto N N N 'StIdle st'
MsgDone -> NobodyHasAgencyProof 'AsServer st'
-> ()
-> Peer (LeiosFetch N N N) 'AsServer 'NonPipelined st' (IOSim s) ()
forall ps (pr :: PeerRole) (pl :: IsPipelined ps) (st :: ps)
       (m :: * -> *) a.
(StateTokenI st, StateAgency st ~ 'NobodyAgency,
 Outstanding pl ~ 'Z, OutstandingSenders pl ~ 'Z) =>
NobodyHasAgencyProof pr st -> a -> Peer ps pr pl st m a
Done ReflRelativeAgency 'NobodyAgency 'NobodyHasAgency 'NobodyHasAgency
NobodyHasAgencyProof 'AsServer st'
forall (r :: RelativeAgency). ReflRelativeAgency 'NobodyAgency r r
ReflNobodyAgency ()