diff --git a/crates/lox-distributor/src/lox_context.rs b/crates/lox-distributor/src/lox_context.rs index 744fd60..e8f674b 100644 --- a/crates/lox-distributor/src/lox_context.rs +++ b/crates/lox-distributor/src/lox_context.rs @@ -4,7 +4,7 @@ use curve25519_dalek::{ristretto::RistrettoBasepointTable, Scalar}; use hyper::{body::Bytes, header::HeaderValue, Body, Response}; use lox_library::{ - bridge_table::{BridgeLine, EncryptedBucket, MAX_BRIDGES_PER_BUCKET}, + bridge_table::{self, BridgeLine, EncryptedBucket, MAX_BRIDGES_PER_BUCKET}, proto::{ blockage_migration, check_blockage, issue_invite, level_up, migration, open_invite, positive_report, redeem_invite, trust_promotion, @@ -16,9 +16,10 @@ use serde::{Deserialize, Serialize}; use lox_zkp::ProofError; use serde_json::json; +use sha1::{Digest, Sha1}; use std::{ cmp::Ordering, - collections::{BTreeMap, HashMap}, + collections::{BTreeMap, HashMap, HashSet}, ops::DerefMut, sync::{Arc, Mutex}, }; @@ -342,6 +343,52 @@ impl LoxServerContext { ba_obj.bridge_update(&bridgeline) } + pub fn generate_bridge_verification_infos(&self) { + let la_obj = self.ba.lock().unwrap(); + + // Recompute table + let mut tp_bridge_infos = self.tp_bridge_infos.lock().unwrap(); + tp_bridge_infos.clear(); + + // Go through all buckets and all bridges in buckets, map bridge to + // buckets containing it. Note that a bridge may be contained within + // multiple buckets (open invitation buckets and invite-only buckets). + let buckets = &la_obj.bridge_table.buckets; + for id in buckets.keys() { + let bridges = buckets.get(id).unwrap(); + let key = la_obj.bridge_table.keys.get(id).unwrap(); + let bucket = bridge_table::to_scalar(*id, key); + for bridge in bridges { + if bridge.fingerprint != [0; 20] { + // Get hashed fingerprint + let mut hasher = Sha1::new(); + hasher.update(&bridge.fingerprint); + let fingerprint: [u8; 20] = hasher.finalize().into(); + + // Add bucket to existing entry or add new entry + if tp_bridge_infos.contains_key(&fingerprint) { + tp_bridge_infos + .get_mut(&fingerprint) + .unwrap() + .buckets + .insert(bucket); + } else { + let mut buckets = HashSet::::new(); + buckets.insert(bucket); + tp_bridge_infos.insert( + fingerprint, + BridgeVerificationInfo { + bridge_line: *bridge, + buckets: buckets, + pubkey: None, // TODO: add pubkey for signed bridge tokens + }, + ); + } + } + } + } + } + //#[cfg(test)] /// For testing only: manually advance the day by the given number /// of days. diff --git a/crates/lox-distributor/src/troll_patrol_handler.rs b/crates/lox-distributor/src/troll_patrol_handler.rs index 85ae577..9dd5fab 100644 --- a/crates/lox-distributor/src/troll_patrol_handler.rs +++ b/crates/lox-distributor/src/troll_patrol_handler.rs @@ -57,10 +57,7 @@ mod tests { }; use troll_patrol::{ bridge_verification_info::BridgeVerificationInfo, - negative_report::{ - HashOfBridgeLine, HashOfBucket, NegativeReport, ProofOfBridgeKnowledge, - SerializableNegativeReport, - }, + negative_report::{HashOfBridgeLine, HashOfBucket, NegativeReport, ProofOfBridgeKnowledge}, positive_report::{PositiveReport, SerializablePositiveReport}, BridgeDistributor, }; @@ -129,57 +126,6 @@ mod tests { }; Self { context } } - - pub fn generate_bridge_infos(&self) { - // We want to ignore empty bridgelines - let mut hasher = Sha1::new(); - hasher.update([0; 20]); - let empty_bridgeline_fingerprint: [u8; 20] = hasher.finalize().into(); - - let mut lox_auth = self.context.ba.lock().unwrap(); - - // Recompute table - let mut tp_bridge_infos = self.context.tp_bridge_infos.lock().unwrap(); - tp_bridge_infos.clear(); - - // Go through all buckets and all bridges in buckets, map bridge to - // buckets containing it. Note that a bridge may be contained within - // multiple buckets (open invitaion buckets and invite-only buckets). - let buckets = &lox_auth.bridge_table.buckets; - for id in buckets.keys() { - let bridges = buckets.get(id).unwrap(); - let key = lox_auth.bridge_table.keys.get(id).unwrap(); - let bucket = bridge_table::to_scalar(*id, key); - for bridge in bridges { - // Get hashed fingerprint - let mut hasher = Sha1::new(); - hasher.update(&bridge.fingerprint); - let fingerprint: [u8; 20] = hasher.finalize().into(); - - if fingerprint != empty_bridgeline_fingerprint { - // Add new entry or add bucket to existing entry - if tp_bridge_infos.contains_key(&fingerprint) { - tp_bridge_infos - .get_mut(&fingerprint) - .unwrap() - .buckets - .insert(bucket); - } else { - let mut buckets = HashSet::::new(); - buckets.insert(bucket); - tp_bridge_infos.insert( - fingerprint, - BridgeVerificationInfo { - bridge_line: *bridge, - buckets: buckets, - pubkey: None, // TODO: add pubkey for signed bridge tokens - }, - ); - } - } - } - } - } } pub fn random() -> BridgeLine { @@ -280,7 +226,6 @@ mod tests { #[tokio::test] async fn test_negative_reports() { let mut th = TestHarness::new(); - th.generate_bridge_infos(); let tpc = TpClientMock {}; let mut Htables = HashMap::::new(); @@ -288,7 +233,7 @@ mod tests { let cred = get_new_credential(&mut th).await; let cred = level_up(&mut th, &cred).await; - th.generate_bridge_infos(); + th.context.generate_bridge_verification_infos(); let mut ba = th.context.ba.lock().unwrap(); @@ -390,7 +335,6 @@ mod tests { #[tokio::test] async fn test_positive_reports() { let mut th = TestHarness::new(); - th.generate_bridge_infos(); let tpc = TpClientMock {}; let mut Htables = HashMap::::new(); @@ -400,7 +344,7 @@ mod tests { let cred = level_up(&mut th, &cred).await; let cred = level_up(&mut th, &cred).await; - th.generate_bridge_infos(); + th.context.generate_bridge_verification_infos(); let mut ba = th.context.ba.lock().unwrap();