Skip to content

Commit

Permalink
add peering_degree
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjoon-lee committed Jun 28, 2024
1 parent 9cd601c commit 5b6ec7c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions mixnet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class GlobalConfig:
@dataclass
class NodeConfig:
private_key: X25519PrivateKey
# The max number of peers a node should maintain in its p2p network
peering_degree: int
mix_path_length: int # TODO: use this when creating Sphinx packets


Expand Down
31 changes: 27 additions & 4 deletions mixnet/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Node:
def __init__(self, config: NodeConfig, global_config: GlobalConfig):
self.config = config
self.global_config = global_config
self.mixgossip_channel = MixGossipChannel(self.__process_sphinx_packet)
self.mixgossip_channel = MixGossipChannel(
config.peering_degree, self.__process_sphinx_packet
)
self.reconstructor = MessageReconstructor()
self.broadcast_channel = asyncio.Queue()

Expand Down Expand Up @@ -58,10 +60,19 @@ async def __process_sphinx_payload(self, payload: Payload):
await self.broadcast_channel.put(msg)

def connect(self, peer: Node):
conn = asyncio.Queue()
peer.mixgossip_channel.add_inbound(conn)
outbound_conn = asyncio.Queue()
peer.mixgossip_channel.add_inbound(outbound_conn)
self.mixgossip_channel.add_outbound(
MixOutboundConnection(conn, self.global_config.transmission_rate_per_sec)
MixOutboundConnection(
outbound_conn, self.global_config.transmission_rate_per_sec
)
)
inbound_conn = asyncio.Queue()
self.mixgossip_channel.add_inbound(inbound_conn)
peer.mixgossip_channel.add_outbound(
MixOutboundConnection(
inbound_conn, self.global_config.transmission_rate_per_sec
)
)

async def send_message(self, msg: bytes):
Expand All @@ -72,14 +83,17 @@ async def send_message(self, msg: bytes):


class MixGossipChannel:
peering_degree: int
inbound_conns: list[Connection]
outbound_conns: list[MixOutboundConnection]
handler: Callable[[SphinxPacket], Awaitable[NetworkPacket | None]]

def __init__(
self,
peer_degree: int,
handler: Callable[[SphinxPacket], Awaitable[NetworkPacket | None]],
):
self.peering_degree = peer_degree
self.inbound_conns = []
self.outbound_conns = []
self.handler = handler
Expand All @@ -88,13 +102,22 @@ def __init__(
self.tasks = set()

def add_inbound(self, conn: Connection):
if len(self.inbound_conns) >= self.peering_degree:
# For simplicity of the spec, reject the connection if the peering degree is reached.
raise ValueError("The peering degree is reached.")

self.inbound_conns.append(conn)
task = asyncio.create_task(self.__process_inbound_conn(conn))
self.tasks.add(task)
# To discard the task from the set automatically when it is done.
task.add_done_callback(self.tasks.discard)
return True

def add_outbound(self, conn: MixOutboundConnection):
if len(self.outbound_conns) >= self.peering_degree:
# For simplicity of the spec, reject the connection if the peering degree is reached.
raise ValueError("The peering degree is reached.")

self.outbound_conns.append(conn)

async def __process_inbound_conn(self, conn: Connection):
Expand Down
5 changes: 4 additions & 1 deletion mixnet/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ async def test_node(self):
global_config, node_configs, _ = init_mixnet_config(10)
nodes = [Node(node_config, global_config) for node_config in node_configs]
for i, node in enumerate(nodes):
node.connect(nodes[(i + 1) % len(nodes)])
try:
node.connect(nodes[(i + 1) % len(nodes)])
except ValueError as e:
print(e)

await nodes[0].send_message(b"block selection")

Expand Down
3 changes: 2 additions & 1 deletion mixnet/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def init_mixnet_config(
num_nodes: int,
) -> tuple[GlobalConfig, list[NodeConfig], dict[bytes, X25519PrivateKey]]:
transmission_rate_per_sec = 3
peering_degree = 6
max_mix_path_length = 3
node_configs = [
NodeConfig(X25519PrivateKey.generate(), max_mix_path_length)
NodeConfig(X25519PrivateKey.generate(), peering_degree, max_mix_path_length)
for _ in range(num_nodes)
]
global_config = GlobalConfig(
Expand Down

0 comments on commit 5b6ec7c

Please sign in to comment.