2022-04-12 19:11:01 +00:00
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-12-10 10:12:24 +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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2022-01-24 10:58:35 +00:00
|
|
|
std/[hashes, sugar],
|
2021-12-10 10:12:24 +00:00
|
|
|
chronos, chronicles,
|
2022-01-24 10:58:35 +00:00
|
|
|
../p2p/discoveryv5/[protocol, messages, encoding],
|
2021-12-10 10:12:24 +00:00
|
|
|
./utp_router,
|
|
|
|
../keys
|
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
export utp_router, protocol
|
2022-01-20 12:20:30 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "utp_discv5_protocol"
|
2021-12-10 10:12:24 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
type
|
2022-01-24 10:58:35 +00:00
|
|
|
NodeAddress* = object
|
|
|
|
nodeId*: NodeId
|
|
|
|
address*: Address
|
2021-12-10 10:12:24 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
UtpDiscv5Protocol* = ref object of TalkProtocol
|
|
|
|
prot: protocol.Protocol
|
|
|
|
router: UtpRouter[NodeAddress]
|
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc init*(T: type NodeAddress, nodeId: NodeId, address: Address): NodeAddress =
|
2022-01-24 10:58:35 +00:00
|
|
|
NodeAddress(nodeId: nodeId, address: address)
|
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc init*(T: type NodeAddress, node: Node): Option[NodeAddress] =
|
2022-01-24 10:58:35 +00:00
|
|
|
node.address.map((address: Address) => NodeAddress(nodeId: node.id, address: address))
|
|
|
|
|
|
|
|
proc hash(x: NodeAddress): Hash =
|
|
|
|
var h = 0
|
|
|
|
h = h !& x.nodeId.hash
|
|
|
|
h = h !& x.address.hash
|
|
|
|
!$h
|
|
|
|
|
|
|
|
proc hash(x: UtpSocketKey[NodeAddress]): Hash =
|
2021-12-10 10:12:24 +00:00
|
|
|
var h = 0
|
|
|
|
h = h !& x.remoteAddress.hash
|
|
|
|
h = h !& x.rcvId.hash
|
|
|
|
!$h
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
func `$`*(x: UtpSocketKey[NodeAddress]): string =
|
|
|
|
"(remoteId: " & $x.remoteAddress.nodeId &
|
2022-01-10 12:49:36 +00:00
|
|
|
", remoteAddress: " & $x.remoteAddress.address &
|
2022-01-07 09:38:19 +00:00
|
|
|
", rcvId: "& $x.rcvId &
|
|
|
|
")"
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
proc talkReqDirect(p: protocol.Protocol, n: NodeAddress, protocol, request: seq[byte]): Future[void] =
|
|
|
|
let
|
|
|
|
reqId = RequestId.init(p.rng[])
|
|
|
|
message = encodeMessage(TalkReqMessage(protocol: protocol, request: request), reqId)
|
|
|
|
|
|
|
|
(data, nonce) = encodeMessagePacket(p.rng[], p.codec, n.nodeId, n.address, message)
|
|
|
|
|
|
|
|
trace "Send message packet", dstId = n.nodeId, address = n.address, kind = MessageKind.talkreq
|
|
|
|
p.send(n.address, data)
|
2022-04-12 19:11:01 +00:00
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
proc initSendCallback(
|
2022-01-24 10:58:35 +00:00
|
|
|
t: protocol.Protocol, subProtocolName: seq[byte]): SendCallback[NodeAddress] =
|
2021-12-10 10:12:24 +00:00
|
|
|
return (
|
2022-01-24 10:58:35 +00:00
|
|
|
proc (to: NodeAddress, data: seq[byte]): Future[void] =
|
2021-12-10 10:12:24 +00:00
|
|
|
let fut = newFuture[void]()
|
2022-01-24 10:58:35 +00:00
|
|
|
# hidden assumption here is that nodes already have established discv5 session
|
|
|
|
# between each other. In our use case this should be true as openning stream
|
|
|
|
# is only done after succesful OFFER/ACCEPT or FINDCONTENT/CONTENT exchange
|
|
|
|
# which forces nodes to establish session between each other.
|
|
|
|
discard t.talkReqDirect(to, subProtocolName, data)
|
2021-12-10 10:12:24 +00:00
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
)
|
|
|
|
|
|
|
|
proc messageHandler(protocol: TalkProtocol, request: seq[byte],
|
|
|
|
srcId: NodeId, srcUdpAddress: Address): seq[byte] =
|
2022-04-12 19:11:01 +00:00
|
|
|
let
|
2022-01-24 10:58:35 +00:00
|
|
|
p = UtpDiscv5Protocol(protocol)
|
|
|
|
nodeAddress = NodeAddress.init(srcId, srcUdpAddress)
|
|
|
|
debug "Received utp payload from known node. Start processing",
|
|
|
|
nodeId = nodeAddress.nodeId, address = nodeAddress.address
|
|
|
|
asyncSpawn p.router.processIncomingBytes(request, nodeAddress)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
proc new*(
|
|
|
|
T: type UtpDiscv5Protocol,
|
|
|
|
p: protocol.Protocol,
|
|
|
|
subProtocolName: seq[byte],
|
2022-01-24 10:58:35 +00:00
|
|
|
acceptConnectionCb: AcceptConnectionCallback[NodeAddress],
|
|
|
|
allowConnectionCb: AllowConnectionCallback[NodeAddress] = nil,
|
2022-01-10 12:49:36 +00:00
|
|
|
socketConfig: SocketConfig = SocketConfig.init()): UtpDiscv5Protocol =
|
2021-12-10 10:12:24 +00:00
|
|
|
doAssert(not(isNil(acceptConnectionCb)))
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
let router = UtpRouter[NodeAddress].new(
|
2021-12-10 10:12:24 +00:00
|
|
|
acceptConnectionCb,
|
|
|
|
allowConnectionCb,
|
|
|
|
socketConfig,
|
2022-01-10 12:49:36 +00:00
|
|
|
p.rng
|
2021-12-10 10:12:24 +00:00
|
|
|
)
|
|
|
|
router.sendCb = initSendCallback(p, subProtocolName)
|
|
|
|
|
|
|
|
let prot = UtpDiscv5Protocol(
|
|
|
|
protocolHandler: messageHandler,
|
|
|
|
prot: p,
|
|
|
|
router: router
|
|
|
|
)
|
|
|
|
|
|
|
|
p.registerTalkProtocol(subProtocolName, prot).expect(
|
|
|
|
"Only one protocol should have this id"
|
|
|
|
)
|
|
|
|
prot
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
proc connectTo*(r: UtpDiscv5Protocol, address: NodeAddress):
|
|
|
|
Future[ConnectionResult[NodeAddress]] =
|
2021-12-10 10:12:24 +00:00
|
|
|
return r.router.connectTo(address)
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
proc connectTo*(r: UtpDiscv5Protocol, address: NodeAddress, connectionId: uint16):
|
|
|
|
Future[ConnectionResult[NodeAddress]] =
|
2021-12-10 10:12:24 +00:00
|
|
|
return r.router.connectTo(address, connectionId)
|
|
|
|
|
|
|
|
proc shutdown*(r: UtpDiscv5Protocol) =
|
|
|
|
## Closes all managed utp connections in background (does not close discovery,
|
|
|
|
## this is up to user)
|
|
|
|
r.router.shutdown()
|
|
|
|
|
|
|
|
proc shutdownWait*(r: UtpDiscv5Protocol) {.async.} =
|
|
|
|
## Closes all managed utp connections in background (does not close discovery,
|
|
|
|
## this is up to user)
|
|
|
|
await r.router.shutdownWait()
|