+mod args;
pub mod bitcoind_client;
mod cli;
mod convert;
mod disk;
mod hex_utils;
+mod sweep;
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};
-use lightning::chain::{BestBlock, Filter, Watch};
-use lightning::ln::channelmanager;
+use disk::{INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME};
+use lightning::chain::{chainmonitor, ChannelMonitorUpdateStatus};
+use lightning::chain::{Filter, Watch};
+use lightning::events::{Event, PaymentFailureReason, PaymentPurpose};
+use lightning::ln::channelmanager::{self, RecentPaymentDetails};
use lightning::ln::channelmanager::{
ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager,
};
+use lightning::ln::msgs::DecodeError;
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::Scorer;
+use lightning::onion_message::{DefaultMessageRouter, SimpleArcOnionMessenger};
+use lightning::routing::gossip;
+use lightning::routing::gossip::{NodeId, P2PGossipSync};
+use lightning::routing::router::DefaultRouter;
+use lightning::routing::scoring::ProbabilisticScoringFeeParameters;
+use lightning::sign::{EntropySource, InMemorySigner, KeysManager, SpendableOutputDescriptor};
use lightning::util::config::UserConfig;
-use lightning::util::events::{Event, PaymentPurpose};
-use lightning::util::ser::ReadableArgs;
-use lightning_background_processor::BackgroundProcessor;
+use lightning::util::persist::KVStorePersister;
+use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
+use lightning::{chain, impl_writeable_tlv_based, impl_writeable_tlv_based_enum};
+use lightning_background_processor::{process_events_async, GossipSync};
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::convert::TryInto;
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::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
+pub(crate) const PENDING_SPENDABLE_OUTPUT_DIR: &'static str = "pending_spendable_outputs";
+
+#[derive(Copy, Clone)]
pub(crate) enum HTLCStatus {
Pending,
Succeeded,
Failed,
}
+impl_writeable_tlv_based_enum!(HTLCStatus,
+ (0, Pending) => {},
+ (1, Succeeded) => {},
+ (2, Failed) => {};
+);
+
pub(crate) struct MillisatAmount(Option<u64>);
impl fmt::Display for MillisatAmount {
}
}
+impl Readable for MillisatAmount {
+ fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+ let amt: Option<u64> = Readable::read(r)?;
+ Ok(MillisatAmount(amt))
+ }
+}
+
+impl Writeable for MillisatAmount {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), std::io::Error> {
+ self.0.write(w)
+ }
+}
+
pub(crate) struct PaymentInfo {
preimage: Option<PaymentPreimage>,
secret: Option<PaymentSecret>,
amt_msat: MillisatAmount,
}
-pub(crate) type PaymentInfoStorage = Arc<Mutex<HashMap<PaymentHash, PaymentInfo>>>;
+impl_writeable_tlv_based!(PaymentInfo, {
+ (0, preimage, required),
+ (2, secret, required),
+ (4, status, required),
+ (6, amt_msat, required),
+});
+
+pub(crate) struct PaymentInfoStorage {
+ payments: HashMap<PaymentHash, PaymentInfo>,
+}
+
+impl_writeable_tlv_based!(PaymentInfoStorage, {
+ (0, payments, required),
+});
type ChainMonitor = chainmonitor::ChainMonitor<
InMemorySigner,
ChainMonitor,
BitcoindClient,
BitcoindClient,
- dyn chain::Access + Send + Sync,
+ BitcoindClient,
FilesystemLogger,
>;
pub(crate) type ChannelManager =
SimpleArcChannelManager<ChainMonitor, BitcoindClient, BitcoindClient, FilesystemLogger>;
-pub(crate) type InvoicePayer<E> = payment::InvoicePayer<
- Arc<ChannelManager>,
- Router,
- Arc<Mutex<Scorer>>,
- Arc<FilesystemLogger>,
- E,
->;
+pub(crate) type NetworkGraph = gossip::NetworkGraph<Arc<FilesystemLogger>>;
-type Router = DefaultRouter<Arc<NetworkGraph>, Arc<FilesystemLogger>>;
+type OnionMessenger = SimpleArcOnionMessenger<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,
+ channel_manager: &Arc<ChannelManager>, bitcoind_client: &BitcoindClient,
+ network_graph: &NetworkGraph, keys_manager: &KeysManager,
+ inbound_payments: Arc<Mutex<PaymentInfoStorage>>,
+ outbound_payments: Arc<Mutex<PaymentInfoStorage>>, persister: &Arc<FilesystemPersister>,
+ network: Network, event: Event,
) {
match event {
Event::FundingGenerationReady {
temporary_channel_id,
+ counterparty_node_id,
channel_value_satoshis,
output_script,
..
Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
Network::Testnet => bitcoin_bech32::constants::Network::Testnet,
Network::Regtest => bitcoin_bech32::constants::Network::Regtest,
- Network::Signet => panic!("Signet unsupported"),
+ 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);
+ 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
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)
+ .funding_transaction_generated(
+ &temporary_channel_id,
+ &counterparty_node_id,
+ final_tx,
+ )
.is_err()
{
println!(
io::stdout().flush().unwrap();
}
}
- Event::PaymentReceived { payment_hash, purpose, amt, .. } => {
- let mut payments = inbound_payments.lock().unwrap();
+ Event::PaymentClaimable {
+ payment_hash,
+ purpose,
+ amount_msat,
+ receiver_node_id: _,
+ via_channel_id: _,
+ via_user_channel_id: _,
+ claim_deadline: _,
+ onion_fields: _,
+ counterparty_skimmed_fee_msat: _,
+ } => {
+ println!(
+ "\nEVENT: received payment from payment hash {} of {} millisatoshis",
+ hex_utils::hex_str(&payment_hash.0),
+ amount_msat,
+ );
+ print!("> ");
+ io::stdout().flush().unwrap();
+ let payment_preimage = match purpose {
+ PaymentPurpose::InvoicePayment { payment_preimage, .. } => payment_preimage,
+ PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
+ };
+ channel_manager.claim_funds(payment_preimage.unwrap());
+ }
+ Event::PaymentClaimed { payment_hash, purpose, amount_msat, receiver_node_id: _ } => {
+ println!(
+ "\nEVENT: claimed payment from payment hash {} of {} millisatoshis",
+ hex_utils::hex_str(&payment_hash.0),
+ amount_msat,
+ );
+ print!("> ");
+ io::stdout().flush().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
+ (payment_preimage, Some(payment_secret))
}
- _ => HTLCStatus::Failed,
+ PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None),
};
- match payments.entry(*payment_hash) {
+ let mut inbound = inbound_payments.lock().unwrap();
+ match inbound.payments.entry(payment_hash) {
Entry::Occupied(mut e) => {
let payment = e.get_mut();
- payment.status = status;
+ payment.status = HTLCStatus::Succeeded;
payment.preimage = payment_preimage;
payment.secret = payment_secret;
}
e.insert(PaymentInfo {
preimage: payment_preimage,
secret: payment_secret,
- status,
- amt_msat: MillisatAmount(Some(*amt)),
+ status: HTLCStatus::Succeeded,
+ amt_msat: MillisatAmount(Some(amount_msat)),
});
}
}
+ persister.persist(INBOUND_PAYMENTS_FNAME, &*inbound).unwrap();
}
- Event::PaymentSent { payment_preimage, payment_hash, .. } => {
- let mut payments = outbound_payments.lock().unwrap();
- for (hash, payment) in payments.iter_mut() {
- if *hash == *payment_hash {
- payment.preimage = Some(*payment_preimage);
+ Event::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
+ let mut outbound = outbound_payments.lock().unwrap();
+ for (hash, payment) in outbound.payments.iter_mut() {
+ if *hash == payment_hash {
+ payment.preimage = Some(payment_preimage);
payment.status = HTLCStatus::Succeeded;
println!(
- "\nEVENT: successfully sent payment of {} millisatoshis from \
+ "\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)
);
io::stdout().flush().unwrap();
}
}
+ persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound).unwrap();
+ }
+ Event::OpenChannelRequest { .. } => {
+ // Unreachable, we don't set manually_accept_inbound_channels
}
Event::PaymentPathSuccessful { .. } => {}
Event::PaymentPathFailed { .. } => {}
- Event::PaymentFailed { payment_hash, .. } => {
+ Event::ProbeSuccessful { .. } => {}
+ Event::ProbeFailed { .. } => {}
+ Event::PaymentFailed { payment_hash, reason, .. } => {
print!(
- "\nEVENT: Failed to send payment to payment hash {:?}: exhausted payment retry attempts",
- hex_utils::hex_str(&payment_hash.0)
+ "\nEVENT: Failed to send payment to payment hash {:?}: {:?}",
+ hex_utils::hex_str(&payment_hash.0),
+ if let Some(r) = reason { r } else { PaymentFailureReason::RetriesExhausted }
);
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();
+ let mut outbound = outbound_payments.lock().unwrap();
+ if outbound.payments.contains_key(&payment_hash) {
+ let payment = outbound.payments.get_mut(&payment_hash).unwrap();
payment.status = HTLCStatus::Failed;
}
+ persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound).unwrap();
}
- Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx } => {
- let from_onchain_str = if *claim_from_onchain_tx {
+ Event::PaymentForwarded {
+ prev_channel_id,
+ next_channel_id,
+ fee_earned_msat,
+ claim_from_onchain_tx,
+ outbound_amount_forwarded_msat,
+ } => {
+ let read_only_network_graph = network_graph.read_only();
+ let nodes = read_only_network_graph.nodes();
+ let channels = channel_manager.list_channels();
+
+ let node_str = |channel_id: &Option<[u8; 32]>| match channel_id {
+ None => String::new(),
+ Some(channel_id) => match channels.iter().find(|c| c.channel_id == *channel_id) {
+ None => String::new(),
+ Some(channel) => {
+ match nodes.get(&NodeId::from_pubkey(&channel.counterparty.node_id)) {
+ None => "private node".to_string(),
+ Some(node) => match &node.announcement_info {
+ None => "unnamed node".to_string(),
+ Some(announcement) => {
+ format!("node {}", announcement.alias)
+ }
+ },
+ }
+ }
+ },
+ };
+ let channel_str = |channel_id: &Option<[u8; 32]>| {
+ channel_id
+ .map(|channel_id| format!(" with channel {}", hex_utils::hex_str(&channel_id)))
+ .unwrap_or_default()
+ };
+ let from_prev_str =
+ format!(" from {}{}", node_str(&prev_channel_id), channel_str(&prev_channel_id));
+ let to_next_str =
+ format!(" to {}{}", node_str(&next_channel_id), channel_str(&next_channel_id));
+
+ let from_onchain_str = if claim_from_onchain_tx {
"from onchain downstream claim"
} else {
"from HTLC fulfill message"
};
+ let amt_args = if let Some(v) = outbound_amount_forwarded_msat {
+ format!("{}", v)
+ } else {
+ "?".to_string()
+ };
if let Some(fee_earned) = fee_earned_msat {
println!(
- "\nEVENT: Forwarded payment, earning {} msat {}",
- fee_earned, from_onchain_str
+ "\nEVENT: Forwarded payment for {} msat{}{}, earning {} msat {}",
+ amt_args, from_prev_str, to_next_str, fee_earned, from_onchain_str
);
} else {
- println!("\nEVENT: Forwarded payment, claiming onchain {}", from_onchain_str);
+ println!(
+ "\nEVENT: Forwarded payment for {} msat{}{}, claiming onchain {}",
+ amt_args, from_prev_str, to_next_str, from_onchain_str
+ );
}
print!("> ");
io::stdout().flush().unwrap();
}
+ Event::HTLCHandlingFailed { .. } => {}
Event::PendingHTLCsForwardable { time_forwardable } => {
let forwarding_channel_manager = channel_manager.clone();
let min = time_forwardable.as_millis() as u64;
});
}
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);
+ // SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
+ // of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
+ // other descriptors are not currently ever regenerated for you by LDK. Once we return
+ // from this method, the descriptor will be gone, and you may lose track of some funds.
+ //
+ // Here we simply persist them to disk, with a background task running which will try
+ // to spend them regularly (possibly duplicatively/RBF'ing them). These can just be
+ // treated as normal funds where possible - they are only spendable by us and there is
+ // no rush to claim them.
+ for output in outputs {
+ let key = hex_utils::hex_str(&keys_manager.get_secure_random_bytes());
+ // Note that if the type here changes our read code needs to change as well.
+ let output: SpendableOutputDescriptor = output;
+ persister
+ .persist(&format!("{}/{}", PENDING_SPENDABLE_OUTPUT_DIR, key), &output)
+ .unwrap();
+ }
+ }
+ Event::ChannelPending { channel_id, counterparty_node_id, .. } => {
+ println!(
+ "\nEVENT: Channel {} with peer {} is pending awaiting funding lock-in!",
+ hex_utils::hex_str(&channel_id),
+ hex_utils::hex_str(&counterparty_node_id.serialize()),
+ );
+ print!("> ");
+ io::stdout().flush().unwrap();
+ }
+ Event::ChannelReady {
+ ref channel_id,
+ user_channel_id: _,
+ ref counterparty_node_id,
+ channel_type: _,
+ } => {
+ println!(
+ "\nEVENT: Channel {} with peer {} is ready to be used!",
+ hex_utils::hex_str(channel_id),
+ hex_utils::hex_str(&counterparty_node_id.serialize()),
+ );
+ print!("> ");
+ io::stdout().flush().unwrap();
}
Event::ChannelClosed { channel_id, reason, user_channel_id: _ } => {
println!(
"\nEVENT: Channel {} closed due to: {:?}",
- hex_utils::hex_str(channel_id),
+ hex_utils::hex_str(&channel_id),
reason
);
print!("> ");
// A "real" node should probably "lock" the UTXOs spent in funding transactions until
// the funding transaction either confirms, or this event is generated.
}
+ Event::HTLCIntercepted { .. } => {}
+ Event::BumpTransaction(_) => {}
}
}
async fn start_ldk() {
- let args = match cli::parse_startup_args() {
+ let args = match args::parse_startup_args() {
Ok(user_args) => user_args,
Err(()) => return,
};
let ldk_data_dir = format!("{}/.ldk", args.ldk_storage_dir_path);
fs::create_dir_all(ldk_data_dir.clone()).unwrap();
+ // ## Setup
+ // Step 1: Initialize the Logger
+ let logger = Arc::new(FilesystemLogger::new(ldk_data_dir.clone()));
+
// Initialize our bitcoind client.
let bitcoind_client = match BitcoindClient::new(
args.bitcoind_rpc_host.clone(),
args.bitcoind_rpc_username.clone(),
args.bitcoind_rpc_password.clone(),
tokio::runtime::Handle::current(),
+ Arc::clone(&logger),
)
.await
{
return;
}
- // ## Setup
- // Step 1: Initialize the FeeEstimator
+ // Step 2: 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
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");
+ Write::write_all(&mut f, &key).expect("Failed to write node keys seed to disk");
f.sync_all().expect("Failed to sync node keys seed to disk");
}
Err(e) => {
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();
+ let mut channelmonitors =
+ persister.read_channelmonitors(keys_manager.clone(), keys_manager.clone()).unwrap();
+
+ // Step 8: Poll for the best chain tip, which may be used by the channel manager & spv client
+ let polled_chain_tip = init::validate_best_block_header(bitcoind_client.as_ref())
+ .await
+ .expect("Failed to fetch best block header and best block");
+
+ // Step 9: Initialize routing ProbabilisticScorer
+ let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone());
+ let network_graph =
+ Arc::new(disk::read_network(Path::new(&network_graph_path), args.network, logger.clone()));
- // Step 8: Initialize the ChannelManager
+ let scorer_path = format!("{}/scorer", ldk_data_dir.clone());
+ let scorer = Arc::new(Mutex::new(disk::read_scorer(
+ Path::new(&scorer_path),
+ Arc::clone(&network_graph),
+ Arc::clone(&logger),
+ )));
+
+ // Step 10: Create Router
+ let scoring_fee_params = ProbabilisticScoringFeeParameters::default();
+ let router = Arc::new(DefaultRouter::new(
+ network_graph.clone(),
+ logger.clone(),
+ keys_manager.get_secure_random_bytes(),
+ scorer.clone(),
+ scoring_fee_params,
+ ));
+
+ // Step 11: Initialize the ChannelManager
let mut user_config = UserConfig::default();
- user_config.peer_channel_config_limits.force_announced_channel_preference = false;
+ user_config.channel_handshake_limits.force_announced_channel_preference = false;
let mut restarting_node = true;
- let (channel_manager_blockhash, mut channel_manager) = {
+ 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(),
+ keys_manager.clone(),
keys_manager.clone(),
fee_estimator.clone(),
chain_monitor.clone(),
broadcaster.clone(),
+ router,
logger.clone(),
user_config,
channel_monitor_mut_references,
} 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 polled_best_block = polled_chain_tip.to_best_block();
+ let polled_best_block_hash = polled_best_block.block_hash();
+ let chain_params =
+ ChainParameters { network: args.network, best_block: polled_best_block };
let fresh_channel_manager = channelmanager::ChannelManager::new(
fee_estimator.clone(),
chain_monitor.clone(),
broadcaster.clone(),
+ router,
logger.clone(),
keys_manager.clone(),
+ keys_manager.clone(),
+ keys_manager.clone(),
user_config,
chain_params,
+ cur.as_secs() as u32,
);
- (getinfo_resp.latest_blockhash, fresh_channel_manager)
+ (polled_best_block_hash, fresh_channel_manager)
}
};
- // Step 9: Sync ChannelMonitors and ChannelManager to chain tip
+ // Step 12: 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, &mut channel_manager as &mut dyn chain::Listen)];
+ let chain_tip = if restarting_node {
+ let mut chain_listeners = vec![(
+ channel_manager_blockhash,
+ &channel_manager as &(dyn chain::Listen + Send + Sync),
+ )];
for (blockhash, channel_monitor) in channelmonitors.drain(..) {
let outpoint = channel_monitor.get_funding_txo().0;
for monitor_listener_info in chain_listener_channel_monitors.iter_mut() {
chain_listeners.push((
monitor_listener_info.0,
- &mut monitor_listener_info.1 as &mut dyn chain::Listen,
+ &monitor_listener_info.1 as &(dyn chain::Listen + Send + Sync),
));
}
- chain_tip = Some(
- init::synchronize_listeners(
- &mut bitcoind_client.deref(),
- args.network,
- &mut cache,
- chain_listeners,
- )
- .await
- .unwrap(),
- );
- }
- // Step 10: Give ChannelMonitors to ChainMonitor
+ init::synchronize_listeners(
+ bitcoind_client.as_ref(),
+ args.network,
+ &mut cache,
+ chain_listeners,
+ )
+ .await
+ .unwrap()
+ } else {
+ polled_chain_tip
+ };
+
+ // Step 13: 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();
+ assert_eq!(
+ chain_monitor.watch_channel(funding_outpoint, channel_monitor),
+ ChannelMonitorUpdateStatus::Completed
+ );
}
- // 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(
+ // Step 14: Optional: Initialize the P2PGossipSync
+ let gossip_sync = Arc::new(P2PGossipSync::new(
Arc::clone(&network_graph),
- None::<Arc<dyn chain::Access + Send + Sync>>,
+ None::<Arc<BitcoindClient>>,
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
+ // Step 15: Initialize the PeerManager
let channel_manager: Arc<ChannelManager> = Arc::new(channel_manager);
+ let onion_messenger: Arc<OnionMessenger> = Arc::new(OnionMessenger::new(
+ Arc::clone(&keys_manager),
+ Arc::clone(&keys_manager),
+ Arc::clone(&logger),
+ Arc::new(DefaultMessageRouter {}),
+ IgnoringMessageHandler {},
+ IgnoringMessageHandler {},
+ ));
let mut ephemeral_bytes = [0; 32];
+ let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
rand::thread_rng().fill_bytes(&mut ephemeral_bytes);
let lightning_msg_handler = MessageHandler {
chan_handler: channel_manager.clone(),
- route_handler: network_gossip.clone(),
+ route_handler: gossip_sync.clone(),
+ onion_message_handler: onion_messenger.clone(),
+ custom_message_handler: IgnoringMessageHandler {},
};
let peer_manager: Arc<PeerManager> = Arc::new(PeerManager::new(
lightning_msg_handler,
- keys_manager.get_node_secret(),
+ current_time.try_into().unwrap(),
&ephemeral_bytes,
logger.clone(),
- Arc::new(IgnoringMessageHandler {}),
+ Arc::clone(&keys_manager),
));
// ## Running LDK
- // Step 13: Initialize networking
+ // Step 16: Initialize networking
let peer_manager_connection_handler = peer_manager.clone();
let listening_port = args.ldk_peer_listening_port;
+ let stop_listen_connect = Arc::new(AtomicBool::new(false));
+ let stop_listen = Arc::clone(&stop_listen_connect);
tokio::spawn(async move {
- let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", listening_port))
+ let listener = tokio::net::TcpListener::bind(format!("[::]:{}", 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;
+ if stop_listen.load(Ordering::Acquire) {
+ return;
+ }
tokio::spawn(async move {
lightning_net_tokio::setup_inbound(
peer_mgr.clone(),
}
});
- // 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());
- }
+ // Step 17: Connect and Disconnect Blocks
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_poller = poll::ChainPoller::new(bitcoind_block_source.as_ref(), 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);
+ let mut spv_client = SpvClient::new(chain_tip, 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 inbound_payments = Arc::new(Mutex::new(disk::read_payment_info(Path::new(&format!(
+ "{}/{}",
+ ldk_data_dir, INBOUND_PAYMENTS_FNAME
+ )))));
+ let outbound_payments = Arc::new(Mutex::new(disk::read_payment_info(Path::new(&format!(
+ "{}/{}",
+ ldk_data_dir, OUTBOUND_PAYMENTS_FNAME
+ )))));
+ let recent_payments_payment_hashes = channel_manager
+ .list_recent_payments()
+ .into_iter()
+ .filter_map(|p| match p {
+ RecentPaymentDetails::Pending { payment_hash, .. } => Some(payment_hash),
+ RecentPaymentDetails::Fulfilled { payment_hash } => payment_hash,
+ RecentPaymentDetails::Abandoned { payment_hash } => Some(payment_hash),
+ })
+ .collect::<Vec<PaymentHash>>();
+ for (payment_hash, payment_info) in outbound_payments
+ .lock()
+ .unwrap()
+ .payments
+ .iter_mut()
+ .filter(|(_, i)| matches!(i.status, HTLCStatus::Pending))
+ {
+ if !recent_payments_payment_hashes.contains(payment_hash) {
+ payment_info.status = HTLCStatus::Failed;
+ }
+ }
+ persister.persist(OUTBOUND_PAYMENTS_FNAME, &*outbound_payments.lock().unwrap()).unwrap();
+
+ // Step 18: Handle LDK Events
+ let channel_manager_event_listener = Arc::clone(&channel_manager);
+ let bitcoind_client_event_listener = Arc::clone(&bitcoind_client);
+ let network_graph_event_listener = Arc::clone(&network_graph);
+ let keys_manager_event_listener = Arc::clone(&keys_manager);
+ let inbound_payments_event_listener = Arc::clone(&inbound_payments);
+ let outbound_payments_event_listener = Arc::clone(&outbound_payments);
+ let persister_event_listener = Arc::clone(&persister);
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,
- ));
+ let event_handler = move |event: Event| {
+ let channel_manager_event_listener = Arc::clone(&channel_manager_event_listener);
+ let bitcoind_client_event_listener = Arc::clone(&bitcoind_client_event_listener);
+ let network_graph_event_listener = Arc::clone(&network_graph_event_listener);
+ let keys_manager_event_listener = Arc::clone(&keys_manager_event_listener);
+ let inbound_payments_event_listener = Arc::clone(&inbound_payments_event_listener);
+ let outbound_payments_event_listener = Arc::clone(&outbound_payments_event_listener);
+ let persister_event_listener = Arc::clone(&persister_event_listener);
+ async move {
+ handle_ldk_events(
+ &channel_manager_event_listener,
+ &bitcoind_client_event_listener,
+ &network_graph_event_listener,
+ &keys_manager_event_listener,
+ inbound_payments_event_listener,
+ outbound_payments_event_listener,
+ &persister_event_listener,
+ network,
+ event,
+ )
+ .await;
+ }
};
- // Step 16: Initialize routing Scorer
- let scorer_path = format!("{}/scorer", ldk_data_dir.clone());
- let scorer = Arc::new(Mutex::new(disk::read_scorer(Path::new(&scorer_path))));
- 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 19: Persist ChannelManager and NetworkGraph
+ let persister = Arc::new(FilesystemPersister::new(ldk_data_dir.clone()));
- // 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(),
+ // Step 20: Background Processing
+ let (bp_exit, bp_exit_check) = tokio::sync::watch::channel(());
+ let mut background_processor = tokio::spawn(process_events_async(
+ Arc::clone(&persister),
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()),
+ GossipSync::p2p(gossip_sync.clone()),
peer_manager.clone(),
logger.clone(),
- );
+ Some(scorer.clone()),
+ move |t| {
+ let mut bp_exit_fut_check = bp_exit_check.clone();
+ Box::pin(async move {
+ tokio::select! {
+ _ = tokio::time::sleep(t) => false,
+ _ = bp_exit_fut_check.changed() => true,
+ }
+ })
+ },
+ false,
+ ));
// 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());
+ let stop_connect = Arc::clone(&stop_listen_connect);
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(1));
+ interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
interval.tick().await;
match disk::read_channel_peer_data(Path::new(&peer_data_path)) {
.list_channels()
.iter()
.map(|chan| chan.counterparty.node_id)
- .filter(|id| !peers.contains(id))
+ .filter(|id| !peers.iter().any(|(pk, _)| id == pk))
{
+ if stop_connect.load(Ordering::Acquire) {
+ return;
+ }
for (pubkey, peer_addr) in info.iter() {
if *pubkey == node_id {
let _ = cli::do_connect_peer(
});
// 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);
+ // some public channels.
+ let peer_man = Arc::clone(&peer_manager);
+ let chan_man = 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(
+ tokio::spawn(async move {
+ // First wait a minute until we have some peers and maybe have opened a channel.
+ tokio::time::sleep(Duration::from_secs(60)).await;
+ // Then, update our announcement once an hour to keep it fresh but avoid unnecessary churn
+ // in the global gossip network.
+ let mut interval = tokio::time::interval(Duration::from_secs(3600));
+ loop {
+ interval.tick().await;
+ // Don't bother trying to announce if we don't have any public channls, though our
+ // peers should drop such an announcement anyway. Note that announcement may not
+ // propagate until we have a channel with 6+ confirmations.
+ if chan_man.list_channels().iter().any(|chan| chan.is_public) {
+ peer_man.broadcast_node_announcement(
[0; 3],
args.ldk_announced_node_name,
args.ldk_announced_listen_addr.clone(),
);
}
- });
- }
+ }
+ });
+
+ tokio::spawn(sweep::periodic_sweep(
+ ldk_data_dir.clone(),
+ Arc::clone(&keys_manager),
+ Arc::clone(&logger),
+ Arc::clone(&persister),
+ Arc::clone(&bitcoind_client),
+ Arc::clone(&channel_manager),
+ ));
// Start the CLI.
- cli::poll_for_user_input(
- invoice_payer.clone(),
- peer_manager.clone(),
- channel_manager.clone(),
- keys_manager.clone(),
- network_graph.clone(),
- scorer.clone(),
+ let cli_poll = tokio::spawn(cli::poll_for_user_input(
+ Arc::clone(&peer_manager),
+ Arc::clone(&channel_manager),
+ Arc::clone(&keys_manager),
+ Arc::clone(&network_graph),
+ Arc::clone(&onion_messenger),
inbound_payments,
outbound_payments,
- ldk_data_dir.clone(),
- logger.clone(),
+ ldk_data_dir,
network,
- )
- .await;
+ Arc::clone(&logger),
+ Arc::clone(&persister),
+ ));
+
+ // Exit if either CLI polling exits or the background processor exits (which shouldn't happen
+ // unless we fail to write to the filesystem).
+ tokio::select! {
+ _ = cli_poll => {},
+ bg_res = &mut background_processor => {
+ stop_listen_connect.store(true, Ordering::Release);
+ peer_manager.disconnect_all_peers();
+ panic!("ERR: background processing stopped with result {:?}, exiting", bg_res);
+ },
+ }
+
+ // Disconnect our peers and stop accepting new connections. This ensures we don't continue
+ // updating our channel data after we've stopped the background processor.
+ stop_listen_connect.store(true, Ordering::Release);
+ peer_manager.disconnect_all_peers();
// Stop the background processor.
- background_processor.stop().unwrap();
+ if !bp_exit.is_closed() {
+ bp_exit.send(()).unwrap();
+ background_processor.await.unwrap().unwrap();
+ }
}
#[tokio::main]
pub async fn main() {
+ #[cfg(not(target_os = "windows"))]
+ {
+ // Catch Ctrl-C with a dummy signal handler.
+ unsafe {
+ let mut new_action: libc::sigaction = core::mem::zeroed();
+ let mut old_action: libc::sigaction = core::mem::zeroed();
+
+ extern "C" fn dummy_handler(
+ _: libc::c_int, _: *const libc::siginfo_t, _: *const libc::c_void,
+ ) {
+ }
+
+ new_action.sa_sigaction = dummy_handler as libc::sighandler_t;
+ new_action.sa_flags = libc::SA_SIGINFO;
+
+ libc::sigaction(
+ libc::SIGINT,
+ &new_action as *const libc::sigaction,
+ &mut old_action as *mut libc::sigaction,
+ );
+ }
+ }
+
start_ldk().await;
}