`impl Display for NodeId`
[rust-lightning] / lightning / src / routing / gossip.rs
index 1b9ef02e501e30c18d9cc05a34847c526ae93eff..19bde38912aed23ab04b9fbe85690955936e8f20 100644 (file)
@@ -29,8 +29,9 @@ use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds
 use crate::ln::msgs;
 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
 use crate::util::logger::{Logger, Level};
-use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
+use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
 use crate::util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
+use crate::util::string::PrintableString;
 
 use crate::io;
 use crate::io_extras::{copy, sink};
@@ -83,6 +84,11 @@ impl fmt::Debug for NodeId {
                write!(f, "NodeId({})", log_bytes!(self.0))
        }
 }
+impl fmt::Display for NodeId {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               write!(f, "{}", log_bytes!(self.0))
+       }
+}
 
 impl core::hash::Hash for NodeId {
        fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
@@ -212,9 +218,6 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
 /// This network graph is then used for routing payments.
 /// Provides interface to help with initial routing sync by
 /// serving historical announcements.
-///
-/// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
-/// [`NetworkGraph`].
 pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
 where C::Target: chain::Access, L::Target: Logger
 {
@@ -274,32 +277,31 @@ where C::Target: chain::Access, L::Target: Logger
        }
 }
 
-impl<L: Deref> EventHandler for NetworkGraph<L> where L::Target: Logger {
-       fn handle_event(&self, event: &Event) {
-               if let Event::PaymentPathFailed { network_update, .. } = event {
-                       if let Some(network_update) = network_update {
-                               match *network_update {
-                                       NetworkUpdate::ChannelUpdateMessage { ref msg } => {
-                                               let short_channel_id = msg.contents.short_channel_id;
-                                               let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
-                                               let status = if is_enabled { "enabled" } else { "disabled" };
-                                               log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
-                                               let _ = self.update_channel(msg);
-                                       },
-                                       NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
-                                               let action = if is_permanent { "Removing" } else { "Disabling" };
-                                               log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
-                                               self.channel_failed(short_channel_id, is_permanent);
-                                       },
-                                       NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
-                                               if is_permanent {
-                                                       log_debug!(self.logger,
-                                                               "Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
-                                                       self.node_failed_permanent(node_id);
-                                               };
-                                       },
-                               }
-                       }
+impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
+       /// Handles any network updates originating from [`Event`]s.
+       ///
+       /// [`Event`]: crate::util::events::Event
+       pub fn handle_network_update(&self, network_update: &NetworkUpdate) {
+               match *network_update {
+                       NetworkUpdate::ChannelUpdateMessage { ref msg } => {
+                               let short_channel_id = msg.contents.short_channel_id;
+                               let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
+                               let status = if is_enabled { "enabled" } else { "disabled" };
+                               log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
+                               let _ = self.update_channel(msg);
+                       },
+                       NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
+                               let action = if is_permanent { "Removing" } else { "Disabling" };
+                               log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
+                               self.channel_failed(short_channel_id, is_permanent);
+                       },
+                       NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
+                               if is_permanent {
+                                       log_debug!(self.logger,
+                                               "Removed node graph entry for {} due to a payment failure.", log_pubkey!(node_id));
+                                       self.node_failed_permanent(node_id);
+                               };
+                       },
                }
        }
 }
@@ -676,13 +678,13 @@ impl Writeable for ChannelUpdateInfo {
 
 impl Readable for ChannelUpdateInfo {
        fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               init_tlv_field_var!(last_update, required);
-               init_tlv_field_var!(enabled, required);
-               init_tlv_field_var!(cltv_expiry_delta, required);
-               init_tlv_field_var!(htlc_minimum_msat, required);
-               init_tlv_field_var!(htlc_maximum_msat, option);
-               init_tlv_field_var!(fees, required);
-               init_tlv_field_var!(last_update_message, required);
+               _init_tlv_field_var!(last_update, required);
+               _init_tlv_field_var!(enabled, required);
+               _init_tlv_field_var!(cltv_expiry_delta, required);
+               _init_tlv_field_var!(htlc_minimum_msat, required);
+               _init_tlv_field_var!(htlc_maximum_msat, option);
+               _init_tlv_field_var!(fees, required);
+               _init_tlv_field_var!(last_update_message, required);
 
                read_tlv_fields!(reader, {
                        (0, last_update, required),
@@ -696,13 +698,13 @@ impl Readable for ChannelUpdateInfo {
 
                if let Some(htlc_maximum_msat) = htlc_maximum_msat {
                        Ok(ChannelUpdateInfo {
-                               last_update: init_tlv_based_struct_field!(last_update, required),
-                               enabled: init_tlv_based_struct_field!(enabled, required),
-                               cltv_expiry_delta: init_tlv_based_struct_field!(cltv_expiry_delta, required),
-                               htlc_minimum_msat: init_tlv_based_struct_field!(htlc_minimum_msat, required),
+                               last_update: _init_tlv_based_struct_field!(last_update, required),
+                               enabled: _init_tlv_based_struct_field!(enabled, required),
+                               cltv_expiry_delta: _init_tlv_based_struct_field!(cltv_expiry_delta, required),
+                               htlc_minimum_msat: _init_tlv_based_struct_field!(htlc_minimum_msat, required),
                                htlc_maximum_msat,
-                               fees: init_tlv_based_struct_field!(fees, required),
-                               last_update_message: init_tlv_based_struct_field!(last_update_message, required),
+                               fees: _init_tlv_based_struct_field!(fees, required),
+                               last_update_message: _init_tlv_based_struct_field!(last_update_message, required),
                        })
                } else {
                        Err(DecodeError::InvalidValue)
@@ -823,14 +825,14 @@ impl MaybeReadable for ChannelUpdateInfoDeserWrapper {
 
 impl Readable for ChannelInfo {
        fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               init_tlv_field_var!(features, required);
-               init_tlv_field_var!(announcement_received_time, (default_value, 0));
-               init_tlv_field_var!(node_one, required);
+               _init_tlv_field_var!(features, required);
+               _init_tlv_field_var!(announcement_received_time, (default_value, 0));
+               _init_tlv_field_var!(node_one, required);
                let mut one_to_two_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
-               init_tlv_field_var!(node_two, required);
+               _init_tlv_field_var!(node_two, required);
                let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
-               init_tlv_field_var!(capacity_sats, required);
-               init_tlv_field_var!(announcement_message, required);
+               _init_tlv_field_var!(capacity_sats, required);
+               _init_tlv_field_var!(announcement_message, required);
                read_tlv_fields!(reader, {
                        (0, features, required),
                        (1, announcement_received_time, (default_value, 0)),
@@ -843,14 +845,14 @@ impl Readable for ChannelInfo {
                });
 
                Ok(ChannelInfo {
-                       features: init_tlv_based_struct_field!(features, required),
-                       node_one: init_tlv_based_struct_field!(node_one, required),
+                       features: _init_tlv_based_struct_field!(features, required),
+                       node_one: _init_tlv_based_struct_field!(node_one, required),
                        one_to_two: one_to_two_wrap.map(|w| w.0).unwrap_or(None),
-                       node_two: init_tlv_based_struct_field!(node_two, required),
+                       node_two: _init_tlv_based_struct_field!(node_two, required),
                        two_to_one: two_to_one_wrap.map(|w| w.0).unwrap_or(None),
-                       capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
-                       announcement_message: init_tlv_based_struct_field!(announcement_message, required),
-                       announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
+                       capacity_sats: _init_tlv_based_struct_field!(capacity_sats, required),
+                       announcement_message: _init_tlv_based_struct_field!(announcement_message, required),
+                       announcement_received_time: _init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
                })
        }
 }
@@ -874,7 +876,7 @@ impl<'a> DirectedChannelInfo<'a> {
                let effective_capacity = match capacity_msat {
                        Some(capacity_msat) => {
                                htlc_maximum_msat = cmp::min(htlc_maximum_msat, capacity_msat);
-                               EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: Some(htlc_maximum_msat) }
+                               EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: htlc_maximum_msat }
                        },
                        None => EffectiveCapacity::MaximumHTLC { amount_msat: htlc_maximum_msat },
                };
@@ -939,7 +941,7 @@ pub enum EffectiveCapacity {
                /// The funding amount denominated in millisatoshi.
                capacity_msat: u64,
                /// The maximum HTLC amount denominated in millisatoshi.
-               htlc_maximum_msat: Option<u64>
+               htlc_maximum_msat: u64
        },
        /// A capacity sufficient to route any payment, typically used for private channels provided by
        /// an invoice.
@@ -1022,23 +1024,17 @@ pub struct NodeAlias(pub [u8; 32]);
 
 impl fmt::Display for NodeAlias {
        fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
-               let control_symbol = core::char::REPLACEMENT_CHARACTER;
                let first_null = self.0.iter().position(|b| *b == 0).unwrap_or(self.0.len());
                let bytes = self.0.split_at(first_null).0;
                match core::str::from_utf8(bytes) {
-                       Ok(alias) => {
-                               for c in alias.chars() {
-                                       let mut bytes = [0u8; 4];
-                                       let c = if !c.is_control() { c } else { control_symbol };
-                                       f.write_str(c.encode_utf8(&mut bytes))?;
-                               }
-                       },
+                       Ok(alias) => PrintableString(alias).fmt(f)?,
                        Err(_) => {
+                               use core::fmt::Write;
                                for c in bytes.iter().map(|b| *b as char) {
                                        // Display printable ASCII characters
-                                       let mut bytes = [0u8; 4];
+                                       let control_symbol = core::char::REPLACEMENT_CHARACTER;
                                        let c = if c >= '\x20' && c <= '\x7e' { c } else { control_symbol };
-                                       f.write_str(c.encode_utf8(&mut bytes))?;
+                                       f.write_char(c)?;
                                }
                        },
                };
@@ -1112,9 +1108,9 @@ impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
 
 impl Readable for NodeInfo {
        fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               init_tlv_field_var!(lowest_inbound_channel_fees, option);
+               _init_tlv_field_var!(lowest_inbound_channel_fees, option);
                let mut announcement_info_wrap: Option<NodeAnnouncementInfoDeserWrapper> = None;
-               init_tlv_field_var!(channels, vec_type);
+               _init_tlv_field_var!(channels, vec_type);
 
                read_tlv_fields!(reader, {
                        (0, lowest_inbound_channel_fees, option),
@@ -1123,9 +1119,9 @@ impl Readable for NodeInfo {
                });
 
                Ok(NodeInfo {
-                       lowest_inbound_channel_fees: init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
+                       lowest_inbound_channel_fees: _init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
                        announcement_info: announcement_info_wrap.map(|w| w.0),
-                       channels: init_tlv_based_struct_field!(channels, vec_type),
+                       channels: _init_tlv_based_struct_field!(channels, vec_type),
                })
        }
 }
@@ -1550,6 +1546,14 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
                #[cfg(not(feature = "std"))]
                let current_time_unix = None;
 
+               self.channel_failed_with_time(short_channel_id, is_permanent, current_time_unix)
+       }
+
+       /// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
+       /// If permanent, removes a channel from the local storage.
+       /// May cause the removal of nodes too, if this was their last channel.
+       /// If not permanent, makes channels unavailable for routing.
+       fn channel_failed_with_time(&self, short_channel_id: u64, is_permanent: bool, current_time_unix: Option<u64>) {
                let mut channels = self.channels.write().unwrap();
                if is_permanent {
                        if let Some(chan) = channels.remove(&short_channel_id) {
@@ -1653,7 +1657,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
                        if info.two_to_one.is_some() && info.two_to_one.as_ref().unwrap().last_update < min_time_unix {
                                info.two_to_one = None;
                        }
-                       if info.one_to_two.is_none() && info.two_to_one.is_none() {
+                       if info.one_to_two.is_none() || info.two_to_one.is_none() {
                                // We check the announcement_received_time here to ensure we don't drop
                                // announcements that we just received and are just waiting for our peer to send a
                                // channel_update for.
@@ -1667,6 +1671,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
                        for scid in scids_to_remove {
                                let info = channels.remove(&scid).expect("We just accessed this scid, it should be present");
                                Self::remove_channel_in_nodes(&mut nodes, &info, scid);
+                               self.removed_channels.lock().unwrap().insert(scid, Some(current_time_unix));
                        }
                }
 
@@ -1935,15 +1940,15 @@ mod tests {
        use crate::chain;
        use crate::ln::channelmanager;
        use crate::ln::chan_utils::make_funding_redeemscript;
-       use crate::ln::PaymentHash;
        use crate::ln::features::InitFeatures;
        use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
        use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
                UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
                ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
+       use crate::util::config::UserConfig;
        use crate::util::test_utils;
        use crate::util::ser::{ReadableArgs, Writeable};
-       use crate::util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
+       use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
        use crate::util::scid_utils::scid_from_parts;
 
        use crate::routing::gossip::REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS;
@@ -2000,7 +2005,7 @@ mod tests {
        fn get_signed_node_announcement<F: Fn(&mut UnsignedNodeAnnouncement)>(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> NodeAnnouncement {
                let node_id = PublicKey::from_secret_key(&secp_ctx, node_key);
                let mut unsigned_announcement = UnsignedNodeAnnouncement {
-                       features: channelmanager::provided_node_features(),
+                       features: channelmanager::provided_node_features(&UserConfig::default()),
                        timestamp: 100,
                        node_id: node_id,
                        rgb: [0; 3],
@@ -2024,7 +2029,7 @@ mod tests {
                let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
 
                let mut unsigned_announcement = UnsignedChannelAnnouncement {
-                       features: channelmanager::provided_channel_features(),
+                       features: channelmanager::provided_channel_features(&UserConfig::default()),
                        chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                        short_channel_id: 0,
                        node_id_1,
@@ -2387,19 +2392,8 @@ mod tests {
                        let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
                        assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
 
-                       network_graph.handle_event(&Event::PaymentPathFailed {
-                               payment_id: None,
-                               payment_hash: PaymentHash([0; 32]),
-                               payment_failed_permanently: false,
-                               all_paths_failed: true,
-                               path: vec![],
-                               network_update: Some(NetworkUpdate::ChannelUpdateMessage {
-                                       msg: valid_channel_update,
-                               }),
-                               short_channel_id: None,
-                               retry: None,
-                               error_code: None,
-                               error_data: None,
+                       network_graph.handle_network_update(&NetworkUpdate::ChannelUpdateMessage {
+                               msg: valid_channel_update,
                        });
 
                        assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
@@ -2414,20 +2408,9 @@ mod tests {
                                }
                        };
 
-                       network_graph.handle_event(&Event::PaymentPathFailed {
-                               payment_id: None,
-                               payment_hash: PaymentHash([0; 32]),
-                               payment_failed_permanently: false,
-                               all_paths_failed: true,
-                               path: vec![],
-                               network_update: Some(NetworkUpdate::ChannelFailure {
-                                       short_channel_id,
-                                       is_permanent: false,
-                               }),
-                               short_channel_id: None,
-                               retry: None,
-                               error_code: None,
-                               error_data: None,
+                       network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
+                               short_channel_id,
+                               is_permanent: false,
                        });
 
                        match network_graph.read_only().channels().get(&short_channel_id) {
@@ -2439,20 +2422,9 @@ mod tests {
                }
 
                // Permanent closing deletes a channel
-               network_graph.handle_event(&Event::PaymentPathFailed {
-                       payment_id: None,
-                       payment_hash: PaymentHash([0; 32]),
-                       payment_failed_permanently: false,
-                       all_paths_failed: true,
-                       path: vec![],
-                       network_update: Some(NetworkUpdate::ChannelFailure {
-                               short_channel_id,
-                               is_permanent: true,
-                       }),
-                       short_channel_id: None,
-                       retry: None,
-                       error_code: None,
-                       error_data: None,
+               network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
+                       short_channel_id,
+                       is_permanent: true,
                });
 
                assert_eq!(network_graph.read_only().channels().len(), 0);
@@ -2471,40 +2443,18 @@ mod tests {
                        assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
 
                        // Non-permanent node failure does not delete any nodes or channels
-                       network_graph.handle_event(&Event::PaymentPathFailed {
-                               payment_id: None,
-                               payment_hash: PaymentHash([0; 32]),
-                               payment_failed_permanently: false,
-                               all_paths_failed: true,
-                               path: vec![],
-                               network_update: Some(NetworkUpdate::NodeFailure {
-                                       node_id: node_2_id,
-                                       is_permanent: false,
-                               }),
-                               short_channel_id: None,
-                               retry: None,
-                               error_code: None,
-                               error_data: None,
+                       network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
+                               node_id: node_2_id,
+                               is_permanent: false,
                        });
 
                        assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
                        assert!(network_graph.read_only().nodes().get(&NodeId::from_pubkey(&node_2_id)).is_some());
 
                        // Permanent node failure deletes node and its channels
-                       network_graph.handle_event(&Event::PaymentPathFailed {
-                               payment_id: None,
-                               payment_hash: PaymentHash([0; 32]),
-                               payment_failed_permanently: false,
-                               all_paths_failed: true,
-                               path: vec![],
-                               network_update: Some(NetworkUpdate::NodeFailure {
-                                       node_id: node_2_id,
-                                       is_permanent: true,
-                               }),
-                               short_channel_id: None,
-                               retry: None,
-                               error_code: None,
-                               error_data: None,
+                       network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
+                               node_id: node_2_id,
+                               is_permanent: true,
                        });
 
                        assert_eq!(network_graph.read_only().nodes().len(), 0);
@@ -2532,32 +2482,57 @@ mod tests {
                assert!(network_graph.update_channel_from_announcement(&valid_channel_announcement, &chain_source).is_ok());
                assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
 
+               // Submit two channel updates for each channel direction (update.flags bit).
                let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
                assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok());
                assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
 
+               let valid_channel_update_2 = get_signed_channel_update(|update| {update.flags |=1;}, node_2_privkey, &secp_ctx);
+               gossip_sync.handle_channel_update(&valid_channel_update_2).unwrap();
+               assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().two_to_one.is_some());
+
                network_graph.remove_stale_channels_and_tracking_with_time(100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
                assert_eq!(network_graph.read_only().channels().len(), 1);
                assert_eq!(network_graph.read_only().nodes().len(), 2);
 
                network_graph.remove_stale_channels_and_tracking_with_time(101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
+               #[cfg(not(feature = "std"))] {
+                       // Make sure removed channels are tracked.
+                       assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
+               }
+               network_graph.remove_stale_channels_and_tracking_with_time(101 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS +
+                       REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
+
                #[cfg(feature = "std")]
                {
                        // In std mode, a further check is performed before fully removing the channel -
                        // the channel_announcement must have been received at least two weeks ago. We
-                       // fudge that here by indicating the time has jumped two weeks. Note that the
-                       // directional channel information will have been removed already..
+                       // fudge that here by indicating the time has jumped two weeks.
                        assert_eq!(network_graph.read_only().channels().len(), 1);
                        assert_eq!(network_graph.read_only().nodes().len(), 2);
-                       assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
 
+                       // Note that the directional channel information will have been removed already..
+                       // We want to check that this will work even if *one* of the channel updates is recent,
+                       // so we should add it with a recent timestamp.
+                       assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
                        use std::time::{SystemTime, UNIX_EPOCH};
                        let announcement_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs();
+                       let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| {
+                               unsigned_channel_update.timestamp = (announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS) as u32;
+                       }, node_1_privkey, &secp_ctx);
+                       assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok());
+                       assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
                        network_graph.remove_stale_channels_and_tracking_with_time(announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS);
+                       // Make sure removed channels are tracked.
+                       assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
+                       // Provide a later time so that sufficient time has passed
+                       network_graph.remove_stale_channels_and_tracking_with_time(announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS +
+                               REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
                }
 
                assert_eq!(network_graph.read_only().channels().len(), 0);
                assert_eq!(network_graph.read_only().nodes().len(), 0);
+               assert!(network_graph.removed_channels.lock().unwrap().is_empty());
 
                #[cfg(feature = "std")]
                {
@@ -2576,18 +2551,18 @@ mod tests {
 
                        // Mark the channel as permanently failed. This will also remove the two nodes
                        // and all of the entries will be tracked as removed.
-                       network_graph.channel_failed(short_channel_id, true);
+                       network_graph.channel_failed_with_time(short_channel_id, true, Some(tracking_time));
 
                        // Should not remove from tracking if insufficient time has passed
                        network_graph.remove_stale_channels_and_tracking_with_time(
                                tracking_time + REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS - 1);
-                       assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1);
+                       assert_eq!(network_graph.removed_channels.lock().unwrap().len(), 1, "Removed channel count ≠ 1 with tracking_time {}", tracking_time);
 
                        // Provide a later time so that sufficient time has passed
                        network_graph.remove_stale_channels_and_tracking_with_time(
                                tracking_time + REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS);
-                       assert!(network_graph.removed_channels.lock().unwrap().is_empty());
-                       assert!(network_graph.removed_nodes.lock().unwrap().is_empty());
+                       assert!(network_graph.removed_channels.lock().unwrap().is_empty(), "Unexpectedly removed channels with tracking_time {}", tracking_time);
+                       assert!(network_graph.removed_nodes.lock().unwrap().is_empty(), "Unexpectedly removed nodes with tracking_time {}", tracking_time);
                }
 
                #[cfg(not(feature = "std"))]
@@ -3188,6 +3163,7 @@ mod tests {
                let node_cfgs = crate::ln::functional_test_utils::create_node_cfgs(2, &chanmon_cfgs);
                let node_chanmgrs = crate::ln::functional_test_utils::create_node_chanmgrs(2, &node_cfgs, &[None, None, None, None]);
                let nodes = crate::ln::functional_test_utils::create_network(2, &node_cfgs, &node_chanmgrs);
+               let config = crate::ln::functional_test_utils::test_default_channel_config();
 
                // 1. Test encoding/decoding of ChannelUpdateInfo
                let chan_update_info = ChannelUpdateInfo {
@@ -3224,7 +3200,7 @@ mod tests {
                // 2. Test encoding/decoding of ChannelInfo
                // Check we can encode/decode ChannelInfo without ChannelUpdateInfo fields present.
                let chan_info_none_updates = ChannelInfo {
-                       features: channelmanager::provided_channel_features(),
+                       features: channelmanager::provided_channel_features(&config),
                        node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
                        one_to_two: None,
                        node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
@@ -3242,7 +3218,7 @@ mod tests {
 
                // Check we can encode/decode ChannelInfo with ChannelUpdateInfo fields present.
                let chan_info_some_updates = ChannelInfo {
-                       features: channelmanager::provided_channel_features(),
+                       features: channelmanager::provided_channel_features(&config),
                        node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
                        one_to_two: Some(chan_update_info.clone()),
                        node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
@@ -3284,7 +3260,7 @@ mod tests {
                // 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
                let valid_netaddr = crate::ln::msgs::NetAddress::Hostname { hostname: crate::util::ser::Hostname::try_from("A".to_string()).unwrap(), port: 1234 };
                let valid_node_ann_info = NodeAnnouncementInfo {
-                       features: channelmanager::provided_node_features(),
+                       features: channelmanager::provided_node_features(&UserConfig::default()),
                        last_update: 0,
                        rgb: [0u8; 3],
                        alias: NodeAlias([0u8; 32]),