add weights for real_message_prob

This commit is contained in:
Youngjoon Lee 2024-05-15 18:55:17 +09:00
parent 661f40680c
commit 91284196c1
No known key found for this signature in database
GPG Key ID: 09B750B5BD6F08A2
3 changed files with 29 additions and 8 deletions

View File

@ -10,9 +10,17 @@ class Config:
running_time: int
num_nodes: int
num_mix_layers: int
# An interval of sending a new real/cover message
# A probability of actually sending a message depends on the following parameters.
message_interval: int
real_message_prob: float # TODO: should be proportional to its stake
# A probability of sending a real message within one cycle
real_message_prob: float
# A weight of real message emission probability of some nodes
# The length of the list should be <= num_nodes.
real_message_prob_weights: list[float]
# A probability of sending a cover message within one cycle if not sending a real message
cover_message_prob: float
# A maximum preparation time (delay) before sending the message
max_message_prep_time: float
@classmethod
@ -26,9 +34,11 @@ class Config:
assert config.num_nodes > 0
assert 0 < config.num_mix_layers <= config.num_nodes
assert config.message_interval > 0
assert config.real_message_prob >= 0
assert config.real_message_prob > 0
assert len(config.real_message_prob_weights) <= config.num_nodes
for weight in config.real_message_prob_weights:
assert weight >= 1
assert config.cover_message_prob >= 0
assert config.real_message_prob + config.cover_message_prob <= 1
assert config.max_message_prep_time >= 0
return config

View File

@ -1,9 +1,15 @@
running_time: 30
num_nodes: 100
num_mix_layers: 3
# An interval of sending a new real/cover message
# A probability of actually sending a message depends on the following parameters.
message_interval: 1
# (real_message_prob + cover_message_prob) should be <= 1
# If the sum is < 1, then the remaining probability is for sending nothing.
real_message_prob: 0.01
# A probability of sending a real message within a cycle
real_message_prob: 0.1
# A weight of real message emission probability of some nodes
# The length of the list should be <= num_nodes.
real_message_prob_weights: [10, 8, 12]
# A probability of sending a cover message within a cycle if not sending a real message
cover_message_prob: 0.2
# A maximum preparation time (delay) before sending the message
max_message_prep_time: 0.3

View File

@ -44,13 +44,18 @@ class Node:
def payload_to_send(self) -> bytes | None:
rnd = random.random()
if rnd < self.config.real_message_prob:
if rnd < self.real_message_prob():
return self.REAL_PAYLOAD
elif rnd < self.config.real_message_prob + self.config.cover_message_prob:
elif rnd < self.config.cover_message_prob:
return self.COVER_PAYLOAD
else:
return None
def real_message_prob(self):
weight = self.config.real_message_prob_weights[self.id] \
if self.id < len(self.config.real_message_prob_weights) else 0
return self.config.real_message_prob * weight
def create_message(self, payload: bytes) -> SphinxPacket:
"""
Creates a message using the Sphinx format