1
- use bitcoin:: secp256k1:: PublicKey ;
1
+ use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
2
+ use rand:: distributions:: Uniform ;
3
+ use rand:: Rng ;
4
+ use sim_lib:: sim_node:: {
5
+ ln_node_from_graph, populate_network_graph, ChannelPolicy , SimGraph , SimulatedChannel ,
6
+ } ;
7
+
2
8
use std:: collections:: HashMap ;
3
9
use std:: path:: PathBuf ;
4
10
use std:: sync:: Arc ;
@@ -189,7 +195,6 @@ async fn main() -> anyhow::Result<()> {
189
195
amount_msat : act. amount_msat ,
190
196
} ) ;
191
197
}
192
-
193
198
let write_results = if !cli. no_results {
194
199
Some ( WriteResults {
195
200
results_dir : mkdir ( cli. data_dir . join ( "results" ) ) . await ?,
@@ -199,8 +204,22 @@ async fn main() -> anyhow::Result<()> {
199
204
None
200
205
} ;
201
206
207
+ let channels = generate_sim_nodes ( ) ;
208
+
209
+ // TODO: use the shutdown trigger and listener across simulator and graph.
210
+ let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
211
+ let graph = match SimGraph :: new ( channels. clone ( ) , shutdown_trigger, shutdown_listener) {
212
+ Ok ( graph) => Arc :: new ( Mutex :: new ( graph) ) ,
213
+ Err ( e) => anyhow:: bail!( "failed: {:?}" , e) ,
214
+ } ;
215
+
216
+ let routing_graph = match populate_network_graph ( channels) {
217
+ Ok ( r) => r,
218
+ Err ( e) => anyhow:: bail!( "failed: {:?}" , e) ,
219
+ } ;
220
+
202
221
let sim = Simulation :: new (
203
- clients ,
222
+ ln_node_from_graph ( graph , Arc :: new ( routing_graph ) ) . await ,
204
223
validated_activities,
205
224
cli. total_time ,
206
225
cli. expected_pmt_amt ,
@@ -219,6 +238,84 @@ async fn main() -> anyhow::Result<()> {
219
238
Ok ( ( ) )
220
239
}
221
240
241
+ fn generate_sim_nodes ( ) -> Vec < SimulatedChannel > {
242
+ let capacity = 300000000 ;
243
+ let mut channels: Vec < SimulatedChannel > = vec ! [ ] ;
244
+ let ( _, first_node) = get_random_keypair ( ) ;
245
+
246
+ // Create channels in a ring so that we'll get long payment paths.
247
+ let mut node_1 = first_node;
248
+ for i in 0 ..10 {
249
+ // Create a new node that we'll create a channel with. If we're on the last node in the circle, we'll loop
250
+ // back around to the first node to close it.
251
+ let node_2 = if i == 10 {
252
+ first_node
253
+ } else {
254
+ let ( _, pk) = get_random_keypair ( ) ;
255
+ pk
256
+ } ;
257
+
258
+ let node_1_to_2 = ChannelPolicy {
259
+ pubkey : node_1,
260
+ max_htlc_count : 483 ,
261
+ max_in_flight_msat : capacity / 2 ,
262
+ min_htlc_size_msat : 1 ,
263
+ max_htlc_size_msat : capacity / 2 ,
264
+ cltv_expiry_delta : 40 ,
265
+ // Alter fee rate a little for different values.
266
+ base_fee : 1000 * i,
267
+ fee_rate_prop : 1500 * i,
268
+ } ;
269
+
270
+ let node_2_to_1 = ChannelPolicy {
271
+ pubkey : node_2,
272
+ max_htlc_count : 483 ,
273
+ max_in_flight_msat : capacity / 2 ,
274
+ min_htlc_size_msat : 1 ,
275
+ max_htlc_size_msat : capacity / 2 ,
276
+ cltv_expiry_delta : 40 + 10 * i as u32 ,
277
+ // Alter fee rate a little for different values.
278
+ base_fee : 2000 * i,
279
+ fee_rate_prop : i,
280
+ } ;
281
+
282
+ channels. push ( SimulatedChannel :: new (
283
+ capacity,
284
+ // Unique channel ID per link.
285
+ 100 + i,
286
+ node_1_to_2,
287
+ node_2_to_1,
288
+ ) ) ;
289
+
290
+ // Once we've created this link in the circle, progress our current node to be node_1 so that we can generate
291
+ // a new edge.
292
+ node_1 = node_2;
293
+ }
294
+
295
+ channels
296
+ }
297
+
298
+ /// COPIED from test utils!
299
+ pub fn get_random_bytes ( size : usize ) -> Vec < u8 > {
300
+ rand:: thread_rng ( )
301
+ . sample_iter ( Uniform :: new ( u8:: MIN , u8:: MAX ) )
302
+ . take ( size)
303
+ . collect ( )
304
+ }
305
+
306
+ pub fn get_random_int ( s : u64 , e : u64 ) -> u64 {
307
+ rand:: thread_rng ( ) . gen_range ( s..e)
308
+ }
309
+
310
+ pub fn get_random_keypair ( ) -> ( SecretKey , PublicKey ) {
311
+ loop {
312
+ if let Ok ( sk) = SecretKey :: from_slice ( & get_random_bytes ( 32 ) ) {
313
+ return ( sk, PublicKey :: from_secret_key ( & Secp256k1 :: new ( ) , & sk) ) ;
314
+ }
315
+ }
316
+ }
317
+ /// COPIED from test utils!
318
+
222
319
async fn read_sim_path ( data_dir : PathBuf , sim_file : PathBuf ) -> anyhow:: Result < PathBuf > {
223
320
let sim_path = if sim_file. is_relative ( ) {
224
321
data_dir. join ( sim_file)
0 commit comments