@@ -82,15 +82,15 @@ pub struct Cli {
82
82
}
83
83
84
84
#[ derive( Debug , Serialize , Deserialize , Clone ) ]
85
- struct SimParams {
85
+ pub struct SimParams {
86
86
pub nodes : Vec < NodeConnection > ,
87
87
#[ serde( default ) ]
88
88
pub activity : Vec < ActivityParser > ,
89
89
}
90
90
91
91
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
92
92
#[ serde( untagged) ]
93
- enum NodeConnection {
93
+ pub enum NodeConnection {
94
94
Lnd ( lnd:: LndConnection ) ,
95
95
Cln ( cln:: ClnConnection ) ,
96
96
Eclair ( eclair:: EclairConnection ) ,
@@ -99,7 +99,7 @@ enum NodeConnection {
99
99
/// Data structure used to parse information from the simulation file. It allows source and destination to be
100
100
/// [NodeId], which enables the use of public keys and aliases in the simulation description.
101
101
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
102
- struct ActivityParser {
102
+ pub struct ActivityParser {
103
103
/// The source of the payment.
104
104
#[ serde( with = "serializers::serde_node_id" ) ]
105
105
pub source : NodeId ,
@@ -142,40 +142,21 @@ impl TryFrom<&Cli> for SimulationCfg {
142
142
143
143
/// Parses the cli options provided and creates a simulation to be run, connecting to lightning nodes and validating
144
144
/// any activity described in the simulation file.
145
- pub async fn create_simulation ( cli : & Cli ) -> Result < Simulation , anyhow:: Error > {
145
+ pub async fn create_simulation (
146
+ cli : & Cli ,
147
+ sim_params : & SimParams ,
148
+ ) -> Result < ( Simulation , HashMap < PublicKey , NodeInfo > ) , anyhow:: Error > {
146
149
let cfg: SimulationCfg = SimulationCfg :: try_from ( cli) ?;
147
150
148
- let sim_path = read_sim_path ( cli. data_dir . clone ( ) , cli. sim_file . clone ( ) ) . await ?;
149
- let SimParams { nodes, activity } = serde_json:: from_str ( & std:: fs:: read_to_string ( sim_path) ?)
150
- . map_err ( |e| {
151
- anyhow ! (
152
- "Could not deserialize node connection data or activity description from simulation file (line {}, col {}, err: {})." ,
153
- e. line( ) ,
154
- e. column( ) ,
155
- e. to_string( )
156
- )
157
- } ) ?;
151
+ let SimParams {
152
+ nodes,
153
+ activity : _activity,
154
+ } = sim_params;
158
155
159
- let ( clients, clients_info) = get_clients ( nodes) . await ?;
160
- // We need to be able to look up destination nodes in the graph, because we allow defined activities to send to
161
- // nodes that we do not control. To do this, we can just grab the first node in our map and perform the lookup.
162
- let get_node = async |pk : & PublicKey | -> Result < NodeInfo , LightningError > {
163
- if let Some ( c) = clients. values ( ) . next ( ) {
164
- return c. lock ( ) . await . get_node_info ( pk) . await ;
165
- }
166
-
167
- Err ( LightningError :: GetNodeInfoError (
168
- "no nodes for query" . to_string ( ) ,
169
- ) )
170
- } ;
171
-
172
- let ( pk_node_map, alias_node_map) = add_node_to_maps ( & clients_info) . await ?;
173
-
174
- let validated_activities =
175
- validate_activities ( activity, pk_node_map, alias_node_map, get_node) . await ?;
156
+ let ( clients, clients_info) = get_clients ( nodes. to_vec ( ) ) . await ?;
176
157
let tasks = TaskTracker :: new ( ) ;
177
158
178
- Ok ( Simulation :: new ( cfg, clients, validated_activities , tasks ) )
159
+ Ok ( ( Simulation :: new ( cfg, clients, tasks ) , clients_info ) )
179
160
}
180
161
181
162
/// Connects to the set of nodes providing, returning a map of node public keys to LightningNode implementations and
@@ -362,3 +343,37 @@ fn mkdir(dir: PathBuf) -> anyhow::Result<PathBuf> {
362
343
fs:: create_dir_all ( & dir) ?;
363
344
Ok ( dir)
364
345
}
346
+
347
+ pub async fn parse_sim_params ( cli : & Cli ) -> anyhow:: Result < SimParams > {
348
+ let sim_path = read_sim_path ( cli. data_dir . clone ( ) , cli. sim_file . clone ( ) ) . await ?;
349
+ let sim_params = serde_json:: from_str ( & std:: fs:: read_to_string ( sim_path) ?) . map_err ( |e| {
350
+ anyhow ! (
351
+ "Could not deserialize node connection data or activity description from simulation file (line {}, col {}, err: {})." ,
352
+ e. line( ) ,
353
+ e. column( ) ,
354
+ e. to_string( )
355
+ )
356
+ } ) ?;
357
+ Ok ( sim_params)
358
+ }
359
+
360
+ pub async fn get_validated_activities (
361
+ clients : & HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > > ,
362
+ nodes_info : HashMap < PublicKey , NodeInfo > ,
363
+ activity : Vec < ActivityParser > ,
364
+ ) -> Result < Vec < ActivityDefinition > , LightningError > {
365
+ // We need to be able to look up destination nodes in the graph, because we allow defined activities to send to
366
+ // nodes that we do not control. To do this, we can just grab the first node in our map and perform the lookup.
367
+ let get_node = async |pk : & PublicKey | -> Result < NodeInfo , LightningError > {
368
+ if let Some ( c) = clients. values ( ) . next ( ) {
369
+ return c. lock ( ) . await . get_node_info ( pk) . await ;
370
+ }
371
+ Err ( LightningError :: GetNodeInfoError (
372
+ "no nodes for query" . to_string ( ) ,
373
+ ) )
374
+ } ;
375
+ let ( pk_node_map, alias_node_map) = add_node_to_maps ( & nodes_info) . await ?;
376
+ let validated_activities =
377
+ validate_activities ( activity. to_vec ( ) , pk_node_map, alias_node_map, get_node) . await ?;
378
+ Ok ( validated_activities)
379
+ }
0 commit comments