do not use the start_delayed helper, but handle network latency in p2p for the future analysis

This commit is contained in:
Youngjoon Lee 2024-05-16 15:59:58 +09:00
parent de554e95d1
commit 8853d74fd5
No known key found for this signature in database
GPG Key ID: 09B750B5BD6F08A2
1 changed files with 8 additions and 5 deletions

View File

@ -3,7 +3,6 @@ import random
from collections import defaultdict from collections import defaultdict
import simpy import simpy
from simpy.util import start_delayed
from config import Config from config import Config
from sphinx import SphinxPacket from sphinx import SphinxPacket
@ -20,6 +19,9 @@ class P2p:
def add_node(self, nodes): def add_node(self, nodes):
self.nodes.extend(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 # TODO: This should accept only bytes, but SphinxPacket is also accepted until we implement the Sphinx serde
def broadcast(self, sender, msg: SphinxPacket | bytes): def broadcast(self, sender, msg: SphinxPacket | bytes):
self.log("Broadcasting a msg: %d bytes" % len(msg)) self.log("Broadcasting a msg: %d bytes" % len(msg))
@ -35,11 +37,12 @@ class P2p:
# TODO: gossipsub or something similar # TODO: gossipsub or something similar
for node in self.nodes: for node in self.nodes:
network_delay = random.uniform(0, self.config.max_network_latency) self.env.process(self.send(msg, node))
start_delayed(self.env, node.receive_message(msg), delay=network_delay)
def get_nodes(self, n: int): def send(self, msg: SphinxPacket | bytes, node):
return random.sample(self.nodes, n) # 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): def log(self, msg):
print("P2P at %g: %s" % (self.env.now, msg)) print("P2P at %g: %s" % (self.env.now, msg))