mirror of
https://github.com/status-im/nim-eth.git
synced 2025-02-20 01:38:08 +00:00
moving all RLP enoding from messages to messages_encoding
This commit is contained in:
parent
cd848a8c2e
commit
99fc5fd01a
@ -14,8 +14,7 @@
|
|||||||
|
|
||||||
import
|
import
|
||||||
std/[hashes, net],
|
std/[hashes, net],
|
||||||
stew/arrayops,
|
".."/../[keys],
|
||||||
".."/../[rlp, keys],
|
|
||||||
./enr
|
./enr
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -104,43 +103,6 @@ template messageKind*(T: typedesc[SomeMessage]): MessageKind =
|
|||||||
elif T is TalkReqMessage: talkReq
|
elif T is TalkReqMessage: talkReq
|
||||||
elif T is TalkRespMessage: talkResp
|
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 =
|
proc hash*(reqId: RequestId): Hash =
|
||||||
hash(reqId.id)
|
hash(reqId.id)
|
||||||
|
|
||||||
|
@ -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
|
import
|
||||||
|
std/net,
|
||||||
|
stew/arrayops,
|
||||||
".."/../[rlp],
|
".."/../[rlp],
|
||||||
"."/[messages, enr]
|
"."/[messages, enr]
|
||||||
|
|
||||||
@ -7,6 +19,43 @@ from stew/objects import checkedEnumAssign
|
|||||||
type
|
type
|
||||||
DecodeResult*[T] = Result[T, cstring]
|
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 =
|
proc numFields(T: typedesc): int =
|
||||||
for k, v in fieldPairs(default(T)): inc result
|
for k, v in fieldPairs(default(T)): inc result
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user