Skip to content

Commit ed11046

Browse files
authored
Merge pull request #4931 from IntersectMBO/karknu/ping_tip_host
Log remote address and port in tip message.
2 parents e6737af + fa237bd commit ed11046

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

cardano-ping/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## next version
44

5+
### Breaking changes
6+
7+
* Log remote address and port in tip message
8+
59
## 0.3.0.0 -- 2024-08-07
610

711
### Breaking changes

cardano-ping/cardano-ping.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ library
2727
cborg >=0.2.8 && <0.3,
2828
bytestring >=0.10 && <0.13,
2929
contra-tracer >=0.1 && <0.3,
30+
iproute,
3031
time,
3132

3233
si-timers ^>=1.5,

cardano-ping/src/Cardano/Network/Ping.hs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Data.Aeson.Text (encodeToLazyText)
3636
import Data.Bits (clearBit, setBit, testBit)
3737
import Data.ByteString.Lazy (ByteString)
3838
import Data.Foldable (toList)
39+
import Data.IP
3940
import Data.List.NonEmpty (NonEmpty (..))
4041
import Data.Maybe (fromMaybe,)
4142
import Data.TDigest (insert, maximumValue, minimumValue, tdigest, mean, quantile, stddev, TDigest)
@@ -223,7 +224,8 @@ instance ToJSON NodeVersion where
223224
["peersharing" .= toJSON peersharing]
224225

225226
data 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

235237
instance 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

240242
instance 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

249253
keepAliveReqEnc :: 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

608613
instance 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\nReceived versions: %s\n" peerStr err (show recVersions)
626+
displayException (PingClientIPAddressFailure peerStr) =
627+
printf "%s expected an IP address\n" peerStr
621628

622629
pingClient :: Tracer IO LogMsg -> Tracer IO String -> PingOpts -> [NodeVersion] -> AddrInfo -> IO ()
623630
pingClient 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

780790
isSameVersionAndMagic :: NodeVersion -> NodeVersion -> Bool
781791
isSameVersionAndMagic v1 v2 = extract v1 == extract v2

0 commit comments

Comments
 (0)