nomos-specs/mixnet/v2/sim/p2p.py

23 lines
548 B
Python
Raw Normal View History

2024-05-09 10:37:39 +00:00
import random
import simpy
2024-05-09 05:29:10 +00:00
class P2p:
2024-05-09 10:37:39 +00:00
def __init__(self, env: simpy.Environment):
self.env = env
self.nodes = []
def add_node(self, nodes):
self.nodes.extend(nodes)
2024-05-09 05:29:10 +00:00
def broadcast(self, msg):
2024-05-09 10:37:39 +00:00
print("Broadcasting a message at time %d" % self.env.now)
yield self.env.timeout(1)
2024-05-09 05:29:10 +00:00
# TODO: gossipsub or something similar
for node in self.nodes:
2024-05-09 10:37:39 +00:00
self.env.process(node.receive_message(msg))
def get_nodes(self, n: int):
return random.choices(self.nodes, k=n)