Skip to content

Commit ff745eb

Browse files
Niolsneilmayhew
authored andcommitted
Log ChainSync mini-protocol events if need be
1 parent 544ebeb commit ff745eb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/ChainSync.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ runChainSyncClient
165165
res <-
166166
try $
167167
runPipelinedPeerWithLimits
168-
nullTracer
168+
(Tracer $ traceWith tracer . TraceChainSyncSendRecvEvent peerId "Client")
169169
codecChainSyncId
170170
chainSyncNoSizeLimits
171171
(timeLimitsChainSync chainSyncTimeouts)
@@ -218,8 +218,8 @@ runChainSyncServer ::
218218
ChainSyncServer (Header blk) (Point blk) (Tip blk) m () ->
219219
Channel m (AnyMessage (ChainSync (Header blk) (Point blk) (Tip blk))) ->
220220
m ()
221-
runChainSyncServer _tracer peerId StateViewTracers {svtPeerSimulatorResultsTracer} server channel =
222-
(try $ runPeer nullTracer codecChainSyncId channel (chainSyncServerPeer server)) >>= \case
221+
runChainSyncServer tracer peerId StateViewTracers {svtPeerSimulatorResultsTracer} server channel =
222+
(try $ runPeer sendRecvTracer codecChainSyncId channel (chainSyncServerPeer server)) >>= \case
223223
Right ((), msgRes) -> traceWith svtPeerSimulatorResultsTracer $
224224
PeerSimulatorResult peerId $ SomeChainSyncServerResult $ Right msgRes
225225
Left exn -> do
@@ -228,3 +228,5 @@ runChainSyncServer _tracer peerId StateViewTracers {svtPeerSimulatorResultsTrace
228228
-- NOTE: here we are able to trace exceptions, as what is done in `runChainSyncClient`
229229
case fromException exn of
230230
(_ :: Maybe SomeException) -> pure ()
231+
where
232+
sendRecvTracer = Tracer $ traceWith tracer . TraceChainSyncSendRecvEvent peerId "Server"

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/Trace.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.Bifunctor (second)
2525
import Data.List (intersperse)
2626
import qualified Data.List.NonEmpty as NE
2727
import Data.Time.Clock (DiffTime, diffTimeToPicoseconds)
28+
import Network.TypedProtocol.Codec (AnyMessage (..))
2829
import Ouroboros.Consensus.Block (GenesisWindow (..), Header, Point,
2930
WithOrigin (NotOrigin, Origin), succWithOrigin)
3031
import Ouroboros.Consensus.Genesis.Governor (DensityBounds (..),
@@ -49,6 +50,9 @@ import Ouroboros.Network.AnchoredFragment (AnchoredFragment,
4950
headPoint)
5051
import qualified Ouroboros.Network.AnchoredFragment as AF
5152
import Ouroboros.Network.Block (SlotNo (SlotNo), Tip, castPoint)
53+
import Ouroboros.Network.Driver.Simple (TraceSendRecv (..))
54+
import Ouroboros.Network.Protocol.ChainSync.Type (ChainSync,
55+
Message (..))
5256
import Test.Consensus.PointSchedule.NodeState (NodeState)
5357
import Test.Consensus.PointSchedule.Peers (Peer (Peer), PeerId)
5458
import Test.Util.TersePrinting (terseAnchor, terseBlock,
@@ -130,6 +134,7 @@ data TraceEvent blk
130134
| TraceChainSyncClientTerminationEvent PeerId TraceChainSyncClientTerminationEvent
131135
| TraceBlockFetchClientTerminationEvent PeerId TraceBlockFetchClientTerminationEvent
132136
| TraceGenesisDDEvent (TraceGDDEvent PeerId blk)
137+
| TraceChainSyncSendRecvEvent PeerId String (TraceSendRecv (ChainSync (Header blk) (Point blk) (Tip blk)))
133138
| TraceOther String
134139

135140
-- * 'TestBlock'-specific tracers for the peer simulator
@@ -182,6 +187,7 @@ traceEventTestBlockWith setTickTime tracer0 tracer = \case
182187
TraceChainSyncClientTerminationEvent peerId traceEvent -> traceChainSyncClientTerminationEventTestBlockWith peerId tracer traceEvent
183188
TraceBlockFetchClientTerminationEvent peerId traceEvent -> traceBlockFetchClientTerminationEventTestBlockWith peerId tracer traceEvent
184189
TraceGenesisDDEvent gddEvent -> traceWith tracer (terseGDDEvent gddEvent)
190+
TraceChainSyncSendRecvEvent peerId peerType traceEvent -> traceChainSyncSendRecvEventTestBlockWith peerId peerType tracer traceEvent
185191
TraceOther msg -> traceWith tracer msg
186192

187193
traceSchedulerEventTestBlockWith ::
@@ -464,6 +470,33 @@ traceBlockFetchClientTerminationEventTestBlockWith pid tracer = \case
464470
where
465471
trace = traceUnitWith tracer ("BlockFetchClient " ++ condense pid)
466472

473+
-- | Trace all the SendRecv events of the ChainSync mini-protocol.
474+
traceChainSyncSendRecvEventTestBlockWith ::
475+
Applicative m =>
476+
PeerId ->
477+
String ->
478+
Tracer m String ->
479+
TraceSendRecv (ChainSync (Header TestBlock) (Point TestBlock) (Tip TestBlock)) ->
480+
m ()
481+
traceChainSyncSendRecvEventTestBlockWith pid ptp tracer = \case
482+
TraceSendMsg amsg -> traceMsg "send" amsg
483+
TraceRecvMsg amsg -> traceMsg "recv" amsg
484+
where
485+
-- This can be very verbose and is only useful in rare situations, so it
486+
-- does nothing by default.
487+
-- trace = traceUnitWith tracer ("ChainSync " ++ condense pid) . ((ptp ++ " ") ++)
488+
trace = (\_ _ _ -> const (pure ())) pid ptp tracer
489+
traceMsg kd amsg = trace $ kd ++ " " ++ case amsg of
490+
AnyMessage msg -> case msg of
491+
MsgRequestNext -> "MsgRequestNext"
492+
MsgAwaitReply -> "MsgAwaitReply"
493+
MsgRollForward header tip -> "MsgRollForward " ++ terseHeader header ++ " " ++ terseTip tip
494+
MsgRollBackward point tip -> "MsgRollBackward " ++ tersePoint point ++ " " ++ terseTip tip
495+
MsgFindIntersect points -> "MsgFindIntersect [" ++ unwords (map tersePoint points) ++ "]"
496+
MsgIntersectFound point tip -> "MsgIntersectFound " ++ tersePoint point ++ " " ++ terseTip tip
497+
MsgIntersectNotFound tip -> "MsgIntersectNotFound " ++ terseTip tip
498+
MsgDone -> "MsgDone"
499+
467500
prettyDensityBounds :: [(PeerId, DensityBounds TestBlock)] -> [String]
468501
prettyDensityBounds bounds =
469502
showPeers (second showBounds <$> bounds)

0 commit comments

Comments
 (0)