2022-09-29 23:49:55 +00:00
|
|
|
# codex-dht - Codex DHT
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
2022-02-14 00:51:28 +00:00
|
|
|
# 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 Protocol Messages as specified at
|
|
|
|
## https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire.md#protocol-messages
|
2022-04-07 19:02:12 +00:00
|
|
|
## These messages get protobuf encoded, while in the spec they get RLP encoded.
|
2022-02-14 00:51:28 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[hashes, net],
|
2022-11-02 16:21:05 +00:00
|
|
|
bearssl/rand,
|
2022-03-07 00:19:49 +00:00
|
|
|
./spr,
|
2022-03-17 00:29:01 +00:00
|
|
|
./node,
|
2022-03-10 06:02:24 +00:00
|
|
|
../../../../dht/providers_messages
|
|
|
|
|
|
|
|
export providers_messages
|
2022-02-14 00:51:28 +00:00
|
|
|
|
|
|
|
type
|
2022-09-12 21:46:24 +00:00
|
|
|
MessageKind* {.pure.} = enum
|
2022-02-14 00:51:28 +00:00
|
|
|
# TODO This is needed only to make Nim 1.2.6 happy
|
|
|
|
# Without it, the `MessageKind` type cannot be used as
|
|
|
|
# a discriminator in case objects.
|
|
|
|
unused = 0x00
|
|
|
|
|
|
|
|
ping = 0x01
|
|
|
|
pong = 0x02
|
|
|
|
findNode = 0x03
|
|
|
|
nodes = 0x04
|
|
|
|
talkReq = 0x05
|
|
|
|
talkResp = 0x06
|
|
|
|
regTopic = 0x07
|
|
|
|
ticket = 0x08
|
|
|
|
regConfirmation = 0x09
|
|
|
|
topicQuery = 0x0A
|
2022-03-10 06:02:24 +00:00
|
|
|
addProvider = 0x0B
|
|
|
|
getProviders = 0x0C
|
|
|
|
providers = 0x0D
|
2022-03-17 00:29:01 +00:00
|
|
|
findNodeFast = 0x83
|
2022-02-14 00:51:28 +00:00
|
|
|
|
|
|
|
RequestId* = object
|
|
|
|
id*: seq[byte]
|
|
|
|
|
|
|
|
PingMessage* = object
|
2022-03-07 00:19:49 +00:00
|
|
|
sprSeq*: uint64
|
2022-02-14 00:51:28 +00:00
|
|
|
|
|
|
|
PongMessage* = object
|
2022-03-07 00:19:49 +00:00
|
|
|
sprSeq*: uint64
|
2022-02-14 00:51:28 +00:00
|
|
|
ip*: IpAddress
|
|
|
|
port*: uint16
|
|
|
|
|
|
|
|
FindNodeMessage* = object
|
|
|
|
distances*: seq[uint16]
|
|
|
|
|
2022-03-17 00:29:01 +00:00
|
|
|
FindNodeFastMessage* = object
|
|
|
|
target*: NodeId
|
|
|
|
|
2022-02-14 00:51:28 +00:00
|
|
|
NodesMessage* = object
|
|
|
|
total*: uint32
|
2022-03-07 00:19:49 +00:00
|
|
|
sprs*: seq[SignedPeerRecord]
|
2022-02-14 00:51:28 +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
|
2022-03-10 06:02:24 +00:00
|
|
|
TalkReqMessage or TalkRespMessage or AddProviderMessage or GetProvidersMessage or
|
2022-03-17 00:29:01 +00:00
|
|
|
ProvidersMessage or FindNodeFastMessage
|
2022-02-14 00:51:28 +00:00
|
|
|
|
|
|
|
Message* = object
|
|
|
|
reqId*: RequestId
|
|
|
|
case kind*: MessageKind
|
|
|
|
of ping:
|
|
|
|
ping*: PingMessage
|
|
|
|
of pong:
|
|
|
|
pong*: PongMessage
|
|
|
|
of findNode:
|
|
|
|
findNode*: FindNodeMessage
|
2022-03-17 00:29:01 +00:00
|
|
|
of findNodeFast:
|
|
|
|
findNodeFast*: FindNodeFastMessage
|
2022-02-14 00:51:28 +00:00
|
|
|
of nodes:
|
|
|
|
nodes*: NodesMessage
|
|
|
|
of talkReq:
|
|
|
|
talkReq*: TalkReqMessage
|
|
|
|
of talkResp:
|
|
|
|
talkResp*: TalkRespMessage
|
|
|
|
of regTopic:
|
|
|
|
regtopic*: RegTopicMessage
|
|
|
|
of ticket:
|
|
|
|
ticket*: TicketMessage
|
|
|
|
of regConfirmation:
|
|
|
|
regConfirmation*: RegConfirmationMessage
|
|
|
|
of topicQuery:
|
|
|
|
topicQuery*: TopicQueryMessage
|
2022-03-10 06:02:24 +00:00
|
|
|
of addProvider:
|
|
|
|
addProvider*: AddProviderMessage
|
|
|
|
of getProviders:
|
|
|
|
getProviders*: GetProvidersMessage
|
|
|
|
of providers:
|
|
|
|
provs*: ProvidersMessage
|
2022-02-14 00:51:28 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
template messageKind*(T: typedesc[SomeMessage]): MessageKind =
|
2022-09-12 21:46:24 +00:00
|
|
|
when T is PingMessage: MessageKind.ping
|
|
|
|
elif T is PongMessage: MessageKind.pong
|
|
|
|
elif T is FindNodeMessage: MessageKind.findNode
|
|
|
|
elif T is FindNodeFastMessage: MessageKind.findNodeFast
|
|
|
|
elif T is NodesMessage: MessageKind.nodes
|
|
|
|
elif T is TalkReqMessage: MessageKind.talkReq
|
|
|
|
elif T is TalkRespMessage: MessageKind.talkResp
|
|
|
|
elif T is AddProviderMessage: MessageKind.addProvider
|
|
|
|
elif T is GetProvidersMessage: MessageKind.getProviders
|
|
|
|
elif T is ProvidersMessage: MessageKind.providers
|
2022-02-14 00:51:28 +00:00
|
|
|
|
|
|
|
proc hash*(reqId: RequestId): Hash =
|
|
|
|
hash(reqId.id)
|
2022-02-15 01:04:40 +00:00
|
|
|
|
2022-11-02 16:21:05 +00:00
|
|
|
proc init*(T: type RequestId, rng: var HmacDrbgContext): T =
|
2022-02-15 01:04:40 +00:00
|
|
|
var reqId = RequestId(id: newSeq[byte](8)) # RequestId must be <= 8 bytes
|
2022-11-02 16:21:05 +00:00
|
|
|
hmacDrbgGenerate(rng, reqId.id)
|
2022-02-15 01:04:40 +00:00
|
|
|
reqId
|