239 lines
7.5 KiB
Python
Raw Normal View History

2024-01-23 10:29:14 +09:00
from __future__ import annotations
import hashlib
2024-06-28 18:06:55 +09:00
from enum import Enum
2024-06-26 15:55:00 +09:00
from typing import Awaitable, Callable, TypeAlias
2024-01-23 10:29:14 +09:00
2024-06-26 15:55:00 +09:00
from pysphinx.payload import DEFAULT_PAYLOAD_SIZE
2024-01-23 10:29:14 +09:00
from pysphinx.sphinx import (
Payload,
ProcessedFinalHopPacket,
ProcessedForwardHopPacket,
SphinxPacket,
)
2024-06-27 17:32:22 +09:00
from mixnet.config import GlobalConfig, NodeConfig
from mixnet.connection import SimplexConnection
from mixnet.framework.framework import Framework, Queue
2024-06-26 15:55:00 +09:00
from mixnet.packet import Fragment, MessageFlag, MessageReconstructor, PacketBuilder
2024-01-23 10:29:14 +09:00
NetworkPacketQueue: TypeAlias = Queue
BroadcastChannel: TypeAlias = Queue
2024-01-23 10:29:14 +09:00
2024-06-26 15:55:00 +09:00
class Node:
framework: Framework
2024-06-26 15:55:00 +09:00
config: NodeConfig
2024-06-27 17:32:22 +09:00
global_config: GlobalConfig
2024-06-26 15:55:00 +09:00
mixgossip_channel: MixGossipChannel
reconstructor: MessageReconstructor
broadcast_channel: BroadcastChannel
2024-01-23 10:29:14 +09:00
def __init__(
self, framework: Framework, config: NodeConfig, global_config: GlobalConfig
):
self.framework = framework
2024-06-26 15:55:00 +09:00
self.config = config
2024-06-27 17:32:22 +09:00
self.global_config = global_config
2024-06-28 12:26:44 +09:00
self.mixgossip_channel = MixGossipChannel(
framework, config.peering_degree, self.__process_sphinx_packet
2024-06-28 12:26:44 +09:00
)
2024-06-26 15:55:00 +09:00
self.reconstructor = MessageReconstructor()
self.broadcast_channel = framework.queue()
2024-06-26 15:55:00 +09:00
async def __process_sphinx_packet(
self, packet: SphinxPacket
2024-06-28 18:06:55 +09:00
) -> SphinxPacket | None:
2024-06-26 15:55:00 +09:00
try:
processed = packet.process(self.config.private_key)
match processed:
case ProcessedForwardHopPacket():
return processed.next_packet
case ProcessedFinalHopPacket():
await self.__process_sphinx_payload(processed.payload)
2024-06-28 17:34:13 +09:00
except ValueError:
# Return SphinxPacket as it is, if it cannot be unwrapped by the private key of this node.
2024-06-26 15:55:00 +09:00
return packet
async def __process_sphinx_payload(self, payload: Payload):
msg_with_flag = self.reconstructor.add(
Fragment.from_bytes(payload.recover_plain_playload())
)
if msg_with_flag is not None:
flag, msg = PacketBuilder.parse_msg_and_flag(msg_with_flag)
if flag == MessageFlag.MESSAGE_FLAG_REAL:
2024-07-05 17:01:11 +09:00
print(f"{self.config.id(True)}: Broadcasting message finally: {msg}")
2024-06-26 15:55:00 +09:00
await self.broadcast_channel.put(msg)
2024-07-03 23:29:26 +09:00
def connect(
self,
peer: Node,
inbound_conn: SimplexConnection,
outbound_conn: SimplexConnection,
2024-07-03 23:29:26 +09:00
):
2024-07-05 17:19:02 +09:00
if (
not self.mixgossip_channel.can_accept_conn()
or not peer.mixgossip_channel.can_accept_conn()
):
raise PeeringDegreeReached
2024-06-28 12:26:44 +09:00
self.mixgossip_channel.add_conn(
DuplexConnection(
inbound_conn,
2024-07-03 23:29:26 +09:00
MixSimplexConnection(
self.framework,
outbound_conn,
self.global_config.transmission_rate_per_sec,
2024-06-28 12:26:44 +09:00
),
)
)
peer.mixgossip_channel.add_conn(
DuplexConnection(
outbound_conn,
2024-07-03 23:29:26 +09:00
MixSimplexConnection(
self.framework,
inbound_conn,
self.global_config.transmission_rate_per_sec,
2024-06-28 12:26:44 +09:00
),
)
2024-06-26 15:55:00 +09:00
)
async def send_message(self, msg: bytes):
2024-07-05 17:01:11 +09:00
print(f"{self.config.id(True)}: Sending message: {msg}")
2024-06-27 17:32:22 +09:00
for packet, _ in PacketBuilder.build_real_packets(
msg, self.global_config.membership
):
2024-06-28 18:06:55 +09:00
await self.mixgossip_channel.gossip(build_msg(MsgType.REAL, packet.bytes()))
2024-06-26 15:55:00 +09:00
class MixGossipChannel:
framework: Framework
2024-06-28 12:26:44 +09:00
peering_degree: int
conns: list[DuplexConnection]
2024-06-28 18:06:55 +09:00
handler: Callable[[SphinxPacket], Awaitable[SphinxPacket | None]]
msg_cache: set[bytes]
2024-06-26 15:55:00 +09:00
def __init__(
self,
framework: Framework,
2024-07-03 11:41:29 +09:00
peering_degree: int,
2024-06-28 18:06:55 +09:00
handler: Callable[[SphinxPacket], Awaitable[SphinxPacket | None]],
2024-06-26 15:55:00 +09:00
):
self.framework = framework
2024-07-03 11:41:29 +09:00
self.peering_degree = peering_degree
2024-06-28 12:26:44 +09:00
self.conns = []
2024-06-26 15:55:00 +09:00
self.handler = handler
self.msg_cache = set()
2024-06-26 15:55:00 +09:00
# A set just for gathering a reference of tasks to prevent them from being garbage collected.
# https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task
self.tasks = set()
2024-01-23 10:29:14 +09:00
2024-07-05 17:19:02 +09:00
def can_accept_conn(self) -> bool:
return len(self.conns) < self.peering_degree
2024-06-28 12:26:44 +09:00
def add_conn(self, conn: DuplexConnection):
2024-07-05 17:19:02 +09:00
if not self.can_accept_conn():
2024-06-28 12:26:44 +09:00
# For simplicity of the spec, reject the connection if the peering degree is reached.
2024-07-05 17:19:02 +09:00
raise PeeringDegreeReached()
2024-06-28 12:26:44 +09:00
self.conns.append(conn)
task = self.framework.spawn(self.__process_inbound_conn(conn))
2024-06-26 15:55:00 +09:00
self.tasks.add(task)
2024-06-28 12:26:44 +09:00
async def __process_inbound_conn(self, conn: DuplexConnection):
2024-06-26 15:55:00 +09:00
while True:
2024-06-28 18:06:55 +09:00
msg = await conn.recv()
# Don't process the same message twice.
msg_hash = hashlib.sha256(msg).digest()
if msg_hash in self.msg_cache:
2024-06-26 15:55:00 +09:00
continue
2024-06-28 18:06:55 +09:00
self.msg_cache.add(msg_hash)
2024-06-26 15:55:00 +09:00
2024-06-28 18:06:55 +09:00
flag, msg = parse_msg(msg)
match flag:
case MsgType.NOISE:
# Drop noise packet
continue
case MsgType.REAL:
# Handle the packet and gossip the result if needed.
sphinx_packet = SphinxPacket.from_bytes(msg)
new_sphinx_packet = await self.handler(sphinx_packet)
if new_sphinx_packet is not None:
await self.gossip(
build_msg(MsgType.REAL, new_sphinx_packet.bytes())
)
async def gossip(self, packet: bytes):
2024-06-28 12:26:44 +09:00
for conn in self.conns:
2024-06-26 15:55:00 +09:00
await conn.send(packet)
2024-06-28 12:26:44 +09:00
class DuplexConnection:
2024-07-03 23:29:26 +09:00
inbound: SimplexConnection
outbound: MixSimplexConnection
2024-06-28 12:26:44 +09:00
2024-07-03 23:29:26 +09:00
def __init__(self, inbound: SimplexConnection, outbound: MixSimplexConnection):
2024-06-28 12:26:44 +09:00
self.inbound = inbound
self.outbound = outbound
2024-06-28 18:06:55 +09:00
async def recv(self) -> bytes:
2024-07-03 23:29:26 +09:00
return await self.inbound.recv()
2024-06-28 12:26:44 +09:00
2024-06-28 18:06:55 +09:00
async def send(self, packet: bytes):
2024-06-28 12:26:44 +09:00
await self.outbound.send(packet)
2024-07-03 23:29:26 +09:00
class MixSimplexConnection:
framework: Framework
2024-06-26 15:55:00 +09:00
queue: NetworkPacketQueue
2024-07-03 23:29:26 +09:00
conn: SimplexConnection
transmission_rate_per_sec: float
2024-06-26 15:55:00 +09:00
def __init__(
self,
framework: Framework,
conn: SimplexConnection,
transmission_rate_per_sec: float,
):
self.framework = framework
self.queue = framework.queue()
2024-06-26 15:55:00 +09:00
self.conn = conn
self.transmission_rate_per_sec = transmission_rate_per_sec
self.task = framework.spawn(self.__run())
async def __run(self):
2024-06-26 15:55:00 +09:00
while True:
await self.framework.sleep(1 / self.transmission_rate_per_sec)
2024-06-26 15:55:00 +09:00
# TODO: time mixing
if self.queue.empty():
elem = build_noise_packet()
else:
elem = await self.queue.get()
2024-07-03 23:29:26 +09:00
await self.conn.send(elem)
2024-01-23 10:29:14 +09:00
2024-06-28 18:06:55 +09:00
async def send(self, elem: bytes):
2024-06-26 15:55:00 +09:00
await self.queue.put(elem)
2024-01-23 10:29:14 +09:00
2024-06-28 18:06:55 +09:00
class MsgType(Enum):
REAL = b"\x00"
NOISE = b"\x01"
def build_msg(flag: MsgType, data: bytes) -> bytes:
return flag.value + data
def parse_msg(data: bytes) -> tuple[MsgType, bytes]:
if len(data) < 1:
raise ValueError("Invalid message format")
return (MsgType(data[:1]), data[1:])
2024-06-26 15:55:00 +09:00
def build_noise_packet() -> bytes:
2024-06-28 18:06:55 +09:00
return build_msg(MsgType.NOISE, bytes(DEFAULT_PAYLOAD_SIZE))
2024-07-05 17:19:02 +09:00
class PeeringDegreeReached(Exception):
pass