From 558539332ffe748349610ebe9e2bc9c33eb0cea2 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 1 Feb 2021 18:33:43 -0800 Subject: [PATCH] Initial cut of integrating with tracing --- Cargo.toml | 4 +- src/main.rs | 108 ++++++++++++++++++++++++++++++------------------- src/parties.rs | 17 +++++++- src/server.rs | 6 +++ 4 files changed, 90 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b0a7a24..9ede177 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ rand = "0.8.3" rand_distr = "0.4.0" hashbrown = "0.9.1" termcolor = "1.1.2" -clap = "3.0.0-beta.2" \ No newline at end of file +clap = "3.0.0-beta.2" +tracing = "0.1.0" +tracing-subscriber = "0.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 23d4ab7..14618e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,12 @@ use std::io::Write; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; mod parties; mod server; - use clap::Clap; +use tracing::event; + +use tracing::Level; +use tracing_subscriber; +use tracing_subscriber::FmtSubscriber; #[derive(Clap)] #[clap(version = "1.0", author = "Sarah Jamie Lewis ")] @@ -29,51 +33,71 @@ struct Opts { /// maximum false positive rate #[clap(short, long, default_value = "8")] max_p: usize, + + #[clap(short, long)] + trace: bool, } fn main() { let opts: Opts = Opts::parse(); - let mut rng = rand::thread_rng(); - let mut server = SimulatedServer::new(); + let level = match opts.trace { + true => Level::TRACE, + _ => Level::INFO, + }; + let subscriber = FmtSubscriber::builder().with_max_level(level).finish(); + tracing::subscriber::with_default(subscriber, || { + let mut rng = rand::thread_rng(); + let mut server = SimulatedServer::new(); - println!("Generating {} Parties...", opts.num_parties); - let simulated_parties = SimulatedParties::new_simulation(opts.num_parties, opts.gamma); - simulated_parties.register_with_server(&mut server, &mut rng, opts.min_p, opts.max_p); - - let pareto = Pareto::new(1.0, 1.0).unwrap(); - - println!("Simulating message sends using {} samples from a pareto distribution...", opts.samples_per_round); - (0..opts.samples_per_round).for_each(|_i| simulated_parties.sample_traffic(&mut server, &mut rng, pareto)); - - println!("Simulating Adversarial Server Processing Messages.."); - server.test_messages(); - - let (round_stats, party_stats) = server.statistics(); - - let if_uniform = (round_stats.num_messages as f64) / (round_stats.num_registered_parties as f64); - println!( - "Round had {} messages, send to {} parties ({:.2} is uniform distribution)", - round_stats.num_messages, round_stats.num_registered_parties, if_uniform - ); - let mut stdout = StandardStream::stdout(ColorChoice::Always); - for (party, stats) in party_stats.iter() { - if stats.trivial_breaks > 0 || (stats.observed_messages > 2 && stats.observed_skew > (if_uniform * stats.ideal_rate)) { - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); - } else { - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green))).unwrap(); + let simulated_parties = SimulatedParties::new_simulation(opts.num_parties, opts.gamma); + { + event!(Level::INFO, "Generating {} Parties and registering them with the server", opts.num_parties); + simulated_parties.register_with_server(&mut server, &mut rng, opts.min_p, opts.max_p); } - writeln!( - &mut stdout, - "Party {} | Ideal: {:>8.2} {:>8.2} | Observed: {:>8.2} ({:>6.2}) | Skew: {:>8.2} ({:>6.2}) | Trivial Attributions this Round: {}", - party, - stats.ideal_rate, - stats.ideal_rate * (round_stats.num_messages as f64), - stats.observed_messages, - 100.0 * stats.observed_rate, - stats.observed_skew_messages, - stats.observed_skew, - stats.trivial_breaks - ) - .unwrap(); - } + + let pareto = Pareto::new(1.0, 1.0).unwrap(); + + { + event!(Level::INFO, "Simulating message sends using {} samples from a pareto distribution...", opts.samples_per_round); + (0..opts.samples_per_round).for_each(|_i| simulated_parties.sample_traffic(&mut server, &mut rng, pareto)); + } + + { + event!(Level::INFO, "Simulating Adversarial Server Processing Messages.."); + server.test_messages(); + } + + { + let (round_stats, party_stats) = server.statistics(); + let if_uniform = (round_stats.num_messages as f64) / (round_stats.num_registered_parties as f64); + event!( + Level::INFO, + "Round had {} messages, send to {} parties ({:.2} is uniform distribution)", + round_stats.num_messages, + round_stats.num_registered_parties, + if_uniform + ); + let mut stdout = StandardStream::stdout(ColorChoice::Always); + for (party, stats) in party_stats.iter() { + if stats.trivial_breaks > 0 || (stats.observed_messages > 2 && stats.observed_skew > (if_uniform * stats.ideal_rate)) { + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red))).unwrap(); + } else { + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green))).unwrap(); + } + writeln!( + &mut stdout, + "Party {} | Ideal: {:>8.2} {:>8.2} | Observed: {:>8.2} ({:>6.2}) | Skew: {:>8.2} ({:>6.2}) | Trivial Attributions this Round: {}", + party, + stats.ideal_rate, + stats.ideal_rate * (round_stats.num_messages as f64), + stats.observed_messages, + 100.0 * stats.observed_rate, + stats.observed_skew_messages, + stats.observed_skew, + stats.trivial_breaks + ) + .unwrap(); + } + } + }); } diff --git a/src/parties.rs b/src/parties.rs index c1ec805..fe0f401 100644 --- a/src/parties.rs +++ b/src/parties.rs @@ -3,6 +3,9 @@ use fuzzytags::FuzzySecretKey; use rand::distributions::Distribution; use rand::Rng; use rand_distr::num_traits::ToPrimitive; +use tracing::event; +use tracing::span; +use tracing::Level; pub struct SimulatedParties { parties: Vec, @@ -22,9 +25,15 @@ impl SimulatedParties { where R: Rng, { + let span = span!(Level::TRACE, "register_with_server"); + let _enter = span.enter(); for party in self.parties.iter() { let n = rng.gen_range(min_p..max_p); + let span = span!(Level::TRACE, "{register}", party = party.public_key().id().as_str()); + let _enter = span.enter(); let detection_key = party.extract(n); + event!(Level::TRACE, "create detection key {detection_key}", detection_key = detection_key.id().as_str()); + event!(Level::TRACE, "register with server"); server.register_key(&detection_key, &party.public_key()); } } @@ -34,14 +43,18 @@ impl SimulatedParties { D: Distribution, R: Rng, { + let span = span!(Level::TRACE, "sample_traffic"); + let _enter = span.enter(); let v = distribution.sample(rng).to_u16().unwrap(); let receiver = rng.gen_range(0..self.parties.len()); let receiver_public_key = self.parties.get(receiver).unwrap().public_key(); - println!("[Oracle] {} received {} messages", receiver_public_key.id(), v); + + let span = span!(Level::TRACE, "{party}", party = receiver_public_key.id().as_str()); + let _enter = span.enter(); for _i in 0..v { let tag = receiver_public_key.generate_tag(); + event!(Level::TRACE, "message sent to {tag}", tag = tag.to_string()); server.add_message(tag); - //message_oracle.push(receiver); } } } diff --git a/src/server.rs b/src/server.rs index fc86b8f..343975d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,8 @@ use fuzzytags::{FuzzyDetectionKey, FuzzyPublicKey, FuzzyTag}; use hashbrown::HashMap; +use tracing::event; +use tracing::span; +use tracing::Level; pub struct SimulatedServer { keybase: Vec<(FuzzyDetectionKey, FuzzyPublicKey)>, @@ -47,7 +50,10 @@ impl SimulatedServer { pub fn test_messages(&mut self) { for message in self.messages.iter() { for (detection_key, public_key) in self.keybase.iter() { + let span = span!(Level::TRACE, "{detection}", party = public_key.id().as_str()); + let _enter = span.enter(); if detection_key.test_tag(message) { + event!(Level::TRACE, "Matched detection key for {key} to tag {tag} ", key = public_key.id(), tag = message.to_string()); self.tags_to_keys_cache.get_mut(message.to_string().as_str()).unwrap().push((*public_key).clone()); self.keys_to_tags_cache.get_mut(public_key.id().as_str()).unwrap().push((*message).clone());