76 lines
2.8 KiB
Python
Raw Normal View History

2024-05-09 19:37:39 +09:00
import random
import simpy
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
2024-05-09 19:37:39 +09:00
2024-05-13 08:49:09 +09:00
from sphinx import SphinxPacket, Attachment
from p2p import P2p
2024-05-09 14:29:10 +09:00
class Node:
N_MIXES_IN_PATH = 2
2024-05-09 19:37:39 +09:00
def __init__(self, id: int, env: simpy.Environment, p2p: P2p):
2024-05-09 19:37:39 +09:00
self.id = id
self.env = env
self.p2p = p2p
self.private_key = X25519PrivateKey.generate()
self.public_key = self.private_key.public_key()
2024-05-09 19:37:39 +09:00
self.action = self.env.process(self.send_message())
2024-05-09 14:29:10 +09:00
def send_message(self):
"""
Creates/encapsulate a message and send it to the network through the mixnet
"""
2024-05-13 09:00:01 +09:00
while True:
2024-05-09 14:29:10 +09:00
msg = self.create_message()
2024-05-09 19:37:39 +09:00
yield self.env.timeout(2)
print("Sending a message at time %d" % self.env.now)
self.env.process(self.p2p.broadcast(msg))
2024-05-09 14:29:10 +09:00
def create_message(self) -> SphinxPacket:
2024-05-09 14:29:10 +09:00
"""
Creates a message using the Sphinx format
@return:
"""
2024-05-09 19:37:39 +09:00
mixes = self.p2p.get_nodes(self.N_MIXES_IN_PATH)
public_keys = [mix.public_key for mix in mixes]
# TODO: replace with realistic tx
incentive_txs = [Node.create_incentive_tx(mix.public_key) for mix in mixes]
return SphinxPacket(public_keys, incentive_txs, b"Hello, world!")
2024-05-09 14:29:10 +09:00
def receive_message(self, msg: SphinxPacket | bytes):
2024-05-09 14:29:10 +09:00
"""
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
"""
# simulating network latency
yield self.env.timeout(random.randint(0, 3))
if isinstance(msg, SphinxPacket):
msg, incentive_tx = msg.unwrap(self.private_key)
if self.is_my_incentive_tx(incentive_tx):
self.log("Receiving SphinxPacket. It's mine!")
if msg.is_all_unwrapped():
self.env.process(self.p2p.broadcast(msg.payload))
else:
# TODO: use Poisson delay
yield self.env.timeout(random.randint(0, 5))
self.env.process(self.p2p.broadcast(msg))
else:
self.log("Receiving SphinxPacket, but not mine")
else:
self.log("Received original message: %s" % msg)
@classmethod
def create_incentive_tx(cls, mix_public_key: X25519PublicKey) -> Attachment:
return Attachment(
mix_public_key.public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw))
def is_my_incentive_tx(self, tx: Attachment) -> bool:
return tx == Node.create_incentive_tx(self.public_key)
def log(self, msg):
print("Node:%d at %d: %s" % (self.id, self.env.now, msg))