From 0825d887ea0cfc70c919d0c6b944ba4c7dbc6021 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Tue, 8 Oct 2024 10:47:05 +0200 Subject: [PATCH] add bandwidth estimate Signed-off-by: Csaba Kiraly --- codexdht/private/eth/p2p/discoveryv5/protocol.nim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/codexdht/private/eth/p2p/discoveryv5/protocol.nim b/codexdht/private/eth/p2p/discoveryv5/protocol.nim index c6386e9..1efecb7 100644 --- a/codexdht/private/eth/p2p/discoveryv5/protocol.nim +++ b/codexdht/private/eth/p2p/discoveryv5/protocol.nim @@ -514,6 +514,18 @@ proc waitNodes(d: Protocol, fromNode: Node, reqId: RequestId): op = await d.waitMessage(fromNode, reqId) if op.isSome and op.get.kind == MessageKind.nodes: res.add(op.get.nodes.sprs) + # Estimate bandwidth based on UDP packet train received, assuming these were + # released fast and spaced in time by bandwidth bottleneck. This is just a rough + # packet-pair based estimate, far from being perfect. + # TODO: get message size from lower layer for better bandwidth estimate + # TODO: get better reception timestamp from lower layers + let + deltaT = Moment.now() - firstTime + bwBps = 500.0 * 8.0 / (deltaT.nanoseconds.float / i.float / 1e9) + debug "bw estimate:", deltaT = deltaT, i, + bw_mbps = bwBps / 1e6, + node = fromNode + fromNode.registerBw(bwBps) else: # No error on this as we received some nodes. break @@ -941,6 +953,7 @@ proc revalidateNode*(d: Protocol, n: Node) {.async.} = discard d.addNode(nodes[][0]) # Get IP and port from pong message and add it to the ip votes + trace "pong rx", n, myip = res.ip, myport = res.port let a = Address(ip: ValidIpAddress.init(res.ip), port: Port(res.port)) d.ipVote.insert(n.id, a)