@@ -58,6 +58,7 @@ use lightning_dns_resolver::OMDomainResolver;
5858use lightning_net_tokio:: SocketDescriptor ;
5959use lightning_persister:: fs_store:: FilesystemStore ;
6060use rand:: { thread_rng, Rng } ;
61+ use serde:: Deserialize ;
6162use std:: collections:: HashMap as StdHashMap ;
6263use std:: convert:: TryInto ;
6364use std:: fmt;
@@ -77,6 +78,13 @@ pub(crate) enum HTLCStatus {
7778 Failed ,
7879}
7980
81+ #[ derive( Deserialize ) ]
82+ struct ProbeConfig {
83+ probe_interval_sec : u64 ,
84+ probe_peers : Vec < String > ,
85+ max_amount_msats : u64 ,
86+ }
87+
8088impl_writeable_tlv_based_enum ! ( HTLCStatus ,
8189 ( 0 , Pending ) => { } ,
8290 ( 1 , Succeeded ) => { } ,
@@ -213,21 +221,20 @@ pub(crate) type OutputSweeper = ldk_sweep::OutputSweeper<
213221// Needed due to rust-lang/rust#63033.
214222struct OutputSweeperWrapper ( Arc < OutputSweeper > ) ;
215223
216- fn send_rand_probe (
224+ fn prepare_probe (
217225 channel_manager : & ChannelManager , graph : & NetworkGraph , logger : & disk:: FilesystemLogger ,
218226 scorer : & RwLock < ProbabilisticScorer < Arc < NetworkGraph > , Arc < disk:: FilesystemLogger > > > ,
227+ pub_key_hex : & str , probe_amount : u64 ,
219228) {
220- let rcpt = {
221- let lck = graph. read_only ( ) ;
222- if lck. nodes ( ) . is_empty ( ) {
223- return ;
224- }
225- let mut it =
226- lck. nodes ( ) . unordered_iter ( ) . skip ( :: rand:: random :: < usize > ( ) % lck. nodes ( ) . len ( ) ) ;
227- it. next ( ) . unwrap ( ) . 0 . clone ( )
229+ if probe_amount == 0 {
230+ return ;
231+ }
232+ let amt = :: rand:: random :: < u64 > ( ) % probe_amount;
233+ let pub_key_bytes = match hex_utils:: to_vec ( pub_key_hex) {
234+ Some ( bytes) => bytes,
235+ None => return ,
228236 } ;
229- let amt = :: rand:: random :: < u64 > ( ) % 500_000_000 ;
230- if let Ok ( pk) = bitcoin:: secp256k1:: PublicKey :: from_slice ( rcpt. as_slice ( ) ) {
237+ if let Ok ( pk) = bitcoin:: secp256k1:: PublicKey :: from_slice ( & pub_key_bytes) {
231238 send_probe ( channel_manager, pk, graph, logger, amt, scorer) ;
232239 }
233240}
@@ -262,6 +269,14 @@ fn send_probe(
262269 }
263270}
264271
272+ fn read_probe_file ( ldk_data_dir : & str ) -> Result < ProbeConfig , std:: io:: Error > {
273+ let prober_file = format ! ( "{}/prober_config.json" , ldk_data_dir) ;
274+ let file = fs:: read_to_string ( prober_file) ?;
275+ let config: ProbeConfig = serde_json:: from_str ( & file)
276+ . map_err ( |err| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , err) ) ?;
277+ Ok ( config)
278+ }
279+
265280fn handle_ldk_events < ' a > (
266281 channel_manager : Arc < ChannelManager > , bitcoind_client : & ' a BitcoindClient ,
267282 network_graph : & ' a NetworkGraph , keys_manager : & ' a KeysManager ,
@@ -1216,13 +1231,35 @@ async fn start_ldk() {
12161231 let probing_graph = Arc :: clone ( & network_graph) ;
12171232 let probing_logger = Arc :: clone ( & logger) ;
12181233 let probing_scorer = Arc :: clone ( & scorer) ;
1219- tokio:: spawn ( async move {
1220- let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 1 ) ) ;
1221- loop {
1222- interval. tick ( ) . await ;
1223- send_rand_probe ( & * probing_cm, & * probing_graph, & * probing_logger, & * probing_scorer) ;
1224- }
1225- } ) ;
1234+ match read_probe_file ( & ldk_data_dir) {
1235+ Ok ( probe_config) => {
1236+ if probe_config. probe_peers . is_empty ( ) {
1237+ println ! ( "WARNING: prober_config.json has no probe_peers. Probing disabled." ) ;
1238+ } else {
1239+ let mut index = 0usize ;
1240+ tokio:: spawn ( async move {
1241+ let mut interval =
1242+ tokio:: time:: interval ( Duration :: from_secs ( probe_config. probe_interval_sec ) ) ;
1243+ loop {
1244+ interval. tick ( ) . await ;
1245+ if index >= probe_config. probe_peers . len ( ) {
1246+ index = 0 ;
1247+ }
1248+ prepare_probe (
1249+ & * probing_cm,
1250+ & * probing_graph,
1251+ & * probing_logger,
1252+ & * probing_scorer,
1253+ & probe_config. probe_peers [ index] ,
1254+ probe_config. max_amount_msats ,
1255+ ) ;
1256+ index += 1 ;
1257+ }
1258+ } ) ;
1259+ }
1260+ } ,
1261+ Err ( _) => println ! ( "WARNING: prober_config.json is missing. Probing disabled." ) ,
1262+ } ;
12261263
12271264 // Start the CLI.
12281265 let cli_channel_manager = Arc :: clone ( & channel_manager) ;
0 commit comments