Skip to content

Commit

Permalink
Merge pull request #4819 from IntersectMBO/karknu/ping_update
Browse files Browse the repository at this point in the history
Ping updates
  • Loading branch information
karknu authored Mar 4, 2024
2 parents c86df02 + 62b24f8 commit 335c186
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cardano-ping/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Non-breaking changes

* Add support for decoding peersharing support
* Add support for NodeToNodeVersionV13

## 0.2.0.12

### Non-breaking changes
Expand Down
27 changes: 27 additions & 0 deletions cardano-ping/src/Cardano/Network/Ping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ supportedNodeToNodeVersions magic =
, NodeToNodeVersionV10 magic InitiatorOnly
, NodeToNodeVersionV11 magic InitiatorOnly
, NodeToNodeVersionV12 magic InitiatorOnly
, NodeToNodeVersionV13 magic InitiatorOnly PeerSharingDisabled
]

supportedNodeToClientVersions :: Word32 -> [NodeVersion]
Expand Down Expand Up @@ -150,6 +151,15 @@ modeFromBool :: Bool -> InitiatorOnly
modeFromBool True = InitiatorOnly
modeFromBool False = InitiatorAndResponder

data PeerSharing = PeerSharingEnabled | PeerSharingDisabled
deriving (Eq, Ord, Show, Bounded, Generic)

instance ToJSON PeerSharing

peerSharingFromWord32 :: Word32 -> PeerSharing
peerSharingFromWord32 1 = PeerSharingEnabled
peerSharingFromWord32 _ = PeerSharingDisabled

data NodeVersion
= NodeToClientVersionV9 Word32
| NodeToClientVersionV10 Word32
Expand All @@ -171,6 +181,7 @@ data NodeVersion
| NodeToNodeVersionV10 Word32 InitiatorOnly
| NodeToNodeVersionV11 Word32 InitiatorOnly
| NodeToNodeVersionV12 Word32 InitiatorOnly
| NodeToNodeVersionV13 Word32 InitiatorOnly PeerSharing
deriving (Eq, Ord, Show)

instance ToJSON NodeVersion where
Expand All @@ -196,9 +207,12 @@ instance ToJSON NodeVersion where
NodeToNodeVersionV10 m i -> go3 "NodeToNodeVersionV10" m i
NodeToNodeVersionV11 m i -> go3 "NodeToNodeVersionV11" m i
NodeToNodeVersionV12 m i -> go3 "NodeToNodeVersionV12" m i
NodeToNodeVersionV13 m i ps -> go4 "NodeToNodeVersionV13" m i ps
where
go2 (version :: String) magic = ["version" .= version, "magic" .= magic]
go3 version magic initiator = go2 version magic <> ["initiator" .= toJSON initiator]
go4 version magic initiator peersharing = go3 version magic initiator <>
["peersharing" .= toJSON peersharing]

keepAliveReqEnc :: NodeVersion -> Word16 -> CBOR.Encoding
keepAliveReqEnc v cookie | v >= NodeToNodeVersionV7 minBound minBound =
Expand Down Expand Up @@ -297,6 +311,7 @@ handshakeReqEnc versions query =
encodeVersion (NodeToNodeVersionV10 magic mode) = encodeWithMode 10 magic mode
encodeVersion (NodeToNodeVersionV11 magic mode) = encodeWithMode 11 magic mode
encodeVersion (NodeToNodeVersionV12 magic mode) = encodeWithMode 12 magic mode
encodeVersion (NodeToNodeVersionV13 magic mode _) = encodeWithMode 13 magic mode

nodeToClientDataWithQuery :: Word32 -> CBOR.Encoding
nodeToClientDataWithQuery magic
Expand Down Expand Up @@ -409,6 +424,7 @@ handshakeDec = do
(10, False) -> decodeWithMode NodeToNodeVersionV10
(11, False) -> decodeWithModeAndQuery NodeToNodeVersionV11
(12, False) -> decodeWithModeAndQuery NodeToNodeVersionV12
(13, False) -> decodeWithModeQueryAndPeerSharing NodeToNodeVersionV13

(9, True) -> Right . NodeToClientVersionV9 <$> CBOR.decodeWord32
(10, True) -> Right . NodeToClientVersionV10 <$> CBOR.decodeWord32
Expand Down Expand Up @@ -436,6 +452,16 @@ handshakeDec = do
_query <- CBOR.decodeBool
return $ Right $ vnFun magic mode

decodeWithModeQueryAndPeerSharing :: (Word32 -> InitiatorOnly -> PeerSharing -> NodeVersion)
-> CBOR.Decoder s (Either HandshakeFailure NodeVersion)
decodeWithModeQueryAndPeerSharing vnFun = do
_len <- CBOR.decodeListLen
magic <- CBOR.decodeWord32
mode <- modeFromBool <$> CBOR.decodeBool
peerSharing <- peerSharingFromWord32 <$> CBOR.decodeWord32
_query <- CBOR.decodeBool
return $ Right $ vnFun magic mode peerSharing

wrap :: MiniProtocolNum -> MiniProtocolDir -> LBS.ByteString -> MuxSDU
wrap ptclNum ptclDir blob = MuxSDU
{ msHeader = MuxSDUHeader
Expand Down Expand Up @@ -683,3 +709,4 @@ isSameVersionAndMagic v1 v2 = extract v1 == extract v2
extract (NodeToNodeVersionV10 m _) = (10, m)
extract (NodeToNodeVersionV11 m _) = (11, m)
extract (NodeToNodeVersionV12 m _) = (12, m)
extract (NodeToNodeVersionV13 m _ _) = (13, m)

0 comments on commit 335c186

Please sign in to comment.