@@ -27,7 +27,7 @@ pub const DEFAULT_DATA_DIR: &str = ".";
27
27
/// The default simulation file to be used by the simulator.
28
28
pub const DEFAULT_SIM_FILE : & str = "sim.json" ;
29
29
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 .
31
31
pub const DEFAULT_EXPECTED_PAYMENT_AMOUNT : u64 = 3_800_000 ;
32
32
33
33
/// 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 {
88
88
89
89
impl Cli {
90
90
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
92
92
if !sim_params. nodes . is_empty ( ) && !sim_params. sim_network . is_empty ( ) {
93
93
return Err ( anyhow ! (
94
94
"Simulation file cannot contain {} nodes and {} sim_graph entries, simulation can only be run with real
@@ -112,7 +112,7 @@ pub struct SimParams {
112
112
#[ serde( default ) ]
113
113
pub sim_network : Vec < NetworkParser > ,
114
114
#[ serde( default ) ]
115
- pub activity : Vec < ActivityParser > ,
115
+ activity : Vec < ActivityParser > ,
116
116
}
117
117
118
118
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
@@ -147,7 +147,7 @@ impl From<NetworkParser> for SimulatedChannel {
147
147
/// Data structure used to parse information from the simulation file. It allows source and destination to be
148
148
/// [NodeId], which enables the use of public keys and aliases in the simulation description.
149
149
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
150
- pub struct ActivityParser {
150
+ struct ActivityParser {
151
151
/// The source of the payment.
152
152
#[ serde( with = "serializers::serde_node_id" ) ]
153
153
pub source : NodeId ,
@@ -188,6 +188,8 @@ impl TryFrom<&Cli> for SimulationCfg {
188
188
}
189
189
}
190
190
191
+ struct NodeMapping ( HashMap < PublicKey , NodeInfo > , HashMap < String , NodeInfo > ) ;
192
+
191
193
pub async fn create_simulation_with_network (
192
194
cli : & Cli ,
193
195
sim_params : & SimParams ,
@@ -208,16 +210,11 @@ pub async fn create_simulation_with_network(
208
210
. collect :: < Vec < SimulatedChannel > > ( ) ;
209
211
210
212
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) ;
220
216
}
217
+
221
218
let get_node_info = async |pk : & PublicKey | -> Result < NodeInfo , LightningError > {
222
219
if let Some ( node) = nodes_info. get ( pk) {
223
220
Ok ( node_info ( node. pubkey ) )
@@ -228,7 +225,7 @@ pub async fn create_simulation_with_network(
228
225
) ) )
229
226
}
230
227
} ;
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) ?;
232
229
let validated_activities = validate_activities (
233
230
activity. to_vec ( ) ,
234
231
pk_node_map,
@@ -285,12 +282,13 @@ pub async fn create_simulation(
285
282
if let Some ( c) = clients. values ( ) . next ( ) {
286
283
return c. lock ( ) . await . get_node_info ( pk) . await ;
287
284
}
285
+
288
286
Err ( LightningError :: GetNodeInfoError (
289
287
"no nodes for query" . to_string ( ) ,
290
288
) )
291
289
} ;
292
290
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) ?;
294
292
let validated_activities =
295
293
validate_activities ( activity. to_vec ( ) , pk_node_map, alias_node_map, get_node) . await ?;
296
294
let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
@@ -338,12 +336,9 @@ async fn get_clients(
338
336
Ok ( ( clients, clients_info) )
339
337
}
340
338
341
- type NodeMapping =
342
- Result < ( HashMap < PublicKey , NodeInfo > , HashMap < String , NodeInfo > ) , LightningError > ;
343
-
344
339
/// Adds a lightning node to a client map and tracking maps used to lookup node pubkeys and aliases for activity
345
340
/// 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 > {
347
342
let mut pk_node_map = HashMap :: new ( ) ;
348
343
let mut alias_node_map = HashMap :: new ( ) ;
349
344
@@ -375,7 +370,7 @@ fn add_node_to_maps(nodes: &HashMap<PublicKey, NodeInfo>) -> NodeMapping {
375
370
pk_node_map. insert ( node_info. pubkey , node_info. clone ( ) ) ;
376
371
}
377
372
378
- Ok ( ( pk_node_map, alias_node_map) )
373
+ Ok ( NodeMapping ( pk_node_map, alias_node_map) )
379
374
}
380
375
381
376
/// 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> {
490
485
fs:: create_dir_all ( & dir) ?;
491
486
Ok ( dir)
492
487
}
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
+ }
0 commit comments