@@ -4,18 +4,19 @@ mod cli;
4
4
mod convert;
5
5
mod disk;
6
6
mod hex_utils;
7
+ mod sweep;
7
8
8
9
use crate :: bitcoind_client:: BitcoindClient ;
9
10
use crate :: disk:: FilesystemLogger ;
10
11
use bitcoin:: blockdata:: transaction:: Transaction ;
11
12
use bitcoin:: consensus:: encode;
12
13
use bitcoin:: network:: constants:: Network ;
13
- use bitcoin:: secp256k1:: Secp256k1 ;
14
14
use bitcoin:: BlockHash ;
15
15
use bitcoin_bech32:: WitnessProgram ;
16
16
use lightning:: chain;
17
- use lightning:: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
18
- use lightning:: chain:: keysinterface:: { EntropySource , InMemorySigner , KeysManager } ;
17
+ use lightning:: chain:: keysinterface:: {
18
+ EntropySource , InMemorySigner , KeysManager , SpendableOutputDescriptor ,
19
+ } ;
19
20
use lightning:: chain:: { chainmonitor, ChannelMonitorUpdateStatus } ;
20
21
use lightning:: chain:: { Filter , Watch } ;
21
22
use lightning:: events:: { Event , PaymentFailureReason , PaymentPurpose } ;
@@ -30,6 +31,7 @@ use lightning::routing::gossip;
30
31
use lightning:: routing:: gossip:: { NodeId , P2PGossipSync } ;
31
32
use lightning:: routing:: router:: DefaultRouter ;
32
33
use lightning:: util:: config:: UserConfig ;
34
+ use lightning:: util:: persist:: KVStorePersister ;
33
35
use lightning:: util:: ser:: ReadableArgs ;
34
36
use lightning_background_processor:: { process_events_async, GossipSync } ;
35
37
use lightning_block_sync:: init;
@@ -52,6 +54,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
52
54
use std:: sync:: { Arc , Mutex } ;
53
55
use std:: time:: { Duration , SystemTime } ;
54
56
57
+ pub ( crate ) const PENDING_SPENDABLE_OUTPUT_DIR : & ' static str = "pending_spendable_outputs" ;
58
+
55
59
pub ( crate ) enum HTLCStatus {
56
60
Pending ,
57
61
Succeeded ,
@@ -107,7 +111,7 @@ async fn handle_ldk_events(
107
111
channel_manager : & Arc < ChannelManager > , bitcoind_client : & BitcoindClient ,
108
112
network_graph : & NetworkGraph , keys_manager : & KeysManager ,
109
113
inbound_payments : & PaymentInfoStorage , outbound_payments : & PaymentInfoStorage ,
110
- network : Network , event : Event ,
114
+ persister : & Arc < FilesystemPersister > , network : Network , event : Event ,
111
115
) {
112
116
match event {
113
117
Event :: FundingGenerationReady {
@@ -331,20 +335,23 @@ async fn handle_ldk_events(
331
335
} ) ;
332
336
}
333
337
Event :: SpendableOutputs { outputs } => {
334
- let destination_address = bitcoind_client. get_new_address ( ) . await ;
335
- let output_descriptors = & outputs. iter ( ) . map ( |a| a) . collect :: < Vec < _ > > ( ) ;
336
- let tx_feerate =
337
- bitcoind_client. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
338
- let spending_tx = keys_manager
339
- . spend_spendable_outputs (
340
- output_descriptors,
341
- Vec :: new ( ) ,
342
- destination_address. script_pubkey ( ) ,
343
- tx_feerate,
344
- & Secp256k1 :: new ( ) ,
345
- )
346
- . unwrap ( ) ;
347
- bitcoind_client. broadcast_transaction ( & spending_tx) ;
338
+ // SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
339
+ // of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
340
+ // other descriptors are not currently ever regenerated for you by LDK. Once we return
341
+ // from this method, the descriptor will be gone, and you may lose track of some funds.
342
+ //
343
+ // Here we simply persist them to disk, with a background task running which will try
344
+ // to spend them regularly (possibly duplicatively/RBF'ing them). These can just be
345
+ // treated as normal funds where possible - they are only spendable by us and there is
346
+ // no rush to claim them.
347
+ for output in outputs {
348
+ let key = hex_utils:: hex_str ( & keys_manager. get_secure_random_bytes ( ) ) ;
349
+ // Note that if the type here changes our read code needs to change as well.
350
+ let output: SpendableOutputDescriptor = output;
351
+ persister
352
+ . persist ( & format ! ( "{}/{}" , PENDING_SPENDABLE_OUTPUT_DIR , key) , & output)
353
+ . unwrap ( ) ;
354
+ }
348
355
}
349
356
Event :: ChannelPending { channel_id, counterparty_node_id, .. } => {
350
357
println ! (
@@ -693,6 +700,7 @@ async fn start_ldk() {
693
700
let keys_manager_event_listener = Arc :: clone ( & keys_manager) ;
694
701
let inbound_payments_event_listener = Arc :: clone ( & inbound_payments) ;
695
702
let outbound_payments_event_listener = Arc :: clone ( & outbound_payments) ;
703
+ let persister_event_listener = Arc :: clone ( & persister) ;
696
704
let network = args. network ;
697
705
let event_handler = move |event : Event | {
698
706
let channel_manager_event_listener = Arc :: clone ( & channel_manager_event_listener) ;
@@ -701,6 +709,7 @@ async fn start_ldk() {
701
709
let keys_manager_event_listener = Arc :: clone ( & keys_manager_event_listener) ;
702
710
let inbound_payments_event_listener = Arc :: clone ( & inbound_payments_event_listener) ;
703
711
let outbound_payments_event_listener = Arc :: clone ( & outbound_payments_event_listener) ;
712
+ let persister_event_listener = Arc :: clone ( & persister_event_listener) ;
704
713
async move {
705
714
handle_ldk_events (
706
715
& channel_manager_event_listener,
@@ -709,6 +718,7 @@ async fn start_ldk() {
709
718
& keys_manager_event_listener,
710
719
& inbound_payments_event_listener,
711
720
& outbound_payments_event_listener,
721
+ & persister_event_listener,
712
722
network,
713
723
event,
714
724
)
@@ -722,7 +732,7 @@ async fn start_ldk() {
722
732
// Step 20: Background Processing
723
733
let ( bp_exit, bp_exit_check) = tokio:: sync:: watch:: channel ( ( ) ) ;
724
734
let background_processor = tokio:: spawn ( process_events_async (
725
- persister,
735
+ Arc :: clone ( & persister) ,
726
736
event_handler,
727
737
chain_monitor. clone ( ) ,
728
738
channel_manager. clone ( ) ,
@@ -781,24 +791,38 @@ async fn start_ldk() {
781
791
} ) ;
782
792
783
793
// Regularly broadcast our node_announcement. This is only required (or possible) if we have
784
- // some public channels, and is only useful if we have public listen address(es) to announce.
785
- // In a production environment, this should occur only after the announcement of new channels
786
- // to avoid churn in the global network graph.
794
+ // some public channels.
787
795
let peer_man = Arc :: clone ( & peer_manager) ;
796
+ let chan_man = Arc :: clone ( & channel_manager) ;
788
797
let network = args. network ;
789
- if !args. ldk_announced_listen_addr . is_empty ( ) {
790
- tokio:: spawn ( async move {
791
- let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 60 ) ) ;
792
- loop {
793
- interval. tick ( ) . await ;
798
+ tokio:: spawn ( async move {
799
+ // First wait a minute until we have some peers and maybe have opened a channel.
800
+ tokio:: time:: sleep ( Duration :: from_secs ( 60 ) ) . await ;
801
+ // Then, update our announcement once an hour to keep it fresh but avoid unnecessary churn
802
+ // in the global gossip network.
803
+ let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 3600 ) ) ;
804
+ loop {
805
+ interval. tick ( ) . await ;
806
+ // Don't bother trying to announce if we don't have any public channls, though our
807
+ // peers should drop such an announcement anyway. Note that announcement may not
808
+ // propagate until we have a channel with 6+ confirmations.
809
+ if chan_man. list_channels ( ) . iter ( ) . any ( |chan| chan. is_public ) {
794
810
peer_man. broadcast_node_announcement (
795
811
[ 0 ; 3 ] ,
796
812
args. ldk_announced_node_name ,
797
813
args. ldk_announced_listen_addr . clone ( ) ,
798
814
) ;
799
815
}
800
- } ) ;
801
- }
816
+ }
817
+ } ) ;
818
+
819
+ tokio:: spawn ( sweep:: periodic_sweep (
820
+ ldk_data_dir. clone ( ) ,
821
+ Arc :: clone ( & keys_manager) ,
822
+ Arc :: clone ( & logger) ,
823
+ Arc :: clone ( & persister) ,
824
+ Arc :: clone ( & bitcoind_client) ,
825
+ ) ) ;
802
826
803
827
// Start the CLI.
804
828
cli:: poll_for_user_input (
@@ -809,7 +833,7 @@ async fn start_ldk() {
809
833
Arc :: clone ( & onion_messenger) ,
810
834
inbound_payments,
811
835
outbound_payments,
812
- ldk_data_dir. clone ( ) ,
836
+ ldk_data_dir,
813
837
network,
814
838
Arc :: clone ( & logger) ,
815
839
)
0 commit comments