Skip to content

Commit 912f3c2

Browse files
Extract util to forward onion messages
In an upcoming commit, we will need to repurpose this logic when, as a static invoice server node, we update our support for forwarding invoice requests from payers to often-offline recipients. We currently treat these forwarded invreqs as outbound onion messages that are initiated by our node, when they should really be treated as forwarded onion messages.
1 parent f707245 commit 912f3c2

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

fuzz/src/onion_message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ mod tests {
430430
super::do_test(&<Vec<u8>>::from_hex(two_unblinded_hops_om).unwrap(), &logger);
431431
{
432432
let log_entries = logger.lines.lock().unwrap();
433-
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
433+
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202 when forwarding peeled onion message from 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
434434
}
435435

436436
let two_unblinded_two_blinded_om = "\
@@ -471,7 +471,7 @@ mod tests {
471471
super::do_test(&<Vec<u8>>::from_hex(two_unblinded_two_blinded_om).unwrap(), &logger);
472472
{
473473
let log_entries = logger.lines.lock().unwrap();
474-
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
474+
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202 when forwarding peeled onion message from 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
475475
}
476476

477477
let three_blinded_om = "\
@@ -512,7 +512,7 @@ mod tests {
512512
super::do_test(&<Vec<u8>>::from_hex(three_blinded_om).unwrap(), &logger);
513513
{
514514
let log_entries = logger.lines.lock().unwrap();
515-
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
515+
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202 when forwarding peeled onion message from 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
516516
}
517517
}
518518
}

lightning/src/onion_message/messenger.rs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,74 @@ where
16201620
}
16211621
}
16221622

1623+
fn enqueue_forwarded_onion_message(
1624+
&self, next_hop: NextMessageHop, onion_message: OnionMessage, log_suffix: fmt::Arguments,
1625+
) -> Result<(), SendError> {
1626+
let next_node_id = match next_hop {
1627+
NextMessageHop::NodeId(pubkey) => pubkey,
1628+
NextMessageHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) {
1629+
Some(pubkey) => pubkey,
1630+
None => {
1631+
log_trace!(self.logger, "Dropping forwarded onion messager: unable to resolve next hop using SCID {} {}", scid, log_suffix);
1632+
return Err(SendError::GetNodeIdFailed);
1633+
},
1634+
},
1635+
};
1636+
1637+
let mut message_recipients = self.message_recipients.lock().unwrap();
1638+
if outbound_buffer_full(&next_node_id, &message_recipients) {
1639+
log_trace!(
1640+
self.logger,
1641+
"Dropping forwarded onion message to peer {}: outbound buffer full {}",
1642+
next_node_id,
1643+
log_suffix
1644+
);
1645+
return Err(SendError::BufferFull);
1646+
}
1647+
1648+
#[cfg(fuzzing)]
1649+
message_recipients
1650+
.entry(next_node_id)
1651+
.or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()));
1652+
1653+
match message_recipients.entry(next_node_id) {
1654+
hash_map::Entry::Occupied(mut e)
1655+
if matches!(e.get(), OnionMessageRecipient::ConnectedPeer(..)) =>
1656+
{
1657+
e.get_mut().enqueue_message(onion_message);
1658+
log_trace!(
1659+
self.logger,
1660+
"Forwarding an onion message to peer {} {}",
1661+
next_node_id,
1662+
log_suffix
1663+
);
1664+
Ok(())
1665+
},
1666+
_ if self.intercept_messages_for_offline_peers => {
1667+
log_trace!(
1668+
self.logger,
1669+
"Generating OnionMessageIntercepted event for peer {} {}",
1670+
next_node_id,
1671+
log_suffix
1672+
);
1673+
self.enqueue_intercepted_event(Event::OnionMessageIntercepted {
1674+
peer_node_id: next_node_id,
1675+
message: onion_message,
1676+
});
1677+
Ok(())
1678+
},
1679+
_ => {
1680+
log_trace!(
1681+
self.logger,
1682+
"Dropping forwarded onion message to disconnected peer {} {}",
1683+
next_node_id,
1684+
log_suffix
1685+
);
1686+
Err(SendError::InvalidFirstHop(next_node_id))
1687+
},
1688+
}
1689+
}
1690+
16231691
/// Forwards an [`OnionMessage`] to `peer_node_id`. Useful if we initialized
16241692
/// the [`OnionMessenger`] with [`Self::new_with_offline_peer_interception`]
16251693
/// and want to forward a previously intercepted onion message to a peer that
@@ -2204,56 +2272,11 @@ where
22042272
}
22052273
},
22062274
Ok(PeeledOnion::Forward(next_hop, onion_message)) => {
2207-
let next_node_id = match next_hop {
2208-
NextMessageHop::NodeId(pubkey) => pubkey,
2209-
NextMessageHop::ShortChannelId(scid) => {
2210-
match self.node_id_lookup.next_node_id(scid) {
2211-
Some(pubkey) => pubkey,
2212-
None => {
2213-
log_trace!(self.logger, "Dropping forwarded onion messager: unable to resolve next hop using SCID {}", scid);
2214-
return;
2215-
},
2216-
}
2217-
},
2218-
};
2219-
2220-
let mut message_recipients = self.message_recipients.lock().unwrap();
2221-
if outbound_buffer_full(&next_node_id, &message_recipients) {
2222-
log_trace!(
2223-
logger,
2224-
"Dropping forwarded onion message to peer {}: outbound buffer full",
2225-
next_node_id
2226-
);
2227-
return;
2228-
}
2229-
2230-
#[cfg(fuzzing)]
2231-
message_recipients
2232-
.entry(next_node_id)
2233-
.or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()));
2234-
2235-
match message_recipients.entry(next_node_id) {
2236-
hash_map::Entry::Occupied(mut e)
2237-
if matches!(e.get(), OnionMessageRecipient::ConnectedPeer(..)) =>
2238-
{
2239-
e.get_mut().enqueue_message(onion_message);
2240-
log_trace!(logger, "Forwarding an onion message to peer {}", next_node_id);
2241-
},
2242-
_ if self.intercept_messages_for_offline_peers => {
2243-
self.enqueue_intercepted_event(Event::OnionMessageIntercepted {
2244-
peer_node_id: next_node_id,
2245-
message: onion_message,
2246-
});
2247-
},
2248-
_ => {
2249-
log_trace!(
2250-
logger,
2251-
"Dropping forwarded onion message to disconnected peer {}",
2252-
next_node_id
2253-
);
2254-
return;
2255-
},
2256-
}
2275+
let _ = self.enqueue_forwarded_onion_message(
2276+
next_hop,
2277+
onion_message,
2278+
format_args!("when forwarding peeled onion message from {}", peer_node_id),
2279+
);
22572280
},
22582281
Err(e) => {
22592282
log_error!(logger, "Failed to process onion message {:?}", e);

0 commit comments

Comments
 (0)