This commit is contained in:
Youngjoon Lee 2024-05-09 14:29:10 +09:00
parent 9390d481ba
commit 85cd064cbe
No known key found for this signature in database
GPG Key ID: 09B750B5BD6F08A2
5 changed files with 70 additions and 1 deletions

5
mixnet/v2/sim/main.py Normal file
View File

@ -0,0 +1,5 @@
from mixnet.v2.sim.simulation import Simulation
if __name__ == "__main__":
sim = Simulation()
print("Simulation complete!")

40
mixnet/v2/sim/node.py Normal file
View File

@ -0,0 +1,40 @@
from mixnet.v2.sim.simulation import Simulation
class Node:
def __init__(self, sim: Simulation):
self.sim = sim
self.sim.env.process(self.send_message())
def send_message(self):
"""
Creates/encapsulate a message and send it to the network through the mixnet
"""
while True:
msg = self.create_message()
yield self.sim.env.timeout(3)
self.sim.env.process(self.sim.p2p.broadcast(msg))
def create_message(self) -> bytes:
"""
Creates a message using the Sphinx format
@return:
"""
return b""
def receive_message(self, msg: bytes):
"""
Receives a message from the network, processes it,
and forwards it to the next mix or the entire network if necessary.
@param msg: the message to be processed
"""
# TODO: this is a dummy logic
if msg[0] == 0x00: # if the msg is to be relayed
if msg[1] == 0x00: # if I'm the exit mix,
self.sim.env.process(self.sim.p2p.broadcast(msg))
else: # Even if not, forward it to the next mix
yield self.sim.env.timeout(1) # TODO: use a random delay
# Use broadcasting here too
self.sim.env.process(self.sim.p2p.broadcast(msg))
else: # if the msg has gone through all mixes
pass

13
mixnet/v2/sim/p2p.py Normal file
View File

@ -0,0 +1,13 @@
from mixnet.v2.sim.node import Node
from mixnet.v2.sim.simulation import Simulation
class P2p:
def __init__(self, sim: Simulation, nodes: list[Node]):
self.sim = sim
self.nodes = nodes
def broadcast(self, msg):
# TODO: gossipsub or something similar
for node in self.nodes:
self.sim.env.process(node.receive_message(msg))

View File

@ -0,0 +1,10 @@
import simpy
class Simulation:
def __init__(self):
self.env = simpy.Environment()
self.p2p = None # TODO: implement p2p
def run(self, until):
self.env.run(until=until)

View File

@ -6,4 +6,5 @@ pycparser==2.21
pysphinx==0.0.1
scipy==1.11.4
black==23.12.1
sympy==1.12
sympy==1.12
simpy==4.1.1