2024-05-09 10:37:39 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
import simpy
|
2024-05-09 05:29:10 +00:00
|
|
|
|
2024-05-12 23:49:09 +00:00
|
|
|
from sphinx import SphinxPacket
|
2024-05-12 10:30:04 +00:00
|
|
|
|
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
|
|
|
|
2024-05-12 10:30:04 +00:00
|
|
|
# TODO: This should accept only bytes, but SphinxPacket is also accepted until we implement the Sphinx serde
|
|
|
|
def broadcast(self, msg: SphinxPacket | bytes):
|
|
|
|
self.log("Broadcasting a msg")
|
2024-05-09 10:37:39 +00:00
|
|
|
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):
|
2024-05-12 10:30:04 +00:00
|
|
|
return random.sample(self.nodes, n)
|
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
print("P2P at %d: %s" % (self.env.now, msg))
|