Skip to content

Commit 66e59f9

Browse files
committed
Make InFlightHtlcs a named-field struct instead of a tuple struct
In the next commit we'll track blinded path in-flights in `InFlightHtlcs` as well as unblinded hops, so here we convert `InFlightHtlcs` to a named-field struct so that we can add more fields to it with less confusion.
1 parent 5ae19b4 commit 66e59f9

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

lightning/src/routing/router.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,25 @@ where
350350
/// A data structure for tracking in-flight HTLCs. May be used during pathfinding to account for
351351
/// in-use channel liquidity.
352352
#[derive(Clone)]
353-
pub struct InFlightHtlcs(
353+
pub struct InFlightHtlcs {
354354
// A map with liquidity value (in msat) keyed by a short channel id and the direction the HTLC
355355
// is traveling in. The direction boolean is determined by checking if the HTLC source's public
356356
// key is less than its destination. See `InFlightHtlcs::used_liquidity_msat` for more
357357
// details.
358-
HashMap<(u64, bool), u64>,
359-
);
358+
unblinded_hops: HashMap<(u64, bool), u64>,
359+
}
360360

361361
impl InFlightHtlcs {
362362
/// Constructs an empty `InFlightHtlcs`.
363-
#[rustfmt::skip]
364-
pub fn new() -> Self { InFlightHtlcs(new_hash_map()) }
363+
pub fn new() -> Self {
364+
InFlightHtlcs { unblinded_hops: new_hash_map() }
365+
}
365366

366367
/// Takes in a path with payer's node id and adds the path's details to `InFlightHtlcs`.
367-
#[rustfmt::skip]
368368
pub fn process_path(&mut self, path: &Path, payer_node_id: PublicKey) {
369-
if path.hops.is_empty() { return };
369+
if path.hops.is_empty() {
370+
return;
371+
}
370372

371373
let mut cumulative_msat = 0;
372374
if let Some(tail) = &path.blinded_tail {
@@ -378,17 +380,17 @@ impl InFlightHtlcs {
378380
// the router excludes the payer node. In the following lines, the payer's information is
379381
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
380382
// in our sliding window of two.
381-
let reversed_hops_with_payer = path.hops.iter().rev().skip(1)
382-
.map(|hop| hop.pubkey)
383-
.chain(core::iter::once(payer_node_id));
383+
let reversed_hops = path.hops.iter().rev().skip(1).map(|hop| hop.pubkey);
384+
let reversed_hops_with_payer = reversed_hops.chain(core::iter::once(payer_node_id));
384385

385386
// Taking the reversed vector from above, we zip it with just the reversed hops list to
386387
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
387388
// the total amount sent.
388389
for (next_hop, prev_hop) in path.hops.iter().rev().zip(reversed_hops_with_payer) {
389390
cumulative_msat += next_hop.fee_msat;
390-
self.0
391-
.entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
391+
let direction = NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey);
392+
self.unblinded_hops
393+
.entry((next_hop.short_channel_id, direction))
392394
.and_modify(|used_liquidity_msat| *used_liquidity_msat += cumulative_msat)
393395
.or_insert(cumulative_msat);
394396
}
@@ -399,7 +401,7 @@ impl InFlightHtlcs {
399401
pub fn add_inflight_htlc(
400402
&mut self, source: &NodeId, target: &NodeId, channel_scid: u64, used_msat: u64,
401403
) {
402-
self.0
404+
self.unblinded_hops
403405
.entry((channel_scid, source < target))
404406
.and_modify(|used_liquidity_msat| *used_liquidity_msat += used_msat)
405407
.or_insert(used_msat);
@@ -410,19 +412,20 @@ impl InFlightHtlcs {
410412
pub fn used_liquidity_msat(
411413
&self, source: &NodeId, target: &NodeId, channel_scid: u64,
412414
) -> Option<u64> {
413-
self.0.get(&(channel_scid, source < target)).map(|v| *v)
415+
self.unblinded_hops.get(&(channel_scid, source < target)).map(|v| *v)
414416
}
415417
}
416418

417419
impl Writeable for InFlightHtlcs {
418-
#[rustfmt::skip]
419-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.0.write(writer) }
420+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
421+
self.unblinded_hops.write(writer)
422+
}
420423
}
421424

422425
impl Readable for InFlightHtlcs {
423426
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
424-
let infight_map: HashMap<(u64, bool), u64> = Readable::read(reader)?;
425-
Ok(Self(infight_map))
427+
let unblinded_hops: HashMap<(u64, bool), u64> = Readable::read(reader)?;
428+
Ok(Self { unblinded_hops })
426429
}
427430
}
428431

@@ -8002,8 +8005,8 @@ mod tests {
80028005
}),
80038006
};
80048007
inflight_htlcs.process_path(&path, ln_test_utils::pubkey(44));
8005-
assert_eq!(*inflight_htlcs.0.get(&(42, true)).unwrap(), 301);
8006-
assert_eq!(*inflight_htlcs.0.get(&(43, false)).unwrap(), 201);
8008+
assert_eq!(*inflight_htlcs.unblinded_hops.get(&(42, true)).unwrap(), 301);
8009+
assert_eq!(*inflight_htlcs.unblinded_hops.get(&(43, false)).unwrap(), 201);
80078010
}
80088011

80098012
#[test]

0 commit comments

Comments
 (0)