refactor GossipConfig

This commit is contained in:
Youngjoon Lee 2024-07-10 08:57:58 +09:00
parent 36eeab05ed
commit e995c4069d
No known key found for this signature in database
GPG Key ID: B4253AFBA618BF4D
3 changed files with 15 additions and 9 deletions

View File

@ -22,9 +22,14 @@ class GlobalConfig:
@dataclass
class NodeConfig:
private_key: X25519PrivateKey
mix_path_length: int # TODO: use this when creating Sphinx packets
gossip: GossipConfig
@dataclass
class GossipConfig:
# The target 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
@dataclass

View File

@ -12,7 +12,7 @@ from pysphinx.sphinx import (
SphinxPacket,
)
from mixnet.config import GlobalConfig, NodeConfig
from mixnet.config import GlobalConfig, GossipConfig, NodeConfig
from mixnet.packet import Fragment, MessageFlag, MessageReconstructor, PacketBuilder
NetworkPacketQueue: TypeAlias = asyncio.Queue[bytes]
@ -32,7 +32,7 @@ class Node:
self.config = config
self.global_config = global_config
self.mixgossip_channel = MixGossipChannel(
config.peering_degree, self.__process_sphinx_packet
config.gossip, self.__process_sphinx_packet
)
self.reconstructor = MessageReconstructor()
self.broadcast_channel = asyncio.Queue()
@ -97,17 +97,17 @@ class Node:
class MixGossipChannel:
peering_degree: int
config: GossipConfig
conns: list[DuplexConnection]
handler: Callable[[SphinxPacket], Awaitable[SphinxPacket | None]]
msg_cache: set[bytes]
def __init__(
self,
peering_degree: int,
config: GossipConfig,
handler: Callable[[SphinxPacket], Awaitable[SphinxPacket | None]],
):
self.peering_degree = peering_degree
self.config = config
self.conns = []
self.handler = handler
self.msg_cache = set()
@ -116,7 +116,7 @@ class MixGossipChannel:
self.tasks = set()
def add_conn(self, conn: DuplexConnection):
if len(self.conns) >= self.peering_degree:
if len(self.conns) >= self.config.peering_degree:
# For simplicity of the spec, reject the connection if the peering degree is reached.
raise ValueError("The peering degree is reached.")

View File

@ -2,6 +2,7 @@ from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from mixnet.config import (
GlobalConfig,
GossipConfig,
MixMembership,
NodeConfig,
NodeInfo,
@ -12,10 +13,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
gossip_config = GossipConfig(peering_degree=6)
node_configs = [
NodeConfig(X25519PrivateKey.generate(), peering_degree, max_mix_path_length)
NodeConfig(X25519PrivateKey.generate(), max_mix_path_length, gossip_config)
for _ in range(num_nodes)
]
global_config = GlobalConfig(