Skip to content

Commit f10ccfa

Browse files
WIP cache peers in Flow
1 parent 302d499 commit f10ccfa

File tree

2 files changed

+16
-40
lines changed

2 files changed

+16
-40
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8025,6 +8025,8 @@ where
80258025

80268026
should_persist
80278027
});
8028+
8029+
self.flow.set_peers(self.get_peers_for_blinded_path());
80288030
}
80298031

80308032
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
@@ -10407,16 +10409,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1040710409
emit_initial_channel_ready_event!(pending_events, chan);
1040810410
}
1040910411

10410-
Ok(())
1041110412
} else {
1041210413
try_channel_entry!(self, peer_state, Err(ChannelError::close(
1041310414
"Got a channel_ready message for an unfunded channel!".into())), chan_entry)
1041410415
}
1041510416
},
1041610417
hash_map::Entry::Vacant(_) => {
10417-
Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
10418+
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
1041810419
}
1041910420
}
10421+
core::mem::drop(peer_state_lock);
10422+
core::mem::drop(per_peer_state);
10423+
10424+
self.flow.set_peers(self.get_peers_for_blinded_path());
10425+
Ok(())
1042010426
}
1042110427

1042210428
fn internal_shutdown(
@@ -10509,6 +10515,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1050910515
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
1051010516
}
1051110517

10518+
self.flow.set_peers(self.get_peers_for_blinded_path());
1051210519
Ok(())
1051310520
}
1051410521

@@ -13267,7 +13274,7 @@ where
1326713274
let _ = handle_error!(self, err, counterparty_node_id);
1326813275
}
1326913276

13270-
let _ = self.flow.peer_disconnected(counterparty_node_id);
13277+
self.flow.set_peers(self.get_peers_for_blinded_path());
1327113278
}
1327213279

1327313280
#[rustfmt::skip]
@@ -13382,7 +13389,7 @@ where
1338213389
// until we have some peer connection(s) to receive onion messages over, so as a minor optimization
1338313390
// refresh the cache when a peer connects.
1338413391
self.check_refresh_async_receive_offer_cache(false);
13385-
let _ = self.flow.peer_connected(counterparty_node_id, &init_msg.features, peer_has_live_channel);
13392+
self.flow.set_peers(self.get_peers_for_blinded_path());
1338613393
res
1338713394
}
1338813395

lightning/src/offers/flow.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey};
6161

6262
use crate::offers::static_invoice::{StaticInvoice, StaticInvoiceBuilder};
6363
use crate::sync::{Mutex, RwLock};
64-
use crate::types::features::InitFeatures;
6564
use crate::types::payment::{PaymentHash, PaymentSecret};
6665
use crate::util::logger::Logger;
6766
use crate::util::ser::Writeable;
@@ -1712,42 +1711,12 @@ where
17121711
self.async_receive_offer_cache.encode()
17131712
}
17141713

1715-
/// Indicates that a peer was connected to our node. Useful for the [`OffersMessageFlow`] to keep
1716-
/// track of which peers are connected, which allows for methods that can create blinded paths
1717-
/// without requiring a fresh set of [`MessageForwardNode`]s to be passed in.
1714+
/// Provides a set of peers of this node that can be used in [`BlindedMessagePath`]s created by
1715+
/// the [`OffersMessageFlow`].
17181716
///
1719-
/// `live_channel` should be set if we have an open, usable channel with this peer that has at
1720-
/// least six onchain confirmations.
1721-
///
1722-
/// MUST be called by always-online nodes that support holding HTLCs on behalf of often-offline
1723-
/// senders.
1724-
///
1725-
/// Errors if the peer does not support onion messages or we don't have a channel with them.
1726-
pub fn peer_connected(
1727-
&self, peer_node_id: PublicKey, features: &InitFeatures, live_channel: bool,
1728-
) -> Result<(), ()> {
1729-
if !features.supports_onion_messages() || !live_channel {
1730-
return Err(());
1731-
}
1732-
1717+
/// All provided peers MUST advertise support for onion messages in their [`InitFeatures`].
1718+
pub fn set_peers(&self, mut peers: Vec<MessageForwardNode>) {
17331719
let mut peers_cache = self.peers_cache.lock().unwrap();
1734-
let peer = MessageForwardNode { node_id: peer_node_id, short_channel_id: None };
1735-
peers_cache.push(peer);
1736-
1737-
Ok(())
1738-
}
1739-
1740-
/// Indicates that a peer was disconnected from our node. See [`Self::peer_connected`].
1741-
///
1742-
/// Errors if the peer is unknown.
1743-
pub fn peer_disconnected(&self, peer_node_id: PublicKey) -> Result<(), ()> {
1744-
let mut peers_cache = self.peers_cache.lock().unwrap();
1745-
let peer_idx = match peers_cache.iter().position(|peer| peer.node_id == peer_node_id) {
1746-
Some(idx) => idx,
1747-
None => return Err(()),
1748-
};
1749-
peers_cache.swap_remove(peer_idx);
1750-
1751-
Ok(())
1720+
core::mem::swap(&mut *peers_cache, &mut peers);
17521721
}
17531722
}

0 commit comments

Comments
 (0)