add p2p type config

This commit is contained in:
Youngjoon Lee 2024-05-23 17:11:23 +09:00
parent 063efcf5c3
commit 6e66eb7233
No known key found for this signature in database
GPG Key ID: 09B750B5BD6F08A2
3 changed files with 23 additions and 4 deletions

View File

@ -10,7 +10,7 @@ import yaml
class Config:
simulation: SimulationConfig
mixnet: MixnetConfig
p2p: P2pConfig
p2p: P2PConfig
measurement: MeasurementConfig
adversary: AdversaryConfig
@ -95,15 +95,22 @@ class MixnetConfig:
@dataclass
class P2pConfig:
class P2PConfig:
# Broadcasting type: naive | gossip
type: str
# A maximum network latency between nodes directly connected with each other
max_network_latency: float
TYPE_NAIVE = "naive"
TYPE_GOSSIP = "gossip"
def validate(self):
assert self.type in [self.TYPE_NAIVE, self.TYPE_GOSSIP]
assert self.max_network_latency >= 0
def description(self):
return (
f"p2p_type: {self.type}\n"
f"max_net_latency: {self.max_network_latency:.2f}"
)

View File

@ -26,6 +26,8 @@ mixnet:
max_mix_delay: 3
p2p:
# Broadcasting type: naive | gossip
type: "naive"
# A maximum network latency between nodes directly connected with each other
max_network_latency: 0.5

View File

@ -2,9 +2,9 @@ import random
import simpy
from config import Config
from config import Config, P2PConfig
from node import Node
from p2p import NaiveBroadcastP2P
from p2p import NaiveBroadcastP2P, GossipP2P
class Simulation:
@ -18,3 +18,13 @@ class Simulation:
def run(self):
self.env.run(until=self.config.simulation.running_time)
@classmethod
def init_p2p(cls, env: simpy.Environment, config: Config):
match config.p2p.type:
case P2PConfig.TYPE_NAIVE:
return NaiveBroadcastP2P(env, config)
case P2PConfig.TYPE_GOSSIP:
return GossipP2P(env, config)
case _:
raise ValueError("Unknown P2P type")