-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.rs
714 lines (664 loc) · 22.7 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
pub mod bitcoind_client;
mod cli;
mod convert;
mod disk;
mod hex_utils;
use crate::bitcoind_client::BitcoindClient;
use crate::disk::FilesystemLogger;
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::BlockHash;
use bitcoin_bech32::WitnessProgram;
use lightning::chain;
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
use lightning::chain::chainmonitor;
use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient};
use lightning::chain::{BestBlock, Filter, Watch};
use lightning::ln::channelmanager;
use lightning::ln::channelmanager::{
ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager,
};
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler, SimpleArcPeerManager};
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
use lightning::routing::scoring::ProbabilisticScorer;
use lightning::util::config::UserConfig;
use lightning::util::events::{Event, PaymentPurpose};
use lightning::util::ser::ReadableArgs;
use lightning_background_processor::BackgroundProcessor;
use lightning_block_sync::init;
use lightning_block_sync::poll;
use lightning_block_sync::SpvClient;
use lightning_block_sync::UnboundedCache;
use lightning_invoice::payment;
use lightning_invoice::utils::DefaultRouter;
use lightning_net_tokio::SocketDescriptor;
use lightning_persister::FilesystemPersister;
use rand::{thread_rng, Rng};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::fs::File;
use std::io;
use std::io::Write;
use std::ops::Deref;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
pub(crate) enum HTLCStatus {
Pending,
Succeeded,
Failed,
}
pub(crate) struct MillisatAmount(Option<u64>);
impl fmt::Display for MillisatAmount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
Some(amt) => write!(f, "{}", amt),
None => write!(f, "unknown"),
}
}
}
pub(crate) struct PaymentInfo {
preimage: Option<PaymentPreimage>,
secret: Option<PaymentSecret>,
status: HTLCStatus,
amt_msat: MillisatAmount,
}
pub(crate) type PaymentInfoStorage = Arc<Mutex<HashMap<PaymentHash, PaymentInfo>>>;
type ChainMonitor = chainmonitor::ChainMonitor<
InMemorySigner,
Arc<dyn Filter + Send + Sync>,
Arc<BitcoindClient>,
Arc<BitcoindClient>,
Arc<FilesystemLogger>,
Arc<FilesystemPersister>,
>;
pub(crate) type PeerManager = SimpleArcPeerManager<
SocketDescriptor,
ChainMonitor,
BitcoindClient,
BitcoindClient,
dyn chain::Access + Send + Sync,
FilesystemLogger,
>;
pub(crate) type ChannelManager =
SimpleArcChannelManager<ChainMonitor, BitcoindClient, BitcoindClient, FilesystemLogger>;
pub(crate) type InvoicePayer<E> = payment::InvoicePayer<
Arc<ChannelManager>,
Router,
Arc<Mutex<ProbabilisticScorer<Arc<NetworkGraph>>>>,
Arc<FilesystemLogger>,
E,
>;
type Router = DefaultRouter<Arc<NetworkGraph>, Arc<FilesystemLogger>>;
async fn handle_ldk_events(
channel_manager: Arc<ChannelManager>, bitcoind_client: Arc<BitcoindClient>,
keys_manager: Arc<KeysManager>, inbound_payments: PaymentInfoStorage,
outbound_payments: PaymentInfoStorage, network: Network, event: &Event,
) {
match event {
Event::FundingGenerationReady {
temporary_channel_id,
channel_value_satoshis,
output_script,
..
} => {
// Construct the raw transaction with one output, that is paid the amount of the
// channel.
let addr = WitnessProgram::from_scriptpubkey(
&output_script[..],
match network {
Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
Network::Testnet => bitcoin_bech32::constants::Network::Testnet,
Network::Regtest => bitcoin_bech32::constants::Network::Regtest,
Network::Signet => bitcoin_bech32::constants::Network::Signet,
},
)
.expect("Lightning funding tx should always be to a SegWit output")
.to_address();
let mut outputs = vec![HashMap::with_capacity(1)];
outputs[0].insert(addr, *channel_value_satoshis as f64 / 100_000_000.0);
let raw_tx = bitcoind_client.create_raw_transaction(outputs).await;
// Have your wallet put the inputs into the transaction such that the output is
// satisfied.
let funded_tx = bitcoind_client.fund_raw_transaction(raw_tx).await;
// Sign the final funding transaction and broadcast it.
let signed_tx = bitcoind_client.sign_raw_transaction_with_wallet(funded_tx.hex).await;
assert_eq!(signed_tx.complete, true);
let final_tx: Transaction =
encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap();
// Give the funding transaction back to LDK for opening the channel.
if channel_manager
.funding_transaction_generated(&temporary_channel_id, final_tx)
.is_err()
{
println!(
"\nERROR: Channel went away before we could fund it. The peer disconnected or refused the channel.");
print!("> ");
io::stdout().flush().unwrap();
}
}
Event::PaymentReceived { payment_hash, purpose, amt, .. } => {
let mut payments = inbound_payments.lock().unwrap();
let (payment_preimage, payment_secret) = match purpose {
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
(*payment_preimage, Some(*payment_secret))
}
PaymentPurpose::SpontaneousPayment(preimage) => (Some(*preimage), None),
};
let status = match channel_manager.claim_funds(payment_preimage.unwrap()) {
true => {
println!(
"\nEVENT: received payment from payment hash {} of {} millisatoshis",
hex_utils::hex_str(&payment_hash.0),
amt
);
print!("> ");
io::stdout().flush().unwrap();
HTLCStatus::Succeeded
}
_ => HTLCStatus::Failed,
};
match payments.entry(*payment_hash) {
Entry::Occupied(mut e) => {
let payment = e.get_mut();
payment.status = status;
payment.preimage = payment_preimage;
payment.secret = payment_secret;
}
Entry::Vacant(e) => {
e.insert(PaymentInfo {
preimage: payment_preimage,
secret: payment_secret,
status,
amt_msat: MillisatAmount(Some(*amt)),
});
}
}
}
Event::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
let mut payments = outbound_payments.lock().unwrap();
for (hash, payment) in payments.iter_mut() {
if *hash == *payment_hash {
payment.preimage = Some(*payment_preimage);
payment.status = HTLCStatus::Succeeded;
println!(
"\nEVENT: successfully sent payment of {} millisatoshis{} from \
payment hash {:?} with preimage {:?}",
payment.amt_msat,
if let Some(fee) = fee_paid_msat {
format!(" (fee {} msat)", fee)
} else {
"".to_string()
},
hex_utils::hex_str(&payment_hash.0),
hex_utils::hex_str(&payment_preimage.0)
);
print!("> ");
io::stdout().flush().unwrap();
}
}
}
Event::OpenChannelRequest { .. } => {
// Unreachable, we don't set manually_accept_inbound_channels
}
Event::PaymentPathSuccessful { .. } => {}
Event::PaymentPathFailed { .. } => {}
Event::PaymentFailed { payment_hash, .. } => {
print!(
"\nEVENT: Failed to send payment to payment hash {:?}: exhausted payment retry attempts",
hex_utils::hex_str(&payment_hash.0)
);
print!("> ");
io::stdout().flush().unwrap();
let mut payments = outbound_payments.lock().unwrap();
if payments.contains_key(&payment_hash) {
let payment = payments.get_mut(&payment_hash).unwrap();
payment.status = HTLCStatus::Failed;
}
}
Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx } => {
let from_onchain_str = if *claim_from_onchain_tx {
"from onchain downstream claim"
} else {
"from HTLC fulfill message"
};
if let Some(fee_earned) = fee_earned_msat {
println!(
"\nEVENT: Forwarded payment, earning {} msat {}",
fee_earned, from_onchain_str
);
} else {
println!("\nEVENT: Forwarded payment, claiming onchain {}", from_onchain_str);
}
print!("> ");
io::stdout().flush().unwrap();
}
Event::PendingHTLCsForwardable { time_forwardable } => {
let forwarding_channel_manager = channel_manager.clone();
let min = time_forwardable.as_millis() as u64;
tokio::spawn(async move {
let millis_to_sleep = thread_rng().gen_range(min, min * 5) as u64;
tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await;
forwarding_channel_manager.process_pending_htlc_forwards();
});
}
Event::SpendableOutputs { outputs } => {
let destination_address = bitcoind_client.get_new_address().await;
let output_descriptors = &outputs.iter().map(|a| a).collect::<Vec<_>>();
let tx_feerate =
bitcoind_client.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let spending_tx = keys_manager
.spend_spendable_outputs(
output_descriptors,
Vec::new(),
destination_address.script_pubkey(),
tx_feerate,
&Secp256k1::new(),
)
.unwrap();
bitcoind_client.broadcast_transaction(&spending_tx);
}
Event::ChannelClosed { channel_id, reason, user_channel_id: _ } => {
println!(
"\nEVENT: Channel {} closed due to: {:?}",
hex_utils::hex_str(channel_id),
reason
);
print!("> ");
io::stdout().flush().unwrap();
}
Event::DiscardFunding { .. } => {
// A "real" node should probably "lock" the UTXOs spent in funding transactions until
// the funding transaction either confirms, or this event is generated.
}
}
}
async fn start_ldk() {
let args = match cli::parse_startup_args() {
Ok(user_args) => user_args,
Err(()) => return,
};
// Initialize the LDK data directory if necessary.
let ldk_data_dir = format!("{}/.ldk", args.ldk_storage_dir_path);
fs::create_dir_all(ldk_data_dir.clone()).unwrap();
// Initialize our bitcoind client.
let bitcoind_client = match BitcoindClient::new(
args.bitcoind_rpc_host.clone(),
args.bitcoind_rpc_port,
args.bitcoind_rpc_username.clone(),
args.bitcoind_rpc_password.clone(),
tokio::runtime::Handle::current(),
)
.await
{
Ok(client) => Arc::new(client),
Err(e) => {
println!("Failed to connect to bitcoind client: {}", e);
return;
}
};
// Check that the bitcoind we've connected to is running the network we expect
let bitcoind_chain = bitcoind_client.get_blockchain_info().await.chain;
if bitcoind_chain
!= match args.network {
bitcoin::Network::Bitcoin => "main",
bitcoin::Network::Testnet => "test",
bitcoin::Network::Regtest => "regtest",
bitcoin::Network::Signet => "signet",
} {
println!(
"Chain argument ({}) didn't match bitcoind chain ({})",
args.network, bitcoind_chain
);
return;
}
// ## Setup
// Step 1: Initialize the FeeEstimator
// BitcoindClient implements the FeeEstimator trait, so it'll act as our fee estimator.
let fee_estimator = bitcoind_client.clone();
// Step 2: Initialize the Logger
let logger = Arc::new(FilesystemLogger::new(ldk_data_dir.clone()));
// Step 3: Initialize the BroadcasterInterface
// BitcoindClient implements the BroadcasterInterface trait, so it'll act as our transaction
// broadcaster.
let broadcaster = bitcoind_client.clone();
// Step 4: Initialize Persist
let persister = Arc::new(FilesystemPersister::new(ldk_data_dir.clone()));
// Step 5: Initialize the ChainMonitor
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
None,
broadcaster.clone(),
logger.clone(),
fee_estimator.clone(),
persister.clone(),
));
// Step 6: Initialize the KeysManager
// The key seed that we use to derive the node privkey (that corresponds to the node pubkey) and
// other secret key material.
let keys_seed_path = format!("{}/keys_seed", ldk_data_dir.clone());
let keys_seed = if let Ok(seed) = fs::read(keys_seed_path.clone()) {
assert_eq!(seed.len(), 32);
let mut key = [0; 32];
key.copy_from_slice(&seed);
key
} else {
let mut key = [0; 32];
thread_rng().fill_bytes(&mut key);
match File::create(keys_seed_path.clone()) {
Ok(mut f) => {
f.write_all(&key).expect("Failed to write node keys seed to disk");
f.sync_all().expect("Failed to sync node keys seed to disk");
}
Err(e) => {
println!("ERROR: Unable to create keys seed file {}: {}", keys_seed_path, e);
return;
}
}
key
};
let cur = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
let keys_manager = Arc::new(KeysManager::new(&keys_seed, cur.as_secs(), cur.subsec_nanos()));
// Step 7: Read ChannelMonitor state from disk
let mut channelmonitors = persister.read_channelmonitors(keys_manager.clone()).unwrap();
// Step 8: Initialize the ChannelManager
let mut user_config = UserConfig::default();
user_config.peer_channel_config_limits.force_announced_channel_preference = false;
let mut restarting_node = true;
let (channel_manager_blockhash, channel_manager) = {
if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) {
let mut channel_monitor_mut_references = Vec::new();
for (_, channel_monitor) in channelmonitors.iter_mut() {
channel_monitor_mut_references.push(channel_monitor);
}
let read_args = ChannelManagerReadArgs::new(
keys_manager.clone(),
fee_estimator.clone(),
chain_monitor.clone(),
broadcaster.clone(),
logger.clone(),
user_config,
channel_monitor_mut_references,
);
<(BlockHash, ChannelManager)>::read(&mut f, read_args).unwrap()
} else {
// We're starting a fresh node.
restarting_node = false;
let getinfo_resp = bitcoind_client.get_blockchain_info().await;
let chain_params = ChainParameters {
network: args.network,
best_block: BestBlock::new(
getinfo_resp.latest_blockhash,
getinfo_resp.latest_height as u32,
),
};
let fresh_channel_manager = channelmanager::ChannelManager::new(
fee_estimator.clone(),
chain_monitor.clone(),
broadcaster.clone(),
logger.clone(),
keys_manager.clone(),
user_config,
chain_params,
);
(getinfo_resp.latest_blockhash, fresh_channel_manager)
}
};
// Step 9: Sync ChannelMonitors and ChannelManager to chain tip
let mut chain_listener_channel_monitors = Vec::new();
let mut cache = UnboundedCache::new();
let mut chain_tip: Option<poll::ValidatedBlockHeader> = None;
if restarting_node {
let mut chain_listeners =
vec![(channel_manager_blockhash, &channel_manager as &dyn chain::Listen)];
for (blockhash, channel_monitor) in channelmonitors.drain(..) {
let outpoint = channel_monitor.get_funding_txo().0;
chain_listener_channel_monitors.push((
blockhash,
(channel_monitor, broadcaster.clone(), fee_estimator.clone(), logger.clone()),
outpoint,
));
}
for monitor_listener_info in chain_listener_channel_monitors.iter_mut() {
chain_listeners
.push((monitor_listener_info.0, &monitor_listener_info.1 as &dyn chain::Listen));
}
chain_tip = Some(
init::synchronize_listeners(
&mut bitcoind_client.deref(),
args.network,
&mut cache,
chain_listeners,
)
.await
.unwrap(),
);
}
// Step 10: Give ChannelMonitors to ChainMonitor
for item in chain_listener_channel_monitors.drain(..) {
let channel_monitor = item.1 .0;
let funding_outpoint = item.2;
chain_monitor.watch_channel(funding_outpoint, channel_monitor).unwrap();
}
// Step 11: Optional: Initialize the NetGraphMsgHandler
let genesis = genesis_block(args.network).header.block_hash();
let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone());
let network_graph = Arc::new(disk::read_network(Path::new(&network_graph_path), genesis));
let network_gossip = Arc::new(NetGraphMsgHandler::new(
Arc::clone(&network_graph),
None::<Arc<dyn chain::Access + Send + Sync>>,
logger.clone(),
));
let network_graph_persist = Arc::clone(&network_graph);
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(600));
loop {
interval.tick().await;
if disk::persist_network(Path::new(&network_graph_path), &network_graph_persist)
.is_err()
{
// Persistence errors here are non-fatal as we can just fetch the routing graph
// again later, but they may indicate a disk error which could be fatal elsewhere.
eprintln!(
"Warning: Failed to persist network graph, check your disk and permissions"
);
}
}
});
// Step 12: Initialize the PeerManager
let channel_manager: Arc<ChannelManager> = Arc::new(channel_manager);
let mut ephemeral_bytes = [0; 32];
rand::thread_rng().fill_bytes(&mut ephemeral_bytes);
let lightning_msg_handler = MessageHandler {
chan_handler: channel_manager.clone(),
route_handler: network_gossip.clone(),
};
let peer_manager: Arc<PeerManager> = Arc::new(PeerManager::new(
lightning_msg_handler,
keys_manager.get_node_secret(Recipient::Node).unwrap(),
&ephemeral_bytes,
logger.clone(),
Arc::new(IgnoringMessageHandler {}),
));
// ## Running LDK
// Step 13: Initialize networking
let peer_manager_connection_handler = peer_manager.clone();
let listening_port = args.ldk_peer_listening_port;
tokio::spawn(async move {
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listening_port))
.await
.expect("Failed to bind to listen port - is something else already listening on it?");
loop {
let peer_mgr = peer_manager_connection_handler.clone();
let tcp_stream = listener.accept().await.unwrap().0;
tokio::spawn(async move {
lightning_net_tokio::setup_inbound(
peer_mgr.clone(),
tcp_stream.into_std().unwrap(),
)
.await;
});
}
});
// Step 14: Connect and Disconnect Blocks
if chain_tip.is_none() {
chain_tip =
Some(init::validate_best_block_header(&mut bitcoind_client.deref()).await.unwrap());
}
let channel_manager_listener = channel_manager.clone();
let chain_monitor_listener = chain_monitor.clone();
let bitcoind_block_source = bitcoind_client.clone();
let network = args.network;
tokio::spawn(async move {
let mut derefed = bitcoind_block_source.deref();
let chain_poller = poll::ChainPoller::new(&mut derefed, network);
let chain_listener = (chain_monitor_listener, channel_manager_listener);
let mut spv_client =
SpvClient::new(chain_tip.unwrap(), chain_poller, &mut cache, &chain_listener);
loop {
spv_client.poll_best_tip().await.unwrap();
tokio::time::sleep(Duration::from_secs(1)).await;
}
});
// Step 15: Handle LDK Events
let channel_manager_event_listener = channel_manager.clone();
let keys_manager_listener = keys_manager.clone();
// TODO: persist payment info to disk
let inbound_payments: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new()));
let outbound_payments: PaymentInfoStorage = Arc::new(Mutex::new(HashMap::new()));
let inbound_pmts_for_events = inbound_payments.clone();
let outbound_pmts_for_events = outbound_payments.clone();
let network = args.network;
let bitcoind_rpc = bitcoind_client.clone();
let handle = tokio::runtime::Handle::current();
let event_handler = move |event: &Event| {
handle.block_on(handle_ldk_events(
channel_manager_event_listener.clone(),
bitcoind_rpc.clone(),
keys_manager_listener.clone(),
inbound_pmts_for_events.clone(),
outbound_pmts_for_events.clone(),
network,
event,
));
};
// Step 16: Initialize routing ProbabilisticScorer
let scorer_path = format!("{}/prob_scorer", ldk_data_dir.clone());
let scorer = Arc::new(Mutex::new(disk::read_scorer(
Path::new(&scorer_path),
Arc::clone(&network_graph),
)));
let scorer_persist = Arc::clone(&scorer);
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(600));
loop {
interval.tick().await;
if disk::persist_scorer(Path::new(&scorer_path), &scorer_persist.lock().unwrap())
.is_err()
{
// Persistence errors here are non-fatal as channels will be re-scored as payments
// fail, but they may indicate a disk error which could be fatal elsewhere.
eprintln!("Warning: Failed to persist scorer, check your disk and permissions");
}
}
});
// Step 17: Create InvoicePayer
let router = DefaultRouter::new(network_graph.clone(), logger.clone());
let invoice_payer = Arc::new(InvoicePayer::new(
channel_manager.clone(),
router,
scorer.clone(),
logger.clone(),
event_handler,
payment::RetryAttempts(5),
));
// Step 18: Persist ChannelManager
let data_dir = ldk_data_dir.clone();
let persist_channel_manager_callback =
move |node: &ChannelManager| FilesystemPersister::persist_manager(data_dir.clone(), &*node);
// Step 19: Background Processing
let background_processor = BackgroundProcessor::start(
persist_channel_manager_callback,
invoice_payer.clone(),
chain_monitor.clone(),
channel_manager.clone(),
Some(network_gossip.clone()),
peer_manager.clone(),
logger.clone(),
);
// Regularly reconnect to channel peers.
let connect_cm = Arc::clone(&channel_manager);
let connect_pm = Arc::clone(&peer_manager);
let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
interval.tick().await;
match disk::read_channel_peer_data(Path::new(&peer_data_path)) {
Ok(info) => {
let peers = connect_pm.get_peer_node_ids();
for node_id in connect_cm
.list_channels()
.iter()
.map(|chan| chan.counterparty.node_id)
.filter(|id| !peers.contains(id))
{
for (pubkey, peer_addr) in info.iter() {
if *pubkey == node_id {
let _ = cli::do_connect_peer(
*pubkey,
peer_addr.clone(),
Arc::clone(&connect_pm),
)
.await;
}
}
}
}
Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
}
}
});
// Regularly broadcast our node_announcement. This is only required (or possible) if we have
// some public channels, and is only useful if we have public listen address(es) to announce.
// In a production environment, this should occur only after the announcement of new channels
// to avoid churn in the global network graph.
let chan_manager = Arc::clone(&channel_manager);
let network = args.network;
if !args.ldk_announced_listen_addr.is_empty() {
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(60));
loop {
interval.tick().await;
chan_manager.broadcast_node_announcement(
[0; 3],
args.ldk_announced_node_name,
args.ldk_announced_listen_addr.clone(),
);
}
});
}
// Start the CLI.
cli::poll_for_user_input(
invoice_payer.clone(),
peer_manager.clone(),
channel_manager.clone(),
keys_manager.clone(),
inbound_payments,
outbound_payments,
ldk_data_dir.clone(),
network,
)
.await;
// Stop the background processor.
background_processor.stop().unwrap();
}
#[tokio::main]
pub async fn main() {
start_ldk().await;
}