diff --git a/eth/p2p/discoveryv5/messages.nim b/eth/p2p/discoveryv5/messages.nim index 46a9117..94bf09f 100644 --- a/eth/p2p/discoveryv5/messages.nim +++ b/eth/p2p/discoveryv5/messages.nim @@ -14,8 +14,7 @@ import std/[hashes, net], - stew/arrayops, - eth/[rlp, keys], + eth/[keys], ./enr type @@ -104,43 +103,6 @@ template messageKind*(T: typedesc[SomeMessage]): MessageKind = elif T is TalkReqMessage: talkReq elif T is TalkRespMessage: talkResp -proc read*(rlp: var Rlp, T: type RequestId): T - {.raises: [ValueError, RlpError, Defect].} = - mixin read - var reqId: RequestId - reqId.id = rlp.toBytes() - if reqId.id.len > 8: - raise newException(ValueError, "RequestId is > 8 bytes") - rlp.skipElem() - - reqId - -proc append*(writer: var RlpWriter, value: RequestId) = - writer.append(value.id) - -proc read*(rlp: var Rlp, T: type IpAddress): T - {.raises: [RlpError, Defect].} = - let ipBytes = rlp.toBytes() - rlp.skipElem() - - if ipBytes.len == 4: - var ip: array[4, byte] - discard copyFrom(ip, ipBytes) - IpAddress(family: IPv4, address_v4: ip) - elif ipBytes.len == 16: - var ip: array[16, byte] - discard copyFrom(ip, ipBytes) - IpAddress(family: IPv6, address_v6: ip) - else: - raise newException(RlpTypeMismatch, - "Amount of bytes for IP address is different from 4 or 16") - -proc append*(writer: var RlpWriter, ip: IpAddress) = - case ip.family: - of IpAddressFamily.IPv4: - writer.append(ip.address_v4) - of IpAddressFamily.IPv6: writer.append(ip.address_v6) - proc hash*(reqId: RequestId): Hash = hash(reqId.id) diff --git a/eth/p2p/discoveryv5/messages_encoding.nim b/eth/p2p/discoveryv5/messages_encoding.nim index 7c13ce2..97f8ce8 100644 --- a/eth/p2p/discoveryv5/messages_encoding.nim +++ b/eth/p2p/discoveryv5/messages_encoding.nim @@ -1,4 +1,16 @@ +# nim-eth - Node Discovery Protocol v5 +# Copyright (c) 2020-2022 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * 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. +# +## Discovery v5 packet encoding as specified at +## https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire.md#packet-encoding + import + std/net, + stew/arrayops, eth/[rlp], "."/[messages, enr] @@ -7,6 +19,43 @@ from stew/objects import checkedEnumAssign type DecodeResult*[T] = Result[T, cstring] +proc read*(rlp: var Rlp, T: type RequestId): T + {.raises: [ValueError, RlpError, Defect].} = + mixin read + var reqId: RequestId + reqId.id = rlp.toBytes() + if reqId.id.len > 8: + raise newException(ValueError, "RequestId is > 8 bytes") + rlp.skipElem() + + reqId + +proc append*(writer: var RlpWriter, value: RequestId) = + writer.append(value.id) + +proc read*(rlp: var Rlp, T: type IpAddress): T + {.raises: [RlpError, Defect].} = + let ipBytes = rlp.toBytes() + rlp.skipElem() + + if ipBytes.len == 4: + var ip: array[4, byte] + discard copyFrom(ip, ipBytes) + IpAddress(family: IPv4, address_v4: ip) + elif ipBytes.len == 16: + var ip: array[16, byte] + discard copyFrom(ip, ipBytes) + IpAddress(family: IPv6, address_v6: ip) + else: + raise newException(RlpTypeMismatch, + "Amount of bytes for IP address is different from 4 or 16") + +proc append*(writer: var RlpWriter, ip: IpAddress) = + case ip.family: + of IpAddressFamily.IPv4: + writer.append(ip.address_v4) + of IpAddressFamily.IPv6: writer.append(ip.address_v6) + proc numFields(T: typedesc): int = for k, v in fieldPairs(default(T)): inc result