Skip to content

Commit 15ddb31

Browse files
committed
Drop the async-setting of the P2PGossipSync utxo_verifier
Now that we do not rely on circular references for `P2PGossipSync` validation, we no longer need the hacky `P2PGossipSync::add_utxo_lookup` method to add the gossip validator after building the `P2PGossipSync` first. Thus, we remove it here, updating some tests that relied on it.
1 parent ad78799 commit 15ddb31

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

lightning/src/routing/gossip.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ where
328328
L::Target: Logger,
329329
{
330330
network_graph: G,
331-
utxo_lookup: RwLock<Option<U>>,
331+
#[cfg(any(feature = "_test_utils", test))]
332+
pub(super) utxo_lookup: Option<U>,
333+
#[cfg(not(any(feature = "_test_utils", test)))]
334+
utxo_lookup: Option<U>,
332335
full_syncs_requested: AtomicUsize,
333336
pending_events: Mutex<Vec<MessageSendEvent>>,
334337
logger: L,
@@ -341,25 +344,19 @@ where
341344
{
342345
/// Creates a new tracker of the actual state of the network of channels and nodes,
343346
/// assuming an existing [`NetworkGraph`].
347+
///
344348
/// UTXO lookup is used to make sure announced channels exist on-chain, channel data is
345349
/// correct, and the announcement is signed with channel owners' keys.
346350
pub fn new(network_graph: G, utxo_lookup: Option<U>, logger: L) -> Self {
347351
P2PGossipSync {
348352
network_graph,
349353
full_syncs_requested: AtomicUsize::new(0),
350-
utxo_lookup: RwLock::new(utxo_lookup),
354+
utxo_lookup,
351355
pending_events: Mutex::new(vec![]),
352356
logger,
353357
}
354358
}
355359

356-
/// Adds a provider used to check new announcements. Does not affect
357-
/// existing announcements unless they are updated.
358-
/// Add, update or remove the provider would replace the current one.
359-
pub fn add_utxo_lookup(&self, utxo_lookup: Option<U>) {
360-
*self.utxo_lookup.write().unwrap() = utxo_lookup;
361-
}
362-
363360
/// Gets a reference to the underlying [`NetworkGraph`] which was provided in
364361
/// [`P2PGossipSync::new`].
365362
///
@@ -564,8 +561,7 @@ where
564561
fn handle_channel_announcement(
565562
&self, _their_node_id: Option<PublicKey>, msg: &msgs::ChannelAnnouncement,
566563
) -> Result<bool, LightningError> {
567-
self.network_graph
568-
.update_channel_from_announcement(msg, &*self.utxo_lookup.read().unwrap())?;
564+
self.network_graph.update_channel_from_announcement(msg, &self.utxo_lookup)?;
569565
Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
570566
}
571567

lightning/src/routing/router.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3943,10 +3943,7 @@ mod tests {
39433943
ChannelUsage, FixedPenaltyScorer, ProbabilisticScorer, ProbabilisticScoringDecayParameters,
39443944
ProbabilisticScoringFeeParameters, ScoreLookUp,
39453945
};
3946-
use crate::routing::test_utils::{
3947-
add_channel, add_or_update_node, build_graph, build_line_graph, get_nodes,
3948-
id_to_feature_flags, update_channel,
3949-
};
3946+
use crate::routing::test_utils::*;
39503947
use crate::routing::utxo::UtxoResult;
39513948
use crate::types::features::{BlindedHopFeatures, ChannelFeatures, InitFeatures, NodeFeatures};
39523949
use crate::util::config::UserConfig;
@@ -5368,7 +5365,7 @@ mod tests {
53685365
fn available_amount_while_routing_test() {
53695366
// Tests whether we choose the correct available channel amount while routing.
53705367

5371-
let (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) = build_graph();
5368+
let (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) = build_graph_with_gossip_validation();
53725369
let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
53735370
let scorer = ln_test_utils::TestScorer::new();
53745371
let random_seed_bytes = [42; 32];
@@ -5588,11 +5585,10 @@ mod tests {
55885585
.push_opcode(opcodes::all::OP_PUSHNUM_2)
55895586
.push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_p2wsh();
55905587

5588+
55915589
*chain_monitor.utxo_ret.lock().unwrap() =
55925590
UtxoResult::Sync(Ok(TxOut { value: Amount::from_sat(15), script_pubkey: good_script.clone() }));
5593-
gossip_sync.add_utxo_lookup(Some(chain_monitor));
5594-
5595-
add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 333);
5591+
add_channel_skipping_utxo_update(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 333);
55965592
update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
55975593
chain_hash: ChainHash::using_genesis_block(Network::Testnet),
55985594
short_channel_id: 333,

lightning/src/routing/test_utils.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
// licenses.
1111

1212
use crate::routing::gossip::{NetworkGraph, NodeAlias, P2PGossipSync};
13+
use crate::routing::utxo::UtxoResult;
1314
use crate::types::features::{ChannelFeatures, NodeFeatures};
15+
use crate::ln::chan_utils::make_funding_redeemscript;
1416
use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, MAX_VALUE_MSAT, NodeAnnouncement, RoutingMessageHandler, SocketAddress, UnsignedChannelAnnouncement, UnsignedChannelUpdate, UnsignedNodeAnnouncement};
1517
use crate::util::test_utils;
1618
use crate::util::ser::Writeable;
@@ -22,6 +24,7 @@ use bitcoin::hex::FromHex;
2224
use bitcoin::network::Network;
2325
use bitcoin::secp256k1::{PublicKey,SecretKey};
2426
use bitcoin::secp256k1::{Secp256k1, All};
27+
use bitcoin::{Amount, TxOut};
2528

2629
#[allow(unused)]
2730
use crate::prelude::*;
@@ -58,19 +61,34 @@ pub(crate) fn channel_announcement(
5861
}
5962

6063
// Using the same keys for LN and BTC ids
61-
pub(crate) fn add_channel(
64+
pub(crate) fn add_channel_skipping_utxo_update(
6265
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
63-
secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
66+
secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64,
6467
) {
6568
let valid_announcement =
6669
channel_announcement(node_1_privkey, node_2_privkey, features, short_channel_id, secp_ctx);
67-
let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
70+
71+
let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
6872
match gossip_sync.handle_channel_announcement(Some(node_1_pubkey), &valid_announcement) {
6973
Ok(res) => assert!(res),
70-
_ => panic!()
74+
Err(e) => panic!("{:?}", e),
7175
};
7276
}
7377

78+
pub(crate) fn add_channel(
79+
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
80+
secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64,
81+
) {
82+
gossip_sync.utxo_lookup.as_ref().map(|checker| {
83+
let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
84+
let node_2_pubkey = PublicKey::from_secret_key(&secp_ctx, &node_2_privkey);
85+
let script_pubkey = make_funding_redeemscript(&node_1_pubkey, &node_2_pubkey).to_p2wsh();
86+
*checker.utxo_ret.lock().unwrap() =
87+
UtxoResult::Sync(Ok(TxOut { value: Amount::from_sat(21_000_000_0000_0000), script_pubkey }));
88+
});
89+
add_channel_skipping_utxo_update(gossip_sync, secp_ctx, node_1_privkey, node_2_privkey, features, short_channel_id);
90+
}
91+
7492
pub(crate) fn add_or_update_node(
7593
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
7694
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
@@ -197,18 +215,43 @@ pub(super) fn build_line_graph() -> (
197215
(secp_ctx, network_graph, gossip_sync, chain_monitor, logger)
198216
}
199217

218+
pub(super) fn build_graph_with_gossip_validation() -> (
219+
Secp256k1<All>,
220+
sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
221+
P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
222+
sync::Arc<test_utils::TestChainSource>,
223+
sync::Arc<test_utils::TestLogger>,
224+
) {
225+
do_build_graph(true)
226+
}
227+
200228
pub(super) fn build_graph() -> (
201229
Secp256k1<All>,
202230
sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
203231
P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
204232
sync::Arc<test_utils::TestChainSource>,
205233
sync::Arc<test_utils::TestLogger>,
234+
) {
235+
do_build_graph(false)
236+
}
237+
238+
fn do_build_graph(with_validation: bool) -> (
239+
Secp256k1<All>,
240+
sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
241+
P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
242+
sync::Arc<test_utils::TestChainSource>,
243+
sync::Arc<test_utils::TestLogger>,
206244
) {
207245
let secp_ctx = Secp256k1::new();
208246
let logger = Arc::new(test_utils::TestLogger::new());
209247
let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
210248
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
211-
let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
249+
let checker = if with_validation {
250+
Some(Arc::clone(&chain_monitor))
251+
} else {
252+
None
253+
};
254+
let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), checker, Arc::clone(&logger));
212255
// Build network from our_id to node6:
213256
//
214257
// -1(1)2- node0 -1(3)2-

0 commit comments

Comments
 (0)