@@ -139,7 +139,7 @@ module Ouroboros.Network.BlockFetch.Decision.BulkSync (
139139 fetchDecisionsBulkSyncM
140140) where
141141
142- import Control.Monad (filterM , when )
142+ import Control.Monad (filterM , guard , when )
143143import Control.Monad.Class.MonadTime.SI (MonadMonotonicTime (getMonotonicTime ), addTime )
144144import Control.Monad.Trans.Maybe (MaybeT (MaybeT , runMaybeT ))
145145import Control.Monad.Writer.Strict (Writer , runWriter , MonadWriter (tell ))
@@ -153,7 +153,7 @@ import Data.Maybe (listToMaybe, mapMaybe, maybeToList, isNothing)
153153import qualified Data.Set as Set
154154import Data.Ord (Down (Down ))
155155
156- import Cardano.Prelude (partitionEithers , (&) )
156+ import Cardano.Prelude (partitionEithers )
157157
158158import Ouroboros.Network.AnchoredFragment (AnchoredFragment , headBlockNo )
159159import qualified Ouroboros.Network.AnchoredFragment as AF
@@ -217,18 +217,10 @@ fetchDecisionsBulkSyncM
217217 )
218218 candidatesAndPeers = do
219219 peersOrder@ PeersOrder {peersOrderCurrent} <-
220- peersOrder0
221- -- Align the peers order with the actual peers; this consists in removing
222- -- all peers from the peers order that are not in the actual peers list and
223- -- adding at the end of the peers order all the actual peers that were not
224- -- there before.
225- & alignPeersOrderWithActualPeers
226- (map (\ (_, (_, _, _, peer, _)) -> peer) candidatesAndPeers)
227- -- If the chain selection has been starved recently, that is after the
228- -- current peer started (and a grace period), then the current peer is
229- -- bad. We push it at the end of the queue, demote it from CSJ dynamo,
230- -- and ignore its in-flight blocks for the future.
231- & checkLastChainSelStarvation
220+ checkLastChainSelStarvation $
221+ alignPeersOrderWithActualPeers
222+ (map (peerInfoPeer . snd ) candidatesAndPeers)
223+ peersOrder0
232224
233225 let peersOrderCurrentInfo = do
234226 currentPeer <- peersOrderCurrent
@@ -264,39 +256,48 @@ fetchDecisionsBulkSyncM
264256 map (first Right ) (maybeToList theDecision)
265257 ++ map (first Left ) declines
266258 where
259+ -- Align the peers order with the actual peers; this consists in removing
260+ -- all peers from the peers order that are not in the actual peers list and
261+ -- adding at the end of the peers order all the actual peers that were not
262+ -- there before.
267263 alignPeersOrderWithActualPeers :: [peer ] -> PeersOrder peer -> PeersOrder peer
268264 alignPeersOrderWithActualPeers
269265 actualPeers
270266 PeersOrder {peersOrderStart, peersOrderCurrent, peersOrderAll} =
271- let peersOrderCurrent' = case peersOrderCurrent of
272- Just peersOrderCurrent_ | peersOrderCurrent_`elem` actualPeers -> peersOrderCurrent
273- _ -> Nothing
267+ let peersOrderCurrent' = do
268+ peer <- peersOrderCurrent
269+ guard (peer `elem` actualPeers)
270+ pure peer
274271 peersOrderAll' =
275272 filter (`elem` actualPeers) peersOrderAll
276- ++ filter (\ peer -> peer `notElem` peersOrderAll) actualPeers
273+ ++ filter (`notElem` peersOrderAll) actualPeers
277274 in PeersOrder
278275 { peersOrderCurrent = peersOrderCurrent',
279276 peersOrderAll = peersOrderAll',
280277 peersOrderStart
281278 }
282279
280+ -- If the chain selection has been starved recently, that is after the
281+ -- current peer started (and a grace period), then the current peer is
282+ -- bad. We push it at the end of the queue, demote it from CSJ dynamo,
283+ -- and ignore its in-flight blocks for the future.
283284 checkLastChainSelStarvation :: PeersOrder peer -> m (PeersOrder peer )
284285 checkLastChainSelStarvation
285286 peersOrder@ PeersOrder {peersOrderStart, peersOrderCurrent, peersOrderAll} = do
286287 lastStarvationTime <- case chainSelStarvation of
287288 ChainSelStarvationEndedAt time -> pure time
288289 ChainSelStarvationOngoing -> getMonotonicTime
289290 case peersOrderCurrent of
290- Just peersOrderCurrent_ ->
291+ Just peer ->
291292 if lastStarvationTime >= addTime bulkSyncGracePeriod peersOrderStart
292293 then do
293- traceWith tracer $ PeerStarvedUs peersOrderCurrent_
294- demoteCSJDynamo peersOrderCurrent_
294+ traceWith tracer ( PeerStarvedUs peer)
295+ demoteCSJDynamo peer
295296 let peersOrder' =
296297 PeersOrder
297298 {
298299 peersOrderCurrent = Nothing ,
299- peersOrderAll = filter (/= peersOrderCurrent_ ) peersOrderAll ++ [peersOrderCurrent_ ],
300+ peersOrderAll = filter (/= peer ) peersOrderAll ++ [peer ],
300301 peersOrderStart
301302 }
302303 writePeersOrder peersOrder'
0 commit comments