handle PeeringDegreeReached properly

This commit is contained in:
Youngjoon Lee 2024-07-05 17:19:02 +09:00
parent 3067a5d6b4
commit 2065060cf3
No known key found for this signature in database
GPG Key ID: B4253AFBA618BF4D
2 changed files with 20 additions and 4 deletions

View File

@ -71,6 +71,12 @@ class Node:
inbound_conn: SimplexConnection,
outbound_conn: SimplexConnection,
):
if (
not self.mixgossip_channel.can_accept_conn()
or not peer.mixgossip_channel.can_accept_conn()
):
raise PeeringDegreeReached
self.mixgossip_channel.add_conn(
DuplexConnection(
inbound_conn,
@ -122,10 +128,13 @@ class MixGossipChannel:
# https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task
self.tasks = set()
def can_accept_conn(self) -> bool:
return len(self.conns) < self.peering_degree
def add_conn(self, conn: DuplexConnection):
if len(self.conns) >= self.peering_degree:
if not self.can_accept_conn():
# For simplicity of the spec, reject the connection if the peering degree is reached.
raise ValueError("The peering degree is reached.")
raise PeeringDegreeReached()
self.conns.append(conn)
task = self.framework.spawn(self.__process_inbound_conn(conn))
@ -223,3 +232,7 @@ def parse_msg(data: bytes) -> tuple[MsgType, bytes]:
def build_noise_packet() -> bytes:
return build_msg(MsgType.NOISE, bytes(DEFAULT_PAYLOAD_SIZE))
class PeeringDegreeReached(Exception):
pass

View File

@ -3,7 +3,7 @@ import usim
import mixnet.framework.usim as usimfw
from mixnet.config import GlobalConfig, MixMembership, NodeInfo
from mixnet.framework.framework import Framework
from mixnet.node import Node
from mixnet.node import Node, PeeringDegreeReached
from mixnet.sim.config import Config
from mixnet.sim.connection import MeteredRemoteSimplexConnection
from mixnet.sim.stats import ConnectionStats
@ -54,7 +54,10 @@ class Simulation:
self.create_conn(),
)
peer = nodes[(i + 1) % len(nodes)]
node.connect(peer, inbound_conn, outbound_conn)
try:
node.connect(peer, inbound_conn, outbound_conn)
except PeeringDegreeReached:
continue
conn_stats.register(node, inbound_conn, outbound_conn)
conn_stats.register(peer, outbound_conn, inbound_conn)