Skip to content

Commit 992a7de

Browse files
committed
tx-submission: debug tracer for SharedTxState
1 parent 188be50 commit 992a7de

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Control.Concurrent.Class.MonadMVar.Strict
1818
import Control.Concurrent.Class.MonadSTM.Strict
1919
import Control.Monad.Class.MonadThrow
2020
import Control.Monad.Class.MonadTimer.SI
21+
import Control.Tracer (Tracer (..), traceWith)
2122

2223
import Data.Foldable (foldl', traverse_)
2324
import Data.Map.Strict (Map)
@@ -84,15 +85,17 @@ withPeer
8485
, Ord peeraddr
8586
, Show peeraddr
8687
)
87-
=> TxChannelsVar m peeraddr txid tx
88+
=> Tracer m (DebugSharedTxState peeraddr txid tx)
89+
-> TxChannelsVar m peeraddr txid tx
8890
-> SharedTxStateVar m peeraddr txid tx
8991
-> TxSubmissionMempoolReader txid tx idx m
9092
-> peeraddr
9193
-- ^ new peer
9294
-> (PeerTxAPI m txid tx -> m a)
9395
-- ^ callback which gives access to `PeerTxStateAPI`
9496
-> m a
95-
withPeer channelsVar
97+
withPeer tracer
98+
channelsVar
9699
sharedStateVar
97100
TxSubmissionMempoolReader { mempoolGetSnapshot }
98101
peeraddr io =
@@ -188,7 +191,8 @@ withPeer channelsVar
188191
-- TODO: hide this inside `receivedTxIds` so it's run in the same STM
189192
-- transaction.
190193
mempoolSnapshot <- atomically mempoolGetSnapshot
191-
receivedTxIds sharedStateVar
194+
receivedTxIds tracer
195+
sharedStateVar
192196
mempoolSnapshot
193197
peeraddr
194198
numTxIdsToReq
@@ -202,7 +206,7 @@ withPeer channelsVar
202206
-- ^ received txs
203207
-> m ()
204208
handleReceivedTxs txids txs =
205-
collectTxs sharedStateVar peeraddr txids txs
209+
collectTxs tracer sharedStateVar peeraddr txids txs
206210

207211

208212
decisionLogicThread
@@ -213,20 +217,21 @@ decisionLogicThread
213217
, Ord peeraddr
214218
, Ord txid
215219
)
216-
=> TxDecisionPolicy
220+
=> Tracer m (DebugSharedTxState peeraddr txid tx)
221+
-> TxDecisionPolicy
217222
-> StrictTVar m (Map peeraddr PeerGSV)
218223
-> TxChannelsVar m peeraddr txid tx
219224
-> SharedTxStateVar m peeraddr txid tx
220225
-> m Void
221-
decisionLogicThread policy gsvVar txChannelsVar sharedStateVar = go
226+
decisionLogicThread tracer policy gsvVar txChannelsVar sharedStateVar = go
222227
where
223228
go :: m Void
224229
go = do
225230
-- We rate limit the decision making process, it could overwhelm the CPU
226231
-- if there are too many inbound connections.
227232
threadDelay 0.005 -- 5ms
228233

229-
decisions <- atomically do
234+
(decisions, st) <- atomically do
230235
sharedCtx <-
231236
SharedDecisionContext
232237
<$> readTVar gsvVar
@@ -238,7 +243,8 @@ decisionLogicThread policy gsvVar txChannelsVar sharedStateVar = go
238243

239244
let (sharedState, decisions) = makeDecisions policy sharedCtx activePeers
240245
writeTVar sharedStateVar sharedState
241-
return decisions
246+
return (decisions, sharedState)
247+
traceWith tracer (DebugSharedTxState st)
242248
TxChannels { txChannelMap } <- readMVar txChannelsVar
243249
traverse_
244250
(\(mvar, d) -> modifyMVar_ mvar (\d' -> pure (d' <> d)))

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Ouroboros.Network.TxSubmission.Inbound.State
1616
, collectTxs
1717
, acknowledgeTxIds
1818
, hasTxIdsToAcknowledge
19+
-- * Debug output
20+
, DebugSharedTxState (..)
1921
-- * Internals, only exported for testing purposes:
2022
, RefCountDiff (..)
2123
, updateRefCounts
@@ -25,6 +27,7 @@ module Ouroboros.Network.TxSubmission.Inbound.State
2527

2628
import Control.Concurrent.Class.MonadSTM.Strict
2729
import Control.Exception (assert)
30+
import Control.Tracer (Tracer, traceWith)
2831

2932
import Data.Foldable (fold, foldl', toList)
3033
import Data.Map.Merge.Strict qualified as Map
@@ -534,7 +537,8 @@ newSharedTxStateVar = newTVarIO SharedTxState { peerTxStates = Map.empty,
534537
receivedTxIds
535538
:: forall m peeraddr idx tx txid.
536539
(MonadSTM m, Ord txid, Ord peeraddr)
537-
=> SharedTxStateVar m peeraddr txid tx
540+
=> Tracer m (DebugSharedTxState peeraddr txid tx)
541+
-> SharedTxStateVar m peeraddr txid tx
538542
-> MempoolSnapshot txid tx idx
539543
-> peeraddr
540544
-> NumTxIdsToReq
@@ -545,9 +549,10 @@ receivedTxIds
545549
-> Map txid SizeInBytes
546550
-- ^ received `txid`s with sizes
547551
-> m ()
548-
receivedTxIds sharedVar MempoolSnapshot{mempoolHasTx} peeraddr reqNo txidsSeq txidsMap =
549-
atomically $
550-
modifyTVar sharedVar (receivedTxIdsImpl mempoolHasTx peeraddr reqNo txidsSeq txidsMap)
552+
receivedTxIds tracer sharedVar MempoolSnapshot{mempoolHasTx} peeraddr reqNo txidsSeq txidsMap = do
553+
st <- atomically $
554+
stateTVar sharedVar ((\a -> (a,a)) . receivedTxIdsImpl mempoolHasTx peeraddr reqNo txidsSeq txidsMap)
555+
traceWith tracer (DebugSharedTxState st)
551556

552557

553558
-- | Include received `tx`s in `SharedTxState`. Return number of `txids`
@@ -556,14 +561,25 @@ receivedTxIds sharedVar MempoolSnapshot{mempoolHasTx} peeraddr reqNo txidsSeq tx
556561
collectTxs
557562
:: forall m peeraddr tx txid.
558563
(MonadSTM m, Ord txid, Ord peeraddr)
559-
=> SharedTxStateVar m peeraddr txid tx
564+
=> Tracer m (DebugSharedTxState peeraddr txid tx)
565+
-> SharedTxStateVar m peeraddr txid tx
560566
-> peeraddr
561567
-> Set txid -- ^ set of requested txids
562568
-> Map txid tx -- ^ received txs
563569
-> m ()
564570
-- ^ number of txids to be acknowledged and txs to be added to the
565571
-- mempool
566-
collectTxs sharedVar peeraddr txidsRequested txsMap =
567-
atomically $
568-
modifyTVar sharedVar
569-
(collectTxsImpl peeraddr txidsRequested txsMap)
572+
collectTxs tracer sharedVar peeraddr txidsRequested txsMap = do
573+
st <- atomically $
574+
stateTVar sharedVar
575+
((\a -> (a,a)) . collectTxsImpl peeraddr txidsRequested txsMap)
576+
traceWith tracer (DebugSharedTxState st)
577+
578+
--
579+
--
580+
--
581+
582+
-- | Debug tracer.
583+
--
584+
newtype DebugSharedTxState peeraddr txid tx = DebugSharedTxState (SharedTxState peeraddr txid tx)
585+
deriving Show

0 commit comments

Comments
 (0)