2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2020-03-23 06:03:36 +00:00
|
|
|
import options, sequtils
|
2022-03-14 08:39:30 +00:00
|
|
|
import "../../.."/[
|
|
|
|
peerid,
|
|
|
|
routing_record,
|
|
|
|
utility
|
|
|
|
]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-16 10:06:57 +00:00
|
|
|
export options
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
type
|
2020-09-21 09:16:29 +00:00
|
|
|
PeerInfoMsg* = object
|
2022-03-14 08:39:30 +00:00
|
|
|
peerId*: PeerId
|
2020-09-21 09:16:29 +00:00
|
|
|
signedPeerRecord*: seq[byte]
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
SubOpts* = object
|
|
|
|
subscribe*: bool
|
|
|
|
topic*: string
|
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
MessageId* = seq[byte]
|
2020-10-21 03:26:04 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
Message* = object
|
2020-06-28 15:56:38 +00:00
|
|
|
fromPeer*: PeerId
|
2019-12-06 02:16:18 +00:00
|
|
|
data*: seq[byte]
|
|
|
|
seqno*: seq[byte]
|
2022-07-27 17:14:05 +00:00
|
|
|
topicIds*: seq[string]
|
2019-12-06 02:16:18 +00:00
|
|
|
signature*: seq[byte]
|
|
|
|
key*: seq[byte]
|
|
|
|
|
|
|
|
ControlMessage* = object
|
|
|
|
ihave*: seq[ControlIHave]
|
|
|
|
iwant*: seq[ControlIWant]
|
|
|
|
graft*: seq[ControlGraft]
|
|
|
|
prune*: seq[ControlPrune]
|
2023-07-28 08:58:05 +00:00
|
|
|
idontwant*: seq[ControlIWant]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
ControlIHave* = object
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId*: string
|
2022-07-27 17:14:05 +00:00
|
|
|
messageIds*: seq[MessageId]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
ControlIWant* = object
|
2022-07-27 17:14:05 +00:00
|
|
|
messageIds*: seq[MessageId]
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
ControlGraft* = object
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId*: string
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
ControlPrune* = object
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId*: string
|
2020-09-21 09:16:29 +00:00
|
|
|
peers*: seq[PeerInfoMsg]
|
|
|
|
backoff*: uint64
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
RPCMsg* = object
|
|
|
|
subscriptions*: seq[SubOpts]
|
|
|
|
messages*: seq[Message]
|
|
|
|
control*: Option[ControlMessage]
|
2023-06-21 08:40:10 +00:00
|
|
|
ping*: seq[byte]
|
|
|
|
pong*: seq[byte]
|
2020-03-23 06:03:36 +00:00
|
|
|
|
2020-09-01 07:33:03 +00:00
|
|
|
func withSubs*(
|
|
|
|
T: type RPCMsg, topics: openArray[string], subscribe: bool): T =
|
|
|
|
T(
|
|
|
|
subscriptions: topics.mapIt(SubOpts(subscribe: subscribe, topic: it)))
|
|
|
|
|
2020-03-23 06:03:36 +00:00
|
|
|
func shortLog*(s: ControlIHave): auto =
|
|
|
|
(
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId: s.topicId.shortLog,
|
2022-07-27 17:14:05 +00:00
|
|
|
messageIds: mapIt(s.messageIds, it.shortLog)
|
2020-03-23 06:03:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func shortLog*(s: ControlIWant): auto =
|
|
|
|
(
|
2022-07-27 17:14:05 +00:00
|
|
|
messageIds: mapIt(s.messageIds, it.shortLog)
|
2020-03-23 06:03:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func shortLog*(s: ControlGraft): auto =
|
|
|
|
(
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId: s.topicId.shortLog
|
2020-03-23 06:03:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func shortLog*(s: ControlPrune): auto =
|
|
|
|
(
|
2022-07-27 11:47:50 +00:00
|
|
|
topicId: s.topicId.shortLog
|
2020-03-23 06:03:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func shortLog*(c: ControlMessage): auto =
|
|
|
|
(
|
|
|
|
ihave: mapIt(c.ihave, it.shortLog),
|
|
|
|
iwant: mapIt(c.iwant, it.shortLog),
|
|
|
|
graft: mapIt(c.graft, it.shortLog),
|
|
|
|
prune: mapIt(c.prune, it.shortLog)
|
|
|
|
)
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-03-23 06:03:36 +00:00
|
|
|
func shortLog*(msg: Message): auto =
|
|
|
|
(
|
2020-09-06 08:31:47 +00:00
|
|
|
fromPeer: msg.fromPeer.shortLog,
|
2020-03-23 06:03:36 +00:00
|
|
|
data: msg.data.shortLog,
|
|
|
|
seqno: msg.seqno.shortLog,
|
2022-07-27 17:14:05 +00:00
|
|
|
topicIds: $msg.topicIds,
|
2020-03-23 06:03:36 +00:00
|
|
|
signature: msg.signature.shortLog,
|
|
|
|
key: msg.key.shortLog
|
|
|
|
)
|
|
|
|
|
|
|
|
func shortLog*(m: RPCMsg): auto =
|
2023-06-28 14:44:58 +00:00
|
|
|
(
|
|
|
|
subscriptions: m.subscriptions,
|
|
|
|
messages: mapIt(m.messages, it.shortLog),
|
|
|
|
control: m.control.get(ControlMessage()).shortLog
|
|
|
|
)
|