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

View File

@ -98,6 +98,8 @@ class MixnetConfig:
class P2PConfig:
# Broadcasting type: naive | gossip
type: str
# A connection density, only if the type is gossip
connection_density: int
# A maximum network latency between nodes directly connected with each other
max_network_latency: float
@ -106,11 +108,14 @@ class P2PConfig:
def validate(self):
assert self.type in [self.TYPE_NAIVE, self.TYPE_GOSSIP]
if self.type == self.TYPE_GOSSIP:
assert self.connection_density > 0
assert self.max_network_latency >= 0
def description(self):
return (
f"p2p_type: {self.type}\n"
f"conn_density: {self.connection_density}"
f"max_net_latency: {self.max_network_latency:.2f}"
)

View File

@ -27,7 +27,9 @@ mixnet:
p2p:
# Broadcasting type: naive | gossip
type: "naive"
type: "gossip"
# A connection density, only if the type is gossip
connection_density: 10
# A maximum network latency between nodes directly connected with each other
max_network_latency: 0.5

View File

@ -25,8 +25,8 @@ class P2P(ABC):
self.measurement = Measurement(env, config)
self.adversary = Adversary(env, config)
def add_node(self, nodes: list["Node"]):
self.nodes.extend(nodes)
def set_nodes(self, nodes: list["Node"]):
self.nodes = nodes
def get_nodes(self, n: int) -> list["Node"]:
return random.sample(self.nodes, n)
@ -57,6 +57,7 @@ class P2P(ABC):
class NaiveBroadcastP2P(P2P):
def __init__(self, env: simpy.Environment, config: Config):
super().__init__(env, config)
self.nodes = []
# This should accept only bytes in practice,
# but we accept SphinxPacket as well because we don't implement Sphinx deserialization.
@ -78,6 +79,24 @@ class GossipP2P(P2P):
self.topology = defaultdict(set)
self.message_cache = defaultdict(set)
def set_nodes(self, nodes: list["Node"]):
super().set_nodes(nodes)
for i, node in enumerate(nodes):
# Each node is chained with the right neighbor, so that no node is not orphaned.
# And then, each node is connected to a random subset of other nodes.
front, back = nodes[:i], nodes[i + 1:]
if len(back) > 0:
neighbor = back[0]
back = back[1:]
else:
neighbor = front[0]
front = front[1:]
others = front + back
n = min(self.config.p2p.connection_density - 1, len(others))
conns = set(random.sample(others, n))
conns.add(neighbor)
self.topology[node] = conns
def broadcast(self, sender: "Node", msg: SphinxPacket | bytes):
yield from super().broadcast(sender, msg)
self.log("Gossiping a msg: %d bytes" % len(msg))
@ -92,3 +111,5 @@ class GossipP2P(P2P):
if msg_hash not in self.message_cache[receiver]:
self.message_cache[receiver].add(msg_hash)
self.env.process(receiver.receive_message(msg))
# gossiping
self.env.process(self.broadcast(receiver, msg))

View File

@ -12,9 +12,9 @@ class Simulation:
random.seed()
self.config = config
self.env = simpy.Environment()
self.p2p = NaiveBroadcastP2P(self.env, config)
self.nodes = [Node(i, self.env, self.p2p, config) for i in range(config.mixnet.num_nodes)]
self.p2p.add_node(self.nodes)
self.p2p = Simulation.init_p2p(self.env, config)
nodes = [Node(i, self.env, self.p2p, config) for i in range(config.mixnet.num_nodes)]
self.p2p.set_nodes(nodes)
def run(self):
self.env.run(until=self.config.simulation.running_time)