2024-01-18 11:07:03 +00:00
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
2021-10-28 09:41:43 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-01-18 11:07:03 +00:00
|
|
|
std/[options, sequtils],
|
2022-06-17 20:45:37 +00:00
|
|
|
chronos,
|
2024-01-24 08:09:11 +00:00
|
|
|
stew/byteutils,
|
2021-10-28 09:41:43 +00:00
|
|
|
testutils/unittests,
|
|
|
|
../../eth/p2p/discoveryv5/[enr, node, routing_table],
|
|
|
|
../../eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2021-12-10 10:12:24 +00:00
|
|
|
../../eth/utp/utp_discv5_protocol,
|
2022-02-02 14:29:45 +00:00
|
|
|
../../eth/keys,
|
2022-08-12 13:09:59 +00:00
|
|
|
../../eth/utp/utp_router as rt,
|
2022-11-10 08:01:58 +00:00
|
|
|
../p2p/discv5_test_helper,
|
|
|
|
../stubloglevel
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2024-01-18 11:07:03 +00:00
|
|
|
procSuite "uTP over discovery v5 protocol":
|
2021-10-28 09:41:43 +00:00
|
|
|
let rng = newRng()
|
|
|
|
let utpProtId = "test-utp".toBytes()
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
proc registerIncomingSocketCallback(serverSockets: AsyncQueue): AcceptConnectionCallback[NodeAddress] =
|
2021-10-28 09:41:43 +00:00
|
|
|
return (
|
2022-01-24 10:58:35 +00:00
|
|
|
proc(server: UtpRouter[NodeAddress], client: UtpSocket[NodeAddress]): Future[void] =
|
2021-10-28 09:41:43 +00:00
|
|
|
serverSockets.addLast(client)
|
|
|
|
)
|
2021-12-11 18:12:55 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
proc allowOneIdCallback(allowedId: uint16): AllowConnectionCallback[NodeAddress] =
|
2021-11-18 09:05:56 +00:00
|
|
|
return (
|
2022-01-24 10:58:35 +00:00
|
|
|
proc(r: UtpRouter[NodeAddress], remoteAddress: NodeAddress, connectionId: uint16): bool =
|
2021-11-18 09:05:56 +00:00
|
|
|
connectionId == allowedId
|
|
|
|
)
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
# TODO Add more tests to discovery v5 suite, especially those which will differ
|
|
|
|
# from standard utp case
|
|
|
|
asyncTest "Success connect to remote host":
|
|
|
|
let
|
2022-01-24 10:58:35 +00:00
|
|
|
queue = newAsyncQueue[UtpSocket[NodeAddress]]()
|
2021-10-28 09:41:43 +00:00
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
|
|
|
utp1 = UtpDiscv5Protocol.new(node1, utpProtId, registerIncomingSocketCallback(queue))
|
2024-01-12 08:49:06 +00:00
|
|
|
utp2 {.used.} = UtpDiscv5Protocol.new(node2, utpProtId, registerIncomingSocketCallback(queue))
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
# nodes must have session between each other
|
2021-10-28 09:41:43 +00:00
|
|
|
check:
|
2022-01-24 10:58:35 +00:00
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
let clientSocketResult = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet())
|
2021-11-18 09:05:56 +00:00
|
|
|
let clientSocket = clientSocketResult.get()
|
2021-11-19 10:36:46 +00:00
|
|
|
let serverSocket = await queue.get()
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
check:
|
|
|
|
clientSocket.isConnected()
|
2021-11-19 10:36:46 +00:00
|
|
|
# in this test we do not configure the socket to be connected just after
|
|
|
|
# accepting incoming connection
|
|
|
|
not serverSocket.isConnected()
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await clientSocket.destroyWait()
|
2021-11-19 10:36:46 +00:00
|
|
|
await serverSocket.destroyWait()
|
2021-10-28 09:41:43 +00:00
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
2022-08-12 13:09:59 +00:00
|
|
|
proc cbUserData(server: UtpRouter[NodeAddress], client: UtpSocket[NodeAddress]): Future[void] =
|
|
|
|
let queue = rt.getUserData[NodeAddress, AsyncQueue[UtpSocket[NodeAddress]]](server)
|
|
|
|
queue.addLast(client)
|
|
|
|
|
|
|
|
asyncTest "Provide user data pointer and use it in callback":
|
|
|
|
let
|
|
|
|
queue = newAsyncQueue[UtpSocket[NodeAddress]]()
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
|
|
|
# constructor which uses connection callback and user data pointer as ref
|
|
|
|
utp1 = UtpDiscv5Protocol.new(node1, utpProtId, cbUserData, queue)
|
2024-01-12 08:49:06 +00:00
|
|
|
utp2 {.used.} = UtpDiscv5Protocol.new(node2, utpProtId, cbUserData, queue)
|
2022-08-12 13:09:59 +00:00
|
|
|
|
|
|
|
# nodes must have session between each other
|
|
|
|
check:
|
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
|
|
|
|
|
|
|
let clientSocketResult = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet())
|
|
|
|
let clientSocket = clientSocketResult.get()
|
|
|
|
let serverSocket = await queue.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientSocket.isConnected()
|
|
|
|
# in this test we do not configure the socket to be connected just after
|
|
|
|
# accepting incoming connection
|
|
|
|
not serverSocket.isConnected()
|
|
|
|
|
|
|
|
await clientSocket.destroyWait()
|
|
|
|
await serverSocket.destroyWait()
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
asyncTest "Success write data over packet size to remote host":
|
|
|
|
let
|
2022-01-24 10:58:35 +00:00
|
|
|
queue = newAsyncQueue[UtpSocket[NodeAddress]]()
|
2021-10-28 09:41:43 +00:00
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
|
|
|
utp1 = UtpDiscv5Protocol.new(node1, utpProtId, registerIncomingSocketCallback(queue))
|
2024-01-12 08:49:06 +00:00
|
|
|
utp2 {.used.} = UtpDiscv5Protocol.new(node2, utpProtId, registerIncomingSocketCallback(queue))
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
# nodes must have session between each other
|
2021-10-28 09:41:43 +00:00
|
|
|
check:
|
2022-01-24 10:58:35 +00:00
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2024-01-12 16:35:44 +00:00
|
|
|
let numOfBytes = 20_000
|
2022-01-24 10:58:35 +00:00
|
|
|
let clientSocketResult = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet())
|
2021-11-18 09:05:56 +00:00
|
|
|
let clientSocket = clientSocketResult.get()
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
let serverSocket = await queue.get()
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
let bytesToTransfer = rng[].generateBytes(numOfBytes)
|
2021-10-28 09:41:43 +00:00
|
|
|
let written = await clientSocket.write(bytesToTransfer)
|
|
|
|
|
|
|
|
let received = await serverSocket.read(numOfBytes)
|
|
|
|
|
|
|
|
check:
|
2021-11-09 14:29:59 +00:00
|
|
|
written.get() == numOfBytes
|
2021-10-28 09:41:43 +00:00
|
|
|
bytesToTransfer == received
|
|
|
|
clientSocket.isConnected()
|
|
|
|
serverSocket.isConnected()
|
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
await clientSocket.destroyWait()
|
|
|
|
await serverSocket.destroyWait()
|
2021-10-28 09:41:43 +00:00
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
2021-11-18 09:05:56 +00:00
|
|
|
|
|
|
|
asyncTest "Accept connection only from allowed peers":
|
|
|
|
let
|
|
|
|
allowedId: uint16 = 10
|
|
|
|
lowSynTimeout = milliseconds(500)
|
2022-01-24 10:58:35 +00:00
|
|
|
queue = newAsyncQueue[UtpSocket[NodeAddress]]()
|
2021-11-18 09:05:56 +00:00
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
|
|
|
utp1 = UtpDiscv5Protocol.new(
|
|
|
|
node1,
|
|
|
|
utpProtId,
|
|
|
|
registerIncomingSocketCallback(queue),
|
2022-01-10 12:49:36 +00:00
|
|
|
socketConfig = SocketConfig.init(lowSynTimeout))
|
2024-01-12 08:49:06 +00:00
|
|
|
utp2 {.used.} =
|
2021-11-18 09:05:56 +00:00
|
|
|
UtpDiscv5Protocol.new(
|
|
|
|
node2,
|
|
|
|
utpProtId,
|
|
|
|
registerIncomingSocketCallback(queue),
|
2022-08-12 13:09:59 +00:00
|
|
|
nil,
|
2022-01-10 12:49:36 +00:00
|
|
|
allowOneIdCallback(allowedId),
|
|
|
|
SocketConfig.init())
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
# nodes must have session between each other
|
2021-11-18 09:05:56 +00:00
|
|
|
check:
|
2022-01-24 10:58:35 +00:00
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
let clientSocketResult1 = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet(), allowedId)
|
|
|
|
let clientSocketResult2 = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet(), allowedId + 1)
|
2021-12-11 18:12:55 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
check:
|
|
|
|
clientSocketResult1.isOk()
|
|
|
|
clientSocketResult2.isErr()
|
|
|
|
|
|
|
|
let clientSocket = clientSocketResult1.get()
|
|
|
|
let serverSocket = await queue.get()
|
2021-12-11 18:12:55 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
check:
|
|
|
|
clientSocket.connectionId() == allowedId
|
|
|
|
serverSocket.connectionId() == allowedId
|
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
await clientSocket.destroyWait()
|
|
|
|
await serverSocket.destroyWait()
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
|
|
|
asyncTest "Configure incoming connections to be in connected state":
|
|
|
|
let
|
2022-01-24 10:58:35 +00:00
|
|
|
queue = newAsyncQueue[UtpSocket[NodeAddress]]()
|
2021-11-19 10:36:46 +00:00
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
|
|
|
utp1 = UtpDiscv5Protocol.new(node1, utpProtId, registerIncomingSocketCallback(queue))
|
2024-01-12 08:49:06 +00:00
|
|
|
utp2 {.used.} = UtpDiscv5Protocol.new(
|
2021-11-19 10:36:46 +00:00
|
|
|
node2,
|
|
|
|
utpProtId,
|
|
|
|
registerIncomingSocketCallback(queue),
|
2022-01-10 12:49:36 +00:00
|
|
|
socketConfig = SocketConfig.init(incomingSocketReceiveTimeout = none[Duration]())
|
2021-11-19 10:36:46 +00:00
|
|
|
)
|
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
# nodes must have session between each other
|
2021-11-19 10:36:46 +00:00
|
|
|
check:
|
2022-01-24 10:58:35 +00:00
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
2021-11-19 10:36:46 +00:00
|
|
|
|
2022-01-24 10:58:35 +00:00
|
|
|
let clientSocketResult = await utp1.connectTo(NodeAddress.init(node2.localNode).unsafeGet())
|
2021-11-19 10:36:46 +00:00
|
|
|
let clientSocket = clientSocketResult.get()
|
|
|
|
let serverSocket = await queue.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientSocket.isConnected()
|
|
|
|
serverSocket.isConnected()
|
|
|
|
|
|
|
|
let serverData = @[1'u8]
|
|
|
|
|
|
|
|
let wResult = await serverSocket.write(serverData)
|
|
|
|
|
|
|
|
check:
|
|
|
|
wResult.isOk()
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-19 10:36:46 +00:00
|
|
|
let readData = await clientSocket.read(len(serverData))
|
|
|
|
|
|
|
|
check:
|
|
|
|
readData == serverData
|
|
|
|
|
|
|
|
await clientSocket.destroyWait()
|
|
|
|
await serverSocket.destroyWait()
|
2021-11-18 09:05:56 +00:00
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
2024-01-18 11:07:03 +00:00
|
|
|
|
|
|
|
asyncTest "Data transfer over multiple sockets":
|
|
|
|
const
|
|
|
|
amountOfTransfers = 25
|
|
|
|
dataToSend: seq[byte] = repeat(byte 0xA0, 1_000_000)
|
|
|
|
|
|
|
|
var readFutures: seq[Future[void]]
|
|
|
|
|
|
|
|
proc readAndCheck(
|
|
|
|
socket: UtpSocket[NodeAddress],
|
|
|
|
): Future[void] {.async.} =
|
|
|
|
let readData = await socket.read()
|
|
|
|
check:
|
|
|
|
readData == dataToSend
|
|
|
|
socket.atEof()
|
|
|
|
|
|
|
|
proc handleIncomingConnection(
|
|
|
|
server: UtpRouter[NodeAddress],
|
|
|
|
client: UtpSocket[NodeAddress]
|
|
|
|
): Future[void] =
|
|
|
|
readFutures.add(client.readAndCheck())
|
|
|
|
|
|
|
|
var fut = newFuture[void]("test.AcceptConnectionCallback")
|
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
|
|
|
|
proc handleIncomingConnectionDummy(
|
|
|
|
server: UtpRouter[NodeAddress],
|
|
|
|
client: UtpSocket[NodeAddress]
|
|
|
|
): Future[void] =
|
|
|
|
var fut = newFuture[void]("test.AcceptConnectionCallback")
|
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
|
|
|
|
let
|
|
|
|
address1 = localAddress(20302)
|
|
|
|
address2 = localAddress(20303)
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), address1)
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), address2)
|
|
|
|
|
|
|
|
utp1 = UtpDiscv5Protocol.new(
|
|
|
|
node1, utpProtId, handleIncomingConnectionDummy)
|
|
|
|
utp2 {.used.} = UtpDiscv5Protocol.new(
|
|
|
|
node2, utpProtId, handleIncomingConnection)
|
|
|
|
|
|
|
|
# nodes must have session between each other
|
|
|
|
check:
|
|
|
|
(await node1.ping(node2.localNode)).isOk()
|
|
|
|
|
|
|
|
proc connectSendAndCheck(
|
|
|
|
utpProto: UtpDiscv5Protocol,
|
|
|
|
address: NodeAddress
|
|
|
|
): Future[void] {.async.} =
|
|
|
|
let socketRes = await utpProto.connectTo(address)
|
|
|
|
check:
|
|
|
|
socketRes.isOk()
|
|
|
|
let socket = socketRes.value()
|
|
|
|
let dataSend = await socket.write(dataToSend)
|
|
|
|
check:
|
|
|
|
dataSend.isOk()
|
|
|
|
dataSend.value() == dataToSend.len()
|
|
|
|
|
|
|
|
await socket.closeWait()
|
|
|
|
|
|
|
|
let t0 = Moment.now()
|
|
|
|
for i in 0..<amountOfTransfers:
|
|
|
|
asyncSpawn utp1.connectSendAndCheck(
|
|
|
|
NodeAddress.init(node2.localNode.id, address2))
|
|
|
|
|
|
|
|
while readFutures.len() < amountOfTransfers:
|
|
|
|
await sleepAsync(milliseconds(100))
|
|
|
|
|
|
|
|
await allFutures(readFutures)
|
|
|
|
let elapsed = Moment.now() - t0
|
|
|
|
|
|
|
|
await utp1.shutdownWait()
|
|
|
|
await utp2.shutdownWait()
|
|
|
|
|
|
|
|
let megabitsSent = amountOfTransfers * dataToSend.len() * 8 / 1_000_000
|
|
|
|
let seconds = float(elapsed.nanoseconds) / 1_000_000_000
|
|
|
|
let throughput = megabitsSent / seconds
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo "Sent ", amountOfTransfers, " asynchronous uTP transfers in ", seconds,
|
|
|
|
" seconds, payload throughput: ", throughput, " Mbit/s"
|