2021-04-28 14:20:05 +00:00
|
|
|
# nim-eth - Node Discovery Protocol v5
|
|
|
|
# Copyright (c) 2020-2021 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.
|
|
|
|
#
|
2021-02-02 21:47:21 +00:00
|
|
|
## Discovery v5 Protocol Messages as specified at
|
|
|
|
## https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire.md#protocol-messages
|
|
|
|
##
|
2021-04-28 14:20:05 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-02-17 16:44:56 +00:00
|
|
|
import
|
2021-01-25 16:26:18 +00:00
|
|
|
std/[hashes, net],
|
2022-10-10 10:13:20 +00:00
|
|
|
./enr
|
2020-05-28 08:19:36 +00:00
|
|
|
|
2019-12-16 19:38:45 +00:00
|
|
|
type
|
2020-04-27 12:13:00 +00:00
|
|
|
MessageKind* = enum
|
2022-10-10 10:13:20 +00:00
|
|
|
# Note:
|
|
|
|
# This is needed only to keep the compiler happy. Without it, the
|
|
|
|
# `MessageKind` type cannot be used as a discriminator in case objects.
|
|
|
|
# If a message with this value is received however, it will fail at the
|
|
|
|
# decoding step.
|
2019-12-17 23:16:28 +00:00
|
|
|
unused = 0x00
|
|
|
|
|
2022-10-10 10:13:20 +00:00
|
|
|
# The supported message types
|
2019-12-16 19:38:45 +00:00
|
|
|
ping = 0x01
|
|
|
|
pong = 0x02
|
2022-02-03 14:51:08 +00:00
|
|
|
findNode = 0x03
|
2019-12-16 19:38:45 +00:00
|
|
|
nodes = 0x04
|
2022-02-03 14:51:08 +00:00
|
|
|
talkReq = 0x05
|
|
|
|
talkResp = 0x06
|
|
|
|
regTopic = 0x07
|
2020-11-13 11:33:07 +00:00
|
|
|
ticket = 0x08
|
2022-02-03 14:51:08 +00:00
|
|
|
regConfirmation = 0x09
|
|
|
|
topicQuery = 0x0A
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
RequestId* = object
|
|
|
|
id*: seq[byte]
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
PingMessage* = object
|
2019-12-16 19:38:45 +00:00
|
|
|
enrSeq*: uint64
|
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
PongMessage* = object
|
2019-12-16 19:38:45 +00:00
|
|
|
enrSeq*: uint64
|
2021-01-25 16:26:18 +00:00
|
|
|
ip*: IpAddress
|
2019-12-16 19:38:45 +00:00
|
|
|
port*: uint16
|
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
FindNodeMessage* = object
|
2021-07-13 08:05:46 +00:00
|
|
|
distances*: seq[uint16]
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
NodesMessage* = object
|
2019-12-16 19:38:45 +00:00
|
|
|
total*: uint32
|
|
|
|
enrs*: seq[Record]
|
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
TalkReqMessage* = object
|
|
|
|
protocol*: seq[byte]
|
|
|
|
request*: seq[byte]
|
|
|
|
|
|
|
|
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
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
Message* = object
|
2019-12-16 19:38:45 +00:00
|
|
|
reqId*: RequestId
|
2020-04-27 12:13:00 +00:00
|
|
|
case kind*: MessageKind
|
2019-12-16 19:38:45 +00:00
|
|
|
of ping:
|
2020-04-27 12:13:00 +00:00
|
|
|
ping*: PingMessage
|
2019-12-16 19:38:45 +00:00
|
|
|
of pong:
|
2020-04-27 12:13:00 +00:00
|
|
|
pong*: PongMessage
|
2022-02-03 14:51:08 +00:00
|
|
|
of findNode:
|
|
|
|
findNode*: FindNodeMessage
|
2019-12-16 19:38:45 +00:00
|
|
|
of nodes:
|
2020-04-27 12:13:00 +00:00
|
|
|
nodes*: NodesMessage
|
2022-02-03 14:51:08 +00:00
|
|
|
of talkReq:
|
|
|
|
talkReq*: TalkReqMessage
|
|
|
|
of talkResp:
|
|
|
|
talkResp*: TalkRespMessage
|
|
|
|
of regTopic:
|
2020-11-13 11:33:07 +00:00
|
|
|
regtopic*: RegTopicMessage
|
|
|
|
of ticket:
|
|
|
|
ticket*: TicketMessage
|
2022-02-03 14:51:08 +00:00
|
|
|
of regConfirmation:
|
|
|
|
regConfirmation*: RegConfirmationMessage
|
|
|
|
of topicQuery:
|
|
|
|
topicQuery*: TopicQueryMessage
|
2019-12-16 19:38:45 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2020-04-27 12:13:00 +00:00
|
|
|
template messageKind*(T: typedesc[SomeMessage]): MessageKind =
|
|
|
|
when T is PingMessage: ping
|
|
|
|
elif T is PongMessage: pong
|
2022-02-03 14:51:08 +00:00
|
|
|
elif T is FindNodeMessage: findNode
|
2020-04-27 12:13:00 +00:00
|
|
|
elif T is NodesMessage: nodes
|
2022-02-03 14:51:08 +00:00
|
|
|
elif T is TalkReqMessage: talkReq
|
|
|
|
elif T is TalkRespMessage: talkResp
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2022-10-10 10:13:20 +00:00
|
|
|
func init*(T: type RequestId, rng: var HmacDrbgContext): T =
|
|
|
|
var reqId = RequestId(id: newSeq[byte](8)) # RequestId must be <= 8 bytes
|
|
|
|
rng.generate(reqId.id)
|
2020-11-13 11:33:07 +00:00
|
|
|
reqId
|
|
|
|
|
2022-10-10 10:13:20 +00:00
|
|
|
func hash*(reqId: RequestId): Hash =
|
2020-11-13 11:33:07 +00:00
|
|
|
hash(reqId.id)
|