Skip to content

Commit adae14f

Browse files
committed
tx-submission: added TxsToMempool newtype
Just to make it more outstanding what the output of `acknowledgeTxIds` is indented for.
1 parent e6d2dc8 commit adae14f

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/Decision.hs

+18-9
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,15 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
214214
sizeInflightOther = sizeInflightAll - requestedTxsInflightSize
215215

216216
in if sizeInflightAll >= maxTxsSizeInflight
217-
then let (numTxIdsToAck, numTxIdsToReq, txsToMempool, RefCountDiff { txIdsToAck }, peerTxState') =
218-
acknowledgeTxIds policy sharedState peerTxState
217+
then let ( numTxIdsToAck
218+
, numTxIdsToReq
219+
, txsToMempool@TxsToMempool { listOfTxsToMempool }
220+
, RefCountDiff { txIdsToAck }
221+
, peerTxState'
222+
) = acknowledgeTxIds policy sharedState peerTxState
219223

220224
stAcknowledged' = Map.unionWith (+) stAcknowledged txIdsToAck
221-
stLimboTx' = stLimboTx <> (Set.fromList $ map fst txsToMempool)
225+
stLimboTx' = stLimboTx <> Set.fromList (map fst listOfTxsToMempool)
222226
in
223227
if requestedTxIdsInflight peerTxState' > 0
224228
then
@@ -300,8 +304,12 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
300304
<> txsToRequest
301305
}
302306

303-
(numTxIdsToAck, numTxIdsToReq, txsToMempool, RefCountDiff { txIdsToAck }, peerTxState'') =
304-
acknowledgeTxIds policy sharedState peerTxState'
307+
( numTxIdsToAck
308+
, numTxIdsToReq
309+
, txsToMempool@TxsToMempool { listOfTxsToMempool }
310+
, RefCountDiff { txIdsToAck }
311+
, peerTxState''
312+
) = acknowledgeTxIds policy sharedState peerTxState'
305313

306314
stAcknowledged' = Map.unionWith (+) stAcknowledged txIdsToAck
307315

@@ -313,7 +321,7 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
313321
stInflight' :: Map txid Int
314322
stInflight' = Map.unionWith (+) stInflightDelta stInflight
315323

316-
stLimboTx' = stLimboTx <> (Set.fromList $ map fst txsToMempool)
324+
stLimboTx' = stLimboTx <> (Set.fromList $ map fst listOfTxsToMempool)
317325
in
318326
if requestedTxIdsInflight peerTxState'' > 0
319327
then
@@ -389,9 +397,9 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
389397
TxDecision { txdTxIdsToAcknowledge = 0,
390398
txdTxIdsToRequest = 0,
391399
txdTxsToRequest,
392-
txdTxsToMempool }
400+
txdTxsToMempool = TxsToMempool { listOfTxsToMempool } }
393401
| null txdTxsToRequest
394-
, null txdTxsToMempool
402+
, null listOfTxsToMempool
395403
-> Nothing
396404
_ -> Just (a, b)
397405
)
@@ -403,7 +411,8 @@ pickTxsToDownload policy@TxDecisionPolicy { txsSizeInflightPerPeer,
403411
Map txid Int
404412
-> (a, TxDecision txid tx)
405413
-> Map txid Int
406-
updateLimboTxs m (_,d) = foldl' fn m $ txdTxsToMempool d
414+
updateLimboTxs m (_,TxDecision { txdTxsToMempool } ) =
415+
foldl' fn m (listOfTxsToMempool txdTxsToMempool)
407416
where
408417
fn :: Map txid Int
409418
-> (txid,tx)

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/Server.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ txSubmissionInboundV2
7777
serverIdle = do
7878
-- Block on next decision.
7979
txd@TxDecision { txdTxsToRequest = txsToRequest,
80-
txdTxsToMempool = txsToMempool }
80+
txdTxsToMempool = TxsToMempool { listOfTxsToMempool } }
8181
<- readTxDecision
8282
traceWith tracer (TraceTxInboundDecision txd)
8383

84-
let !collected = length txsToMempool
84+
let !collected = length listOfTxsToMempool
8585

8686
-- Only attempt to add TXs if we have some work to do
8787
when (collected > 0) $ do
88-
mapM_ (withMempoolSem . addTx) txsToMempool
88+
mapM_ (withMempoolSem . addTx) listOfTxsToMempool
8989

9090
traceWith tracer $
9191
TraceTxSubmissionCollected collected

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/State.hs

+8-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ acknowledgeTxIds
100100
=> TxDecisionPolicy
101101
-> SharedTxState peeraddr txid tx
102102
-> PeerTxState txid tx
103-
-> (NumTxIdsToAck, NumTxIdsToReq, [(txid,tx)], RefCountDiff txid, PeerTxState txid tx)
103+
-> ( NumTxIdsToAck
104+
, NumTxIdsToReq
105+
, TxsToMempool txid tx
106+
, RefCountDiff txid
107+
, PeerTxState txid tx
108+
)
104109
-- ^ number of txid to acknowledge, requests, txs which we can submit to the
105110
-- mempool, txids to acknowledge with multiplicities, updated PeerTxState.
106111
{-# INLINE acknowledgeTxIds #-}
@@ -124,7 +129,7 @@ acknowledgeTxIds
124129
then
125130
( txIdsToAcknowledge
126131
, txIdsToRequest
127-
, txsToMempool
132+
, TxsToMempool txsToMempool
128133
, refCountDiff
129134
, ps { unacknowledgedTxIds = unacknowledgedTxIds',
130135
availableTxIds = availableTxIds',
@@ -138,7 +143,7 @@ acknowledgeTxIds
138143
else
139144
( 0
140145
, 0
141-
, []
146+
, TxsToMempool []
142147
, RefCountDiff Map.empty
143148
, ps
144149
)

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/Types.hs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
{-# LANGUAGE DeriveGeneric #-}
2-
{-# LANGUAGE ExistentialQuantification #-}
3-
{-# LANGUAGE FlexibleContexts #-}
4-
{-# LANGUAGE NamedFieldPuns #-}
5-
{-# LANGUAGE StandaloneDeriving #-}
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE DerivingStrategies #-}
3+
{-# LANGUAGE ExistentialQuantification #-}
4+
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
6+
{-# LANGUAGE NamedFieldPuns #-}
7+
{-# LANGUAGE StandaloneDeriving #-}
68

79
module Ouroboros.Network.TxSubmission.Inbound.Types
810
( -- * PeerTxState
911
PeerTxState (..)
1012
-- * SharedTxState
1113
, SharedTxState (..)
1214
-- * Decisions
15+
, TxsToMempool (..)
1316
, TxDecision (..)
1417
, emptyTxDecision
1518
, SharedDecisionContext (..)
@@ -209,6 +212,10 @@ instance ( NoThunks peeraddr
209212
-- Decisions
210213
--
211214

215+
newtype TxsToMempool txid tx = TxsToMempool { listOfTxsToMempool :: [(txid, tx)] }
216+
deriving newtype (Eq, Show, Semigroup, Monoid)
217+
218+
212219
-- | Decision made by the decision logic. Each peer will receive a 'Decision'.
213220
--
214221
-- /note:/ it is rather non-standard to represent a choice between requesting
@@ -235,7 +242,7 @@ data TxDecision txid tx = TxDecision {
235242
txdTxsToRequest :: !(Set txid),
236243
-- ^ txid's to download.
237244

238-
txdTxsToMempool :: ![(txid,tx)]
245+
txdTxsToMempool :: !(TxsToMempool txid tx)
239246
-- ^ list of `tx`s to submit to the mempool.
240247
}
241248
deriving (Show, Eq)
@@ -262,7 +269,7 @@ instance Ord txid => Semigroup (TxDecision txid tx) where
262269
txdTxIdsToRequest = txdTxIdsToRequest + txdTxIdsToRequest',
263270
txdPipelineTxIds = txdPipelineTxIds',
264271
txdTxsToRequest = txdTxsToRequest <> txdTxsToRequest',
265-
txdTxsToMempool = txdTxsToMempool ++ txdTxsToMempool'
272+
txdTxsToMempool = txdTxsToMempool <> txdTxsToMempool'
266273
}
267274

268275
-- | A no-op decision.
@@ -272,7 +279,7 @@ emptyTxDecision = TxDecision {
272279
txdTxIdsToRequest = 0,
273280
txdPipelineTxIds = False,
274281
txdTxsToRequest = Set.empty,
275-
txdTxsToMempool = []
282+
txdTxsToMempool = mempty
276283
}
277284

278285
data SharedDecisionContext peeraddr txid tx = SharedDecisionContext {

ouroboros-network/testlib/Test/Ouroboros/Network/TxSubmission/TxLogic.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ prop_acknowledgeTxIds :: ArbDecisionContextWithReceivedTxIds
654654
-> Property
655655
prop_acknowledgeTxIds (ArbDecisionContextWithReceivedTxIds policy SharedDecisionContext { sdcSharedTxState = st } ps _ _ _) =
656656
case TXS.acknowledgeTxIds policy st ps of
657-
(numTxIdsToAck, txIdsToRequest, txIdsTxs, TXS.RefCountDiff { TXS.txIdsToAck }, ps') | txIdsToRequest > 0 ->
657+
(numTxIdsToAck, txIdsToRequest, TXS.TxsToMempool txIdsTxs, TXS.RefCountDiff { TXS.txIdsToAck }, ps') | txIdsToRequest > 0 ->
658658
counterexample "number of tx ids to ack must agree with RefCountDiff"
659659
( fromIntegral numTxIdsToAck
660660
===

0 commit comments

Comments
 (0)