@@ -125,7 +125,7 @@ module Ouroboros.Network.BlockFetch.Decision.BulkSync (
125125 fetchDecisionsBulkSyncM
126126) where
127127
128- import Control.Monad (filterM , guard , when )
128+ import Control.Monad (filterM , guard )
129129import Control.Monad.Class.MonadTime.SI (MonadMonotonicTime (getMonotonicTime ), addTime )
130130import Control.Monad.Trans.Maybe (MaybeT (MaybeT , runMaybeT ))
131131import Control.Monad.Writer.Strict (Writer , runWriter , MonadWriter (tell ))
@@ -135,16 +135,17 @@ import Data.Function (on)
135135import qualified Data.List as List
136136import Data.List.NonEmpty (nonEmpty )
137137import qualified Data.List.NonEmpty as NE
138- import Data.Maybe (listToMaybe , mapMaybe , maybeToList , isNothing )
139138import qualified Data.Set as Set
139+ import Data.Maybe (mapMaybe , maybeToList )
140140import Data.Ord (Down (Down ))
141141
142142import Cardano.Prelude (partitionEithers )
143143
144144import Ouroboros.Network.AnchoredFragment (AnchoredFragment , headBlockNo )
145145import qualified Ouroboros.Network.AnchoredFragment as AF
146146import Ouroboros.Network.Block
147- import Ouroboros.Network.BlockFetch.ClientState (FetchRequest (.. ), PeersOrder (.. ), PeerFetchInFlight (.. ))
147+ import Ouroboros.Network.BlockFetch.ClientState
148+ (FetchRequest (.. ), PeersOrder (.. ), peerFetchBlocksInFlight )
148149import Ouroboros.Network.BlockFetch.ConsensusInterface (FetchMode (FetchModeBulkSync ))
149150import Ouroboros.Network.BlockFetch.DeltaQ (calculatePeerFetchInFlightLimits )
150151import Ouroboros.Network.BlockFetch.ConsensusInterface (ChainSelStarvation (.. ))
@@ -202,21 +203,12 @@ fetchDecisionsBulkSyncM
202203 demoteCSJDynamo
203204 )
204205 candidatesAndPeers = do
205- peersOrder@ PeersOrder {peersOrderCurrent} <-
206+ peersOrder <-
206207 checkLastChainSelStarvation $
207208 alignPeersOrderWithActualPeers
208209 (map (peerInfoPeer . snd ) candidatesAndPeers)
209210 peersOrder0
210211
211- let peersOrderCurrentInfo = do
212- currentPeer <- peersOrderCurrent
213- listToMaybe
214- [ peerCurrentInfo
215- | (_, peerCurrentInfo@ (_, inflight, _, peer, _)) <- candidatesAndPeers
216- , peer == currentPeer
217- , not (Set. null (peerFetchBlocksInFlight inflight))
218- ]
219-
220212 -- Compute the actual block fetch decision. This contains only declines and
221213 -- at most one request. 'theDecision' is therefore a 'Maybe'.
222214 let (theDecision, declines) =
@@ -226,20 +218,20 @@ fetchDecisionsBulkSyncM
226218 fetchedBlocks
227219 fetchedMaxSlotNo
228220 peersOrder
229- peersOrderCurrentInfo
230221 candidatesAndPeers
231222
232223 -- If there were no blocks in flight, then this will be the first request,
233224 -- so we take a new current time.
234- when (isNothing peersOrderCurrent) $
235- case theDecision of
236- Just (_, peerInfo) -> do
225+ case theDecision of
226+ Just (_, peerInfo@ (_, inflight, _, _, _))
227+ | Set. null (peerFetchBlocksInFlight inflight)
228+ -> do
237229 peersOrderStart <- getMonotonicTime
238230 writePeersOrder $ peersOrder
239231 { peersOrderCurrent = Just (peerInfoPeer peerInfo),
240232 peersOrderStart
241233 }
242- _ -> pure ()
234+ _ -> pure ()
243235
244236 pure $
245237 map (first Right ) (maybeToList theDecision)
@@ -307,8 +299,6 @@ fetchDecisionsBulkSync ::
307299 (Point block -> Bool ) ->
308300 MaxSlotNo ->
309301 PeersOrder peer ->
310- -- | The current peer, if there is one.
311- Maybe (PeerInfo header peer extra ) ->
312302 -- | Association list of the candidate fragments and their associated peers.
313303 -- The candidate fragments are anchored in the current chain (not necessarily
314304 -- at the tip; and not necessarily forking off immediately).
@@ -326,7 +316,6 @@ fetchDecisionsBulkSync
326316 fetchedBlocks
327317 fetchedMaxSlotNo
328318 peersOrder
329- mCurrentPeer
330319 candidatesAndPeers = combineWithDeclined $ do
331320 -- Step 1: Select the candidate to sync from. This already eliminates peers
332321 -- that have an implausible candidate. It returns the remaining candidates
@@ -355,7 +344,6 @@ fetchDecisionsBulkSync
355344 MaybeT $
356345 selectThePeer
357346 peersOrder
358- mCurrentPeer
359347 theFragments
360348 candidatesAndPeers'
361349
@@ -435,8 +423,6 @@ selectThePeer ::
435423 Eq peer
436424 ) =>
437425 PeersOrder peer ->
438- -- | The current peer
439- Maybe (PeerInfo header peer extra ) ->
440426 -- | The candidate fragment that we have selected to sync from, as suffix of
441427 -- the immutable tip.
442428 FetchDecision (CandidateFragments header ) ->
@@ -448,7 +434,6 @@ selectThePeer ::
448434 (Maybe (ChainSuffix header , PeerInfo header peer extra ))
449435selectThePeer
450436 peersOrder
451- mCurrentPeer
452437 theFragments
453438 candidates = do
454439 -- Create a fetch request for the blocks in question. The request has exactly
@@ -459,9 +444,13 @@ selectThePeer
459444 let firstBlock = FetchRequest . map (AF. takeOldest 1 ) . take 1 . filter (not . AF. null )
460445 (grossRequest :: FetchDecision (FetchRequest header )) = firstBlock . snd <$> theFragments
461446
447+ peersOrderCurrentInfo = do
448+ currentPeer <- peersOrderCurrent peersOrder
449+ List. find ((currentPeer == ) . peerInfoPeer) $ map snd candidates
450+
462451 -- If there is a current peer, then that is the one we choose. Otherwise, we
463452 -- can choose any peer, so we choose a “good” one.
464- case mCurrentPeer of
453+ case peersOrderCurrentInfo of
465454 Just thePeerInfo -> do
466455 case List. break (((==) `on` peerInfoPeer) thePeerInfo . snd ) candidates of
467456 (_, [] ) -> tell (List [(FetchDeclineChainNotPlausible , thePeerInfo)]) >> return Nothing
0 commit comments