Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
target
data
results/*
reputation_*
revenue_*
reputation*.csv
revenue*.csv
118 changes: 113 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ln-simln-jamming/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ name = "forward-builder"
path = "src/bin/forward_builder.rs"

[dependencies]
simln-lib = { git = "https://github.com/bitcoin-dev-project/sim-ln", rev = "66daf6918c4d2774bf700c430a4bf4417a1377ee" }
sim-cli = { git = "https://github.com/bitcoin-dev-project/sim-ln", rev = "66daf6918c4d2774bf700c430a4bf4417a1377ee" }
simln-lib = { git = "https://github.com/bitcoin-dev-project/sim-ln", rev = "cefd0c4f23478fd5a9d84b49f8ce304b2c74b4e0" }
sim-cli = { git = "https://github.com/bitcoin-dev-project/sim-ln", rev = "cefd0c4f23478fd5a9d84b49f8ce304b2c74b4e0" }
ln-resource-mgr = { path = "../ln-resource-mgr" }
bitcoin = { version = "0.30.1" }
async-trait = "0.1.73"
Expand Down
2 changes: 1 addition & 1 deletion ln-simln-jamming/src/attacks/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ mod tests {
network: &[NetworkParser],
) -> SinkAttack<MockReputationInterceptor, MockPeacetimeMonitor, MockJammer> {
SinkAttack::new(
Arc::new(SimulationClock::new(1).unwrap()),
Arc::new(SimulationClock::new(std::time::SystemTime::now())),
network,
target,
vec![attacker],
Expand Down
2 changes: 1 addition & 1 deletion ln-simln-jamming/src/attacks/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ mod tests {

let target_channel_id: u64 = edges[2].scid.into();

let clock = Arc::new(SimulationClock::new(1).unwrap());
let clock = Arc::new(SimulationClock::new(std::time::SystemTime::now()));
let reputation_interceptor: ReputationInterceptor<BatchForwardWriter, ForwardManager> =
ReputationInterceptor::new_for_network(params, &edges, Arc::clone(&clock), None)
.unwrap();
Expand Down
20 changes: 13 additions & 7 deletions ln-simln-jamming/src/bin/forward_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ use ln_simln_jamming::parsing::{
parse_duration, AttackType, NetworkParams, NetworkType, ReputationParams,
};
use ln_simln_jamming::reputation_interceptor::{BootstrapForward, ReputationInterceptor};
use ln_simln_jamming::{BoxError, ACCOUNTABLE_TYPE, UPGRADABLE_TYPE};
use ln_simln_jamming::{BoxError, ACCOUNTABLE_TYPE, SIM_SEED, UPGRADABLE_TYPE};
use log::LevelFilter;
use sim_cli::parsing::{create_simulation_with_network, SimParams};
use simln_lib::batched_writer::BatchedWriter;
use simln_lib::clock::{Clock, SimulationClock};
use simln_lib::latency_interceptor::LatencyIntercepor;
use simln_lib::runtime::block_on_virtual_time;
use simln_lib::sim_node::CustomRecords;
use simln_lib::SimulationCfg;
use simple_logger::SimpleLogger;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, UNIX_EPOCH};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex;
use tokio_util::task::TaskTracker;

Expand All @@ -43,8 +44,7 @@ struct Cli {
pub attack_type: Option<AttackType>,
}

#[tokio::main]
async fn main() -> Result<(), BoxError> {
fn main() -> Result<(), BoxError> {
SimpleLogger::new()
.with_level(LevelFilter::Debug)
// Lower logging from sim-ln so that we can focus on our own logs.
Expand All @@ -57,13 +57,19 @@ async fn main() -> Result<(), BoxError> {

let cli = Cli::parse();

let start_time = SystemTime::now();
block_on_virtual_time(start_time, |clock| run(clock, cli))??;

Ok(())
}

async fn run(clock: Arc<SimulationClock>, cli: Cli) -> Result<(), BoxError> {
let network = NetworkType::new(&cli.network, cli.attack_type, None)?;
if matches!(network, NetworkType::BootstrapAttackTime(_, _, _)) {
return Err("cannot run forward builder in bootstrap mode".into());
}

let sim_network = network.active_network();
let clock = Arc::new(SimulationClock::new(1000)?);
let tasks = TaskTracker::new();

// Create a reputation interceptor without any bootstrap (since here we're creating the
Expand All @@ -87,14 +93,14 @@ async fn main() -> Result<(), BoxError> {
.to_string(),
)?))),
)?);
let latency_interceptor = Arc::new(LatencyIntercepor::new_poisson(300.0)?);
let latency_interceptor = Arc::new(LatencyIntercepor::new_poisson(300.0, Some(SIM_SEED))?);

let sim_cfg = SimulationCfg::new(
Some(cli.duration.as_secs() as u32),
3_800_000,
2.0,
None,
Some(13995354354227336701),
Some(SIM_SEED),
);

let exclude_pubkeys = [network.target().1]
Expand Down
15 changes: 11 additions & 4 deletions ln-simln-jamming/src/bin/reputation_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
fs::{File, OpenOptions},
io::Write,
sync::Arc,
time::Duration,
time::{Duration, SystemTime},
};

use bitcoin::secp256k1::PublicKey;
Expand All @@ -22,6 +22,7 @@ use ln_simln_jamming::{
};
use log::LevelFilter;
use simln_lib::clock::SimulationClock;
use simln_lib::runtime::block_on_virtual_time;
use simple_logger::SimpleLogger;

#[derive(Parser)]
Expand All @@ -43,15 +44,22 @@ struct Cli {
pub attacker_bootstrap: Option<Duration>,
}

#[tokio::main]
async fn main() -> Result<(), BoxError> {
fn main() -> Result<(), BoxError> {
SimpleLogger::new()
.with_level(LevelFilter::Debug)
.with_module_level("simln_lib::sim_node", LevelFilter::Debug)
.init()
.unwrap();

let cli = Cli::parse();

let start_time = SystemTime::now();
block_on_virtual_time(start_time, |clock| run(clock, cli))??;

Ok(())
}

async fn run(clock: Arc<SimulationClock>, cli: Cli) -> Result<(), BoxError> {
let forward_params: ForwardManagerParams = cli.reputation_params.into();

let network = NetworkType::new(&cli.network, cli.attack_type, cli.attacker_bootstrap)?;
Expand Down Expand Up @@ -114,7 +122,6 @@ async fn main() -> Result<(), BoxError> {
(bootstrap_records, 0)
};

let clock = Arc::new(SimulationClock::new(1)?);
let reputation_clock = Arc::clone(&clock);
let mut reputation_interceptor: ReputationInterceptor<BatchForwardWriter, ForwardManager> =
ReputationInterceptor::new_for_network(
Expand Down
9 changes: 4 additions & 5 deletions ln-simln-jamming/src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use simln_lib::clock::SimulationClock;
use std::ops::Add;
use std::time::Instant;

pub trait InstantClock {
fn now(&self) -> Instant;
}

impl InstantClock for SimulationClock {
/// Reads the current instant from tokio's clock, which tracks virtual time when the runtime is paused. The
/// returned instant is only meaningful for relative duration arithmetic, never compare it to a wall-clock
/// `Instant::now()`.
fn now(&self) -> Instant {
let start_instant_std = self.get_start_instant().into();
let elapsed = Instant::now().duration_since(start_instant_std);

start_instant_std.add(elapsed * self.get_speedup_multiplier().into())
tokio::time::Instant::now().into_std()
}
}
3 changes: 3 additions & 0 deletions ln-simln-jamming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub(crate) mod test_utils;
/// Error type for errors that can be erased, includes 'static so that down-casting is possible.
pub type BoxError = Box<dyn Error + Send + Sync + 'static>;

/// Fixes randomness used in the simulation so that runs are reproducible.
pub const SIM_SEED: u64 = 13995354354227336701;

/// The TLV type used to represent experimental accountable signals.
pub const ACCOUNTABLE_TYPE: u64 = 106823;

Expand Down
Loading
Loading