2222{-# OPTIONS_GHC -Wno-name-shadowing #-}
2323#endif
2424
25- module Cardano.Tracing.OrphanInstances.Network () where
25+ module Cardano.Tracing.OrphanInstances.Network
26+ ( Verbose (.. )
27+ , FetchDecisionToJSON (.. )
28+ ) where
2629
2730import Cardano.Node.Queries (ConvertTxId )
2831import Cardano.Tracing.OrphanInstances.Common
@@ -630,7 +633,7 @@ instance HasTextFormatter NtN.AcceptConnectionsPolicyTrace where
630633 formatText a _ = pack (show a)
631634
632635
633- instance (StandardHash header , Show peer , ToObject peer )
636+ instance (StandardHash header , Show peer , ToJSON peer , ConvertRawHash header )
634637 => Transformable Text IO [TraceLabelPeer peer (FetchDecision [Point header ])] where
635638 trTransformer = trStructuredText
636639instance (StandardHash header , Show peer )
@@ -644,7 +647,7 @@ instance (Show header, StandardHash header, Show peer)
644647 => HasTextFormatter (TraceLabelPeer peer (TraceFetchClientState header )) where
645648 formatText a _ = pack (show a)
646649
647- instance (StandardHash header , Show peer , ToObject peer )
650+ instance (StandardHash header , Show peer , ToJSON peer , ConvertRawHash header )
648651 => Transformable Text IO (BlockFetch. TraceDecisionEvent peer header ) where
649652 trTransformer = trStructuredText
650653instance (StandardHash header , Show peer )
@@ -1129,16 +1132,6 @@ instance Aeson.ToJSON ConnectionManagerCounters where
11291132 , " outbound" .= outboundConns
11301133 ]
11311134
1132- instance ToObject (FetchDecision [Point header ]) where
1133- toObject _verb (Left decline) =
1134- mconcat [ " kind" .= String " FetchDecision declined"
1135- , " declined" .= String (pack (show decline))
1136- ]
1137- toObject _verb (Right results) =
1138- mconcat [ " kind" .= String " FetchDecision results"
1139- , " length" .= String (pack $ show $ length results)
1140- ]
1141-
11421135-- TODO: use 'ToJSON' constraints
11431136instance (Show ntnAddr , Show ntcAddr ) => ToObject (ND. DiffusionTracer ntnAddr ntcAddr ) where
11441137 toObject _verb (ND. RunServer sockAddr) = mconcat
@@ -1244,17 +1237,45 @@ instance ToObject NtN.AcceptConnectionsPolicyTrace where
12441237 ]
12451238
12461239
1240+ instance ConvertRawHash header
1241+ => ToJSON (Point header ) where
1242+ toJSON GenesisPoint = String " GenesisPoint"
1243+ toJSON (BlockPoint (SlotNo slotNo) hash) =
1244+ -- it is unlikely that there will be two short hashes in the same slot
1245+ String $ renderHeaderHashForVerbosity
1246+ (Proxy @ header )
1247+ MinimalVerbosity
1248+ hash
1249+ <> " @"
1250+ <> pack (show slotNo)
1251+
1252+
1253+ newtype Verbose a = Verbose a
1254+
1255+ instance ConvertRawHash header
1256+ => ToJSON (Verbose (Point header )) where
1257+ toJSON (Verbose GenesisPoint ) = String " GenesisPoint"
1258+ toJSON (Verbose (BlockPoint (SlotNo slotNo) hash)) =
1259+ -- it is unlikely that there will be two short hashes in the same slot
1260+ String $ renderHeaderHashForVerbosity
1261+ (Proxy @ header )
1262+ MaximalVerbosity
1263+ hash
1264+ <> " @"
1265+ <> pack (show slotNo)
1266+
1267+
12471268instance ConvertRawHash blk
12481269 => ToObject (Point blk ) where
12491270 toObject _verb GenesisPoint =
1250- mconcat
1251- [ " kind " .= String " GenesisPoint " ]
1252- toObject verb ( BlockPoint slot h) =
1253- mconcat
1254- [ " kind " .= String " BlockPoint "
1255- , " slot " .= toJSON (unSlotNo slot )
1256- , " headerHash " .= renderHeaderHashForVerbosity ( Proxy @ blk ) verb h
1257- ]
1271+ mconcat [ " point " .= String " GenesisPoint " ]
1272+ toObject verb point @ BlockPoint {} =
1273+ mconcat [ " point " . =
1274+ case verb of
1275+ MaximalVerbosity
1276+ -> toJSON (Verbose point )
1277+ _ -> toJSON point
1278+ ]
12581279
12591280
12601281instance ToObject SlotNo where
@@ -1330,26 +1351,51 @@ instance (HasHeader header, ConvertRawHash header)
13301351 , " outstanding" .= outstanding
13311352 ]
13321353
1333-
1334- instance (ToObject peer )
1354+ instance (ToJSON peer , ConvertRawHash header )
13351355 => ToObject [TraceLabelPeer peer (FetchDecision [Point header ])] where
13361356 toObject MinimalVerbosity _ = mempty
13371357 toObject _ [] = mempty
13381358 toObject _ xs = mconcat
1339- [ " kind" .= String " PeersFetch "
1340- , " peers " .= toJSON
1341- (foldl' ( \ acc x -> toObject MaximalVerbosity x : acc) [] xs) ]
1359+ [ " kind" .= String " FetchDecisions "
1360+ , " decisions " .= toJSON xs
1361+ ]
13421362
13431363instance (ToObject peer , ToObject a ) => ToObject (TraceLabelPeer peer a ) where
13441364 toObject verb (TraceLabelPeer peerid a) =
13451365 mconcat [ " peer" .= toObject verb peerid ] <> toObject verb a
13461366
1347- instance ToObject peer
1367+ instance (ToJSON peer , ToJSON point )
1368+ => ToJSON (TraceLabelPeer peer (FetchDecision [point ])) where
1369+ toJSON (TraceLabelPeer peer decision) =
1370+ Aeson. object
1371+ [ " peer" .= toJSON peer
1372+ , " decision" .= toJSON (FetchDecisionToJSON decision)
1373+ ]
1374+
1375+ instance (ToJSON peer , ToJSON (Verbose point ))
1376+ => ToJSON (Verbose (TraceLabelPeer peer (FetchDecision [point ]))) where
1377+ toJSON (Verbose (TraceLabelPeer peer decision)) =
1378+ Aeson. object
1379+ [ " peer" .= toJSON peer
1380+ , " decision" .= toJSON (FetchDecisionToJSON $ map Verbose <$> decision)
1381+ ]
1382+
1383+ newtype FetchDecisionToJSON point =
1384+ FetchDecisionToJSON (FetchDecision [point ])
1385+
1386+ instance ToJSON point
1387+ => ToJSON (FetchDecisionToJSON point ) where
1388+ toJSON (FetchDecisionToJSON (Left decline)) =
1389+ Aeson. object [ " declined" .= String (pack . show $ decline) ]
1390+ toJSON (FetchDecisionToJSON (Right points)) =
1391+ toJSON points
1392+
1393+ instance (ToJSON peer , ConvertRawHash header )
13481394 => ToObject (BlockFetch. TraceDecisionEvent peer header ) where
1349- toObject verb (BlockFetch. PeersFetch as) = toObject verb as
1350- toObject verb (BlockFetch. PeerStarvedUs peer) = mconcat
1351- [ " kind" .= String " PeersStarvedUs "
1352- , " peer" .= toObject verb peer
1395+ toObject verb (BlockFetch. PeersFetch as) = toObject verb as
1396+ toObject _verb (BlockFetch. PeerStarvedUs peer) = mconcat
1397+ [ " kind" .= String " PeerStarvedUs "
1398+ , " peer" .= toJSON peer
13531399 ]
13541400
13551401instance ToObject (AnyMessage ps )
0 commit comments