Skip to content

Commit 9a3bc6b

Browse files
committed
Make the P2PGossipSync UtxoLookup exchangable without &mut self
Because a `UtxoLookup` implementation is likely to need a reference to the `PeerManager` which contains a reference to the `P2PGossipSync`, it is likely to be impossible to get a mutable reference to the `P2PGossipSync` by the time we want to add a `UtxoLookup` without a ton of boilerplate and trait wrapping. Instead, we simply place the `UtxoLookup` in a `RwLock`, allowing us to modify it without a mutable self reference. The lifetime bounds updates in tests required in this commit are entirely unclear to me, but do allow tests to continue building, so somehow make rustc happier.
1 parent 9a8b94f commit 9a3bc6b

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,11 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
542542
}
543543
}
544544

545-
pub fn create_chan_between_nodes<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
545+
pub fn create_chan_between_nodes<'a, 'b, 'c: 'd, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
546546
create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001)
547547
}
548548

549-
pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
549+
pub fn create_chan_between_nodes_with_value<'a, 'b, 'c: 'd, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
550550
let (channel_ready, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat);
551551
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &channel_ready);
552552
(announcement, as_update, bs_update, channel_id, tx)
@@ -1119,7 +1119,7 @@ pub fn create_chan_between_nodes_with_value_confirm_second<'a, 'b, 'c>(node_recv
11191119
}), channel_id)
11201120
}
11211121

1122-
pub fn create_chan_between_nodes_with_value_confirm<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, tx: &Transaction) -> ((msgs::ChannelReady, msgs::AnnouncementSignatures), [u8; 32]) {
1122+
pub fn create_chan_between_nodes_with_value_confirm<'a, 'b, 'c: 'd, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, tx: &Transaction) -> ((msgs::ChannelReady, msgs::AnnouncementSignatures), [u8; 32]) {
11231123
let conf_height = core::cmp::max(node_a.best_block_info().1 + 1, node_b.best_block_info().1 + 1);
11241124
create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx, conf_height);
11251125
confirm_transaction_at(node_a, tx, conf_height);
@@ -1128,7 +1128,7 @@ pub fn create_chan_between_nodes_with_value_confirm<'a, 'b, 'c, 'd>(node_a: &'a
11281128
create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
11291129
}
11301130

1131-
pub fn create_chan_between_nodes_with_value_a<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64) -> ((msgs::ChannelReady, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
1131+
pub fn create_chan_between_nodes_with_value_a<'a, 'b, 'c: 'd, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64) -> ((msgs::ChannelReady, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
11321132
let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat);
11331133
let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
11341134
(msgs, chan_id, tx)
@@ -1168,11 +1168,11 @@ pub fn create_chan_between_nodes_with_value_b<'a, 'b, 'c>(node_a: &Node<'a, 'b,
11681168
((*announcement).clone(), as_update, bs_update)
11691169
}
11701170

1171-
pub fn create_announced_chan_between_nodes<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
1171+
pub fn create_announced_chan_between_nodes<'a, 'b, 'c: 'd, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
11721172
create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001)
11731173
}
11741174

1175-
pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize, channel_value: u64, push_msat: u64) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
1175+
pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c: 'd, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize, channel_value: u64, push_msat: u64) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
11761176
let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat);
11771177
update_nodes_with_chan_announce(nodes, a, b, &chan_announcement.0, &chan_announcement.1, &chan_announcement.2);
11781178
(chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)

lightning/src/routing/gossip.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref>
254254
where U::Target: UtxoLookup, L::Target: Logger
255255
{
256256
network_graph: G,
257-
utxo_lookup: Option<U>,
257+
utxo_lookup: RwLock<Option<U>>,
258258
#[cfg(feature = "std")]
259259
full_syncs_requested: AtomicUsize,
260260
pending_events: Mutex<Vec<MessageSendEvent>>,
@@ -273,7 +273,7 @@ where U::Target: UtxoLookup, L::Target: Logger
273273
network_graph,
274274
#[cfg(feature = "std")]
275275
full_syncs_requested: AtomicUsize::new(0),
276-
utxo_lookup,
276+
utxo_lookup: RwLock::new(utxo_lookup),
277277
pending_events: Mutex::new(vec![]),
278278
logger,
279279
}
@@ -282,8 +282,8 @@ where U::Target: UtxoLookup, L::Target: Logger
282282
/// Adds a provider used to check new announcements. Does not affect
283283
/// existing announcements unless they are updated.
284284
/// Add, update or remove the provider would replace the current one.
285-
pub fn add_utxo_lookup(&mut self, utxo_lookup: Option<U>) {
286-
self.utxo_lookup = utxo_lookup;
285+
pub fn add_utxo_lookup(&self, utxo_lookup: Option<U>) {
286+
*self.utxo_lookup.write().unwrap() = utxo_lookup;
287287
}
288288

289289
/// Gets a reference to the underlying [`NetworkGraph`] which was provided in
@@ -420,7 +420,7 @@ where U::Target: UtxoLookup, L::Target: Logger
420420
}
421421

422422
fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
423-
self.network_graph.update_channel_from_announcement(msg, &self.utxo_lookup)?;
423+
self.network_graph.update_channel_from_announcement(msg, &*self.utxo_lookup.read().unwrap())?;
424424
Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
425425
}
426426

lightning/src/routing/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,7 @@ mod tests {
36703670
fn available_amount_while_routing_test() {
36713671
// Tests whether we choose the correct available channel amount while routing.
36723672

3673-
let (secp_ctx, network_graph, mut gossip_sync, chain_monitor, logger) = build_graph();
3673+
let (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) = build_graph();
36743674
let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
36753675
let scorer = ln_test_utils::TestScorer::new();
36763676
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);

0 commit comments

Comments
 (0)