Skip to content

Commit 910c29c

Browse files
WIP cache peers in Flow
1 parent 20fdbec commit 910c29c

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
@@ -8018,6 +8018,8 @@ where
80188018

80198019
should_persist
80208020
});
8021+
8022+
self.flow.set_peers(self.get_peers_for_blinded_path());
80218023
}
80228024

80238025
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
@@ -10400,16 +10402,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1040010402
emit_initial_channel_ready_event!(pending_events, chan);
1040110403
}
1040210404

10403-
Ok(())
1040410405
} else {
1040510406
try_channel_entry!(self, peer_state, Err(ChannelError::close(
1040610407
"Got a channel_ready message for an unfunded channel!".into())), chan_entry)
1040710408
}
1040810409
},
1040910410
hash_map::Entry::Vacant(_) => {
10410-
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))
10411+
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))
1041110412
}
1041210413
}
10414+
core::mem::drop(peer_state_lock);
10415+
core::mem::drop(per_peer_state);
10416+
10417+
self.flow.set_peers(self.get_peers_for_blinded_path());
10418+
Ok(())
1041310419
}
1041410420

1041510421
fn internal_shutdown(
@@ -10502,6 +10508,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1050210508
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
1050310509
}
1050410510

10511+
self.flow.set_peers(self.get_peers_for_blinded_path());
1050510512
Ok(())
1050610513
}
1050710514

@@ -13260,7 +13267,7 @@ where
1326013267
let _ = handle_error!(self, err, counterparty_node_id);
1326113268
}
1326213269

13263-
let _ = self.flow.peer_disconnected(counterparty_node_id);
13270+
self.flow.set_peers(self.get_peers_for_blinded_path());
1326413271
}
1326513272

1326613273
#[rustfmt::skip]
@@ -13375,7 +13382,7 @@ where
1337513382
// until we have some peer connection(s) to receive onion messages over, so as a minor optimization
1337613383
// refresh the cache when a peer connects.
1337713384
self.check_refresh_async_receive_offer_cache(false);
13378-
let _ = self.flow.peer_connected(counterparty_node_id, &init_msg.features, peer_has_live_channel);
13385+
self.flow.set_peers(self.get_peers_for_blinded_path());
1337913386
res
1338013387
}
1338113388

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)