{-# LANGUAGE BangPatterns     #-}
{-# LANGUAGE DeriveFunctor    #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase       #-}
{-# LANGUAGE NamedFieldPuns   #-}
{-# LANGUAGE RecordWildCards  #-}

module Ouroboros.Network.BlockFetch.ClientState
  ( FetchClientContext (..)
  , FetchClientPolicy (..)
  , FetchClientStateVars (..)
  , newFetchClientStateVars
  , readFetchClientState
  , PeerFetchStatus (..)
  , IsIdle (..)
  , PeerFetchInFlight (..)
  , initialPeerFetchInFlight
  , FetchRequest (..)
  , addNewFetchRequest
  , acknowledgeFetchRequest
  , startedFetchBatch
  , completeBlockDownload
  , completeFetchBatch
  , rejectedFetchBatch
  , TraceFetchClientState (..)
  , TraceLabelPeer (..)
  , ChainRange (..)
    -- * Ancillary
  , FromConsensus (..)
  , WhetherReceivingTentativeBlocks (..)
  ) where

import           Data.List (foldl')
import           Data.Maybe (mapMaybe)
import           Data.Semigroup (Last (..))
import           Data.Set (Set)
import qualified Data.Set as Set

import           Control.Concurrent.Class.MonadSTM.Strict
import           Control.Exception (assert)
import           Control.Monad (when)
import           Control.Monad.Class.MonadTime
import           Control.Tracer (Tracer, traceWith)

import           Network.Mux.Trace (TraceLabelPeer (..))

import           Ouroboros.Network.AnchoredFragment (AnchoredFragment)
import qualified Ouroboros.Network.AnchoredFragment as AF
import           Ouroboros.Network.Block (HasHeader, MaxSlotNo (..), Point,
                     blockPoint)
import           Ouroboros.Network.BlockFetch.ConsensusInterface
                     (FromConsensus (..), WhetherReceivingTentativeBlocks (..))
import           Ouroboros.Network.BlockFetch.DeltaQ
                     (PeerFetchInFlightLimits (..), PeerGSV, SizeInBytes,
                     calculatePeerFetchInFlightLimits)
import           Ouroboros.Network.ControlMessage (ControlMessageSTM,
                     timeoutWithControlMessage)
import           Ouroboros.Network.Point (withOriginToMaybe)
import           Ouroboros.Network.Protocol.BlockFetch.Type (ChainRange (..))

-- | The context that is passed into the block fetch protocol client when it
-- is started.
--
data FetchClientContext header block m =
     FetchClientContext {
       FetchClientContext header block m
-> Tracer m (TraceFetchClientState header)
fetchClientCtxTracer    :: Tracer m (TraceFetchClientState header),
       FetchClientContext header block m
-> FetchClientPolicy header block m
fetchClientCtxPolicy    :: FetchClientPolicy header block m,
       FetchClientContext header block m -> FetchClientStateVars m header
fetchClientCtxStateVars :: FetchClientStateVars m header
     }


-- | The policy used by the fetch clients. It is set by the central block fetch
-- logic, and passed to them via the 'FetchClientRegistry'.
--
data FetchClientPolicy header block m =
     FetchClientPolicy {
       FetchClientPolicy header block m -> header -> SizeInBytes
blockFetchSize     :: header -> SizeInBytes,
       FetchClientPolicy header block m -> header -> block -> Bool
blockMatchesHeader :: header -> block -> Bool,
       FetchClientPolicy header block m -> Point block -> block -> m ()
addFetchedBlock    :: Point block -> block -> m (),
       FetchClientPolicy header block m
-> FromConsensus block -> STM m UTCTime
blockForgeUTCTime  :: FromConsensus block -> STM m UTCTime
     }

-- | A set of variables shared between the block fetch logic thread and each
-- thread executing the client side of the block fetch protocol. That is, these
-- are the shared variables per peer. The 'FetchClientRegistry' contains the
-- mapping of these for all peers.
--
-- The variables are used for communicating from the protocol thread to the
-- decision making thread the status of things with that peer. And in the other
-- direction one shared variable is for providing new fetch requests.
--
data FetchClientStateVars m header =
     FetchClientStateVars {

       -- | The current status of communication with the peer. It is written
       -- by the protocol thread and monitored and read by the decision logic
       -- thread. Changes in this state trigger re-evaluation of fetch
       -- decisions.
       --
       FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar   :: StrictTVar m (PeerFetchStatus header),

       -- | The current number of requests in-flight and the amount of data
       -- in-flight with the peer. It is written by the protocol thread and
       -- read by the decision logic thread. This is used in fetch decisions
       -- but changes here do not trigger re-evaluation of fetch decisions.
       --
       FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header),

       -- | The shared variable used to communicate fetch requests to the thread
       -- running the block fetch protocol. Fetch requests are posted by the
       -- decision logic thread. The protocol thread accepts the requests and
       -- acts on them, updating the in-flight stats. While this is a 'TMVar',
       -- it is not used as a one-place queue: the requests can be updated
       -- before being accepted.
       --
       FetchClientStateVars m header -> TFetchRequestVar m header
fetchClientRequestVar  :: TFetchRequestVar m header
     }

newFetchClientStateVars :: MonadSTM m => STM m (FetchClientStateVars m header)
newFetchClientStateVars :: STM m (FetchClientStateVars m header)
newFetchClientStateVars = do
    StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar <- PeerFetchInFlight header
-> STM m (StrictTVar m (PeerFetchInFlight header))
forall (m :: * -> *) a. MonadSTM m => a -> STM m (StrictTVar m a)
newTVar PeerFetchInFlight header
forall header. PeerFetchInFlight header
initialPeerFetchInFlight
    StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar   <- PeerFetchStatus header
-> STM m (StrictTVar m (PeerFetchStatus header))
forall (m :: * -> *) a. MonadSTM m => a -> STM m (StrictTVar m a)
newTVar (Set (Point header) -> IsIdle -> PeerFetchStatus header
forall header.
Set (Point header) -> IsIdle -> PeerFetchStatus header
PeerFetchStatusReady Set (Point header)
forall a. Set a
Set.empty IsIdle
IsIdle)
    TFetchRequestVar m header
fetchClientRequestVar  <- STM m (TFetchRequestVar m header)
forall (m :: * -> *) header.
MonadSTM m =>
STM m (TFetchRequestVar m header)
newTFetchRequestVar
    FetchClientStateVars m header
-> STM m (FetchClientStateVars m header)
forall (m :: * -> *) a. Monad m => a -> m a
return FetchClientStateVars :: forall (m :: * -> *) header.
StrictTVar m (PeerFetchStatus header)
-> StrictTVar m (PeerFetchInFlight header)
-> TFetchRequestVar m header
-> FetchClientStateVars m header
FetchClientStateVars {StrictTVar m (PeerFetchInFlight header)
StrictTVar m (PeerFetchStatus header)
TFetchRequestVar m header
fetchClientRequestVar :: TFetchRequestVar m header
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientRequestVar :: TFetchRequestVar m header
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
..}

readFetchClientState :: MonadSTM m
                     => FetchClientStateVars m header
                     -> STM m (PeerFetchStatus header,
                               PeerFetchInFlight header,
                               FetchClientStateVars m header)
readFetchClientState :: FetchClientStateVars m header
-> STM
     m
     (PeerFetchStatus header, PeerFetchInFlight header,
      FetchClientStateVars m header)
readFetchClientState vars :: FetchClientStateVars m header
vars@FetchClientStateVars{StrictTVar m (PeerFetchInFlight header)
StrictTVar m (PeerFetchStatus header)
TFetchRequestVar m header
fetchClientRequestVar :: TFetchRequestVar m header
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientRequestVar :: forall (m :: * -> *) header.
FetchClientStateVars m header -> TFetchRequestVar m header
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
..} =
    (,,) (PeerFetchStatus header
 -> PeerFetchInFlight header
 -> FetchClientStateVars m header
 -> (PeerFetchStatus header, PeerFetchInFlight header,
     FetchClientStateVars m header))
-> STM m (PeerFetchStatus header)
-> STM
     m
     (PeerFetchInFlight header
      -> FetchClientStateVars m header
      -> (PeerFetchStatus header, PeerFetchInFlight header,
          FetchClientStateVars m header))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictTVar m (PeerFetchStatus header)
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
         STM
  m
  (PeerFetchInFlight header
   -> FetchClientStateVars m header
   -> (PeerFetchStatus header, PeerFetchInFlight header,
       FetchClientStateVars m header))
-> STM m (PeerFetchInFlight header)
-> STM
     m
     (FetchClientStateVars m header
      -> (PeerFetchStatus header, PeerFetchInFlight header,
          FetchClientStateVars m header))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
         STM
  m
  (FetchClientStateVars m header
   -> (PeerFetchStatus header, PeerFetchInFlight header,
       FetchClientStateVars m header))
-> STM m (FetchClientStateVars m header)
-> STM
     m
     (PeerFetchStatus header, PeerFetchInFlight header,
      FetchClientStateVars m header)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> FetchClientStateVars m header
-> STM m (FetchClientStateVars m header)
forall (f :: * -> *) a. Applicative f => a -> f a
pure FetchClientStateVars m header
vars

-- | The status of the block fetch communication with a peer. This is maintained
-- by fetch protocol threads and used in the block fetch decision making logic.
-- Changes in this status trigger re-evaluation of fetch decisions.
--
data PeerFetchStatus header =
       -- | Communication with the peer has failed. This is a temporary status
       -- that may occur during the process of shutting down the thread that
       -- runs the block fetch protocol. The peer will promptly be removed from
       -- the peer registry and so will not be considered at all.
       --
       PeerFetchStatusShutdown

       -- | The peer is in a potentially-temporary state in which it has not
       -- responded to us within a certain expected time limit. This is not
       -- a hard protocol timeout where the whole connection will be abandoned,
       -- it is simply a reply that has taken longer than expected. This status
       -- is used to trigger re-evaluating which peer to ask for blocks from,
       -- so that we can swiftly ask other peers for blocks if one unexpectedly
       -- responds too slowly
       --
       -- Peers in this state may later return to normal states if communication
       -- resumes, or they may eventually hit a hard timeout and fail.
       --
     | PeerFetchStatusAberrant

       -- | Communication with the peer is in a normal state, and the peer is
       -- considered too busy to accept new requests. Changing from this state
       -- to the ready state is used to trigger re-evaluating fetch decisions
       -- and may eventually result in new fetch requests. This state is used
       -- as part of a policy to batch new requests: instead of switching to
       -- the ready state the moment there is tiny bit of capacity available,
       -- the state is changed once the capacity reaches a certain threshold.
       --
     | PeerFetchStatusBusy

       -- | Communication with the peer is in a normal state, and the peer is
       -- considered ready to accept new requests.
       --
       -- The 'Set' is the blocks in flight.
     | PeerFetchStatusReady (Set (Point header)) IsIdle
  deriving (PeerFetchStatus header -> PeerFetchStatus header -> Bool
(PeerFetchStatus header -> PeerFetchStatus header -> Bool)
-> (PeerFetchStatus header -> PeerFetchStatus header -> Bool)
-> Eq (PeerFetchStatus header)
forall header.
StandardHash header =>
PeerFetchStatus header -> PeerFetchStatus header -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PeerFetchStatus header -> PeerFetchStatus header -> Bool
$c/= :: forall header.
StandardHash header =>
PeerFetchStatus header -> PeerFetchStatus header -> Bool
== :: PeerFetchStatus header -> PeerFetchStatus header -> Bool
$c== :: forall header.
StandardHash header =>
PeerFetchStatus header -> PeerFetchStatus header -> Bool
Eq, Int -> PeerFetchStatus header -> ShowS
[PeerFetchStatus header] -> ShowS
PeerFetchStatus header -> String
(Int -> PeerFetchStatus header -> ShowS)
-> (PeerFetchStatus header -> String)
-> ([PeerFetchStatus header] -> ShowS)
-> Show (PeerFetchStatus header)
forall header.
StandardHash header =>
Int -> PeerFetchStatus header -> ShowS
forall header.
StandardHash header =>
[PeerFetchStatus header] -> ShowS
forall header.
StandardHash header =>
PeerFetchStatus header -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PeerFetchStatus header] -> ShowS
$cshowList :: forall header.
StandardHash header =>
[PeerFetchStatus header] -> ShowS
show :: PeerFetchStatus header -> String
$cshow :: forall header.
StandardHash header =>
PeerFetchStatus header -> String
showsPrec :: Int -> PeerFetchStatus header -> ShowS
$cshowsPrec :: forall header.
StandardHash header =>
Int -> PeerFetchStatus header -> ShowS
Show)

-- | Whether this mini protocol instance is in the @Idle@ State
--
data IsIdle = IsIdle | IsNotIdle
  deriving (IsIdle -> IsIdle -> Bool
(IsIdle -> IsIdle -> Bool)
-> (IsIdle -> IsIdle -> Bool) -> Eq IsIdle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IsIdle -> IsIdle -> Bool
$c/= :: IsIdle -> IsIdle -> Bool
== :: IsIdle -> IsIdle -> Bool
$c== :: IsIdle -> IsIdle -> Bool
Eq, Int -> IsIdle -> ShowS
[IsIdle] -> ShowS
IsIdle -> String
(Int -> IsIdle -> ShowS)
-> (IsIdle -> String) -> ([IsIdle] -> ShowS) -> Show IsIdle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IsIdle] -> ShowS
$cshowList :: [IsIdle] -> ShowS
show :: IsIdle -> String
$cshow :: IsIdle -> String
showsPrec :: Int -> IsIdle -> ShowS
$cshowsPrec :: Int -> IsIdle -> ShowS
Show)

idleIf :: Bool -> IsIdle
idleIf :: Bool -> IsIdle
idleIf Bool
b = if Bool
b then IsIdle
IsIdle else IsIdle
IsNotIdle

-- | The number of requests in-flight and the amount of data in-flight with a
-- peer. This is maintained by fetch protocol threads and used in the block
-- fetch decision making logic.
--
data PeerFetchInFlight header = PeerFetchInFlight {
       -- | The number of block fetch requests that are currently in-flight.
       -- This is the number of /requests/ not the number of blocks. Each
       -- request is for a range of blocks.
       --
       -- We track this because there is a fixed maximum number of outstanding
       -- requests that the protocol allows.
       --
       PeerFetchInFlight header -> Word
peerFetchReqsInFlight   :: !Word,

       -- | The sum of the byte count of blocks expected from all in-flight
       -- fetch requests. This is a close approximation of the amount of data
       -- we expect to receive, assuming no failures.
       --
       -- We track this because we pipeline fetch requests and we want to keep
       -- some but not too much data in flight at once.
       --
       PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight  :: !SizeInBytes,

       -- | The points for the set of blocks that are currently in-flight.
       -- Note that since requests are for ranges of blocks this does not
       -- correspond to the number of requests in flight.
       --
       -- We track this because as part of the decision for which blocks to
       -- fetch from which peers we take into account what blocks are already
       -- in-flight with peers.
       --
       PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight :: Set (Point header),

       -- | The maximum slot of a block that /has ever been/ in flight for
       -- this peer.
       --
       -- We track this to more efficiently remove blocks that are already
       -- in-flight from the candidate fragments: blocks with a slot number
       -- higher than this one do not have to be filtered out.
       PeerFetchInFlight header -> MaxSlotNo
peerFetchMaxSlotNo      :: !MaxSlotNo
     }
  deriving (PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
(PeerFetchInFlight header -> PeerFetchInFlight header -> Bool)
-> (PeerFetchInFlight header -> PeerFetchInFlight header -> Bool)
-> Eq (PeerFetchInFlight header)
forall header.
StandardHash header =>
PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
$c/= :: forall header.
StandardHash header =>
PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
== :: PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
$c== :: forall header.
StandardHash header =>
PeerFetchInFlight header -> PeerFetchInFlight header -> Bool
Eq, Int -> PeerFetchInFlight header -> ShowS
[PeerFetchInFlight header] -> ShowS
PeerFetchInFlight header -> String
(Int -> PeerFetchInFlight header -> ShowS)
-> (PeerFetchInFlight header -> String)
-> ([PeerFetchInFlight header] -> ShowS)
-> Show (PeerFetchInFlight header)
forall header.
StandardHash header =>
Int -> PeerFetchInFlight header -> ShowS
forall header.
StandardHash header =>
[PeerFetchInFlight header] -> ShowS
forall header.
StandardHash header =>
PeerFetchInFlight header -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PeerFetchInFlight header] -> ShowS
$cshowList :: forall header.
StandardHash header =>
[PeerFetchInFlight header] -> ShowS
show :: PeerFetchInFlight header -> String
$cshow :: forall header.
StandardHash header =>
PeerFetchInFlight header -> String
showsPrec :: Int -> PeerFetchInFlight header -> ShowS
$cshowsPrec :: forall header.
StandardHash header =>
Int -> PeerFetchInFlight header -> ShowS
Show)

initialPeerFetchInFlight :: PeerFetchInFlight header
initialPeerFetchInFlight :: PeerFetchInFlight header
initialPeerFetchInFlight =
    PeerFetchInFlight :: forall header.
Word
-> SizeInBytes
-> Set (Point header)
-> MaxSlotNo
-> PeerFetchInFlight header
PeerFetchInFlight {
      peerFetchReqsInFlight :: Word
peerFetchReqsInFlight   = Word
0,
      peerFetchBytesInFlight :: SizeInBytes
peerFetchBytesInFlight  = SizeInBytes
0,
      peerFetchBlocksInFlight :: Set (Point header)
peerFetchBlocksInFlight = Set (Point header)
forall a. Set a
Set.empty,
      peerFetchMaxSlotNo :: MaxSlotNo
peerFetchMaxSlotNo      = MaxSlotNo
NoMaxSlotNo
    }

-- | Update the 'PeerFetchInFlight' in-flight tracking numbers.
--
-- Note that it takes both the existing \"old\" request, the \"added\" request
-- and resulting \"merged\" request. The relationship between the three is
-- @old <> added = merged@.
--
addHeadersInFlight :: HasHeader header
                   => (header -> SizeInBytes)
                   -> Maybe (FetchRequest header) -- ^ The old request (if any).
                   -> FetchRequest header         -- ^ The added request.
                   -> FetchRequest header         -- ^ The merged request.
                   -> PeerFetchInFlight header
                   -> PeerFetchInFlight header
addHeadersInFlight :: (header -> SizeInBytes)
-> Maybe (FetchRequest header)
-> FetchRequest header
-> FetchRequest header
-> PeerFetchInFlight header
-> PeerFetchInFlight header
addHeadersInFlight header -> SizeInBytes
blockFetchSize Maybe (FetchRequest header)
oldReq FetchRequest header
addedReq FetchRequest header
mergedReq PeerFetchInFlight header
inflight =

    -- This assertion checks the pre-condition 'addNewFetchRequest' that all
    -- requested blocks are new. This is true irrespective of fetch-request
    -- command merging.
    Bool -> PeerFetchInFlight header -> PeerFetchInFlight header
forall a. (?callStack::CallStack) => Bool -> a -> a
assert ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ header -> Point header
forall block. HasHeader block => block -> Point block
blockPoint header
header Point header -> Set (Point header) -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight
                | AnchoredFragment header
fragment <- FetchRequest header -> [AnchoredFragment header]
forall header. FetchRequest header -> [AnchoredFragment header]
fetchRequestFragments FetchRequest header
addedReq
                , header
header   <- AnchoredFragment header -> [header]
forall v a b. AnchoredSeq v a b -> [b]
AF.toOldestFirst AnchoredFragment header
fragment ]) (PeerFetchInFlight header -> PeerFetchInFlight header)
-> PeerFetchInFlight header -> PeerFetchInFlight header
forall a b. (a -> b) -> a -> b
$

    PeerFetchInFlight :: forall header.
Word
-> SizeInBytes
-> Set (Point header)
-> MaxSlotNo
-> PeerFetchInFlight header
PeerFetchInFlight {

      -- Fetch request merging makes the update of the number of in-flight
      -- requests rather subtle. See the 'FetchRequest' semigroup instance
      -- documentation for details. The upshot is that we have to look at the
      -- /difference/ in the number of fragments for the old request
      -- (if any) and merged request.
      peerFetchReqsInFlight :: Word
peerFetchReqsInFlight   = PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight
                              Word -> Word -> Word
forall a. Num a => a -> a -> a
+         FetchRequest header -> Word
forall header. FetchRequest header -> Word
numFetchReqs FetchRequest header
mergedReq
                              Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
-> (FetchRequest header -> Word)
-> Maybe (FetchRequest header)
-> Word
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word
0 FetchRequest header -> Word
forall header. FetchRequest header -> Word
numFetchReqs Maybe (FetchRequest header)
oldReq,

      -- For the bytes and blocks in flight however we can rely on the
      -- pre-condition that is asserted above.
      peerFetchBytesInFlight :: SizeInBytes
peerFetchBytesInFlight  = PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight
                              SizeInBytes -> SizeInBytes -> SizeInBytes
forall a. Num a => a -> a -> a
+ [SizeInBytes] -> SizeInBytes
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ header -> SizeInBytes
blockFetchSize header
header
                                    | AnchoredFragment header
fragment <- FetchRequest header -> [AnchoredFragment header]
forall header. FetchRequest header -> [AnchoredFragment header]
fetchRequestFragments FetchRequest header
addedReq
                                    , header
header   <- AnchoredFragment header -> [header]
forall v a b. AnchoredSeq v a b -> [b]
AF.toOldestFirst AnchoredFragment header
fragment ],

      peerFetchBlocksInFlight :: Set (Point header)
peerFetchBlocksInFlight = PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight
                    Set (Point header) -> Set (Point header) -> Set (Point header)
forall a. Ord a => Set a -> Set a -> Set a
`Set.union` [Point header] -> Set (Point header)
forall a. Ord a => [a] -> Set a
Set.fromList
                                  [ header -> Point header
forall block. HasHeader block => block -> Point block
blockPoint header
header
                                  | AnchoredFragment header
fragment <- FetchRequest header -> [AnchoredFragment header]
forall header. FetchRequest header -> [AnchoredFragment header]
fetchRequestFragments FetchRequest header
addedReq
                                  , header
header   <- AnchoredFragment header -> [header]
forall v a b. AnchoredSeq v a b -> [b]
AF.toOldestFirst AnchoredFragment header
fragment ],

      peerFetchMaxSlotNo :: MaxSlotNo
peerFetchMaxSlotNo      = PeerFetchInFlight header -> MaxSlotNo
forall header. PeerFetchInFlight header -> MaxSlotNo
peerFetchMaxSlotNo PeerFetchInFlight header
inflight
                          MaxSlotNo -> MaxSlotNo -> MaxSlotNo
forall a. Ord a => a -> a -> a
`max` FetchRequest header -> MaxSlotNo
forall header. HasHeader header => FetchRequest header -> MaxSlotNo
fetchRequestMaxSlotNo FetchRequest header
addedReq
    }
  where
    numFetchReqs :: FetchRequest header -> Word
    numFetchReqs :: FetchRequest header -> Word
numFetchReqs = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word)
-> (FetchRequest header -> Int) -> FetchRequest header -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AnchoredFragment header] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([AnchoredFragment header] -> Int)
-> (FetchRequest header -> [AnchoredFragment header])
-> FetchRequest header
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FetchRequest header -> [AnchoredFragment header]
forall header. FetchRequest header -> [AnchoredFragment header]
fetchRequestFragments

deleteHeaderInFlight :: HasHeader header
                     => (header -> SizeInBytes)
                     -> header
                     -> PeerFetchInFlight header
                     -> PeerFetchInFlight header
deleteHeaderInFlight :: (header -> SizeInBytes)
-> header -> PeerFetchInFlight header -> PeerFetchInFlight header
deleteHeaderInFlight header -> SizeInBytes
blockFetchSize header
header PeerFetchInFlight header
inflight =
    Bool -> PeerFetchInFlight header -> PeerFetchInFlight header
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight SizeInBytes -> SizeInBytes -> Bool
forall a. Ord a => a -> a -> Bool
>= header -> SizeInBytes
blockFetchSize header
header) (PeerFetchInFlight header -> PeerFetchInFlight header)
-> PeerFetchInFlight header -> PeerFetchInFlight header
forall a b. (a -> b) -> a -> b
$
    Bool -> PeerFetchInFlight header -> PeerFetchInFlight header
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (header -> Point header
forall block. HasHeader block => block -> Point block
blockPoint header
header Point header -> Set (Point header) -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight) (PeerFetchInFlight header -> PeerFetchInFlight header)
-> PeerFetchInFlight header -> PeerFetchInFlight header
forall a b. (a -> b) -> a -> b
$
    PeerFetchInFlight header
inflight {
      peerFetchBytesInFlight :: SizeInBytes
peerFetchBytesInFlight  = PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight
                              SizeInBytes -> SizeInBytes -> SizeInBytes
forall a. Num a => a -> a -> a
- header -> SizeInBytes
blockFetchSize header
header,

      peerFetchBlocksInFlight :: Set (Point header)
peerFetchBlocksInFlight = header -> Point header
forall block. HasHeader block => block -> Point block
blockPoint header
header
                   Point header -> Set (Point header) -> Set (Point header)
forall a. Ord a => a -> Set a -> Set a
`Set.delete` PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight
    }

deleteHeadersInFlight :: HasHeader header
                      => (header -> SizeInBytes)
                      -> [header]
                      -> PeerFetchInFlight header
                      -> PeerFetchInFlight header
deleteHeadersInFlight :: (header -> SizeInBytes)
-> [header] -> PeerFetchInFlight header -> PeerFetchInFlight header
deleteHeadersInFlight header -> SizeInBytes
blockFetchSize [header]
headers PeerFetchInFlight header
inflight =
    -- Reusing 'deleteHeaderInFlight' rather than a direct impl still
    -- gives us O(n log m) which is fine
    (PeerFetchInFlight header -> header -> PeerFetchInFlight header)
-> PeerFetchInFlight header -> [header] -> PeerFetchInFlight header
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((header -> PeerFetchInFlight header -> PeerFetchInFlight header)
-> PeerFetchInFlight header -> header -> PeerFetchInFlight header
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((header -> SizeInBytes)
-> header -> PeerFetchInFlight header -> PeerFetchInFlight header
forall header.
HasHeader header =>
(header -> SizeInBytes)
-> header -> PeerFetchInFlight header -> PeerFetchInFlight header
deleteHeaderInFlight header -> SizeInBytes
blockFetchSize)) PeerFetchInFlight header
inflight [header]
headers


newtype FetchRequest header =
        FetchRequest { FetchRequest header -> [AnchoredFragment header]
fetchRequestFragments :: [AnchoredFragment header] }
  deriving Int -> FetchRequest header -> ShowS
[FetchRequest header] -> ShowS
FetchRequest header -> String
(Int -> FetchRequest header -> ShowS)
-> (FetchRequest header -> String)
-> ([FetchRequest header] -> ShowS)
-> Show (FetchRequest header)
forall header.
(StandardHash header, Show header) =>
Int -> FetchRequest header -> ShowS
forall header.
(StandardHash header, Show header) =>
[FetchRequest header] -> ShowS
forall header.
(StandardHash header, Show header) =>
FetchRequest header -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FetchRequest header] -> ShowS
$cshowList :: forall header.
(StandardHash header, Show header) =>
[FetchRequest header] -> ShowS
show :: FetchRequest header -> String
$cshow :: forall header.
(StandardHash header, Show header) =>
FetchRequest header -> String
showsPrec :: Int -> FetchRequest header -> ShowS
$cshowsPrec :: forall header.
(StandardHash header, Show header) =>
Int -> FetchRequest header -> ShowS
Show

-- | We sometimes have the opportunity to merge fetch request fragments to
-- reduce the number of separate range request messages that we send. We send
-- one message per fragment. It is better to send fewer requests for bigger
-- ranges, rather than lots of requests for small ranges.
--
-- We never expect fetch requests to overlap (ie have blocks in common) but we
-- do expect a common case that requests will \"touch\" so that two ranges
-- could be merged into a single contiguous range.
--
-- This semigroup instance implements this merging when possible, otherwise the
-- two lists of fragments are just appended.
--
-- A consequence of merging and sending fewer request messages is that tracking
-- the number of requests in-flight a bit more subtle. To track this accurately
-- we have to look at the /old request/ as well a the updated request after any
-- merging. We meed to account for the /difference/ in the number of fragments
-- in the existing request (if any) and in new request.
--
instance HasHeader header => Semigroup (FetchRequest header) where
  FetchRequest afs :: [AnchoredFragment header]
afs@(AnchoredFragment header
_:[AnchoredFragment header]
_) <> :: FetchRequest header -> FetchRequest header -> FetchRequest header
<> FetchRequest bfs :: [AnchoredFragment header]
bfs@(AnchoredFragment header
_:[AnchoredFragment header]
_)
    | Just AnchoredFragment header
f <- AnchoredFragment header
-> AnchoredFragment header -> Maybe (AnchoredFragment header)
forall block.
HasHeader block =>
AnchoredFragment block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
AF.join ([AnchoredFragment header] -> AnchoredFragment header
forall a. [a] -> a
last [AnchoredFragment header]
afs) ([AnchoredFragment header] -> AnchoredFragment header
forall a. [a] -> a
head [AnchoredFragment header]
bfs)
    = [AnchoredFragment header] -> FetchRequest header
forall header. [AnchoredFragment header] -> FetchRequest header
FetchRequest ([AnchoredFragment header] -> [AnchoredFragment header]
forall a. [a] -> [a]
init [AnchoredFragment header]
afs [AnchoredFragment header]
-> [AnchoredFragment header] -> [AnchoredFragment header]
forall a. [a] -> [a] -> [a]
++ AnchoredFragment header
f AnchoredFragment header
-> [AnchoredFragment header] -> [AnchoredFragment header]
forall a. a -> [a] -> [a]
: [AnchoredFragment header] -> [AnchoredFragment header]
forall a. [a] -> [a]
tail [AnchoredFragment header]
bfs)

  FetchRequest [AnchoredFragment header]
afs <> FetchRequest [AnchoredFragment header]
bfs
    = [AnchoredFragment header] -> FetchRequest header
forall header. [AnchoredFragment header] -> FetchRequest header
FetchRequest ([AnchoredFragment header]
afs [AnchoredFragment header]
-> [AnchoredFragment header] -> [AnchoredFragment header]
forall a. [a] -> [a] -> [a]
++ [AnchoredFragment header]
bfs)

fetchRequestMaxSlotNo :: HasHeader header => FetchRequest header -> MaxSlotNo
fetchRequestMaxSlotNo :: FetchRequest header -> MaxSlotNo
fetchRequestMaxSlotNo (FetchRequest [AnchoredFragment header]
afs) =
    (MaxSlotNo -> MaxSlotNo -> MaxSlotNo)
-> MaxSlotNo -> [MaxSlotNo] -> MaxSlotNo
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' MaxSlotNo -> MaxSlotNo -> MaxSlotNo
forall a. Ord a => a -> a -> a
max MaxSlotNo
NoMaxSlotNo ([MaxSlotNo] -> MaxSlotNo) -> [MaxSlotNo] -> MaxSlotNo
forall a b. (a -> b) -> a -> b
$ (SlotNo -> MaxSlotNo) -> [SlotNo] -> [MaxSlotNo]
forall a b. (a -> b) -> [a] -> [b]
map SlotNo -> MaxSlotNo
MaxSlotNo ([SlotNo] -> [MaxSlotNo]) -> [SlotNo] -> [MaxSlotNo]
forall a b. (a -> b) -> a -> b
$
      (AnchoredFragment header -> Maybe SlotNo)
-> [AnchoredFragment header] -> [SlotNo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (WithOrigin SlotNo -> Maybe SlotNo
forall t. WithOrigin t -> Maybe t
withOriginToMaybe (WithOrigin SlotNo -> Maybe SlotNo)
-> (AnchoredFragment header -> WithOrigin SlotNo)
-> AnchoredFragment header
-> Maybe SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment header -> WithOrigin SlotNo
forall block.
HasHeader block =>
AnchoredFragment block -> WithOrigin SlotNo
AF.headSlot) [AnchoredFragment header]
afs

-- | Tracing types for the various events that change the state
-- (i.e. 'FetchClientStateVars') for a block fetch client.
--
-- Note that while these are all state changes, the 'AddedFetchRequest' occurs
-- in the decision thread while the other state changes occur in the block
-- fetch client threads.
--
data TraceFetchClientState header =

       -- | The block fetch decision thread has added a new fetch instruction
       -- consisting of one or more individual request ranges.
       --
       AddedFetchRequest
         (FetchRequest header)
         (PeerFetchInFlight header)
          PeerFetchInFlightLimits
         (PeerFetchStatus header)

       -- | Mark the point when the fetch client picks up the request added
       -- by the block fetch decision thread. Note that this event can happen
       -- fewer times than the 'AddedFetchRequest' due to fetch request merging.
       --
     | AcknowledgedFetchRequest
         (FetchRequest header)

       -- | Mark the point when fetch request for a fragment is actually sent
       -- over the wire.
     | SendFetchRequest
         (AnchoredFragment header)
         PeerGSV

       -- | Mark the start of receiving a streaming batch of blocks. This will
       -- be followed by one or more 'CompletedBlockFetch' and a final
       -- 'CompletedFetchBatch'.
       --
     | StartedFetchBatch
         (ChainRange (Point header))
         (PeerFetchInFlight header)
          PeerFetchInFlightLimits
         (PeerFetchStatus header)

       -- | Mark the completion of of receiving a single block within a
       -- streaming batch of blocks.
       --
     | CompletedBlockFetch
         (Point header)
         (PeerFetchInFlight header)
          PeerFetchInFlightLimits
         (PeerFetchStatus header)
         NominalDiffTime
         SizeInBytes

       -- | Mark the successful end of receiving a streaming batch of blocks
       --
     | CompletedFetchBatch
         (ChainRange (Point header))
         (PeerFetchInFlight header)
          PeerFetchInFlightLimits
         (PeerFetchStatus header)

       -- | If the other peer rejects our request then we have this event
       -- instead of 'StartedFetchBatch' and 'CompletedFetchBatch'.
       --
     | RejectedFetchBatch
         (ChainRange (Point header))
         (PeerFetchInFlight header)
          PeerFetchInFlightLimits
         (PeerFetchStatus header)

        -- | The client is terminating.  Log the number of outstanding
        -- requests.
        --
      | ClientTerminating Int
  deriving Int -> TraceFetchClientState header -> ShowS
[TraceFetchClientState header] -> ShowS
TraceFetchClientState header -> String
(Int -> TraceFetchClientState header -> ShowS)
-> (TraceFetchClientState header -> String)
-> ([TraceFetchClientState header] -> ShowS)
-> Show (TraceFetchClientState header)
forall header.
(StandardHash header, Show header) =>
Int -> TraceFetchClientState header -> ShowS
forall header.
(StandardHash header, Show header) =>
[TraceFetchClientState header] -> ShowS
forall header.
(StandardHash header, Show header) =>
TraceFetchClientState header -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TraceFetchClientState header] -> ShowS
$cshowList :: forall header.
(StandardHash header, Show header) =>
[TraceFetchClientState header] -> ShowS
show :: TraceFetchClientState header -> String
$cshow :: forall header.
(StandardHash header, Show header) =>
TraceFetchClientState header -> String
showsPrec :: Int -> TraceFetchClientState header -> ShowS
$cshowsPrec :: forall header.
(StandardHash header, Show header) =>
Int -> TraceFetchClientState header -> ShowS
Show


-- | Add a new fetch request for a single peer. This is used by the fetch
-- decision logic thread to add new fetch requests.
--
-- We have as a pre-condition that all requested blocks are new, i.e. none
-- should appear in the existing 'peerFetchBlocksInFlight'. This is a
-- relatively easy precondition to satisfy since the decision logic can filter
-- its requests based on this in-flight blocks state, and this operation is the
-- only operation that grows the in-flight blocks, and is only used by the
-- fetch decision logic thread.
--
addNewFetchRequest :: (MonadSTM m, HasHeader header)
                   => Tracer m (TraceFetchClientState header)
                   -> (header -> SizeInBytes)
                   -> FetchRequest header
                   -> PeerGSV
                   -> FetchClientStateVars m header
                   -> m (PeerFetchStatus header)
addNewFetchRequest :: Tracer m (TraceFetchClientState header)
-> (header -> SizeInBytes)
-> FetchRequest header
-> PeerGSV
-> FetchClientStateVars m header
-> m (PeerFetchStatus header)
addNewFetchRequest Tracer m (TraceFetchClientState header)
tracer header -> SizeInBytes
blockFetchSize FetchRequest header
addedReq PeerGSV
gsvs
                   FetchClientStateVars{
                     TFetchRequestVar m header
fetchClientRequestVar :: TFetchRequestVar m header
fetchClientRequestVar :: forall (m :: * -> *) header.
FetchClientStateVars m header -> TFetchRequestVar m header
fetchClientRequestVar,
                     StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar,
                     StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                   } = do
    (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus') <- STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
STM m a -> m a
atomically (STM m (PeerFetchInFlight header, PeerFetchStatus header)
 -> m (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall a b. (a -> b) -> a -> b
$ do

      -- Add a new fetch request, or extend or merge with the existing
      -- unacknowledged one.
      --
      -- Fetch request merging makes the update of the in-flight stats subtle.
      -- See the 'FetchRequest' semigroup instance documentation for details.
      -- The upshot is that our in-flight stats update is based on the existing
      -- \"old\" request (if any), the \"added\" one and the resulting
      -- \"merged\" one.
      --
      Maybe (FetchRequest header)
oldReq    <- TFetchRequestVar m header -> STM m (Maybe (FetchRequest header))
forall (m :: * -> *) header.
MonadSTM m =>
TFetchRequestVar m header -> STM m (Maybe (FetchRequest header))
peekTFetchRequestVar TFetchRequestVar m header
fetchClientRequestVar
      FetchRequest header
mergedReq <- TFetchRequestVar m header
-> FetchRequest header
-> PeerGSV
-> PeerFetchInFlightLimits
-> STM m (FetchRequest header)
forall (m :: * -> *) header.
(MonadSTM m, HasHeader header) =>
TFetchRequestVar m header
-> FetchRequest header
-> PeerGSV
-> PeerFetchInFlightLimits
-> STM m (FetchRequest header)
writeTFetchRequestVar TFetchRequestVar m header
fetchClientRequestVar
                                           FetchRequest header
addedReq PeerGSV
gsvs PeerFetchInFlightLimits
inflightlimits

      -- Update our in-flight stats
      PeerFetchInFlight header
inflight <- StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
      let !inflight' :: PeerFetchInFlight header
inflight' = (header -> SizeInBytes)
-> Maybe (FetchRequest header)
-> FetchRequest header
-> FetchRequest header
-> PeerFetchInFlight header
-> PeerFetchInFlight header
forall header.
HasHeader header =>
(header -> SizeInBytes)
-> Maybe (FetchRequest header)
-> FetchRequest header
-> FetchRequest header
-> PeerFetchInFlight header
-> PeerFetchInFlight header
addHeadersInFlight header -> SizeInBytes
blockFetchSize
                                          Maybe (FetchRequest header)
oldReq FetchRequest header
addedReq FetchRequest header
mergedReq
                                          PeerFetchInFlight header
inflight
      StrictTVar m (PeerFetchInFlight header)
-> PeerFetchInFlight header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar PeerFetchInFlight header
inflight'

      -- Set the peer status to busy if it went over the high watermark.
      PeerFetchStatus header
currentStatus' <- (PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) header.
(MonadSTM m, HasHeader header) =>
(PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
updateCurrentStatus
                          (PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
forall header.
PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
busyIfOverHighWatermark PeerFetchInFlightLimits
inflightlimits)
                          StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                          PeerFetchInFlight header
inflight'

      --TODO: think about status aberrant

      (PeerFetchInFlight header, PeerFetchStatus header)
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus')

    Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (TraceFetchClientState header -> m ())
-> TraceFetchClientState header -> m ()
forall a b. (a -> b) -> a -> b
$
      FetchRequest header
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
forall header.
FetchRequest header
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
AddedFetchRequest
        FetchRequest header
addedReq
        PeerFetchInFlight header
inflight' PeerFetchInFlightLimits
inflightlimits
        PeerFetchStatus header
currentStatus'
    PeerFetchStatus header -> m (PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return PeerFetchStatus header
currentStatus'
  where
    inflightlimits :: PeerFetchInFlightLimits
inflightlimits = PeerGSV -> PeerFetchInFlightLimits
calculatePeerFetchInFlightLimits PeerGSV
gsvs
    --TODO: if recalculating the limits here is expensive we can pass them
    -- along with the fetch request and the gsvs


-- | This is used by the fetch client threads.
--
acknowledgeFetchRequest :: MonadSTM m
                        => Tracer m (TraceFetchClientState header)
                        -> ControlMessageSTM m
                        -> FetchClientStateVars m header
                        -> m (Maybe
                               ( FetchRequest header
                               , PeerGSV
                               , PeerFetchInFlightLimits ))
acknowledgeFetchRequest :: Tracer m (TraceFetchClientState header)
-> ControlMessageSTM m
-> FetchClientStateVars m header
-> m (Maybe
        (FetchRequest header, PeerGSV, PeerFetchInFlightLimits))
acknowledgeFetchRequest Tracer m (TraceFetchClientState header)
tracer ControlMessageSTM m
controlMessageSTM FetchClientStateVars {TFetchRequestVar m header
fetchClientRequestVar :: TFetchRequestVar m header
fetchClientRequestVar :: forall (m :: * -> *) header.
FetchClientStateVars m header -> TFetchRequestVar m header
fetchClientRequestVar} = do
    Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
result <-
      ControlMessageSTM m
-> STM m (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
-> m (Maybe
        (FetchRequest header, PeerGSV, PeerFetchInFlightLimits))
forall (m :: * -> *) a.
MonadSTM m =>
ControlMessageSTM m -> STM m a -> m (Maybe a)
timeoutWithControlMessage ControlMessageSTM m
controlMessageSTM (TFetchRequestVar m header
-> STM m (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
forall (m :: * -> *) header.
MonadSTM m =>
TFetchRequestVar m header
-> STM m (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
takeTFetchRequestVar TFetchRequestVar m header
fetchClientRequestVar)
    case Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
result of
      Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
Nothing -> Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
-> m (Maybe
        (FetchRequest header, PeerGSV, PeerFetchInFlightLimits))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
result
      Just (FetchRequest header
request, PeerGSV
_, PeerFetchInFlightLimits
_) -> do
        Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (FetchRequest header -> TraceFetchClientState header
forall header. FetchRequest header -> TraceFetchClientState header
AcknowledgedFetchRequest FetchRequest header
request)
        Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
-> m (Maybe
        (FetchRequest header, PeerGSV, PeerFetchInFlightLimits))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
result

startedFetchBatch :: MonadSTM m
                  => Tracer m (TraceFetchClientState header)
                  -> PeerFetchInFlightLimits
                  -> ChainRange (Point header)
                  -> FetchClientStateVars m header
                  -> m ()
startedFetchBatch :: Tracer m (TraceFetchClientState header)
-> PeerFetchInFlightLimits
-> ChainRange (Point header)
-> FetchClientStateVars m header
-> m ()
startedFetchBatch Tracer m (TraceFetchClientState header)
tracer PeerFetchInFlightLimits
inflightlimits ChainRange (Point header)
range
                  FetchClientStateVars {
                    StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar,
                    StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                  } = do
    (PeerFetchInFlight header
inflight, PeerFetchStatus header
currentStatus) <-
      STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
STM m a -> m a
atomically (STM m (PeerFetchInFlight header, PeerFetchStatus header)
 -> m (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall a b. (a -> b) -> a -> b
$ (,) (PeerFetchInFlight header
 -> PeerFetchStatus header
 -> (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header)
-> STM
     m
     (PeerFetchStatus header
      -> (PeerFetchInFlight header, PeerFetchStatus header))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
                       STM
  m
  (PeerFetchStatus header
   -> (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchStatus header)
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> StrictTVar m (PeerFetchStatus header)
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
    Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (TraceFetchClientState header -> m ())
-> TraceFetchClientState header -> m ()
forall a b. (a -> b) -> a -> b
$
      ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
forall header.
ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
StartedFetchBatch
        ChainRange (Point header)
range
        PeerFetchInFlight header
inflight PeerFetchInFlightLimits
inflightlimits
        PeerFetchStatus header
currentStatus

completeBlockDownload :: (MonadSTM m, HasHeader header)
                      => Tracer m (TraceFetchClientState header)
                      -> (header -> SizeInBytes)
                      -> PeerFetchInFlightLimits
                      -> header
                      -> NominalDiffTime
                      -> FetchClientStateVars m header
                      -> m ()

completeBlockDownload :: Tracer m (TraceFetchClientState header)
-> (header -> SizeInBytes)
-> PeerFetchInFlightLimits
-> header
-> NominalDiffTime
-> FetchClientStateVars m header
-> m ()
completeBlockDownload Tracer m (TraceFetchClientState header)
tracer header -> SizeInBytes
blockFetchSize PeerFetchInFlightLimits
inflightlimits header
header NominalDiffTime
blockDelay
                      FetchClientStateVars {
                        StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar,
                        StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                      } = do
    (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus') <- STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
STM m a -> m a
atomically (STM m (PeerFetchInFlight header, PeerFetchStatus header)
 -> m (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall a b. (a -> b) -> a -> b
$ do
      PeerFetchInFlight header
inflight <- StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
      let !inflight' :: PeerFetchInFlight header
inflight' = (header -> SizeInBytes)
-> header -> PeerFetchInFlight header -> PeerFetchInFlight header
forall header.
HasHeader header =>
(header -> SizeInBytes)
-> header -> PeerFetchInFlight header -> PeerFetchInFlight header
deleteHeaderInFlight header -> SizeInBytes
blockFetchSize header
header PeerFetchInFlight header
inflight
      StrictTVar m (PeerFetchInFlight header)
-> PeerFetchInFlight header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar PeerFetchInFlight header
inflight'

      -- Set our status to ready if we're under the low watermark.
      PeerFetchStatus header
currentStatus' <- (PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) header.
(MonadSTM m, HasHeader header) =>
(PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
updateCurrentStatus
                          (PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
forall header.
PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
readyIfUnderLowWatermark PeerFetchInFlightLimits
inflightlimits)
                          StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                          PeerFetchInFlight header
inflight'

    -- TODO: when do we reset the status from PeerFetchStatusAberrant
    -- to PeerFetchStatusReady/Busy?

      (PeerFetchInFlight header, PeerFetchStatus header)
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus')

    Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (TraceFetchClientState header -> m ())
-> TraceFetchClientState header -> m ()
forall a b. (a -> b) -> a -> b
$
      Point header
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> NominalDiffTime
-> SizeInBytes
-> TraceFetchClientState header
forall header.
Point header
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> NominalDiffTime
-> SizeInBytes
-> TraceFetchClientState header
CompletedBlockFetch
        (header -> Point header
forall block. HasHeader block => block -> Point block
blockPoint header
header)
        PeerFetchInFlight header
inflight' PeerFetchInFlightLimits
inflightlimits
        PeerFetchStatus header
currentStatus'
        NominalDiffTime
blockDelay
        (header -> SizeInBytes
blockFetchSize header
header)


completeFetchBatch :: MonadSTM m
                   => Tracer m (TraceFetchClientState header)
                   -> PeerFetchInFlightLimits
                   -> ChainRange (Point header)
                   -> FetchClientStateVars m header
                   -> m ()
completeFetchBatch :: Tracer m (TraceFetchClientState header)
-> PeerFetchInFlightLimits
-> ChainRange (Point header)
-> FetchClientStateVars m header
-> m ()
completeFetchBatch Tracer m (TraceFetchClientState header)
tracer PeerFetchInFlightLimits
inflightlimits ChainRange (Point header)
range
                   FetchClientStateVars {
                     StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar,
                     StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                   } = do
    (PeerFetchInFlight header
inflight, PeerFetchStatus header
currentStatus) <- STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
STM m a -> m a
atomically (STM m (PeerFetchInFlight header, PeerFetchStatus header)
 -> m (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall a b. (a -> b) -> a -> b
$ do
      PeerFetchInFlight header
inflight <- StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
      let !inflight' :: PeerFetchInFlight header
inflight' =
            Bool -> PeerFetchInFlight header -> PeerFetchInFlight header
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (if PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
1
                       then PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight SizeInBytes -> SizeInBytes -> Bool
forall a. Eq a => a -> a -> Bool
== SizeInBytes
0
                         Bool -> Bool -> Bool
&& Set (Point header) -> Bool
forall a. Set a -> Bool
Set.null (PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight)
                       else Bool
True)
            PeerFetchInFlight header
inflight {
              peerFetchReqsInFlight :: Word
peerFetchReqsInFlight = PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1
            }
      StrictTVar m (PeerFetchInFlight header)
-> PeerFetchInFlight header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar PeerFetchInFlight header
inflight'
      PeerFetchStatus header
currentStatus' <- StrictTVar m (PeerFetchStatus header)
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar STM m (PeerFetchStatus header)
-> (PeerFetchStatus header -> STM m (PeerFetchStatus header))
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        PeerFetchStatusReady Set (Point header)
bs IsIdle
IsNotIdle
          | Set (Point header) -> Bool
forall a. Set a -> Bool
Set.null Set (Point header)
bs
            Bool -> Bool -> Bool
&& Word
0 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight'
          -> let status :: PeerFetchStatus header
status = Set (Point header) -> IsIdle -> PeerFetchStatus header
forall header.
Set (Point header) -> IsIdle -> PeerFetchStatus header
PeerFetchStatusReady Set (Point header)
forall a. Set a
Set.empty IsIdle
IsIdle
             in PeerFetchStatus header
forall header. PeerFetchStatus header
status PeerFetchStatus header
-> STM m () -> STM m (PeerFetchStatus header)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ StrictTVar m (PeerFetchStatus header)
-> PeerFetchStatus header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar PeerFetchStatus header
forall header. PeerFetchStatus header
status
        PeerFetchStatus header
currentStatus -> PeerFetchStatus header -> STM m (PeerFetchStatus header)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PeerFetchStatus header
currentStatus

      (PeerFetchInFlight header, PeerFetchStatus header)
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus')

    Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (TraceFetchClientState header -> m ())
-> TraceFetchClientState header -> m ()
forall a b. (a -> b) -> a -> b
$
      ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
forall header.
ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
CompletedFetchBatch
        ChainRange (Point header)
range
        PeerFetchInFlight header
inflight PeerFetchInFlightLimits
inflightlimits
        PeerFetchStatus header
currentStatus


rejectedFetchBatch :: (MonadSTM m, HasHeader header)
                   => Tracer m (TraceFetchClientState header)
                   -> (header -> SizeInBytes)
                   -> PeerFetchInFlightLimits
                   -> ChainRange (Point header)
                   -> [header]
                   -> FetchClientStateVars m header
                   -> m ()
rejectedFetchBatch :: Tracer m (TraceFetchClientState header)
-> (header -> SizeInBytes)
-> PeerFetchInFlightLimits
-> ChainRange (Point header)
-> [header]
-> FetchClientStateVars m header
-> m ()
rejectedFetchBatch Tracer m (TraceFetchClientState header)
tracer header -> SizeInBytes
blockFetchSize PeerFetchInFlightLimits
inflightlimits ChainRange (Point header)
range [header]
headers
                   FetchClientStateVars {
                     StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar,
                     StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar :: forall (m :: * -> *) header.
FetchClientStateVars m header
-> StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                   } = do
    (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus') <- STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
STM m a -> m a
atomically (STM m (PeerFetchInFlight header, PeerFetchStatus header)
 -> m (PeerFetchInFlight header, PeerFetchStatus header))
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
-> m (PeerFetchInFlight header, PeerFetchStatus header)
forall a b. (a -> b) -> a -> b
$ do
      PeerFetchInFlight header
inflight <- StrictTVar m (PeerFetchInFlight header)
-> STM m (PeerFetchInFlight header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar
      let !inflight' :: PeerFetchInFlight header
inflight' =
            ((header -> SizeInBytes)
-> [header] -> PeerFetchInFlight header -> PeerFetchInFlight header
forall header.
HasHeader header =>
(header -> SizeInBytes)
-> [header] -> PeerFetchInFlight header -> PeerFetchInFlight header
deleteHeadersInFlight header -> SizeInBytes
blockFetchSize [header]
headers PeerFetchInFlight header
inflight) {
              peerFetchReqsInFlight :: Word
peerFetchReqsInFlight = PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1
            }
      StrictTVar m (PeerFetchInFlight header)
-> PeerFetchInFlight header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchInFlight header)
fetchClientInFlightVar PeerFetchInFlight header
inflight'

      -- Set our status to ready if we're under the low watermark.
      PeerFetchStatus header
currentStatus' <- (PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) header.
(MonadSTM m, HasHeader header) =>
(PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
updateCurrentStatus
                          (PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
forall header.
PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
readyIfUnderLowWatermark PeerFetchInFlightLimits
inflightlimits)
                          StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
                          PeerFetchInFlight header
inflight'

    -- TODO: when do we reset the status from PeerFetchStatusAberrant
    -- to PeerFetchStatusReady/Busy?

      (PeerFetchInFlight header, PeerFetchStatus header)
-> STM m (PeerFetchInFlight header, PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return (PeerFetchInFlight header
inflight', PeerFetchStatus header
currentStatus')

    Tracer m (TraceFetchClientState header)
-> TraceFetchClientState header -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceFetchClientState header)
tracer (TraceFetchClientState header -> m ())
-> TraceFetchClientState header -> m ()
forall a b. (a -> b) -> a -> b
$
      ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
forall header.
ChainRange (Point header)
-> PeerFetchInFlight header
-> PeerFetchInFlightLimits
-> PeerFetchStatus header
-> TraceFetchClientState header
RejectedFetchBatch
        ChainRange (Point header)
range
        PeerFetchInFlight header
inflight' PeerFetchInFlightLimits
inflightlimits
        PeerFetchStatus header
currentStatus'


-- | Given a 'PeerFetchInFlight' update the 'PeerFetchStatus' accordingly.
-- This can be used with one of two policies:
--
-- * 'busyIfOverHighWatermark'
-- * 'readyIfUnderLowWatermark'
--
updateCurrentStatus :: (MonadSTM m, HasHeader header)
                    => (PeerFetchInFlight header -> PeerFetchStatus header)
                    -> StrictTVar m (PeerFetchStatus header)
                    -> PeerFetchInFlight header
                    -> STM m (PeerFetchStatus header)
updateCurrentStatus :: (PeerFetchInFlight header -> PeerFetchStatus header)
-> StrictTVar m (PeerFetchStatus header)
-> PeerFetchInFlight header
-> STM m (PeerFetchStatus header)
updateCurrentStatus PeerFetchInFlight header -> PeerFetchStatus header
decideCurrentStatus StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar PeerFetchInFlight header
inflight = do

    let currentStatus' :: PeerFetchStatus header
currentStatus' = PeerFetchInFlight header -> PeerFetchStatus header
decideCurrentStatus PeerFetchInFlight header
inflight

    -- Only update the variable if it changed, to avoid spurious wakeups.
    PeerFetchStatus header
currentStatus <- StrictTVar m (PeerFetchStatus header)
-> STM m (PeerFetchStatus header)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar
    Bool -> STM m () -> STM m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PeerFetchStatus header
currentStatus' PeerFetchStatus header -> PeerFetchStatus header -> Bool
forall a. Eq a => a -> a -> Bool
/= PeerFetchStatus header
currentStatus) (STM m () -> STM m ()) -> STM m () -> STM m ()
forall a b. (a -> b) -> a -> b
$
      StrictTVar m (PeerFetchStatus header)
-> PeerFetchStatus header -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, ?callStack::CallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (PeerFetchStatus header)
fetchClientStatusVar PeerFetchStatus header
currentStatus'
    PeerFetchStatus header -> STM m (PeerFetchStatus header)
forall (m :: * -> *) a. Monad m => a -> m a
return PeerFetchStatus header
currentStatus'

-- | Return 'PeerFetchStatusBusy' if we're now over the high watermark.
--
busyIfOverHighWatermark :: PeerFetchInFlightLimits
                        -> PeerFetchInFlight header
                        -> PeerFetchStatus header
busyIfOverHighWatermark :: PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
busyIfOverHighWatermark PeerFetchInFlightLimits
inflightlimits PeerFetchInFlight header
inflight
  | PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight SizeInBytes -> SizeInBytes -> Bool
forall a. Ord a => a -> a -> Bool
>= PeerFetchInFlightLimits -> SizeInBytes
inFlightBytesHighWatermark PeerFetchInFlightLimits
inflightlimits
  = PeerFetchStatus header
forall header. PeerFetchStatus header
PeerFetchStatusBusy
  | Bool
otherwise
  = Set (Point header) -> IsIdle -> PeerFetchStatus header
forall header.
Set (Point header) -> IsIdle -> PeerFetchStatus header
PeerFetchStatusReady
      (PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight)
      (Bool -> IsIdle
idleIf (Word
0 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight))

-- | Return 'PeerFetchStatusReady' if we're now under the low watermark.
--
readyIfUnderLowWatermark :: PeerFetchInFlightLimits
                         -> PeerFetchInFlight header
                         -> PeerFetchStatus header
readyIfUnderLowWatermark :: PeerFetchInFlightLimits
-> PeerFetchInFlight header -> PeerFetchStatus header
readyIfUnderLowWatermark PeerFetchInFlightLimits
inflightlimits PeerFetchInFlight header
inflight
  | PeerFetchInFlight header -> SizeInBytes
forall header. PeerFetchInFlight header -> SizeInBytes
peerFetchBytesInFlight PeerFetchInFlight header
inflight SizeInBytes -> SizeInBytes -> Bool
forall a. Ord a => a -> a -> Bool
<= PeerFetchInFlightLimits -> SizeInBytes
inFlightBytesLowWatermark PeerFetchInFlightLimits
inflightlimits
  = Set (Point header) -> IsIdle -> PeerFetchStatus header
forall header.
Set (Point header) -> IsIdle -> PeerFetchStatus header
PeerFetchStatusReady
      (PeerFetchInFlight header -> Set (Point header)
forall header. PeerFetchInFlight header -> Set (Point header)
peerFetchBlocksInFlight PeerFetchInFlight header
inflight)
      (Bool -> IsIdle
idleIf (Word
0 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== PeerFetchInFlight header -> Word
forall header. PeerFetchInFlight header -> Word
peerFetchReqsInFlight PeerFetchInFlight header
inflight))
  | Bool
otherwise
  = PeerFetchStatus header
forall header. PeerFetchStatus header
PeerFetchStatusBusy


--
-- STM TFetchRequestVar
--

-- | The 'TFetchRequestVar' is a 'TMergeVar' for communicating the
-- 'FetchRequest's from the logic thread to a fetch client thread.
--
-- The pattern is that the logic thread determines a current request and this
-- is written to the var with 'writeTMergeVar'. The fetch client thread uses
-- 'takeTMergeVar', which blocks until a value is available. On the other hand,
-- 'writeTMergeVar' never blocks, if a value is already present then it
-- overwrites it. This makes sense for the fetch requests because if a fetch
-- client has not accepted the request yet then we can replace it with the
-- request based on the more recent state.
--
type TFetchRequestVar m header =
       TMergeVar m (FetchRequest header,
                    Last PeerGSV,
                    Last PeerFetchInFlightLimits)

newTFetchRequestVar :: MonadSTM m => STM m (TFetchRequestVar m header)
newTFetchRequestVar :: STM m (TFetchRequestVar m header)
newTFetchRequestVar = STM m (TFetchRequestVar m header)
forall (m :: * -> *) a. MonadSTM m => STM m (TMergeVar m a)
newTMergeVar


-- | Write to the underlying 'TMergeVar' and return the updated 'FetchRequest'
--
writeTFetchRequestVar :: (MonadSTM m, HasHeader header)
                      => TFetchRequestVar m header
                      -> FetchRequest header
                      -> PeerGSV
                      -> PeerFetchInFlightLimits
                      -> STM m (FetchRequest header)
writeTFetchRequestVar :: TFetchRequestVar m header
-> FetchRequest header
-> PeerGSV
-> PeerFetchInFlightLimits
-> STM m (FetchRequest header)
writeTFetchRequestVar TFetchRequestVar m header
v FetchRequest header
r PeerGSV
g PeerFetchInFlightLimits
l = do
    (FetchRequest header
r', Last PeerGSV
_, Last PeerFetchInFlightLimits
_) <- TFetchRequestVar m header
-> (FetchRequest header, Last PeerGSV,
    Last PeerFetchInFlightLimits)
-> STM
     m (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
forall (m :: * -> *) a.
(MonadSTM m, Semigroup a) =>
TMergeVar m a -> a -> STM m a
writeTMergeVar TFetchRequestVar m header
v (FetchRequest header
r, PeerGSV -> Last PeerGSV
forall a. a -> Last a
Last PeerGSV
g, PeerFetchInFlightLimits -> Last PeerFetchInFlightLimits
forall a. a -> Last a
Last PeerFetchInFlightLimits
l)
    FetchRequest header -> STM m (FetchRequest header)
forall (m :: * -> *) a. Monad m => a -> m a
return FetchRequest header
r'

peekTFetchRequestVar :: MonadSTM m
                     => TFetchRequestVar m header
                     -> STM m (Maybe (FetchRequest header))
peekTFetchRequestVar :: TFetchRequestVar m header -> STM m (Maybe (FetchRequest header))
peekTFetchRequestVar TFetchRequestVar m header
v = ((FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
 -> FetchRequest header)
-> Maybe
     (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
-> Maybe (FetchRequest header)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(FetchRequest header
x, Last PeerGSV
_, Last PeerFetchInFlightLimits
_) -> FetchRequest header
x) (Maybe
   (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
 -> Maybe (FetchRequest header))
-> STM
     m
     (Maybe
        (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits))
-> STM m (Maybe (FetchRequest header))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TFetchRequestVar m header
-> STM
     m
     (Maybe
        (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits))
forall (m :: * -> *) a.
MonadSTM m =>
TMergeVar m a -> STM m (Maybe a)
tryReadTMergeVar TFetchRequestVar m header
v

takeTFetchRequestVar :: MonadSTM m
                     => TFetchRequestVar m header
                     -> STM m (FetchRequest header,
                               PeerGSV,
                               PeerFetchInFlightLimits)
takeTFetchRequestVar :: TFetchRequestVar m header
-> STM m (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
takeTFetchRequestVar TFetchRequestVar m header
v = (\(FetchRequest header
r,Last PeerGSV
g,Last PeerFetchInFlightLimits
l) -> (FetchRequest header
r, Last PeerGSV -> PeerGSV
forall a. Last a -> a
getLast Last PeerGSV
g, Last PeerFetchInFlightLimits -> PeerFetchInFlightLimits
forall a. Last a -> a
getLast Last PeerFetchInFlightLimits
l))
                     ((FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
 -> (FetchRequest header, PeerGSV, PeerFetchInFlightLimits))
-> STM
     m (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
-> STM m (FetchRequest header, PeerGSV, PeerFetchInFlightLimits)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TFetchRequestVar m header
-> STM
     m (FetchRequest header, Last PeerGSV, Last PeerFetchInFlightLimits)
forall (m :: * -> *) a. MonadSTM m => TMergeVar m a -> STM m a
takeTMergeVar TFetchRequestVar m header
v

--
-- STM TMergeVar mini-abstraction
--

-- | The 'TMergeVar' is like a 'TMVar' in that we take it, leaving it empty.
-- Unlike an ordinary 'TMVar' with a blocking \'put\' operation, it has a
-- non-blocking combiing write operation: if a value is already present then
-- the values are combined using the 'Semigroup' operator.
--
-- This is used much like a 'TMVar' as a one-place queue between threads but
-- with the property that we can \"improve\" the current value (if any).
--
newtype TMergeVar m a = TMergeVar (StrictTMVar m a)

newTMergeVar :: MonadSTM m => STM m (TMergeVar m a)
newTMergeVar :: STM m (TMergeVar m a)
newTMergeVar = StrictTMVar m a -> TMergeVar m a
forall (m :: * -> *) a. StrictTMVar m a -> TMergeVar m a
TMergeVar (StrictTMVar m a -> TMergeVar m a)
-> STM m (StrictTMVar m a) -> STM m (TMergeVar m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m (StrictTMVar m a)
forall (m :: * -> *) a. MonadSTM m => STM m (StrictTMVar m a)
newEmptyTMVar

-- | Merge the current value with the given one and store it, return the updated
-- value.
--
writeTMergeVar :: (MonadSTM m, Semigroup a) => TMergeVar m a -> a -> STM m a
writeTMergeVar :: TMergeVar m a -> a -> STM m a
writeTMergeVar (TMergeVar StrictTMVar m a
v) a
x = do
    Maybe a
mx0 <- StrictTMVar m a -> STM m (Maybe a)
forall (m :: * -> *) a.
MonadSTM m =>
StrictTMVar m a -> STM m (Maybe a)
tryTakeTMVar StrictTMVar m a
v
    case Maybe a
mx0 of
      Maybe a
Nothing -> a
x  a -> STM m () -> STM m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ StrictTMVar m a -> a -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTMVar m a -> a -> STM m ()
putTMVar StrictTMVar m a
v a
x
      Just a
x0 -> a
x' a -> STM m () -> STM m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ StrictTMVar m a -> a -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTMVar m a -> a -> STM m ()
putTMVar StrictTMVar m a
v a
x' where !x' :: a
x' = a
x0 a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
x

takeTMergeVar :: MonadSTM m => TMergeVar m a -> STM m a
takeTMergeVar :: TMergeVar m a -> STM m a
takeTMergeVar (TMergeVar StrictTMVar m a
v) = StrictTMVar m a -> STM m a
forall (m :: * -> *) a. MonadSTM m => StrictTMVar m a -> STM m a
takeTMVar StrictTMVar m a
v

tryReadTMergeVar :: MonadSTM m
                 => TMergeVar m a
                 -> STM m (Maybe a)
tryReadTMergeVar :: TMergeVar m a -> STM m (Maybe a)
tryReadTMergeVar (TMergeVar StrictTMVar m a
v) = StrictTMVar m a -> STM m (Maybe a)
forall (m :: * -> *) a.
MonadSTM m =>
StrictTMVar m a -> STM m (Maybe a)
tryReadTMVar StrictTMVar m a
v