@@ -18,6 +18,7 @@ module Ouroboros.Network.TxSubmission.Inbound.V2.Decision
1818
1919import Control.Arrow ((>>>) )
2020import Control.Exception (assert )
21+ import Control.Monad.Class.MonadTime.SI (addTime , Time )
2122
2223import Data.Bifunctor (second )
2324import Data.Hashable
@@ -46,7 +47,9 @@ makeDecisions
4647 , Ord txid
4748 , Hashable peeraddr
4849 )
49- => TxDecisionPolicy
50+ => Time
51+ -- ^ current time
52+ -> TxDecisionPolicy
5053 -- ^ decision policy
5154 -> SharedTxState peeraddr txid tx
5255 -- ^ decision context
@@ -60,11 +63,11 @@ makeDecisions
6063 -> ( SharedTxState peeraddr txid tx
6164 , Map peeraddr (TxDecision txid tx )
6265 )
63- makeDecisions policy st =
66+ makeDecisions now policy st =
6467 let (salt, rng') = random (peerRng st)
6568 st' = st { peerRng = rng' }
6669 in fn
67- . pickTxsToDownload policy st'
70+ . pickTxsToDownload now policy st'
6871 . orderByRejections salt
6972 where
7073 fn :: forall a .
@@ -93,7 +96,7 @@ orderByRejections salt =
9396-- | Internal state of `pickTxsToDownload` computation.
9497--
9598data St peeraddr txid tx =
96- St { stInflight :: ! (Map txid Int ),
99+ St { stInflight :: ! (Map txid InFlightState ),
97100 -- ^ `txid`s in-flight.
98101
99102 stAcknowledged :: ! (Map txid Int ),
@@ -123,7 +126,9 @@ pickTxsToDownload
123126 ( Ord peeraddr
124127 , Ord txid
125128 )
126- => TxDecisionPolicy
129+ => Time
130+ -- ^ current time
131+ -> TxDecisionPolicy
127132 -- ^ decision policy
128133 -> SharedTxState peeraddr txid tx
129134 -- ^ shared state
@@ -133,8 +138,9 @@ pickTxsToDownload
133138 , [(peeraddr , TxDecision txid tx )]
134139 )
135140
136- pickTxsToDownload policy@ TxDecisionPolicy { txsSizeInflightPerPeer,
137- txInflightMultiplicity }
141+ pickTxsToDownload now policy@ TxDecisionPolicy { txsSizeInflightPerPeer,
142+ txInflightMultiplicity,
143+ interTxSpace }
138144 sharedState@ SharedTxState { peerTxStates,
139145 inflightTxs,
140146 bufferedTxs,
@@ -180,7 +186,8 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
180186 -- does not allow to short circuit the fold, unlike
181187 -- `foldWithState`.
182188 foldWithState
183- (\ (txid, (txSize, inflightMultiplicity)) sizeInflight ->
189+ (\ (txid, (txSize, inflightSt)) sizeInflight ->
190+ let inflightMultiplicity = inFlightCount inflightSt in
184191 if -- note that we pick `txid`'s as long the `s` is
185192 -- smaller or equal to `txsSizeInflightPerPeer`.
186193 sizeInflight <= txsSizeInflightPerPeer
@@ -196,7 +203,7 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
196203 -- merge `availableTxIds` with `stInflight`, so we don't
197204 -- need to lookup into `stInflight` on every `txid` which
198205 -- is in `availableTxIds`.
199- Map. merge (Map. mapMaybeMissing \ _txid -> Just . (,0 ))
206+ Map. merge (Map. mapMaybeMissing \ _txid -> Just . (, mempty ))
200207 Map. dropMissing
201208 (Map. zipWithMatched \ _txid -> (,))
202209
@@ -233,13 +240,14 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
233240
234241 stAcknowledged' = Map. unionWith (+) stAcknowledged txIdsToAck
235242
236- stInflightDelta :: Map txid Int
237- stInflightDelta = Map. fromSet (\ _ -> 1 ) txsToRequest
243+ stInflightDelta :: Map txid InFlightState
244+ stInflightDelta = Map. fromSet (\ _ -> InFlightState 1 $ addTime interTxSpace now)
245+ txsToRequest
238246 -- note: this is right since every `txid`
239247 -- could be picked at most once
240248
241- stInflight' :: Map txid Int
242- stInflight' = Map. unionWith (+ ) stInflightDelta stInflight
249+ stInflight' :: Map txid InFlightState
250+ stInflight' = Map. unionWith (<> ) stInflightDelta stInflight
243251
244252 stInSubmissionToMempoolTxs' = stInSubmissionToMempoolTxs
245253 <> Set. fromList (map fst listOfTxsToMempool)
@@ -346,10 +354,12 @@ filterActivePeers
346354 :: forall peeraddr txid tx .
347355 Ord txid
348356 => HasCallStack
349- => TxDecisionPolicy
357+ => Time
358+ -> TxDecisionPolicy
350359 -> SharedTxState peeraddr txid tx
351360 -> Map peeraddr (PeerTxState txid tx )
352361filterActivePeers
362+ now
353363 policy@ TxDecisionPolicy {
354364 maxUnacknowledgedTxIds,
355365 txsSizeInflightPerPeer,
@@ -362,7 +372,13 @@ filterActivePeers
362372 inSubmissionToMempoolTxs
363373 } = Map. filter gn peerTxStates
364374 where
365- unrequestable = Map. keysSet (Map. filter (>= txInflightMultiplicity) inflightTxs)
375+
376+ unrequestableFilter :: InFlightState -> Bool
377+ unrequestableFilter InFlightState {inFlightCount, inFlightNextReq} =
378+ inFlightCount >= txInflightMultiplicity || inFlightNextReq > now
379+
380+ unrequestable :: Set txid
381+ unrequestable = Map. keysSet (Map. filter unrequestableFilter inflightTxs)
366382 <> Map. keysSet bufferedTxs
367383
368384 gn :: PeerTxState txid tx -> Bool
0 commit comments