2023-05-10 13:50:04 +00:00
|
|
|
# Copyright (c) 2021-2023 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.
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-12-10 10:12:24 +00:00
|
|
|
|
|
|
|
import
|
2022-01-24 10:58:35 +00:00
|
|
|
std/[hashes, sugar],
|
2021-12-10 10:12:24 +00:00
|
|
|
chronos, chronicles,
|
2022-10-10 10:13:20 +00:00
|
|
|
../p2p/discoveryv5/[protocol, messages_encoding, 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:
|
2022-12-06 13:54:03 +00:00
|
|
|
topics = "eth utp 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] =
|
2023-06-22 11:31:30 +00:00
|
|
|
node.address.map((address: Address) =>
|
|
|
|
NodeAddress(nodeId: node.id, address: address))
|
2022-01-24 10:58:35 +00:00
|
|
|
|
|
|
|
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-07-09 08:55:15 +00:00
|
|
|
", rcvId: " & $x.rcvId &
|
2022-01-07 09:38:19 +00:00
|
|
|
")"
|
|
|
|
|
2023-06-22 11:31:30 +00:00
|
|
|
proc talkReqDirect(
|
|
|
|
p: protocol.Protocol, n: NodeAddress, protocol, request: seq[byte]):
|
|
|
|
Future[void] =
|
2022-01-24 10:58:35 +00:00
|
|
|
let
|
|
|
|
reqId = RequestId.init(p.rng[])
|
2023-06-22 11:31:30 +00:00
|
|
|
message = encodeMessage(
|
|
|
|
TalkReqMessage(protocol: protocol, request: request), reqId)
|
2024-01-12 08:49:06 +00:00
|
|
|
(data, _) = encodeMessagePacket(
|
2023-06-22 11:31:30 +00:00
|
|
|
p.rng[], p.codec, n.nodeId, n.address, message)
|
2022-01-24 10:58:35 +00:00
|
|
|
|
2023-06-22 11:31:30 +00:00
|
|
|
trace "Send message packet",
|
|
|
|
dstId = n.nodeId, address = n.address, kind = MessageKind.talkreq
|
2022-01-24 10:58:35 +00:00
|
|
|
p.send(n.address, data)
|
2022-04-12 19:11:01 +00:00
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
proc initSendCallback(
|
2023-06-22 11:31:30 +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]()
|
2023-06-22 11:31:30 +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
|
|
|
|
# opening stream is only done after successful OFFER/ACCEPT or
|
|
|
|
# FINDCONTENT/CONTENT exchange which forces nodes to establish session
|
|
|
|
# between each other.
|
2022-01-24 10:58:35 +00:00
|
|
|
discard t.talkReqDirect(to, subProtocolName, data)
|
2021-12-10 10:12:24 +00:00
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
)
|
|
|
|
|
2023-08-30 15:44:05 +00:00
|
|
|
proc messageHandler(
|
|
|
|
protocol: TalkProtocol, request: seq[byte],
|
|
|
|
srcId: NodeId, srcUdpAddress: Address, node: Opt[Node]): 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],
|
2022-08-12 13:09:59 +00:00
|
|
|
udata: pointer = nil,
|
2022-01-24 10:58:35 +00:00
|
|
|
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,
|
2022-08-12 13:09:59 +00:00
|
|
|
udata,
|
2021-12-10 10:12:24 +00:00
|
|
|
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-08-12 13:09:59 +00:00
|
|
|
proc new*(
|
|
|
|
T: type UtpDiscv5Protocol,
|
|
|
|
p: protocol.Protocol,
|
|
|
|
subProtocolName: seq[byte],
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[NodeAddress],
|
|
|
|
udata: ref,
|
|
|
|
allowConnectionCb: AllowConnectionCallback[NodeAddress] = nil,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init()): UtpDiscv5Protocol =
|
|
|
|
GC_ref(udata)
|
|
|
|
UtpDiscv5Protocol.new(
|
|
|
|
p,
|
|
|
|
subProtocolName,
|
|
|
|
acceptConnectionCb,
|
|
|
|
cast[pointer](udata),
|
|
|
|
allowConnectionCb,
|
|
|
|
socketConfig
|
|
|
|
)
|
|
|
|
|
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()
|