@@ -36,6 +36,7 @@ import Data.Aeson.Text (encodeToLazyText)
3636import Data.Bits (clearBit , setBit , testBit )
3737import Data.ByteString.Lazy (ByteString )
3838import Data.Foldable (toList )
39+ import Data.IP
3940import Data.List.NonEmpty (NonEmpty (.. ))
4041import Data.Maybe (fromMaybe ,)
4142import Data.TDigest (insert , maximumValue , minimumValue , tdigest , mean , quantile , stddev , TDigest )
@@ -223,7 +224,8 @@ instance ToJSON NodeVersion where
223224 [" peersharing" .= toJSON peersharing]
224225
225226data PingTip = PingTip {
226- ptRtt :: ! Double
227+ ptHost :: ! (IP , Socket. PortNumber )
228+ , ptRtt :: ! Double
227229 , ptHash :: ! ByteString
228230 , ptBlockNo :: ! Word64
229231 , ptSlotNo :: ! Word64
@@ -234,8 +236,8 @@ hexStr = LBS.foldr (\b -> (<>) (printf "%02x" b)) ""
234236
235237instance Show PingTip where
236238 show PingTip {.. } =
237- printf " rtt: %f, hash %s, blockNo: %d slotNo: %d" ptRtt (hexStr ptHash )
238- ptBlockNo ptSlotNo
239+ printf " host: %s:%d, rtt: %f, hash %s, blockNo: %d slotNo: %d" ( show $ fst ptHost )
240+ ( fromIntegral $ snd ptHost :: Word16 ) ptRtt (hexStr ptHash) ptBlockNo ptSlotNo
239241
240242instance ToJSON PingTip where
241243 toJSON PingTip {.. } =
@@ -244,6 +246,8 @@ instance ToJSON PingTip where
244246 , " hash" .= hexStr ptHash
245247 , " blockNo" .= ptBlockNo
246248 , " slotNo" .= ptSlotNo
249+ , " addr" .= (show $ fst $ ptHost :: String )
250+ , " port" .= (fromIntegral $ snd $ ptHost :: Word16 )
247251 ]
248252
249253keepAliveReqEnc :: NodeVersion -> Word16 -> CBOR. Encoding
@@ -603,6 +607,7 @@ data PingClientError = PingClientDeserialiseFailure DeserialiseFailure String
603607 | PingClientKeepAliveProtocolFailure KeepAliveFailure String
604608 | PingClientHandshakeFailure HandshakeFailure String
605609 | PingClientNegotiationError String [NodeVersion ] String
610+ | PingClientIPAddressFailure String
606611 deriving Show
607612
608613instance Exception PingClientError where
@@ -618,6 +623,8 @@ instance Exception PingClientError where
618623 printf " %s Protocol error: %s" peerStr (show err)
619624 displayException (PingClientNegotiationError err recVersions peerStr) =
620625 printf " %s Version negotiation error %s\n Received versions: %s\n " peerStr err (show recVersions)
626+ displayException (PingClientIPAddressFailure peerStr) =
627+ printf " %s expected an IP address\n " peerStr
621628
622629pingClient :: Tracer IO LogMsg -> Tracer IO String -> PingOpts -> [NodeVersion ] -> AddrInfo -> IO ()
623630pingClient stdout stderr PingOpts {.. } versions peer = bracket
@@ -773,9 +780,12 @@ pingClient stdout stderr PingOpts{..} versions peer = bracket
773780 case CBOR. deserialiseFromBytes chainSyncIntersectNotFoundDec msg of
774781 Left err -> throwIO (PingClientFindIntersectDeserialiseFailure err peerStr)
775782 Right (_, (slotNo, blockNo, hash)) ->
776- let tip = PingTip (toSample t_e t_s) hash blockNo slotNo in
777- if pingOptsJson then traceWith stdout $ LogMsg (encode tip)
778- else traceWith stdout $ LogMsg $ LBS.Char. pack $ show tip <> " \n "
783+ case fromSockAddr $ Socket. addrAddress peer of
784+ Nothing -> throwIO (PingClientIPAddressFailure peerStr)
785+ Just host ->
786+ let tip = PingTip host (toSample t_e t_s) hash blockNo slotNo in
787+ if pingOptsJson then traceWith stdout $ LogMsg (encode tip)
788+ else traceWith stdout $ LogMsg $ LBS.Char. pack $ show tip <> " \n "
779789
780790isSameVersionAndMagic :: NodeVersion -> NodeVersion -> Bool
781791isSameVersionAndMagic v1 v2 = extract v1 == extract v2
0 commit comments