From 8853d74fd5dbe7794fdb0805158dbea423678064 Mon Sep 17 00:00:00 2001 From: Youngjoon Lee <5462944+youngjoon-lee@users.noreply.github.com> Date: Thu, 16 May 2024 15:59:58 +0900 Subject: [PATCH] do not use the start_delayed helper, but handle network latency in p2p for the future analysis --- mixnet/v2/sim/p2p.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mixnet/v2/sim/p2p.py b/mixnet/v2/sim/p2p.py index 27aeef5..5de2af9 100644 --- a/mixnet/v2/sim/p2p.py +++ b/mixnet/v2/sim/p2p.py @@ -3,7 +3,6 @@ import random from collections import defaultdict import simpy -from simpy.util import start_delayed from config import Config from sphinx import SphinxPacket @@ -20,6 +19,9 @@ class P2p: def add_node(self, nodes): self.nodes.extend(nodes) + def get_nodes(self, n: int): + return random.sample(self.nodes, n) + # TODO: This should accept only bytes, but SphinxPacket is also accepted until we implement the Sphinx serde def broadcast(self, sender, msg: SphinxPacket | bytes): self.log("Broadcasting a msg: %d bytes" % len(msg)) @@ -35,11 +37,12 @@ class P2p: # TODO: gossipsub or something similar for node in self.nodes: - network_delay = random.uniform(0, self.config.max_network_latency) - start_delayed(self.env, node.receive_message(msg), delay=network_delay) + self.env.process(self.send(msg, node)) - def get_nodes(self, n: int): - return random.sample(self.nodes, n) + def send(self, msg: SphinxPacket | bytes, node): + # simulate network latency + yield self.env.timeout(random.uniform(0, self.config.max_network_latency)) + self.env.process(node.receive_message(msg)) def log(self, msg): print("P2P at %g: %s" % (self.env.now, msg)) \ No newline at end of file