@@ -21,10 +21,11 @@ import Control.Tracer (Tracer, traceWith)
21
21
22
22
import Network.TypedProtocol
23
23
24
- import Control.Monad (unless )
24
+ import Control.Monad (unless , when )
25
25
import Ouroboros.Network.Protocol.TxSubmission2.Server
26
26
import Ouroboros.Network.TxSubmission.Inbound.Registry (PeerTxAPI (.. ))
27
27
import Ouroboros.Network.TxSubmission.Inbound.Types
28
+ import Ouroboros.Network.TxSubmission.Mempool.Reader
28
29
29
30
-- | Flag to enable/disable the usage of the new tx submission protocol
30
31
--
@@ -48,19 +49,25 @@ txSubmissionInboundV2
48
49
, Ord txid
49
50
)
50
51
=> Tracer m (TraceTxSubmissionInbound txid tx )
52
+ -> TxSubmissionMempoolReader txid tx idx m
51
53
-> TxSubmissionMempoolWriter txid tx idx m
52
54
-> PeerTxAPI m txid tx
53
55
-> TxSubmissionServerPipelined txid tx m ()
54
56
txSubmissionInboundV2
55
57
tracer
58
+ TxSubmissionMempoolReader {
59
+ mempoolGetSnapshot
60
+ }
56
61
TxSubmissionMempoolWriter {
57
62
txId,
58
63
mempoolAddTxs
59
64
}
60
65
PeerTxAPI {
61
66
readTxDecision,
62
67
handleReceivedTxIds,
63
- handleReceivedTxs
68
+ handleReceivedTxs,
69
+ countRejectedTxs,
70
+ consumeFetchedTxs
64
71
}
65
72
=
66
73
TxSubmissionServerPipelined serverIdle
@@ -73,23 +80,52 @@ txSubmissionInboundV2
73
80
<- readTxDecision
74
81
traceWith tracer (TraceTxInboundDecision txd)
75
82
76
- ! start <- getMonotonicTime
77
- txidsAccepted <- mempoolAddTxs txs
78
- ! end <- getMonotonicTime
79
- let duration = diffTime end start
80
-
81
- traceWith tracer $
82
- TraceTxInboundAddedToMempool txidsAccepted duration
83
-
84
83
let ! collected = length txs
85
- let ! accepted = length txidsAccepted
86
- traceWith tracer $
87
- TraceTxSubmissionCollected collected
88
-
89
- traceWith tracer $ TraceTxSubmissionProcessed ProcessedTxCount {
90
- ptxcAccepted = accepted
91
- , ptxcRejected = collected - accepted
92
- }
84
+ mpSnapshot <- atomically mempoolGetSnapshot
85
+ let receivedL = [ (txId tx, tx) | tx <- txs ]
86
+ fetchedSet <- consumeFetchedTxs (Set. fromList (map fst receivedL))
87
+
88
+ -- Only attempt to add TXs if we actually has fetched some.
89
+ when (not $ Set. null fetchedSet) $ do
90
+ let fetched = filter
91
+ (\ (txid, _) -> Set. member txid fetchedSet)
92
+ receivedL
93
+ fetchedS = Set. fromList $ map fst fetched
94
+
95
+ -- Note that checking if the mempool contains a TX before
96
+ -- spending several ms attempting to add it to the pool has
97
+ -- been judged immoral.
98
+ let fresh = filter
99
+ (\ (txid, _) -> not $ mempoolHasTx mpSnapshot txid)
100
+ receivedL
101
+
102
+ ! start <- getMonotonicTime
103
+ txidsAccepted <- mempoolAddTxs $ map snd fresh
104
+ ! end <- getMonotonicTime
105
+ let duration = diffTime end start
106
+
107
+ let acceptedS = Set. fromList txidsAccepted
108
+ acceptedFetched = Set. intersection fetchedS acceptedS
109
+ ! accepted = Set. size acceptedFetched
110
+ ! rejected = Set. size fetchedS - accepted
111
+
112
+ traceWith tracer $
113
+ TraceTxInboundAddedToMempool txidsAccepted duration
114
+ traceWith tracer $
115
+ TraceTxSubmissionCollected collected
116
+
117
+ -- Accepted TXs are discounted from rejected.
118
+ --
119
+ -- The number of rejected TXs may be too high.
120
+ -- The reason for that is that any peer which has downloaded a
121
+ -- TX is permitted to add TXs for all TXids hit has offered.
122
+ -- This is done to preserve TX ordering.
123
+ ! s <- countRejectedTxs (rejected - accepted) -- accepted TXs are discounted
124
+ traceWith tracer $ TraceTxSubmissionProcessed ProcessedTxCount {
125
+ ptxcAccepted = accepted
126
+ , ptxcRejected = rejected
127
+ , ptxcScore = s
128
+ }
93
129
94
130
-- TODO:
95
131
-- We can update the state so that other `tx-submission` servers will
0 commit comments