From ce4cde65572a4b353c7fd4edd0a2ddcb17d533b5 Mon Sep 17 00:00:00 2001 From: kdeme Date: Thu, 20 Apr 2023 12:48:01 +0200 Subject: [PATCH 1/4] Update discv5 version to 5.2 --- eth/p2p/discoveryv5/encoding.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth/p2p/discoveryv5/encoding.nim b/eth/p2p/discoveryv5/encoding.nim index 9e553b5a..30c9f719 100644 --- a/eth/p2p/discoveryv5/encoding.nim +++ b/eth/p2p/discoveryv5/encoding.nim @@ -34,7 +34,7 @@ logScope: # Support overriding the default discv5 protocol version and protocol id # via compile time defines (e.g., '-d:discv5_protocol_id=d5waku') const - discv5_protocol_version {.intdefine.} : uint16 = 1 + discv5_protocol_version {.intdefine.} : uint16 = 2 discv5_protocol_id {.strdefine.} = "discv5" const From a3c008b80faeb294c4424058af96b398de810435 Mon Sep 17 00:00:00 2001 From: kdeme Date: Thu, 20 Apr 2023 12:52:18 +0200 Subject: [PATCH 2/4] Remove Topic related message types --- eth/p2p/discoveryv5/messages.nim | 18 ------------------ eth/p2p/discoveryv5/messages_encoding.nim | 6 ------ eth/p2p/discoveryv5/protocol.nim | 5 ----- .../discoveryv5/fuzz_decode_message.nim | 2 -- 4 files changed, 31 deletions(-) diff --git a/eth/p2p/discoveryv5/messages.nim b/eth/p2p/discoveryv5/messages.nim index cbec3c0f..5dfe3d8f 100644 --- a/eth/p2p/discoveryv5/messages.nim +++ b/eth/p2p/discoveryv5/messages.nim @@ -31,10 +31,6 @@ type nodes = 0x04 talkReq = 0x05 talkResp = 0x06 - regTopic = 0x07 - ticket = 0x08 - regConfirmation = 0x09 - topicQuery = 0x0A RequestId* = object id*: seq[byte] @@ -61,12 +57,6 @@ type TalkRespMessage* = object response*: seq[byte] - # Not implemented, specification is not final here. - RegTopicMessage* = object - TicketMessage* = object - RegConfirmationMessage* = object - TopicQueryMessage* = object - SomeMessage* = PingMessage or PongMessage or FindNodeMessage or NodesMessage or TalkReqMessage or TalkRespMessage @@ -85,14 +75,6 @@ type talkReq*: TalkReqMessage of talkResp: talkResp*: TalkRespMessage - of regTopic: - regtopic*: RegTopicMessage - of ticket: - ticket*: TicketMessage - of regConfirmation: - regConfirmation*: RegConfirmationMessage - of topicQuery: - topicQuery*: TopicQueryMessage else: discard diff --git a/eth/p2p/discoveryv5/messages_encoding.nim b/eth/p2p/discoveryv5/messages_encoding.nim index d07ddb79..cb02ae4c 100644 --- a/eth/p2p/discoveryv5/messages_encoding.nim +++ b/eth/p2p/discoveryv5/messages_encoding.nim @@ -106,12 +106,6 @@ func decodeMessage*(body: openArray[byte]): Result[Message, cstring] = of nodes: rlp.decode(message.nodes) of talkReq: rlp.decode(message.talkReq) of talkResp: rlp.decode(message.talkResp) - of regTopic, ticket, regConfirmation, topicQuery: - # We just pass the empty type of this message without attempting to - # decode, so that the protocol knows what was received. - # But we ignore the message as per specification as "the content and - # semantics of this message are not final". - discard except RlpError, ValueError: return err("Invalid message encoding") diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index bb6aa59d..a180c06f 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -368,11 +368,6 @@ proc handleMessage(d: Protocol, srcId: NodeId, fromAddr: Address, of talkReq: discovery_message_requests_incoming.inc() d.handleTalkReq(srcId, fromAddr, message.talkReq, message.reqId) - of regTopic, topicQuery: - discovery_message_requests_incoming.inc() - discovery_message_requests_incoming.inc(labelValues = ["no_response"]) - trace "Received unimplemented message kind", kind = message.kind, - origin = fromAddr else: var waiter: Future[Option[Message]] if d.awaitedMessages.take((srcId, message.reqId), waiter): diff --git a/tests/fuzzing/discoveryv5/fuzz_decode_message.nim b/tests/fuzzing/discoveryv5/fuzz_decode_message.nim index 44cf604e..5474f447 100644 --- a/tests/fuzzing/discoveryv5/fuzz_decode_message.nim +++ b/tests/fuzzing/discoveryv5/fuzz_decode_message.nim @@ -17,8 +17,6 @@ test: of nodes: encoded = encodeMessage(message.nodes, message.reqId) of talkReq: encoded = encodeMessage(message.talkReq, message.reqId) of talkResp: encoded = encodeMessage(message.talkResp, message.reqId) - of regTopic, ticket, regConfirmation, topicQuery: - break # This will hit assert because of issue: # https://github.com/status-im/nim-eth/issues/255 From b1a99d5ea396191af675198d0197ec7b3d98ced1 Mon Sep 17 00:00:00 2001 From: kdeme Date: Fri, 28 Apr 2023 22:08:52 +0200 Subject: [PATCH 3/4] Add session message packet WIP --- eth/p2p/discoveryv5/encoding.nim | 8 ++++++++ eth/p2p/discoveryv5/protocol.nim | 3 +++ 2 files changed, 11 insertions(+) diff --git a/eth/p2p/discoveryv5/encoding.nim b/eth/p2p/discoveryv5/encoding.nim index 30c9f719..3a2fb8d2 100644 --- a/eth/p2p/discoveryv5/encoding.nim +++ b/eth/p2p/discoveryv5/encoding.nim @@ -82,6 +82,7 @@ type OrdinaryMessage = 0x00 Whoareyou = 0x01 HandshakeMessage = 0x02 + SessionMessage = 0x03 Packet* = object case flag*: Flag @@ -96,6 +97,9 @@ type # TODO record or node immediately? node*: Option[Node] srcIdHs*: NodeId + of SessionMessage: + # TODO: Implement + discard HandshakeKey* = object nodeId*: NodeId @@ -570,3 +574,7 @@ proc decodePacket*(c: var Codec, fromAddr: Address, input: openArray[byte]): return decodeHandshakePacket(c, fromAddr, staticHeader.nonce, input.toOpenArray(0, ivSize - 1), header, input.toOpenArray(ivSize + header.len, input.high)) + + of SessionMessage: + # TODO: Implement + return ok(Packet(flag: Flag.SessionMessage)) \ No newline at end of file diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index a180c06f..cc5af2ff 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -454,6 +454,9 @@ proc receive*(d: Protocol, a: Address, packet: openArray[byte]) = if node.address.isSome() and a == node.address.get(): if d.addNode(node): trace "Added new node to routing table after handshake", node + of SessionMessage: + # TODO: implement + discard else: trace "Packet decoding error", error = decoded.error, address = a From 89903c452425db8ff006e4b93769577e719b4266 Mon Sep 17 00:00:00 2001 From: kdeme Date: Fri, 28 Apr 2023 22:30:26 +0200 Subject: [PATCH 4/4] Add relay notifications WIP --- eth/p2p/discoveryv5/messages.nim | 17 ++++++++++++++++- eth/p2p/discoveryv5/messages_encoding.nim | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/eth/p2p/discoveryv5/messages.nim b/eth/p2p/discoveryv5/messages.nim index 5dfe3d8f..6cf68694 100644 --- a/eth/p2p/discoveryv5/messages.nim +++ b/eth/p2p/discoveryv5/messages.nim @@ -13,7 +13,7 @@ import std/[hashes, net], - ./enr + "."/[enr, node] type MessageKind* = enum @@ -25,12 +25,16 @@ type unused = 0x00 # The supported message types + # Request & response ping = 0x01 pong = 0x02 findNode = 0x03 nodes = 0x04 talkReq = 0x05 talkResp = 0x06 + # Notifications + relayInit = 0x07 + relayMsg = 0x08 RequestId* = object id*: seq[byte] @@ -57,9 +61,20 @@ type TalkRespMessage* = object response*: seq[byte] + RelayInitNotification* = object + initiatorEnr*: Record + targetId*: NodeId + nonce*: array[12, byte] # TODO: is this ok? + + RelayMsgNotification* = object + initiatorEnr*: Record + nonce*: array[12, byte] + SomeMessage* = PingMessage or PongMessage or FindNodeMessage or NodesMessage or TalkReqMessage or TalkRespMessage + SomeNotification* = RelayInitNotification or RelayMsgNotification + Message* = object reqId*: RequestId case kind*: MessageKind diff --git a/eth/p2p/discoveryv5/messages_encoding.nim b/eth/p2p/discoveryv5/messages_encoding.nim index cb02ae4c..04c3a165 100644 --- a/eth/p2p/discoveryv5/messages_encoding.nim +++ b/eth/p2p/discoveryv5/messages_encoding.nim @@ -106,6 +106,8 @@ func decodeMessage*(body: openArray[byte]): Result[Message, cstring] = of nodes: rlp.decode(message.nodes) of talkReq: rlp.decode(message.talkReq) of talkResp: rlp.decode(message.talkResp) + of relayInit: return err("To implement") + of relayMsg: return err("To implement") except RlpError, ValueError: return err("Invalid message encoding")