From fea709d5a9fba7553c8bbc25236a6640ef52dee5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 18 Sep 2023 05:00:52 +0000 Subject: [PATCH] Split inbound+outbound payments, use PaymentId as outbound key --- src/cli.rs | 38 +++++++++++++++-------------- src/disk.rs | 17 ++++++++++--- src/main.rs | 70 ++++++++++++++++++++++++++++++++--------------------- 3 files changed, 75 insertions(+), 50 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 297900a..1856cbd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,8 @@ use crate::disk::{self, INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME}; use crate::hex_utils; use crate::{ - ChannelManager, HTLCStatus, MillisatAmount, NetworkGraph, OnionMessenger, PaymentInfo, - PaymentInfoStorage, PeerManager, + ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, NetworkGraph, + OnionMessenger, OutboundPaymentInfoStorage, PaymentInfo, PeerManager, }; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::Hash; @@ -63,9 +63,9 @@ impl Writeable for UserOnionMessageContents { pub(crate) fn poll_for_user_input( peer_manager: Arc, channel_manager: Arc, keys_manager: Arc, network_graph: Arc, - onion_messenger: Arc, inbound_payments: Arc>, - outbound_payments: Arc>, ldk_data_dir: String, network: Network, - logger: Arc, fs_store: Arc, + onion_messenger: Arc, inbound_payments: Arc>, + outbound_payments: Arc>, ldk_data_dir: String, + network: Network, logger: Arc, fs_store: Arc, ) { println!( "LDK startup successful. Enter \"help\" to view available commands. Press Ctrl-D to quit." @@ -553,7 +553,9 @@ fn list_channels(channel_manager: &Arc, network_graph: &Arc, + outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc, ) { - let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner()); + let payment_id = PaymentId((*invoice.payment_hash()).into_inner()); let payment_secret = Some(*invoice.payment_secret()); outbound_payments.payments.insert( - payment_hash, + payment_id, PaymentInfo { preimage: None, secret: payment_secret, @@ -708,7 +710,7 @@ fn send_payment( Err(e) => { println!("ERROR: failed to send payment: {:?}", e); print!("> "); - outbound_payments.payments.get_mut(&payment_hash).unwrap().status = HTLCStatus::Failed; + outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed; fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap(); } }; @@ -716,17 +718,17 @@ fn send_payment( fn keysend( channel_manager: &ChannelManager, payee_pubkey: PublicKey, amt_msat: u64, entropy_source: &E, - outbound_payments: &mut PaymentInfoStorage, fs_store: Arc, + outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc, ) { let payment_preimage = PaymentPreimage(entropy_source.get_secure_random_bytes()); - let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()); + let payment_id = PaymentId(Sha256::hash(&payment_preimage.0[..]).into_inner()); let route_params = RouteParameters::from_payment_params_and_value( PaymentParameters::for_keysend(payee_pubkey, 40, false), amt_msat, ); outbound_payments.payments.insert( - payment_hash, + payment_id, PaymentInfo { preimage: None, secret: None, @@ -738,7 +740,7 @@ fn keysend( match channel_manager.send_spontaneous_payment_with_retry( Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), - PaymentId(payment_hash.0), + payment_id, route_params, Retry::Timeout(Duration::from_secs(10)), ) { @@ -749,16 +751,16 @@ fn keysend( Err(e) => { println!("ERROR: failed to send payment: {:?}", e); print!("> "); - outbound_payments.payments.get_mut(&payment_hash).unwrap().status = HTLCStatus::Failed; + outbound_payments.payments.get_mut(&payment_id).unwrap().status = HTLCStatus::Failed; fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap(); } }; } fn get_invoice( - amt_msat: u64, inbound_payments: &mut PaymentInfoStorage, channel_manager: &ChannelManager, - keys_manager: Arc, network: Network, expiry_secs: u32, - logger: Arc, + amt_msat: u64, inbound_payments: &mut InboundPaymentInfoStorage, + channel_manager: &ChannelManager, keys_manager: Arc, network: Network, + expiry_secs: u32, logger: Arc, ) { let currency = match network { Network::Bitcoin => Currency::Bitcoin, diff --git a/src/disk.rs b/src/disk.rs index a69fdef..d301180 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -1,4 +1,4 @@ -use crate::{cli, NetworkGraph, PaymentInfoStorage}; +use crate::{cli, InboundPaymentInfoStorage, NetworkGraph, OutboundPaymentInfoStorage}; use bitcoin::secp256k1::PublicKey; use bitcoin::Network; use chrono::Utc; @@ -86,13 +86,22 @@ pub(crate) fn read_network( NetworkGraph::new(network, logger) } -pub(crate) fn read_payment_info(path: &Path) -> PaymentInfoStorage { +pub(crate) fn read_inbound_payment_info(path: &Path) -> InboundPaymentInfoStorage { if let Ok(file) = File::open(path) { - if let Ok(info) = PaymentInfoStorage::read(&mut BufReader::new(file)) { + if let Ok(info) = InboundPaymentInfoStorage::read(&mut BufReader::new(file)) { return info; } } - PaymentInfoStorage { payments: HashMap::new() } + InboundPaymentInfoStorage { payments: HashMap::new() } +} + +pub(crate) fn read_outbound_payment_info(path: &Path) -> OutboundPaymentInfoStorage { + if let Ok(file) = File::open(path) { + if let Ok(info) = OutboundPaymentInfoStorage::read(&mut BufReader::new(file)) { + return info; + } + } + OutboundPaymentInfoStorage { payments: HashMap::new() } } pub(crate) fn read_scorer( diff --git a/src/main.rs b/src/main.rs index a33d667..6480805 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet}; use lightning::events::{Event, PaymentFailureReason, PaymentPurpose}; use lightning::ln::channelmanager::{self, RecentPaymentDetails}; use lightning::ln::channelmanager::{ - ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager, + ChainParameters, ChannelManagerReadArgs, PaymentId, SimpleArcChannelManager, }; use lightning::ln::msgs::DecodeError; use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler, SimpleArcPeerManager}; @@ -109,11 +109,19 @@ impl_writeable_tlv_based!(PaymentInfo, { (6, amt_msat, required), }); -pub(crate) struct PaymentInfoStorage { +pub(crate) struct InboundPaymentInfoStorage { payments: HashMap, } -impl_writeable_tlv_based!(PaymentInfoStorage, { +impl_writeable_tlv_based!(InboundPaymentInfoStorage, { + (0, payments, required), +}); + +pub(crate) struct OutboundPaymentInfoStorage { + payments: HashMap, +} + +impl_writeable_tlv_based!(OutboundPaymentInfoStorage, { (0, payments, required), }); @@ -171,8 +179,9 @@ pub(crate) type BumpTxEventHandler = BumpTransactionEventHandler< async fn handle_ldk_events( channel_manager: &Arc, bitcoind_client: &BitcoindClient, network_graph: &NetworkGraph, keys_manager: &KeysManager, - bump_tx_event_handler: &BumpTxEventHandler, inbound_payments: Arc>, - outbound_payments: Arc>, fs_store: &Arc, + bump_tx_event_handler: &BumpTxEventHandler, + inbound_payments: Arc>, + outbound_payments: Arc>, fs_store: &Arc, network: Network, event: Event, ) { match event { @@ -286,10 +295,12 @@ async fn handle_ldk_events( } fs_store.write("", "", INBOUND_PAYMENTS_FNAME, &inbound.encode()).unwrap(); } - Event::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => { + Event::PaymentSent { + payment_preimage, payment_hash, fee_paid_msat, payment_id, .. + } => { let mut outbound = outbound_payments.lock().unwrap(); - for (hash, payment) in outbound.payments.iter_mut() { - if *hash == payment_hash { + for (id, payment) in outbound.payments.iter_mut() { + if *id == payment_id.unwrap() { payment.preimage = Some(payment_preimage); payment.status = HTLCStatus::Succeeded; println!( @@ -343,7 +354,7 @@ async fn handle_ldk_events( Event::PaymentPathFailed { .. } => {} Event::ProbeSuccessful { .. } => {} Event::ProbeFailed { .. } => {} - Event::PaymentFailed { payment_hash, reason, .. } => { + Event::PaymentFailed { payment_hash, reason, payment_id, .. } => { print!( "\nEVENT: Failed to send payment to payment hash {}: {:?}", payment_hash, @@ -353,8 +364,8 @@ async fn handle_ldk_events( io::stdout().flush().unwrap(); let mut outbound = outbound_payments.lock().unwrap(); - if outbound.payments.contains_key(&payment_hash) { - let payment = outbound.payments.get_mut(&payment_hash).unwrap(); + if outbound.payments.contains_key(&payment_id) { + let payment = outbound.payments.get_mut(&payment_id).unwrap(); payment.status = HTLCStatus::Failed; } fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap(); @@ -364,7 +375,12 @@ async fn handle_ldk_events( print!("> "); io::stdout().flush().unwrap(); - // TODO: mark the payment as failed + let mut outbound = outbound_payments.lock().unwrap(); + if outbound.payments.contains_key(&payment_id) { + let payment = outbound.payments.get_mut(&payment_id).unwrap(); + payment.status = HTLCStatus::Failed; + } + fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap(); } Event::PaymentForwarded { prev_channel_id, @@ -833,32 +849,30 @@ async fn start_ldk() { } }); - 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 + let inbound_payments = Arc::new(Mutex::new(disk::read_inbound_payment_info(Path::new( + &format!("{}/{}", ldk_data_dir, INBOUND_PAYMENTS_FNAME), + )))); + let outbound_payments = Arc::new(Mutex::new(disk::read_outbound_payment_info(Path::new( + &format!("{}/{}", ldk_data_dir, OUTBOUND_PAYMENTS_FNAME), + )))); + let recent_payments_payment_ids = 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), - RecentPaymentDetails::AwaitingInvoice { payment_id: _ } => todo!(), + RecentPaymentDetails::Pending { payment_id, .. } => Some(payment_id), + RecentPaymentDetails::Fulfilled { payment_id, .. } => Some(payment_id), + RecentPaymentDetails::Abandoned { payment_id, .. } => Some(payment_id), + RecentPaymentDetails::AwaitingInvoice { payment_id } => Some(payment_id), }) - .collect::>(); - for (payment_hash, payment_info) in outbound_payments + .collect::>(); + for (payment_id, 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) { + if !recent_payments_payment_ids.contains(payment_id) { payment_info.status = HTLCStatus::Failed; } } -- 2.39.5