1 use std::io::{Read, Seek, SeekFrom};
2 use std::path::PathBuf;
4 use std::time::Duration;
7 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
8 use lightning::sign::{EntropySource, KeysManager, SpendableOutputDescriptor};
9 use lightning::util::logger::Logger;
10 use lightning::util::persist::KVStorePersister;
11 use lightning::util::ser::{Readable, WithoutLength};
13 use bitcoin::secp256k1::Secp256k1;
14 use bitcoin::{LockTime, PackedLockTime};
17 use crate::BitcoindClient;
18 use crate::ChannelManager;
19 use crate::FilesystemLogger;
20 use crate::FilesystemPersister;
22 /// If we have any pending claimable outputs, we should slowly sweep them to our Bitcoin Core
23 /// wallet. We technically don't need to do this - they're ours to spend when we want and can just
24 /// use them to build new transactions instead, but we cannot feed them direclty into Bitcoin
25 /// Core's wallet so we have to sweep.
27 /// Note that this is unececssary for [`SpendableOutputDescriptor::StaticOutput`]s, which *do* have
28 /// an associated secret key we could simply import into Bitcoin Core's wallet, but for consistency
29 /// we don't do that here either.
30 pub(crate) async fn periodic_sweep(
31 ldk_data_dir: String, keys_manager: Arc<KeysManager>, logger: Arc<FilesystemLogger>,
32 persister: Arc<FilesystemPersister>, bitcoind_client: Arc<BitcoindClient>,
33 channel_manager: Arc<ChannelManager>,
35 // Regularly claim outputs which are exclusively spendable by us and send them to Bitcoin Core.
36 // Note that if you more tightly integrate your wallet with LDK you may not need to do this -
37 // these outputs can just be treated as normal outputs during coin selection.
38 let pending_spendables_dir =
39 format!("{}/{}", crate::PENDING_SPENDABLE_OUTPUT_DIR, ldk_data_dir);
40 let processing_spendables_dir = format!("{}/processing_spendable_outputs", ldk_data_dir);
41 let spendables_dir = format!("{}/spendable_outputs", ldk_data_dir);
43 // We batch together claims of all spendable outputs generated each day, however only after
44 // batching any claims of spendable outputs which were generated prior to restart. On a mobile
45 // device we likely won't ever be online for more than a minute, so we have to ensure we sweep
46 // any pending claims on startup, but for an always-online node you may wish to sweep even less
47 // frequently than this (or move the interval await to the top of the loop)!
49 // There is no particular rush here, we just have to ensure funds are availably by the time we
50 // need to send funds.
51 let mut interval = tokio::time::interval(Duration::from_secs(60 * 60 * 24));
54 interval.tick().await; // Note that the first tick completes immediately
55 if let Ok(dir_iter) = fs::read_dir(&pending_spendables_dir) {
56 // Move any spendable descriptors from pending folder so that we don't have any
57 // races with new files being added.
58 for file_res in dir_iter {
59 let file = file_res.unwrap();
60 // Only move a file if its a 32-byte-hex'd filename, otherwise it might be a
62 if file.file_name().len() == 64 {
63 fs::create_dir_all(&processing_spendables_dir).unwrap();
64 let mut holding_path = PathBuf::new();
65 holding_path.push(&processing_spendables_dir);
66 holding_path.push(&file.file_name());
67 fs::rename(file.path(), holding_path).unwrap();
70 // Now concatenate all the pending files we moved into one file in the
71 // `spendable_outputs` directory and drop the processing directory.
72 let mut outputs = Vec::new();
73 if let Ok(processing_iter) = fs::read_dir(&processing_spendables_dir) {
74 for file_res in processing_iter {
75 outputs.append(&mut fs::read(file_res.unwrap().path()).unwrap());
78 if !outputs.is_empty() {
79 let key = hex_utils::hex_str(&keys_manager.get_secure_random_bytes());
81 .persist(&format!("spendable_outputs/{}", key), &WithoutLength(&outputs))
83 fs::remove_dir_all(&processing_spendables_dir).unwrap();
86 // Iterate over all the sets of spendable outputs in `spendables_dir` and try to claim
88 // Note that here we try to claim each set of spendable outputs over and over again
89 // forever, even long after its been claimed. While this isn't an issue per se, in practice
90 // you may wish to track when the claiming transaction has confirmed and remove the
91 // spendable outputs set. You may also wish to merge groups of unspent spendable outputs to
93 if let Ok(dir_iter) = fs::read_dir(&spendables_dir) {
94 for file_res in dir_iter {
95 let mut outputs: Vec<SpendableOutputDescriptor> = Vec::new();
96 let mut file = fs::File::open(file_res.unwrap().path()).unwrap();
98 // Check if there are any bytes left to read, and if so read a descriptor.
99 match file.read_exact(&mut [0; 1]) {
101 file.seek(SeekFrom::Current(-1)).unwrap();
103 Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
104 Err(e) => Err(e).unwrap(),
106 outputs.push(Readable::read(&mut file).unwrap());
108 let destination_address = bitcoind_client.get_new_address().await;
109 let output_descriptors = &outputs.iter().map(|a| a).collect::<Vec<_>>();
111 bitcoind_client.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
113 // We set nLockTime to the current height to discourage fee sniping.
114 let cur_height = channel_manager.current_best_block().height();
115 let locktime: PackedLockTime =
116 LockTime::from_height(cur_height).map_or(PackedLockTime::ZERO, |l| l.into());
118 if let Ok(spending_tx) = keys_manager.spend_spendable_outputs(
121 destination_address.script_pubkey(),
126 // Note that, most likely, we've already sweeped this set of outputs
127 // and they're already confirmed on-chain, so this broadcast will fail.
128 bitcoind_client.broadcast_transactions(&[&spending_tx]);
130 lightning::log_error!(
132 "Failed to sweep spendable outputs! This may indicate the outputs are dust. Will try again in a day.");