Skip to content

Commit cac73aa

Browse files
committed
fixup! 0a36a79
1 parent b09ac15 commit cac73aa

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

sim-cli/src/main.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use anyhow::anyhow;
2-
use clap::Parser;
31
use log::LevelFilter;
4-
use sim_cli::parsing::{create_simulation, create_simulation_with_network, read_sim_path, Cli};
2+
use sim_cli::parsing::{create_simulation, create_simulation_with_network, parse_cli};
53
use simple_logger::SimpleLogger;
64
use tokio_util::task::TaskTracker;
75

@@ -13,7 +11,7 @@ async fn main() -> anyhow::Result<()> {
1311
console_subscriber::init();
1412
}
1513

16-
let cli = Cli::parse();
14+
let (cli, sim_params) = parse_cli().await?;
1715

1816
SimpleLogger::new()
1917
.with_level(LevelFilter::Warn)
@@ -22,16 +20,6 @@ async fn main() -> anyhow::Result<()> {
2220
.init()
2321
.unwrap();
2422

25-
let sim_path = read_sim_path(cli.data_dir.clone(), cli.sim_file.clone()).await?;
26-
let sim_params = serde_json::from_str(&std::fs::read_to_string(sim_path)?).map_err(|e| {
27-
anyhow!(
28-
"Could not deserialize node connection data or activity description from simulation file (line {}, col {}, err: {}).",
29-
e.line(),
30-
e.column(),
31-
e.to_string()
32-
)
33-
})?;
34-
3523
cli.validate(&sim_params)?;
3624

3725
let tasks = TaskTracker::new();

sim-cli/src/parsing.rs

+29-20
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub const DEFAULT_DATA_DIR: &str = ".";
2727
/// The default simulation file to be used by the simulator.
2828
pub const DEFAULT_SIM_FILE: &str = "sim.json";
2929

30-
/// The default expected payment amount for the simulation, around ~$10 at the time of w.
30+
/// The default expected payment amount for the simulation, around ~$10 at the time of writing.
3131
pub const DEFAULT_EXPECTED_PAYMENT_AMOUNT: u64 = 3_800_000;
3232

3333
/// The number of times over each node in the network sends its total deployed capacity in a calendar month.
@@ -88,7 +88,7 @@ pub struct Cli {
8888

8989
impl Cli {
9090
pub fn validate(&self, sim_params: &SimParams) -> Result<(), anyhow::Error> {
91-
// Validate that nodes and sim_graph are exclusively set, and setup node clients from the populated field.
91+
// Validate that nodes and sim_graph are exclusively set
9292
if !sim_params.nodes.is_empty() && !sim_params.sim_network.is_empty() {
9393
return Err(anyhow!(
9494
"Simulation file cannot contain {} nodes and {} sim_graph entries, simulation can only be run with real
@@ -112,7 +112,7 @@ pub struct SimParams {
112112
#[serde(default)]
113113
pub sim_network: Vec<NetworkParser>,
114114
#[serde(default)]
115-
pub activity: Vec<ActivityParser>,
115+
activity: Vec<ActivityParser>,
116116
}
117117

118118
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -147,7 +147,7 @@ impl From<NetworkParser> for SimulatedChannel {
147147
/// Data structure used to parse information from the simulation file. It allows source and destination to be
148148
/// [NodeId], which enables the use of public keys and aliases in the simulation description.
149149
#[derive(Debug, Clone, Serialize, Deserialize)]
150-
pub struct ActivityParser {
150+
struct ActivityParser {
151151
/// The source of the payment.
152152
#[serde(with = "serializers::serde_node_id")]
153153
pub source: NodeId,
@@ -188,6 +188,8 @@ impl TryFrom<&Cli> for SimulationCfg {
188188
}
189189
}
190190

191+
struct NodeMapping(HashMap<PublicKey, NodeInfo>, HashMap<String, NodeInfo>);
192+
191193
pub async fn create_simulation_with_network(
192194
cli: &Cli,
193195
sim_params: &SimParams,
@@ -208,16 +210,11 @@ pub async fn create_simulation_with_network(
208210
.collect::<Vec<SimulatedChannel>>();
209211

210212
let mut nodes_info = HashMap::new();
211-
for sim_channel in sim_network {
212-
nodes_info.insert(
213-
sim_channel.node_1.pubkey,
214-
node_info(sim_channel.node_1.pubkey),
215-
);
216-
nodes_info.insert(
217-
sim_channel.node_2.pubkey,
218-
node_info(sim_channel.node_2.pubkey),
219-
);
213+
for channel in &channels {
214+
let (pubkey, node_info) = channel.create_simulated_node();
215+
nodes_info.insert(pubkey, node_info);
220216
}
217+
221218
let get_node_info = async |pk: &PublicKey| -> Result<NodeInfo, LightningError> {
222219
if let Some(node) = nodes_info.get(pk) {
223220
Ok(node_info(node.pubkey))
@@ -228,7 +225,7 @@ pub async fn create_simulation_with_network(
228225
)))
229226
}
230227
};
231-
let (pk_node_map, alias_node_map) = add_node_to_maps(&nodes_info)?;
228+
let NodeMapping(pk_node_map, alias_node_map) = add_node_to_maps(&nodes_info)?;
232229
let validated_activities = validate_activities(
233230
activity.to_vec(),
234231
pk_node_map,
@@ -285,12 +282,13 @@ pub async fn create_simulation(
285282
if let Some(c) = clients.values().next() {
286283
return c.lock().await.get_node_info(pk).await;
287284
}
285+
288286
Err(LightningError::GetNodeInfoError(
289287
"no nodes for query".to_string(),
290288
))
291289
};
292290

293-
let (pk_node_map, alias_node_map) = add_node_to_maps(&clients_info)?;
291+
let NodeMapping(pk_node_map, alias_node_map) = add_node_to_maps(&clients_info)?;
294292
let validated_activities =
295293
validate_activities(activity.to_vec(), pk_node_map, alias_node_map, get_node).await?;
296294
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
@@ -338,12 +336,9 @@ async fn get_clients(
338336
Ok((clients, clients_info))
339337
}
340338

341-
type NodeMapping =
342-
Result<(HashMap<PublicKey, NodeInfo>, HashMap<String, NodeInfo>), LightningError>;
343-
344339
/// Adds a lightning node to a client map and tracking maps used to lookup node pubkeys and aliases for activity
345340
/// validation.
346-
fn add_node_to_maps(nodes: &HashMap<PublicKey, NodeInfo>) -> NodeMapping {
341+
fn add_node_to_maps(nodes: &HashMap<PublicKey, NodeInfo>) -> Result<NodeMapping, LightningError> {
347342
let mut pk_node_map = HashMap::new();
348343
let mut alias_node_map = HashMap::new();
349344

@@ -375,7 +370,7 @@ fn add_node_to_maps(nodes: &HashMap<PublicKey, NodeInfo>) -> NodeMapping {
375370
pk_node_map.insert(node_info.pubkey, node_info.clone());
376371
}
377372

378-
Ok((pk_node_map, alias_node_map))
373+
Ok(NodeMapping(pk_node_map, alias_node_map))
379374
}
380375

381376
/// Validates a set of defined activities, cross-checking aliases and public keys against the set of clients that
@@ -490,3 +485,17 @@ fn mkdir(dir: PathBuf) -> anyhow::Result<PathBuf> {
490485
fs::create_dir_all(&dir)?;
491486
Ok(dir)
492487
}
488+
489+
pub async fn parse_cli() -> anyhow::Result<(Cli, SimParams)> {
490+
let cli = Cli::parse();
491+
let sim_path = read_sim_path(cli.data_dir.clone(), cli.sim_file.clone()).await?;
492+
let sim_params = serde_json::from_str(&std::fs::read_to_string(sim_path)?).map_err(|e| {
493+
anyhow!(
494+
"Could not deserialize node connection data or activity description from simulation file (line {}, col {}, err: {}).",
495+
e.line(),
496+
e.column(),
497+
e.to_string()
498+
)
499+
})?;
500+
Ok((cli, sim_params))
501+
}

simln-lib/src/sim_node.rs

+7
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ impl SimulatedChannel {
424424
self.get_node(forwarding_node)?
425425
.check_htlc_forward(cltv_delta, amount_msat, fee_msat)
426426
}
427+
428+
pub fn create_simulated_node(&self) -> (PublicKey, NodeInfo) {
429+
(
430+
self.node_1.policy.pubkey,
431+
node_info(self.node_1.policy.pubkey),
432+
)
433+
}
427434
}
428435

429436
/// SimNetwork represents a high level network coordinator that is responsible for the task of actually propagating

0 commit comments

Comments
 (0)