+ };
+
+ Self::check_replace_previous_entry(msg, full_msg, None,
+ &mut self.internal.lock().unwrap().channels)?;
+
+ match utxo_lookup {
+ &None => {
+ // Tentatively accept, potentially exposing us to DoS attacks
+ Ok(None)
+ },
+ &Some(ref utxo_lookup) => {
+ match utxo_lookup.get_utxo(&msg.chain_hash, msg.short_channel_id) {
+ UtxoResult::Sync(res) => handle_result(res),
+ UtxoResult::Async(future) => {
+ let mut pending_checks = self.internal.lock().unwrap();
+ let mut async_messages = future.state.lock().unwrap();
+ if let Some(res) = async_messages.complete.take() {
+ // In the unlikely event the future resolved before we managed to get it,
+ // handle the result in-line.
+ handle_result(res)
+ } else {
+ Self::check_replace_previous_entry(msg, full_msg,
+ Some(Arc::downgrade(&future.state)), &mut pending_checks.channels)?;
+ async_messages.channel_announce = Some(
+ if let Some(msg) = full_msg { ChannelAnnouncement::Full(msg.clone()) }
+ else { ChannelAnnouncement::Unsigned(msg.clone()) });
+ pending_checks.nodes.entry(msg.node_id_1)
+ .or_insert(Vec::new()).push(Arc::downgrade(&future.state));
+ pending_checks.nodes.entry(msg.node_id_2)
+ .or_insert(Vec::new()).push(Arc::downgrade(&future.state));
+ Err(LightningError {
+ err: "Channel being checked async".to_owned(),
+ action: ErrorAction::IgnoreAndLog(Level::Gossip),
+ })
+ }
+ },
+ }
+ }
+ }
+ }
+
+ /// The maximum number of pending gossip checks before [`Self::too_many_checks_pending`]
+ /// returns `true`. Note that this isn't a strict upper-bound on the number of checks pending -
+ /// each peer may, at a minimum, read one more socket buffer worth of `channel_announcement`s
+ /// which we'll have to process. With a socket buffer of 4KB and a minimum
+ /// `channel_announcement` size of, roughly, 429 bytes, this may leave us with `10*our peer
+ /// count` messages to process beyond this limit. Because we'll probably have a few peers,
+ /// there's no reason for this constant to be materially less than 30 or so, and 32 in-flight
+ /// checks should be more than enough for decent parallelism.
+ const MAX_PENDING_LOOKUPS: usize = 32;
+
+ /// Returns true if there are a large number of async checks pending and future
+ /// `channel_announcement` messages should be delayed. Note that this is only a hint and
+ /// messages already in-flight may still have to be handled for various reasons.
+ pub(super) fn too_many_checks_pending(&self) -> bool {
+ let mut pending_checks = self.internal.lock().unwrap();
+ if pending_checks.channels.len() > Self::MAX_PENDING_LOOKUPS {
+ // If we have many channel checks pending, ensure we don't have any dangling checks
+ // (i.e. checks where the user told us they'd call back but drop'd the `UtxoFuture`
+ // instead) before we commit to applying backpressure.
+ pending_checks.channels.retain(|_, chan| {
+ Weak::upgrade(&chan).is_some()
+ });
+ pending_checks.nodes.retain(|_, channels| {
+ channels.retain(|chan| Weak::upgrade(&chan).is_some());
+ !channels.is_empty()
+ });
+ pending_checks.channels.len() > Self::MAX_PENDING_LOOKUPS
+ } else {
+ false
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::routing::gossip::tests::*;
+ use crate::util::test_utils::{TestChainSource, TestLogger};
+ use crate::ln::msgs;
+
+ use bitcoin::secp256k1::{Secp256k1, SecretKey};
+
+ use core::sync::atomic::Ordering;
+
+ fn get_network() -> (TestChainSource, NetworkGraph<Box<TestLogger>>) {
+ let logger = Box::new(TestLogger::new());
+ let chain_source = TestChainSource::new(bitcoin::Network::Testnet);
+ let network_graph = NetworkGraph::new(bitcoin::Network::Testnet, logger);
+
+ (chain_source, network_graph)
+ }
+
+ fn get_test_objects() -> (msgs::ChannelAnnouncement, TestChainSource,
+ NetworkGraph<Box<TestLogger>>, bitcoin::Script, msgs::NodeAnnouncement,
+ msgs::NodeAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, msgs::ChannelUpdate)
+ {
+ let secp_ctx = Secp256k1::new();
+
+ let (chain_source, network_graph) = get_network();
+
+ let good_script = get_channel_script(&secp_ctx);
+ let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
+ let valid_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
+
+ let node_a_announce = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx);
+ let node_b_announce = get_signed_node_announcement(|_| {}, node_2_privkey, &secp_ctx);
+
+ // Note that we have to set the "direction" flag correctly on both messages
+ let chan_update_a = get_signed_channel_update(|msg| msg.flags = 0, node_1_privkey, &secp_ctx);
+ let chan_update_b = get_signed_channel_update(|msg| msg.flags = 1, node_2_privkey, &secp_ctx);
+ let chan_update_c = get_signed_channel_update(|msg| {
+ msg.flags = 1; msg.timestamp += 1; }, node_2_privkey, &secp_ctx);
+
+ (valid_announcement, chain_source, network_graph, good_script, node_a_announce,
+ node_b_announce, chan_update_a, chan_update_b, chan_update_c)
+ }
+
+ #[test]
+ fn test_fast_async_lookup() {
+ // Check that async lookups which resolve quicker than the future is returned to the
+ // `get_utxo` call can read it still resolve properly.
+ let (valid_announcement, chain_source, network_graph, good_script, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap();
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_some());
+ }
+
+ #[test]
+ fn test_async_lookup() {
+ // Test a simple async lookup
+ let (valid_announcement, chain_source, network_graph, good_script,
+ node_a_announce, node_b_announce, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 0, script_pubkey: good_script }));
+ network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).unwrap();
+ network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).unwrap();
+
+ assert!(network_graph.read_only().nodes().get(&valid_announcement.contents.node_id_1)
+ .unwrap().announcement_info.is_none());
+
+ network_graph.update_node_from_announcement(&node_a_announce).unwrap();
+ network_graph.update_node_from_announcement(&node_b_announce).unwrap();
+
+ assert!(network_graph.read_only().nodes().get(&valid_announcement.contents.node_id_1)
+ .unwrap().announcement_info.is_some());
+ }
+
+ #[test]
+ fn test_invalid_async_lookup() {
+ // Test an async lookup which returns an incorrect script
+ let (valid_announcement, chain_source, network_graph, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 1_000_000, script_pubkey: bitcoin::Script::new() }));
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+ }
+
+ #[test]
+ fn test_failing_async_lookup() {
+ // Test an async lookup which returns an error
+ let (valid_announcement, chain_source, network_graph, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+
+ future.resolve_without_forwarding(&network_graph, Err(UtxoLookupError::UnknownTx));
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+ }
+
+ #[test]
+ fn test_updates_async_lookup() {
+ // Test async lookups will process pending channel_update/node_announcements once they
+ // complete.
+ let (valid_announcement, chain_source, network_graph, good_script, node_a_announce,
+ node_b_announce, chan_update_a, chan_update_b, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+
+ assert_eq!(
+ network_graph.update_node_from_announcement(&node_a_announce).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept node_announcement");
+ assert_eq!(
+ network_graph.update_node_from_announcement(&node_b_announce).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept node_announcement");
+
+ assert_eq!(network_graph.update_channel(&chan_update_a).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept channel_update");
+ assert_eq!(network_graph.update_channel(&chan_update_b).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept channel_update");
+
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+
+ assert!(network_graph.read_only().channels()
+ .get(&valid_announcement.contents.short_channel_id).unwrap().one_to_two.is_some());
+ assert!(network_graph.read_only().channels()
+ .get(&valid_announcement.contents.short_channel_id).unwrap().two_to_one.is_some());
+
+ assert!(network_graph.read_only().nodes().get(&valid_announcement.contents.node_id_1)
+ .unwrap().announcement_info.is_some());
+ assert!(network_graph.read_only().nodes().get(&valid_announcement.contents.node_id_2)
+ .unwrap().announcement_info.is_some());
+ }
+
+ #[test]
+ fn test_latest_update_async_lookup() {
+ // Test async lookups will process the latest channel_update if two are received while
+ // awaiting an async UTXO lookup.
+ let (valid_announcement, chain_source, network_graph, good_script, _,
+ _, chan_update_a, chan_update_b, chan_update_c, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert!(network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id).is_none());
+
+ assert_eq!(network_graph.update_channel(&chan_update_a).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept channel_update");
+ assert_eq!(network_graph.update_channel(&chan_update_b).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept channel_update");
+ assert_eq!(network_graph.update_channel(&chan_update_c).unwrap_err().err,
+ "Awaiting channel_announcement validation to accept channel_update");
+
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+
+ assert_eq!(chan_update_a.contents.timestamp, chan_update_b.contents.timestamp);
+ let graph_lock = network_graph.read_only();
+ assert!(graph_lock.channels()
+ .get(&valid_announcement.contents.short_channel_id).as_ref().unwrap()
+ .one_to_two.as_ref().unwrap().last_update !=
+ graph_lock.channels()
+ .get(&valid_announcement.contents.short_channel_id).as_ref().unwrap()
+ .two_to_one.as_ref().unwrap().last_update);
+ }
+
+ #[test]
+ fn test_no_double_lookups() {
+ // Test that a pending async lookup will prevent a second async lookup from flying, but
+ // only if the channel_announcement message is identical.
+ let (valid_announcement, chain_source, network_graph, good_script, ..) = get_test_objects();
+
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert_eq!(chain_source.get_utxo_call_count.load(Ordering::Relaxed), 1);
+
+ // If we make a second request with the same message, the call count doesn't increase...
+ let future_b = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future_b.clone());
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel announcement is already being checked");
+ assert_eq!(chain_source.get_utxo_call_count.load(Ordering::Relaxed), 1);
+
+ // But if we make a third request with a tweaked message, we should get a second call
+ // against our new future...
+ let secp_ctx = Secp256k1::new();
+ let replacement_pk_1 = &SecretKey::from_slice(&[99; 32]).unwrap();
+ let replacement_pk_2 = &SecretKey::from_slice(&[98; 32]).unwrap();
+ let invalid_announcement = get_signed_channel_announcement(|_| {}, replacement_pk_1, replacement_pk_2, &secp_ctx);
+ assert_eq!(
+ network_graph.update_channel_from_announcement(&invalid_announcement, &Some(&chain_source)).unwrap_err().err,
+ "Channel being checked async");
+ assert_eq!(chain_source.get_utxo_call_count.load(Ordering::Relaxed), 2);
+
+ // Still, if we resolve the original future, the original channel will be accepted.
+ future.resolve_without_forwarding(&network_graph,
+ Ok(TxOut { value: 1_000_000, script_pubkey: good_script }));
+ assert!(!network_graph.read_only().channels()
+ .get(&valid_announcement.contents.short_channel_id).unwrap()
+ .announcement_message.as_ref().unwrap()
+ .contents.features.supports_unknown_test_feature());
+ }
+
+ #[test]
+ fn test_checks_backpressure() {
+ // Test that too_many_checks_pending returns true when there are many checks pending, and
+ // returns false once they complete.
+ let secp_ctx = Secp256k1::new();
+ let (chain_source, network_graph) = get_network();
+
+ // We cheat and use a single future for all the lookups to complete them all at once.
+ let future = UtxoFuture::new();
+ *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Async(future.clone());
+
+ let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
+
+ for i in 0..PendingChecks::MAX_PENDING_LOOKUPS {
+ let valid_announcement = get_signed_channel_announcement(
+ |msg| msg.short_channel_id += 1 + i as u64, node_1_privkey, node_2_privkey, &secp_ctx);
+ network_graph.update_channel_from_announcement(&valid_announcement, &Some(&chain_source)).unwrap_err();
+ assert!(!network_graph.pending_checks.too_many_checks_pending());