-
Notifications
You must be signed in to change notification settings - Fork 39
Use time-paused tokio for zero sleeps #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5bc274
simln-lib: reimplement SimulationClock as a runtime-driven virtual clock
carlaKC d6d6452
simln-lib: add a virtual-time runtime helper
carlaKC 080b2e0
sim-cli: make parse_sim_params non-async
carlaKC 6d9a610
sim-cli: add --virtual-time to run simulations on virtual time
carlaKC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,14 +84,15 @@ pub struct Cli { | |
| /// Seed to run random activity generator deterministically | ||
| #[clap(long, short)] | ||
| pub fix_seed: Option<u64>, | ||
| /// A multiplier to wall time to speed up the simulation's clock. Only available when when running on a network of | ||
| /// simulated nodes. | ||
| #[clap(long)] | ||
| pub speedup_clock: Option<u16>, | ||
| /// Latency to optionally introduce for payments in a simulated network expressed in | ||
| /// milliseconds. | ||
| #[clap(long)] | ||
| pub latency_ms: Option<u32>, | ||
| /// Run the simulated network on virtual time: time advances instantly to the next event instead of sleeping on the | ||
| /// wall clock, so the run finishes as fast as the CPU allows and is reproducible for a given seed. Only available on | ||
| /// a simulated network, and requires --total-time to bound the run. | ||
| #[clap(long, default_value_t = false)] | ||
| pub virtual_time: bool, | ||
| } | ||
|
|
||
| impl Cli { | ||
|
|
@@ -111,18 +112,25 @@ impl Cli { | |
| nodes or sim_graph to run with simulated nodes" | ||
| )); | ||
| } | ||
| if !sim_params.nodes.is_empty() && self.speedup_clock.is_some() { | ||
| return Err(anyhow!( | ||
| "Clock speedup is only allowed when running on a simulated network" | ||
| )); | ||
| } | ||
|
|
||
| if !sim_params.nodes.is_empty() && self.latency_ms.is_some() { | ||
| return Err(anyhow!( | ||
| "Latency for payments is only allowed when running on a simulated network" | ||
| )); | ||
| } | ||
|
|
||
| if self.virtual_time { | ||
| if !sim_params.nodes.is_empty() { | ||
| return Err(anyhow!( | ||
| "Virtual time is only allowed when running on a simulated network; real nodes run on wall time" | ||
| )); | ||
| } | ||
| if self.total_time.is_none() { | ||
| return Err(anyhow!( | ||
| "Virtual time requires --total-time, otherwise it advances forever" | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| if !sim_params.exclude.is_empty() { | ||
| if sim_params.sim_network.is_empty() { | ||
| return Err(anyhow!( | ||
|
|
@@ -338,14 +346,15 @@ pub async fn create_simulation_with_network( | |
| )) | ||
| } | ||
|
|
||
| /// Parses the cli options provided and creates a simulation to be run, connecting to lightning nodes and validating | ||
| /// any activity described in the simulation file. | ||
| /// Creates a simulation to be run against a set of real lightning nodes, connecting to the nodes described in | ||
| /// `sim_params` and validating any activity described in the simulation file. The simulation is driven by `clock`, | ||
| /// which must be constructed on the runtime that will run the simulation. | ||
| pub async fn create_simulation( | ||
| cli: &Cli, | ||
| cfg: SimulationCfg, | ||
| sim_params: &SimParams, | ||
| clock: Arc<SimulationClock>, | ||
| tasks: TaskTracker, | ||
| ) -> Result<(Simulation<SimulationClock>, Vec<ActivityDefinition>), anyhow::Error> { | ||
| let cfg: SimulationCfg = SimulationCfg::try_from(cli)?; | ||
| let SimParams { | ||
| nodes, | ||
| sim_network: _sim_network, | ||
|
|
@@ -364,9 +373,7 @@ pub async fn create_simulation( | |
| cfg, | ||
| clients, | ||
| tasks, | ||
| // When running on a real network, the underlying node may use wall time so we always use a clock with no | ||
| // speedup. | ||
| Arc::new(SimulationClock::new(1)?), | ||
| clock, | ||
| shutdown_trigger, | ||
| shutdown_listener, | ||
| ), | ||
|
|
@@ -529,7 +536,7 @@ async fn validate_activities( | |
| Ok(validated_activities) | ||
| } | ||
|
|
||
| async fn read_sim_path(data_dir: PathBuf, sim_file: PathBuf) -> anyhow::Result<PathBuf> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool |
||
| fn read_sim_path(data_dir: PathBuf, sim_file: PathBuf) -> anyhow::Result<PathBuf> { | ||
| if sim_file.exists() { | ||
| Ok(sim_file) | ||
| } else if sim_file.is_relative() { | ||
|
|
@@ -538,15 +545,15 @@ async fn read_sim_path(data_dir: PathBuf, sim_file: PathBuf) -> anyhow::Result<P | |
| Ok(sim_path) | ||
| } else { | ||
| log::info!("Simulation file '{}' does not exist.", sim_path.display()); | ||
| select_sim_file(data_dir).await | ||
| select_sim_file(data_dir) | ||
| } | ||
| } else { | ||
| log::info!("Simulation file '{}' does not exist.", sim_file.display()); | ||
| select_sim_file(data_dir).await | ||
| select_sim_file(data_dir) | ||
| } | ||
| } | ||
|
|
||
| pub async fn select_sim_file(data_dir: PathBuf) -> anyhow::Result<PathBuf> { | ||
| pub fn select_sim_file(data_dir: PathBuf) -> anyhow::Result<PathBuf> { | ||
| let sim_files = std::fs::read_dir(data_dir.clone())? | ||
| .filter_map(|f| { | ||
| f.ok().and_then(|f| { | ||
|
|
@@ -583,8 +590,8 @@ fn mkdir(dir: PathBuf) -> anyhow::Result<PathBuf> { | |
| Ok(dir) | ||
| } | ||
|
|
||
| pub async fn parse_sim_params(cli: &Cli) -> anyhow::Result<SimParams> { | ||
| let sim_path = read_sim_path(cli.data_dir.clone(), cli.sim_file.clone()).await?; | ||
| pub fn parse_sim_params(cli: &Cli) -> anyhow::Result<SimParams> { | ||
| let sim_path = read_sim_path(cli.data_dir.clone(), cli.sim_file.clone())?; | ||
| let sim_params = serde_json::from_str(&std::fs::read_to_string(sim_path)?).map_err(|e| { | ||
| anyhow!( | ||
| "Could not deserialize node connection data or activity description from simulation file (line {}, col {}, err: {}).", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can fix this comment
sim-ln/simln-lib/src/lib.rs
Line 567 in 96c8f6d