Skip to content

Commit 1840cae

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 1840cae

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

lightning-invoice/src/payment.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
3939
//! # use lightning::ln::msgs::LightningError;
4040
//! # use lightning::routing::gossip::NodeId;
41-
//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
41+
//! # use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters};
4242
//! # use lightning::routing::scoring::{ChannelUsage, Score};
4343
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
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};
145144
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
146145
use lightning::ln::msgs::LightningError;
147146
use lightning::routing::gossip::NodeId;
148-
use lightning::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters};
147+
use lightning::routing::router::{InFlightHtlcs, 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,31 +728,6 @@ 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::*;
@@ -767,7 +740,7 @@ mod tests {
767740
use lightning::ln::functional_test_utils::*;
768741
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
769742
use lightning::routing::gossip::{EffectiveCapacity, NodeId};
770-
use lightning::routing::router::{PaymentParameters, Route, RouteHop};
743+
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop};
771744
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
772745
use lightning::util::test_utils::TestLogger;
773746
use lightning::util::errors::APIError;
@@ -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

Lines changed: 2 additions & 2 deletions
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;
@@ -16,7 +16,7 @@ use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1616
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
1717
use lightning::ln::msgs::LightningError;
1818
use lightning::routing::gossip::{NetworkGraph, NodeId, RoutingFees};
19-
use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop};
19+
use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop};
2020
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
2121
use lightning::util::logger::Logger;
2222
use secp256k1::PublicKey;

lightning/src/routing/router.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ use alloc::collections::BinaryHeap;
2929
use core::cmp;
3030
use core::ops::Deref;
3131

32+
/// A map with liquidity value (in msat) keyed by a short channel id and the direction the HTLC
33+
/// is traveling in. The direction boolean is determined by checking if the HTLC source's public
34+
/// key is less than its destination. See [`InFlightHtlcs::used_liquidity_msat`] for more
35+
/// details.
36+
#[cfg(not(any(test, feature = "_test_utils")))]
37+
pub struct InFlightHtlcs(HashMap<(u64, bool), u64>);
38+
#[cfg(any(test, feature = "_test_utils"))]
39+
pub struct InFlightHtlcs(pub HashMap<(u64, bool), u64>);
40+
41+
impl InFlightHtlcs {
42+
/// Create a new `InFlightHtlcs` via a mapping from:
43+
/// (short_channel_id, source_pubkey < target_pubkey) -> used_liquidity_msat
44+
pub fn new(inflight_map: HashMap<(u64, bool), u64>) -> Self {
45+
InFlightHtlcs(inflight_map)
46+
}
47+
48+
/// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
49+
/// id.
50+
pub fn used_liquidity_msat(&self, source: &NodeId, target: &NodeId, channel_scid: u64) -> Option<u64> {
51+
self.0.get(&(channel_scid, source < target)).map(|v| *v)
52+
}
53+
}
54+
55+
impl Writeable for InFlightHtlcs {
56+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.0.write(writer) }
57+
}
58+
59+
impl Readable for InFlightHtlcs {
60+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
61+
let infight_map: HashMap<(u64, bool), u64> = Readable::read(reader)?;
62+
Ok(Self(infight_map))
63+
}
64+
}
65+
3266
/// A hop in a route
3367
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
3468
pub struct RouteHop {

0 commit comments

Comments
 (0)