2024-01-15 16:29:29 +00:00
|
|
|
# Fluffy
|
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-01-19 15:06:23 +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.
|
|
|
|
|
|
|
|
import
|
2022-02-07 13:19:57 +00:00
|
|
|
std/[options, sequtils, sugar, strutils],
|
2022-01-19 15:06:23 +00:00
|
|
|
unittest2, testutils, chronos,
|
|
|
|
json_rpc/rpcclient, stew/byteutils,
|
2022-01-31 17:40:00 +00:00
|
|
|
eth/keys,
|
2024-01-15 16:29:29 +00:00
|
|
|
./utp_test_rpc_client
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
proc generateBytesHex(rng: var HmacDrbgContext, length: int): string =
|
|
|
|
rng.generateBytes(length).toHex()
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
# Before running this test suite, there need to be two instances of the
|
|
|
|
# utp_test_app running under the tested ports: 9042, 9041.
|
2022-01-19 15:06:23 +00:00
|
|
|
# Those could be launched locally by running either
|
|
|
|
# ./utp_test_app --udp-listen-address=127.0.0.1 --rpc-listen-address=0.0.0.0 --udp-port=9041 --rpc-port=9041
|
|
|
|
# ./utp_test_app --udp-listen-address=127.0.0.1 --rpc-listen-address=0.0.0.0 --udp-port=9042 --rpc-port=9042
|
2022-07-04 07:38:02 +00:00
|
|
|
# or
|
2023-05-10 16:04:35 +00:00
|
|
|
# running from docker dir:
|
|
|
|
# 1. docker build -t test-utp --no-cache --build-arg BRANCH_NAME=branch-name .
|
|
|
|
# 2. SCENARIO="scenario name and params " docker-compose up
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
procSuite "uTP network simulator tests":
|
|
|
|
const
|
|
|
|
clientContainerAddress = "127.0.0.1"
|
|
|
|
clientContainerPort = Port(9042)
|
|
|
|
serverContainerAddress = "127.0.0.1"
|
|
|
|
serverContainerPort = Port(9041)
|
|
|
|
|
|
|
|
let rng = newRng()
|
2022-07-04 07:38:02 +00:00
|
|
|
|
|
|
|
type
|
2023-05-10 16:04:35 +00:00
|
|
|
FutureCallback[A] = proc (): Future[A] {.gcsafe, raises: [].}
|
2022-07-04 07:38:02 +00:00
|
|
|
# combinator which repeatedly calls passed closure until returned future is
|
2022-01-31 17:40:00 +00:00
|
|
|
# successfull
|
2022-02-03 12:11:54 +00:00
|
|
|
# TODO: currently works only for non void types
|
2023-05-10 16:04:35 +00:00
|
|
|
proc repeatTillSuccess[A](
|
|
|
|
f: FutureCallback[A], maxTries: int = 20): Future[A] {.async.} =
|
2022-02-03 12:11:54 +00:00
|
|
|
var i = 0
|
2022-01-31 17:40:00 +00:00
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
let res = await f()
|
|
|
|
return res
|
2022-02-03 12:11:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
echo "Call failed due to " & exc.msg
|
|
|
|
inc i
|
|
|
|
|
|
|
|
if i < maxTries:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise exc
|
2022-01-31 17:40:00 +00:00
|
|
|
except CancelledError as canc:
|
|
|
|
raise canc
|
|
|
|
|
|
|
|
proc findServerConnection(
|
2024-01-15 16:29:29 +00:00
|
|
|
connections: openArray[SKey],
|
|
|
|
clientId: NodeId,
|
|
|
|
clientConnectionId: uint16): Option[Skey] =
|
2022-07-04 07:38:02 +00:00
|
|
|
let conns: seq[SKey] =
|
2023-05-10 16:04:35 +00:00
|
|
|
connections.filter((key:Skey) => key.id == (clientConnectionId + 1) and
|
|
|
|
key.nodeId == clientId)
|
2022-01-31 17:40:00 +00:00
|
|
|
if len(conns) == 0:
|
|
|
|
none[Skey]()
|
|
|
|
else:
|
|
|
|
some[Skey](conns[0])
|
|
|
|
|
2023-05-10 16:04:35 +00:00
|
|
|
proc setupTest():
|
|
|
|
Future[(RpcHttpClient, NodeInfo, RpcHttpClient, NodeInfo)] {.async.} =
|
2022-01-19 15:06:23 +00:00
|
|
|
let client = newRpcHttpClient()
|
|
|
|
let server = newRpcHttpClient()
|
|
|
|
|
|
|
|
await client.connect(clientContainerAddress, clientContainerPort, false)
|
|
|
|
await server.connect(serverContainerAddress, serverContainerPort, false)
|
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
# we may need to retry few times if the sim is not ready yet
|
2022-02-03 12:11:54 +00:00
|
|
|
let clientInfo = await repeatTillSuccess(() => client.discv5_nodeInfo(), 10)
|
|
|
|
let serverInfo = await repeatTillSuccess(() => server.discv5_nodeInfo(), 10)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
# nodes need to have an established discv5 session before the uTP test
|
2022-12-13 18:22:36 +00:00
|
|
|
discard await repeatTillSuccess(() => client.discv5_ping(serverInfo.enr))
|
2022-07-04 07:38:02 +00:00
|
|
|
|
2022-02-07 13:19:57 +00:00
|
|
|
return (client, clientInfo, server, serverInfo)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
asyncTest "100kb transfer from client to server":
|
|
|
|
const amountOfBytes = 100_000
|
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
let
|
2024-01-15 16:29:29 +00:00
|
|
|
(client, clientInfo, server, serverInfo) = await setupTest()
|
2023-05-10 16:04:35 +00:00
|
|
|
clientConnectionKey = await repeatTillSuccess(() =>
|
|
|
|
client.utp_connect(serverInfo.enr))
|
|
|
|
serverConnections = await repeatTillSuccess(() =>
|
|
|
|
server.utp_get_connections())
|
|
|
|
maybeServerConnectionKey = serverConnections.findServerConnection(
|
|
|
|
clientInfo.nodeId, clientConnectionKey.id)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
check:
|
2022-01-31 17:40:00 +00:00
|
|
|
maybeServerConnectionKey.isSome()
|
|
|
|
|
2022-01-19 15:06:23 +00:00
|
|
|
let
|
2024-01-15 16:29:29 +00:00
|
|
|
serverConnectionKey = maybeServerConnectionKey.unsafeGet()
|
|
|
|
bytesToWrite = generateBytesHex(rng[], amountOfBytes)
|
2022-01-31 17:40:00 +00:00
|
|
|
writeRes = await client.utp_write(clientConnectionKey, bytesToWrite)
|
2024-01-15 16:29:29 +00:00
|
|
|
dataRead = await server.utp_read(serverConnectionKey, amountOfBytes)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
check:
|
2022-01-31 17:40:00 +00:00
|
|
|
writeRes == true
|
2024-01-15 16:29:29 +00:00
|
|
|
dataRead == bytesToWrite
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
asyncTest "100kb transfer from server to client":
|
2023-05-10 16:04:35 +00:00
|
|
|
# In classic uTP this would not be possible, as when uTP works over UDP the
|
|
|
|
# client needs to transfer first, but when working over discv5 it should be
|
|
|
|
# possible to transfer data from server to client from the start.
|
2024-01-15 16:29:29 +00:00
|
|
|
const amountOfBytes = 100_000
|
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
let
|
2024-01-15 16:29:29 +00:00
|
|
|
(client, clientInfo, server, serverInfo) = await setupTest()
|
2023-05-10 16:04:35 +00:00
|
|
|
clientConnectionKey = await repeatTillSuccess(() =>
|
|
|
|
client.utp_connect(serverInfo.enr))
|
|
|
|
serverConnections = await repeatTillSuccess(() =>
|
|
|
|
server.utp_get_connections())
|
|
|
|
maybeServerConnectionKey = serverConnections.findServerConnection(
|
|
|
|
clientInfo.nodeId, clientConnectionKey.id)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
maybeServerConnectionKey.isSome()
|
|
|
|
|
|
|
|
let
|
2024-01-15 16:29:29 +00:00
|
|
|
serverConnectionKey = maybeServerConnectionKey.unsafeGet()
|
|
|
|
bytesToWrite = generateBytesHex(rng[], amountOfBytes)
|
2022-02-07 13:19:57 +00:00
|
|
|
writeRes = await server.utp_write(serverConnectionKey, bytesToWrite)
|
2024-01-15 16:29:29 +00:00
|
|
|
dataRead = await client.utp_read(clientConnectionKey, amountOfBytes)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
writeRes == true
|
2024-01-15 16:29:29 +00:00
|
|
|
dataRead == bytesToWrite
|
|
|
|
|
|
|
|
asyncTest "Multiple 10kb transfers from client to server":
|
|
|
|
const
|
|
|
|
amountOfBytes = 10_000
|
|
|
|
amountOfTransfers = 3
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
let
|
2024-01-15 16:29:29 +00:00
|
|
|
(client, clientInfo, server, serverInfo) = await setupTest()
|
2023-05-10 16:04:35 +00:00
|
|
|
clientConnectionKey = await repeatTillSuccess(() =>
|
|
|
|
client.utp_connect(serverInfo.enr))
|
|
|
|
serverConnections = await repeatTillSuccess(() =>
|
|
|
|
server.utp_get_connections())
|
|
|
|
maybeServerConnectionKey = serverConnections.findServerConnection(
|
|
|
|
clientInfo.nodeId, clientConnectionKey.id)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
maybeServerConnectionKey.isSome()
|
|
|
|
|
|
|
|
let serverConnectionKey = maybeServerConnectionKey.unsafeGet()
|
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
var totalBytesToWrite: string
|
|
|
|
for i in 0..<amountOfTransfers:
|
|
|
|
let
|
|
|
|
bytesToWrite = generateBytesHex(rng[], amountOfBytes)
|
|
|
|
writeRes = await client.utp_write(clientConnectionKey, bytesToWrite)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
check writeRes == true
|
|
|
|
totalBytesToWrite.add(bytesToWrite)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
let dataRead = await server.utp_read(
|
|
|
|
serverConnectionKey, amountOfBytes * amountOfTransfers)
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
check dataRead == totalBytesToWrite
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
asyncTest "Multiple 10kb transfers over multiple sockets from client to server":
|
|
|
|
const
|
|
|
|
amountOfBytes = 10_000
|
|
|
|
amountOfSockets = 3
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
let (client, clientInfo, server, serverInfo) = await setupTest()
|
2022-02-07 13:19:57 +00:00
|
|
|
|
2024-01-15 16:29:29 +00:00
|
|
|
var connectionKeys: seq[(SKey, SKey)]
|
|
|
|
for i in 0..<amountOfSockets:
|
|
|
|
let
|
|
|
|
clientConnectionKey = await repeatTillSuccess(() =>
|
|
|
|
client.utp_connect(serverInfo.enr))
|
|
|
|
serverConnections = await repeatTillSuccess(() =>
|
|
|
|
server.utp_get_connections())
|
|
|
|
serverConnectionKeyRes = serverConnections.findServerConnection(
|
|
|
|
clientInfo.nodeId, clientConnectionKey.id)
|
|
|
|
|
|
|
|
check serverConnectionKeyRes.isSome()
|
|
|
|
|
|
|
|
connectionKeys.add((clientConnectionKey, serverConnectionKeyRes.unsafeGet()))
|
|
|
|
|
|
|
|
for (clientConnectionKey, serverConnectionKey) in connectionKeys:
|
|
|
|
let
|
|
|
|
bytesToWrite = generateBytesHex(rng[], amountOfBytes)
|
|
|
|
writeRes = await client.utp_write(clientConnectionKey, bytesToWrite)
|
|
|
|
dataRead = await server.utp_read(serverConnectionKey, amountOfBytes)
|
|
|
|
|
|
|
|
check:
|
|
|
|
writeRes == true
|
|
|
|
dataRead == bytesToWrite
|