From d8a3f3b56481275087cdff95d98e7b2462cfcefe Mon Sep 17 00:00:00 2001 From: Vecna Date: Tue, 28 May 2024 16:00:00 -0400 Subject: [PATCH] Initialize simulation with some number of trusted users --- src/bin/simulation.rs | 70 ++++++++++++++++++++++++++++++++++++++++-- src/simulation/user.rs | 19 +++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/bin/simulation.rs b/src/bin/simulation.rs index a201e5d..10c8986 100644 --- a/src/bin/simulation.rs +++ b/src/bin/simulation.rs @@ -5,7 +5,7 @@ use troll_patrol::{ extra_info::ExtraInfo, - increment_simulated_date, + get_date, increment_simulated_date, set_simulated_date, simulation::{ bridge::Bridge, censor::{self, Censor}, @@ -17,6 +17,7 @@ use troll_patrol::{ use clap::Parser; use lox_cli::{networking::*, *}; +use lox_library::proto::{level_up::LEVEL_INTERVAL, trust_promotion::UNTRUSTED_INTERVAL}; use rand::Rng; use serde::Deserialize; use std::{ @@ -52,6 +53,8 @@ pub struct Config { pub max_new_users_per_day: u32, // How many days to simulate pub num_days: u32, + // We start with this many level 4 users + pub num_initial_trusted_users: u32, pub prob_connection_fails: f64, pub prob_user_invites_friend: f64, pub prob_user_is_censor: f64, @@ -109,9 +112,72 @@ pub async fn main() { // Set up bridges (no bridges yet) let mut bridges = HashMap::<[u8; 20], Bridge>::new(); - // Set up users (no users yet) + // Set up users let mut users = Vec::::new(); + if config.num_initial_trusted_users > 0 { + // Add some number of trusted users initially + for _ in 0..config.num_initial_trusted_users { + users.push(User::trusted_user(&sconfig).await); + } + + // Level trusted users up to level 4 + + // Advance LA's time + la_net_test + .request( + "/advancedays".to_string(), + serde_json::to_string(&(UNTRUSTED_INTERVAL as u16)) + .unwrap() + .into(), + ) + .await; + // Advance simulated time + set_simulated_date(get_date() + UNTRUSTED_INTERVAL); + + for user in &mut users { + user.primary_cred = trust_migration( + &sconfig.la_net, + &user.primary_cred, + &trust_promotion( + &sconfig.la_net, + &user.primary_cred, + get_lox_pub(&sconfig.la_pubkeys), + ) + .await, + get_lox_pub(&sconfig.la_pubkeys), + get_migration_pub(&sconfig.la_pubkeys), + ) + .await; + } + + for i in 1..LEVEL_INTERVAL.len() - 2 { + // Advance LA's time to tomorrow + la_net_test + .request( + "/advancedays".to_string(), + serde_json::to_string(&(LEVEL_INTERVAL[i] as u16)) + .unwrap() + .into(), + ) + .await; + // Advance simulated time to tomorrow + set_simulated_date(get_date() + LEVEL_INTERVAL[i]); + + for user in &mut users { + let reachcred = get_bucket(&sconfig.la_net, &user.primary_cred).await.1; + user.primary_cred = level_up( + &sconfig.la_net, + &user.primary_cred, + &reachcred.unwrap(), + get_lox_pub(&sconfig.la_pubkeys), + get_reachability_pub(&sconfig.la_pubkeys), + ) + .await; + } + } + } + // Set up extra-infos server spawn(async move { extra_infos_server::server().await; diff --git a/src/simulation/user.rs b/src/simulation/user.rs index 556f6ef..1061d2c 100644 --- a/src/simulation/user.rs +++ b/src/simulation/user.rs @@ -33,7 +33,7 @@ pub struct User { // The user always has a primary credential. If this credential's bucket is // blocked, the user may replace it or temporarily hold two credentials // while waiting to migrate from the primary credential. - primary_cred: Lox, + pub primary_cred: Lox, secondary_cred: Option, // Does the user submit reports to Troll Patrol? @@ -77,6 +77,23 @@ impl User { } } + pub async fn trusted_user(config: &Config) -> Self { + let cred = get_lox_credential( + &config.la_net, + &get_open_invitation(&config.la_net).await, + get_lox_pub(&config.la_pubkeys), + ) + .await + .0; + Self { + is_censor: false, + primary_cred: cred, + secondary_cred: None, + submits_reports: true, + prob_use_bridges: 1.0, + } + } + // TODO: This should probably return an actual error type pub async fn invite(&mut self, config: &Config, censor: &mut Censor) -> Result { let etable = get_reachability_credential(&config.la_net).await;