From 4b85c73f43d6117fc8913a65c8f9bc6472d8cb31 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Wed, 20 Oct 2021 22:31:05 +0200 Subject: [PATCH] Update portal wire protocol ids to latest spec (#873) --- fluffy/network/history/history_network.nim | 2 +- fluffy/network/state/state_network.nim | 2 +- fluffy/network/wire/messages.nim | 4 ++-- fluffy/network/wire/portal_protocol.nim | 13 +++++++++---- fluffy/tests/test_portal_wire_protocol.nim | 2 +- fluffy/tools/portalcli.nim | 3 ++- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/fluffy/network/history/history_network.nim b/fluffy/network/history/history_network.nim index 3f25acbbe..ffd1e7d28 100644 --- a/fluffy/network/history/history_network.nim +++ b/fluffy/network/history/history_network.nim @@ -14,7 +14,7 @@ import ./history_content const - HistoryProtocolId* = "portal:history".toBytes() + HistoryProtocolId* = [byte 0x50, 0x0B] # TODO: Extract common parts from the different networks type HistoryNetwork* = ref object diff --git a/fluffy/network/state/state_network.nim b/fluffy/network/state/state_network.nim index cafabd6b6..c12f4a829 100644 --- a/fluffy/network/state/state_network.nim +++ b/fluffy/network/state/state_network.nim @@ -15,7 +15,7 @@ import ./state_distance const - StateProtocolId* = "portal:state".toBytes() + StateProtocolId* = [byte 0x50, 0x0A] type StateNetwork* = ref object portalProtocol*: PortalProtocol diff --git a/fluffy/network/wire/messages.nim b/fluffy/network/wire/messages.nim index d43717591..2a935b154 100644 --- a/fluffy/network/wire/messages.nim +++ b/fluffy/network/wire/messages.nim @@ -5,8 +5,8 @@ # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -# As per spec: -# https://github.com/ethereum/stateless-ethereum-specs/blob/master/state-network.md#wire-protocol +## Definitions and encoding of the messages of the Portal wire protocol: +## https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md#request---response-messages {.push raises: [Defect].} diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index 1ddd63c54..945b97137 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -5,6 +5,9 @@ # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. +## Implementation of the Portal wire protocol as specified at: +## https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md + {.push raises: [Defect].} import @@ -50,8 +53,10 @@ type ContentHandler* = proc(contentKey: ByteList): ContentResult {.raises: [Defect], gcsafe.} + PortalProtocolId* = array[2, byte] + PortalProtocol* = ref object of TalkProtocol - protocolId: seq[byte] + protocolId: PortalProtocolId routingTable*: RoutingTable baseProtocol*: protocol.Protocol dataRadius*: UInt256 @@ -200,7 +205,7 @@ proc messageHandler*(protocol: TalkProtocol, request: seq[byte], proc new*(T: type PortalProtocol, baseProtocol: protocol.Protocol, - protocolId: seq[byte], + protocolId: PortalProtocolId, contentHandler: ContentHandler, dataRadius = UInt256.high(), bootstrapRecords: openarray[Record] = [], @@ -216,7 +221,7 @@ proc new*(T: type PortalProtocol, handleContentRequest: contentHandler, bootstrapRecords: @bootstrapRecords) - proto.baseProtocol.registerTalkProtocol(proto.protocolId, proto).expect( + proto.baseProtocol.registerTalkProtocol(@(proto.protocolId), proto).expect( "Only one protocol should have this id") return proto @@ -229,7 +234,7 @@ proc reqResponse[Request: SomeMessage, Response: SomeMessage]( request: Request ): Future[PortalResult[Response]] {.async.} = let talkresp = - await talkreq(p.baseProtocol, toNode, p.protocolId, encodeMessage(request)) + await talkreq(p.baseProtocol, toNode, @(p.protocolId), encodeMessage(request)) # Note: Failure of `decodeMessage` might also simply mean that the peer is # not supporting the specific talk protocol, as according to specification diff --git a/fluffy/tests/test_portal_wire_protocol.nim b/fluffy/tests/test_portal_wire_protocol.nim index 8892729a3..deeb34daf 100644 --- a/fluffy/tests/test_portal_wire_protocol.nim +++ b/fluffy/tests/test_portal_wire_protocol.nim @@ -14,7 +14,7 @@ import ../network/wire/portal_protocol, ./test_helpers -const protocolId = "portal".toBytes() +const protocolId = [byte 0x50, 0x00] type Default2NodeTest = ref object node1: discv5_protocol.Protocol diff --git a/fluffy/tools/portalcli.nim b/fluffy/tools/portalcli.nim index 0247897c8..90b88e9b6 100644 --- a/fluffy/tools/portalcli.nim +++ b/fluffy/tools/portalcli.nim @@ -182,7 +182,8 @@ proc run(config: DiscoveryConf) = d.open() - let portal = PortalProtocol.new(d, "portal".toBytes(), testHandler, + # TODO: Configurable protocol id + let portal = PortalProtocol.new(d, [byte 0x50, 0x0A], testHandler, bootstrapRecords = config.portalBootnodes) if config.metricsEnabled: