Skip to content

Commit 9dc68e8

Browse files
Move InFlightHtlcs into ChannelManager
This is part of moving the Router trait into ChannelManager, which will help allow ChannelManager to fetch routes on-the-fly as part of supporting trampoline payments.
1 parent dc7f65f commit 9dc68e8

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

lightning-invoice/src/payment.rs

+6-33
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! #
3636
//! # use lightning::io;
3737
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
38-
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
38+
//! # use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure};
3939
//! # use lightning::ln::msgs::LightningError;
4040
//! # use lightning::routing::gossip::NodeId;
4141
//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
@@ -44,7 +44,7 @@
4444
//! # use lightning::util::logger::{Logger, Record};
4545
//! # use lightning::util::ser::{Writeable, Writer};
4646
//! # use lightning_invoice::Invoice;
47-
//! # use lightning_invoice::payment::{InFlightHtlcs, InvoicePayer, Payer, Retry, Router};
47+
//! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry, Router};
4848
//! # use secp256k1::PublicKey;
4949
//! # use std::cell::RefCell;
5050
//! # use std::ops::Deref;
@@ -140,16 +140,14 @@ use bitcoin_hashes::Hash;
140140
use bitcoin_hashes::sha256::Hash as Sha256;
141141

142142
use crate::prelude::*;
143-
use lightning::io;
144143
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
145-
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
144+
use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure};
146145
use lightning::ln::msgs::LightningError;
147146
use lightning::routing::gossip::NodeId;
148147
use lightning::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters};
149148
use lightning::util::errors::APIError;
150149
use lightning::util::events::{Event, EventHandler};
151150
use lightning::util::logger::Logger;
152-
use lightning::util::ser::Writeable;
153151
use crate::time_utils::Time;
154152
use crate::sync::Mutex;
155153

@@ -641,7 +639,7 @@ where
641639
}
642640
}
643641

644-
InFlightHtlcs(total_inflight_map)
642+
InFlightHtlcs::new(total_inflight_map)
645643
}
646644
}
647645

@@ -730,39 +728,14 @@ where
730728
}
731729
}
732730

733-
/// A map with liquidity value (in msat) keyed by a short channel id and the direction the HTLC
734-
/// is traveling in. The direction boolean is determined by checking if the HTLC source's public
735-
/// key is less than its destination. See [`InFlightHtlcs::used_liquidity_msat`] for more
736-
/// details.
737-
pub struct InFlightHtlcs(HashMap<(u64, bool), u64>);
738-
739-
impl InFlightHtlcs {
740-
/// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
741-
/// id.
742-
pub fn used_liquidity_msat(&self, source: &NodeId, target: &NodeId, channel_scid: u64) -> Option<u64> {
743-
self.0.get(&(channel_scid, source < target)).map(|v| *v)
744-
}
745-
}
746-
747-
impl Writeable for InFlightHtlcs {
748-
fn write<W: lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.0.write(writer) }
749-
}
750-
751-
impl lightning::util::ser::Readable for InFlightHtlcs {
752-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, lightning::ln::msgs::DecodeError> {
753-
let infight_map: HashMap<(u64, bool), u64> = lightning::util::ser::Readable::read(reader)?;
754-
Ok(Self(infight_map))
755-
}
756-
}
757-
758731
#[cfg(test)]
759732
mod tests {
760733
use super::*;
761734
use crate::{InvoiceBuilder, Currency};
762735
use crate::utils::{ScorerAccountingForInFlightHtlcs, create_invoice_from_channelmanager_and_duration_since_epoch};
763736
use bitcoin_hashes::sha256::Hash as Sha256;
764737
use lightning::ln::PaymentPreimage;
765-
use lightning::ln::channelmanager;
738+
use lightning::ln::channelmanager::{self, InFlightHtlcs};
766739
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
767740
use lightning::ln::functional_test_utils::*;
768741
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
@@ -1864,7 +1837,7 @@ mod tests {
18641837
impl Router for FailingRouter {
18651838
fn find_route(
18661839
&self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>,
1867-
_inflight_htlcs: InFlightHtlcs
1840+
_inflight_htlcs: InFlightHtlcs,
18681841
) -> Result<Route, LightningError> {
18691842
Err(LightningError { err: String::new(), action: ErrorAction::IgnoreError })
18701843
}

lightning-invoice/src/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Convenient utilities to create an invoice.
22
33
use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
4-
use crate::payment::{InFlightHtlcs, Payer, Router};
4+
use crate::payment::{Payer, Router};
55

66
use crate::{prelude::*, Description, InvoiceDescription, Sha256};
77
use bech32::ToBase32;
@@ -10,7 +10,7 @@ use lightning::chain;
1010
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
1111
use lightning::chain::keysinterface::{Recipient, KeysInterface, Sign};
1212
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
13-
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
13+
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, InFlightHtlcs, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
1414
#[cfg(feature = "std")]
1515
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1616
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};

lightning/src/ln/channelmanager.rs

+35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use crate::ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfi
4646
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
4747
#[cfg(any(feature = "_test_utils", test))]
4848
use crate::ln::features::InvoiceFeatures;
49+
use crate::routing::gossip::NodeId;
4950
use crate::routing::router::{PaymentParameters, Route, RouteHop, RoutePath, RouteParameters};
5051
use crate::ln::msgs;
5152
use crate::ln::onion_utils;
@@ -71,6 +72,40 @@ use core::sync::atomic::{AtomicUsize, Ordering};
7172
use core::time::Duration;
7273
use core::ops::Deref;
7374

75+
/// A map with liquidity value (in msat) keyed by a short channel id and the direction the HTLC
76+
/// is traveling in. The direction boolean is determined by checking if the HTLC source's public
77+
/// key is less than its destination. See [`InFlightHtlcs::used_liquidity_msat`] for more
78+
/// details.
79+
#[cfg(not(any(test, feature = "_test_utils")))]
80+
pub struct InFlightHtlcs(HashMap<(u64, bool), u64>);
81+
#[cfg(any(test, feature = "_test_utils"))]
82+
pub struct InFlightHtlcs(pub HashMap<(u64, bool), u64>);
83+
84+
impl InFlightHtlcs {
85+
/// Create a new `InFlightHtlcs` via a mapping from:
86+
/// (short_channel_id, source_pubkey < target_pubkey) -> used_liquidity_msat
87+
pub fn new(inflight_map: HashMap<(u64, bool), u64>) -> Self {
88+
InFlightHtlcs(inflight_map)
89+
}
90+
91+
/// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
92+
/// id.
93+
pub fn used_liquidity_msat(&self, source: &NodeId, target: &NodeId, channel_scid: u64) -> Option<u64> {
94+
self.0.get(&(channel_scid, source < target)).map(|v| *v)
95+
}
96+
}
97+
98+
impl Writeable for InFlightHtlcs {
99+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.0.write(writer) }
100+
}
101+
102+
impl Readable for InFlightHtlcs {
103+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, msgs::DecodeError> {
104+
let infight_map: HashMap<(u64, bool), u64> = Readable::read(reader)?;
105+
Ok(Self(infight_map))
106+
}
107+
}
108+
74109
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
75110
//
76111
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should

0 commit comments

Comments
 (0)