Merge pull request #119 from nymtech/feature/config_files_cleanup

Feature/config files cleanup
This commit is contained in:
Dave Hrycyszyn
2020-02-19 16:22:59 +00:00
committed by GitHub
22 changed files with 50 additions and 70 deletions
@@ -29,11 +29,10 @@ impl NymTopology for Topology {
};
let directory = Client::new(directory_config);
let topology = directory
directory
.presence_topology
.get()
.expect("Failed to retrieve network topology.");
topology
.expect("Failed to retrieve network topology.")
}
fn new_from_nodes(
+5 -6
View File
@@ -43,7 +43,7 @@ pub fn loop_cover_message<T: NymTopology>(
our_address: DestinationAddressBytes,
surb_id: SURBIdentifier,
topology: &T,
average_delay_duration: time::Duration,
average_delay: time::Duration,
) -> Result<(SocketAddr, SphinxPacket), SphinxPacketEncapsulationError> {
let destination = Destination::new(our_address, surb_id);
@@ -51,7 +51,7 @@ pub fn loop_cover_message<T: NymTopology>(
destination,
LOOP_COVER_MESSAGE_PAYLOAD.to_vec(),
topology,
average_delay_duration,
average_delay,
)
}
@@ -59,10 +59,10 @@ pub fn encapsulate_message<T: NymTopology>(
recipient: Destination,
message: Vec<u8>,
topology: &T,
average_delay_duration: time::Duration,
average_delay: time::Duration,
) -> Result<(SocketAddr, SphinxPacket), SphinxPacketEncapsulationError> {
let mut providers = topology.providers();
if providers.len() == 0 {
if providers.is_empty() {
return Err(SphinxPacketEncapsulationError::NoValidProvidersError);
}
// unwrap is fine here as we asserted there is at least single provider
@@ -70,8 +70,7 @@ pub fn encapsulate_message<T: NymTopology>(
let route = topology.route_to(provider)?;
let delays =
sphinx::header::delays::generate_from_average_duration(route.len(), average_delay_duration);
let delays = sphinx::header::delays::generate_from_average_duration(route.len(), average_delay);
// build the packet
let packet = sphinx::SphinxPacket::new(message, &route[..], &recipient, &delays)?;
+1 -1
View File
@@ -1,7 +1,7 @@
use rand_distr::{Distribution, Exp};
use std::time;
pub fn sample_from_duration(average_duration: time::Duration) -> time::Duration {
pub fn sample(average_duration: time::Duration) -> time::Duration {
// this is our internal code used by our traffic streams
// the error is only thrown if average delay is less than 0, which will never happen
// so call to unwrap is perfectly safe here
+6 -6
View File
@@ -46,19 +46,19 @@ pub trait NymConfig: Default + Serialize + DeserializeOwned {
}?;
fs::write(
custom_location.unwrap_or(self.config_directory().join(Self::config_file_name())),
custom_location
.unwrap_or_else(|| self.config_directory().join(Self::config_file_name())),
templated_config,
)
}
fn load_from_file(custom_location: Option<PathBuf>, id: Option<&str>) -> io::Result<Self> {
let config_contents = fs::read_to_string(
custom_location.unwrap_or(Self::default_config_directory(id).join("config.toml")),
custom_location
.unwrap_or_else(|| Self::default_config_directory(id).join("config.toml")),
)?;
let parsing_result = toml::from_str(&config_contents)
.map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err));
parsing_result
toml::from_str(&config_contents)
.map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err))
}
}
+6
View File
@@ -39,6 +39,12 @@ impl KeyPair {
}
}
impl Default for KeyPair {
fn default() -> Self {
KeyPair::new()
}
}
impl PemStorableKeyPair for KeyPair {
type PrivatePemKey = PrivateKey;
type PublicPemKey = PublicKey;
+6
View File
@@ -36,6 +36,12 @@ impl MixIdentityKeyPair {
}
}
impl Default for MixIdentityKeyPair {
fn default() -> Self {
MixIdentityKeyPair::new()
}
}
impl PemStorableKeyPair for MixIdentityKeyPair {
type PrivatePemKey = MixIdentityPrivateKey;
type PublicPemKey = MixIdentityPublicKey;
+4 -4
View File
@@ -73,7 +73,7 @@ impl PathChecker {
// iteration is used to distinguish packets sent through the same path (as the healthcheck
// may try to send say 10 packets through given path)
fn unique_path_key(path: &Vec<SphinxNode>, check_id: [u8; 16], iteration: u8) -> Vec<u8> {
fn unique_path_key(path: &[SphinxNode], check_id: [u8; 16], iteration: u8) -> Vec<u8> {
check_id
.iter()
.cloned()
@@ -173,8 +173,8 @@ impl PathChecker {
self.update_path_statuses(provider_messages);
}
pub(crate) async fn send_test_packet(&mut self, path: &Vec<SphinxNode>, iteration: u8) {
if path.len() == 0 {
pub(crate) async fn send_test_packet(&mut self, path: &[SphinxNode], iteration: u8) {
if path.is_empty() {
warn!("trying to send test packet through an empty path!");
return;
}
@@ -221,7 +221,7 @@ impl PathChecker {
let first_node_client = self
.layer_one_clients
.entry(first_node_key)
.or_insert(Some(mix_client::MixClient::new()));
.or_insert_with(|| Some(mix_client::MixClient::new()));
if first_node_client.is_none() {
debug!("we can ignore this path as layer one mix is inaccessible");
+3 -7
View File
@@ -16,7 +16,7 @@ impl std::fmt::Display for HealthCheckResult {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "NETWORK HEALTH\n==============\n")?;
for score in self.0.iter() {
write!(f, "{}\n", score)?
writeln!(f, "{}", score)?
}
Ok(())
}
@@ -34,12 +34,8 @@ impl HealthCheckResult {
let health = mixes
.into_iter()
.map(|node| NodeScore::from_mixnode(node))
.chain(
providers
.into_iter()
.map(|node| NodeScore::from_provider(node)),
)
.map(NodeScore::from_mixnode)
.chain(providers.into_iter().map(NodeScore::from_provider))
.collect();
HealthCheckResult(health)
+1 -1
View File
@@ -110,7 +110,7 @@ impl NodeScore {
pub_key: NodeAddressBytes::from_base58_string(node.pub_key),
addresses: vec![node.mixnet_listener, node.client_listener],
version: node.version,
layer: format!("provider"),
layer: "provider".to_string(),
packets_sent: 0,
packets_received: 0,
}
+4 -7
View File
@@ -30,7 +30,7 @@ pub trait NymTopology: Sized + std::fmt::Debug + Send + Sync {
}
highest_layer = max(highest_layer, mix.layer);
let layer_nodes = layered_topology.entry(mix.layer).or_insert(Vec::new());
let layer_nodes = layered_topology.entry(mix.layer).or_insert_with(Vec::new);
layer_nodes.push(mix);
}
@@ -40,12 +40,12 @@ pub trait NymTopology: Sized + std::fmt::Debug + Send + Sync {
if !layered_topology.contains_key(&layer) {
missing_layers.push(layer);
}
if layered_topology[&layer].len() == 0 {
if layered_topology[&layer].is_empty() {
missing_layers.push(layer);
}
}
if missing_layers.len() > 0 {
if !missing_layers.is_empty() {
return Err(NymTopologyError::MissingLayerError(missing_layers));
}
@@ -112,10 +112,7 @@ pub trait NymTopology: Sized + std::fmt::Debug + Send + Sync {
}
fn can_construct_path_through(&self) -> bool {
match self.make_layered_topology() {
Ok(_) => true,
Err(_) => false,
}
self.make_layered_topology().is_ok()
}
}
-3
View File
@@ -64,10 +64,7 @@ pub fn execute(matches: &ArgMatches) {
config = override_config(config, matches);
// TODO: which one should be used?
let sphinx_keys = encryption::KeyPair::new();
// let alternative_keypair = MixIdentityKeyPair::new();
let pathfinder = MixNodePathfinder::new_from_config(&config);
let pem_store = PemStore::new(pathfinder);
pem_store
+2 -3
View File
@@ -34,13 +34,12 @@ impl MixPeer {
}
pub async fn send(&self, bytes: Vec<u8>) -> Result<(), Box<dyn Error>> {
let next_hop_address = self.connection.clone();
let mut stream = tokio::net::TcpStream::connect(next_hop_address).await?;
let mut stream = tokio::net::TcpStream::connect(self.connection).await?;
stream.write_all(&bytes).await?;
Ok(())
}
pub fn to_string(&self) -> String {
pub fn stringify(&self) -> String {
self.connection.to_string()
}
}
+1 -1
View File
@@ -135,7 +135,7 @@ impl PacketProcessor {
if forwarding_data
.sent_metrics_tx
.send(forwarding_data.recipient.to_string())
.send(forwarding_data.recipient.stringify())
.await
.is_err()
{
@@ -16,8 +16,7 @@ pub(crate) async fn start_loop_cover_traffic_stream<T: NymTopology>(
info!("Starting loop cover traffic stream");
loop {
trace!("next cover message!");
let delay_duration =
mix_client::poisson::sample_from_duration(average_cover_message_delay_duration);
let delay_duration = mix_client::poisson::sample(average_cover_message_delay_duration);
tokio::time::delay_for(delay_duration).await;
let read_lock = topology_ctrl_ref.read().await;
+2 -3
View File
@@ -38,8 +38,7 @@ impl<T: NymTopology> Stream for OutQueueControl<T> {
// we know it's time to send a message, so let's prepare delay for the next one
// Get the `now` by looking at the current `delay` deadline
let now = self.next_delay.deadline();
let next_poisson_delay =
mix_client::poisson::sample_from_duration(self.average_message_sending_delay);
let next_poisson_delay = mix_client::poisson::sample(self.average_message_sending_delay);
// The next interval value is `next_poisson_delay` after the one that just
// yielded.
@@ -83,7 +82,7 @@ impl<T: NymTopology> OutQueueControl<T> {
pub(crate) async fn run_out_queue_control(mut self) {
// we should set initial delay only when we actually start the stream
self.next_delay = time::delay_for(mix_client::poisson::sample_from_duration(
self.next_delay = time::delay_for(mix_client::poisson::sample(
self.average_message_sending_delay,
));
-5
View File
@@ -318,12 +318,10 @@ pub struct Debug {
/// The parameter of Poisson distribution determining how long, on average,
/// it is going to take for another loop cover traffic message to be sent.
/// If set to a negative value, the loop cover traffic stream will be disabled.
/// The provided value is interpreted as milliseconds.
loop_cover_traffic_average_delay: u64,
/// The uniform delay every which clients are querying the providers for received packets.
/// If set to a negative value, client will never try to fetch their messages.
/// The provided value is interpreted as milliseconds.
fetch_message_delay: u64,
@@ -331,7 +329,6 @@ pub struct Debug {
/// it is going to take another 'real traffic stream' message to be sent.
/// If no real packets are available and cover traffic is enabled,
/// a loop cover message is sent instead in order to preserve the rate.
/// If set to a negative value, client will never try to send real traffic data.
/// The provided value is interpreted as milliseconds.
message_sending_average_delay: u64,
@@ -343,8 +340,6 @@ pub struct Debug {
/// The uniform delay every which clients are querying the directory server
/// to try to obtain a compatible network topology to send sphinx packets through.
/// If set to a negative value, client will never try to refresh its topology,
/// meaning it will always try to use whatever it obtained on startup.
/// The provided value is interpreted as milliseconds.
topology_refresh_rate: u64,
-5
View File
@@ -71,12 +71,10 @@ average_packet_delay = {{ debug.average_packet_delay }}
# The parameter of Poisson distribution determining how long, on average,
# it is going to take for another loop cover traffic message to be sent.
# If set to a negative value, the loop cover traffic stream will be disabled.
# The provided value is interpreted as milliseconds.
loop_cover_traffic_average_delay = {{ debug.loop_cover_traffic_average_delay }}
# The uniform delay every which clients are querying the providers for received packets.
# If set to a negative value, client will never try to fetch their messages.
# The provided value is interpreted as milliseconds.
fetch_message_delay = {{ debug.fetch_message_delay }}
@@ -84,7 +82,6 @@ fetch_message_delay = {{ debug.fetch_message_delay }}
# it is going to take another 'real traffic stream' message to be sent.
# If no real packets are available and cover traffic is enabled,
# a loop cover message is sent instead in order to preserve the rate.
# If set to a negative value, client will never try to send real traffic data.
# The provided value is interpreted as milliseconds.
message_sending_average_delay = {{ debug.message_sending_average_delay }}
@@ -96,8 +93,6 @@ rate_compliant_cover_messages_disabled = {{ debug.rate_compliant_cover_messages_
# The uniform delay every which clients are querying the directory server
# to try to obtain a compatible network topology to send sphinx packets through.
# If set to a negative value, client will never try to refresh its topology,
# meaning it will always try to use whatever it obtained on startup.
# The provided value is interpreted as milliseconds.
topology_refresh_rate = {{ debug.topology_refresh_rate }}
+2 -2
View File
@@ -125,7 +125,7 @@ impl ClientRequest {
Err(e) => {
warn!("Failed to fetch client messages - {:?}", e);
return ServerResponse::Error {
message: format!("Server failed to receive messages").to_string(),
message: "Server failed to receive messages".to_string(),
};
}
};
@@ -275,7 +275,7 @@ async fn accept_connection<T: 'static + NymTopology>(
topology: topology.clone(),
msg_input: msg_input.clone(),
msg_query: msg_query.clone(),
self_address: self_address.clone(),
self_address,
};
match handle_connection(&buf[..n], request_handling_data).await {
Ok(res) => res,
+2 -5
View File
@@ -239,7 +239,7 @@ impl ClientRequest {
Err(e) => {
warn!("Failed to fetch client messages - {:?}", e);
return ServerResponse::Error {
message: format!("Server failed to receive messages").to_string(),
message: "Server failed to receive messages".to_string(),
};
}
};
@@ -353,10 +353,7 @@ async fn accept_connection<T: 'static + NymTopology>(
}
};
let mut should_close = false;
if message.is_close() {
should_close = true;
}
let should_close = message.is_close();
if let Err(err) = msg_tx.unbounded_send(message) {
error!(
@@ -72,7 +72,7 @@ impl ProviderResponse for PullResponse {
return Err(ProviderResponseError::UnmarshalErrorInvalidLength);
}
let mut bytes_copy = bytes.clone();
let mut bytes_copy = bytes;
let num_msgs = read_be_u16(&mut bytes_copy);
// can we read all lengths of messages?
-3
View File
@@ -93,10 +93,7 @@ pub fn execute(matches: &ArgMatches) {
config = override_config(config, matches);
// TODO: which one should be used?
let sphinx_keys = encryption::KeyPair::new();
// let alternative_keypair = MixIdentityKeyPair::new();
let pathfinder = ProviderPathfinder::new_from_config(&config);
let pem_store = PemStore::new(pathfinder);
pem_store
+1 -2
View File
@@ -63,7 +63,7 @@ impl ClientLedger {
}
fn has_token(&self, auth_token: &AuthToken) -> bool {
return self.0.contains_key(auth_token);
self.0.contains_key(auth_token)
}
fn insert_token(
@@ -155,7 +155,6 @@ impl ServiceProvider {
)
.unwrap_or_else(|e| {
error!("failed to store processed sphinx message; err = {:?}", e);
return;
});
}
Err(e) => {