Merge pull request #1977 from jkczyz/2023-01-offers-fuzz
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Sat, 25 Feb 2023 18:03:51 +0000 (18:03 +0000)
committerGitHub <noreply@github.com>
Sat, 25 Feb 2023 18:03:51 +0000 (18:03 +0000)
BOLT 12 deserialization fuzzers

1  2 
lightning/src/routing/gossip.rs
lightning/src/util/ser_macros.rs

index cf51b6ab52827943626ede2cbc5d0e87e1f95366,d3e476fd5fc0391c3fd6ff06e5bbf7933d4a4da7..957aecb09486568efe1486342ce0695994e4aa41
@@@ -7,7 -7,7 +7,7 @@@
  // You may not use this file except in accordance with one or both of these
  // licenses.
  
 -//! The top-level network map tracking logic lives here.
 +//! The [`NetworkGraph`] stores the network gossip and [`P2PGossipSync`] fetches it from peers
  
  use bitcoin::secp256k1::constants::PUBLIC_KEY_SIZE;
  use bitcoin::secp256k1::PublicKey;
@@@ -16,14 -16,17 +16,14 @@@ use bitcoin::secp256k1
  
  use bitcoin::hashes::sha256d::Hash as Sha256dHash;
  use bitcoin::hashes::Hash;
 -use bitcoin::blockdata::transaction::TxOut;
  use bitcoin::hash_types::BlockHash;
  
 -use crate::chain;
 -use crate::chain::Access;
 -use crate::ln::chan_utils::make_funding_redeemscript;
  use crate::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
  use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
  use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter};
  use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
  use crate::ln::msgs;
 +use crate::routing::utxo::{self, UtxoLookup};
  use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
  use crate::util::logger::{Logger, Level};
  use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
@@@ -40,6 -43,7 +40,6 @@@ use crate::sync::{RwLock, RwLockReadGua
  use core::sync::atomic::{AtomicUsize, Ordering};
  use crate::sync::Mutex;
  use core::ops::{Bound, Deref};
 -use bitcoin::hashes::hex::ToHex;
  
  #[cfg(feature = "std")]
  use std::time::{SystemTime, UNIX_EPOCH};
@@@ -155,8 -159,6 +155,8 @@@ pub struct NetworkGraph<L: Deref> wher
        /// resync them from gossip. Each `NodeId` is mapped to the time (in seconds) it was removed so
        /// that once some time passes, we can potentially resync it from gossip again.
        removed_nodes: Mutex<HashMap<NodeId, Option<u64>>>,
 +      /// Announcement messages which are awaiting an on-chain lookup to be processed.
 +      pub(super) pending_checks: utxo::PendingChecks,
  }
  
  /// A read-only view of [`NetworkGraph`].
@@@ -216,30 -218,31 +216,30 @@@ impl_writeable_tlv_based_enum_upgradabl
  /// This network graph is then used for routing payments.
  /// Provides interface to help with initial routing sync by
  /// serving historical announcements.
 -pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
 -where C::Target: chain::Access, L::Target: Logger
 +pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref>
 +where U::Target: UtxoLookup, L::Target: Logger
  {
        network_graph: G,
 -      chain_access: Option<C>,
 +      utxo_lookup: Option<U>,
        #[cfg(feature = "std")]
        full_syncs_requested: AtomicUsize,
        pending_events: Mutex<Vec<MessageSendEvent>>,
        logger: L,
  }
  
 -impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> P2PGossipSync<G, C, L>
 -where C::Target: chain::Access, L::Target: Logger
 +impl<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref> P2PGossipSync<G, U, L>
 +where U::Target: UtxoLookup, L::Target: Logger
  {
        /// Creates a new tracker of the actual state of the network of channels and nodes,
        /// assuming an existing Network Graph.
 -      /// Chain monitor is used to make sure announced channels exist on-chain,
 -      /// channel data is correct, and that the announcement is signed with
 -      /// channel owners' keys.
 -      pub fn new(network_graph: G, chain_access: Option<C>, logger: L) -> Self {
 +      /// UTXO lookup is used to make sure announced channels exist on-chain, channel data is
 +      /// correct, and the announcement is signed with channel owners' keys.
 +      pub fn new(network_graph: G, utxo_lookup: Option<U>, logger: L) -> Self {
                P2PGossipSync {
                        network_graph,
                        #[cfg(feature = "std")]
                        full_syncs_requested: AtomicUsize::new(0),
 -                      chain_access,
 +                      utxo_lookup,
                        pending_events: Mutex::new(vec![]),
                        logger,
                }
        /// Adds a provider used to check new announcements. Does not affect
        /// existing announcements unless they are updated.
        /// Add, update or remove the provider would replace the current one.
 -      pub fn add_chain_access(&mut self, chain_access: Option<C>) {
 -              self.chain_access = chain_access;
 +      pub fn add_utxo_lookup(&mut self, utxo_lookup: Option<U>) {
 +              self.utxo_lookup = utxo_lookup;
        }
  
        /// Gets a reference to the underlying [`NetworkGraph`] which was provided in
                        false
                }
        }
 +
 +      /// Used to broadcast forward gossip messages which were validated async.
 +      ///
 +      /// Note that this will ignore events other than `Broadcast*` or messages with too much excess
 +      /// data.
 +      pub(super) fn forward_gossip_msg(&self, mut ev: MessageSendEvent) {
 +              match &mut ev {
 +                      MessageSendEvent::BroadcastChannelAnnouncement { msg, ref mut update_msg } => {
 +                              if msg.contents.excess_data.len() > MAX_EXCESS_BYTES_FOR_RELAY { return; }
 +                              if update_msg.as_ref()
 +                                      .map(|msg| msg.contents.excess_data.len()).unwrap_or(0) > MAX_EXCESS_BYTES_FOR_RELAY
 +                              {
 +                                      *update_msg = None;
 +                              }
 +                      },
 +                      MessageSendEvent::BroadcastChannelUpdate { msg } => {
 +                              if msg.contents.excess_data.len() > MAX_EXCESS_BYTES_FOR_RELAY { return; }
 +                      },
 +                      MessageSendEvent::BroadcastNodeAnnouncement { msg } => {
 +                              if msg.contents.excess_data.len() >  MAX_EXCESS_BYTES_FOR_RELAY ||
 +                                 msg.contents.excess_address_data.len() > MAX_EXCESS_BYTES_FOR_RELAY ||
 +                                 msg.contents.excess_data.len() + msg.contents.excess_address_data.len() > MAX_EXCESS_BYTES_FOR_RELAY
 +                              {
 +                                      return;
 +                              }
 +                      },
 +                      _ => return,
 +              }
 +              self.pending_events.lock().unwrap().push(ev);
 +      }
  }
  
  impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
@@@ -353,24 -326,8 +353,24 @@@ macro_rules! secp_verify_sig 
        };
  }
  
 -impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync<G, C, L>
 -where C::Target: chain::Access, L::Target: Logger
 +macro_rules! get_pubkey_from_node_id {
 +      ( $node_id: expr, $msg_type: expr ) => {
 +              PublicKey::from_slice($node_id.as_slice())
 +                      .map_err(|_| LightningError {
 +                              err: format!("Invalid public key on {} message", $msg_type),
 +                              action: ErrorAction::SendWarningMessage {
 +                                      msg: msgs::WarningMessage {
 +                                              channel_id: [0; 32],
 +                                              data: format!("Invalid public key on {} message", $msg_type),
 +                                      },
 +                                      log_level: Level::Trace
 +                              }
 +                      })?
 +      }
 +}
 +
 +impl<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync<G, U, L>
 +where U::Target: UtxoLookup, L::Target: Logger
  {
        fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
                self.network_graph.update_node_from_announcement(msg)?;
        }
  
        fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, LightningError> {
 -              self.network_graph.update_channel_from_announcement(msg, &self.chain_access)?;
 -              log_gossip!(self.logger, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
 +              self.network_graph.update_channel_from_announcement(msg, &self.utxo_lookup)?;
                Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
        }
  
        }
  
        fn get_next_channel_announcement(&self, starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
 -              let channels = self.network_graph.channels.read().unwrap();
 +              let mut channels = self.network_graph.channels.write().unwrap();
                for (_, ref chan) in channels.range(starting_point..) {
                        if chan.announcement_message.is_some() {
                                let chan_announcement = chan.announcement_message.clone().unwrap();
                None
        }
  
 -      fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> {
 -              let nodes = self.network_graph.nodes.read().unwrap();
 -              let iter = if let Some(pubkey) = starting_point {
 -                              nodes.range((Bound::Excluded(NodeId::from_pubkey(pubkey)), Bound::Unbounded))
 +      fn get_next_node_announcement(&self, starting_point: Option<&NodeId>) -> Option<NodeAnnouncement> {
 +              let mut nodes = self.network_graph.nodes.write().unwrap();
 +              let iter = if let Some(node_id) = starting_point {
 +                              nodes.range((Bound::Excluded(node_id), Bound::Unbounded))
                        } else {
                                nodes.range(..)
                        };
        /// to request gossip messages for each channel. The sync is considered complete
        /// when the final reply_scids_end message is received, though we are not
        /// tracking this directly.
 -      fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &Init) -> Result<(), ()> {
 +      fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &Init, _inbound: bool) -> Result<(), ()> {
                // We will only perform a sync with peers that support gossip_queries.
                if !init_msg.features.supports_gossip_queries() {
                        // Don't disconnect peers for not supporting gossip queries. We may wish to have
                // (has at least one update). A peer may still want to know the channel
                // exists even if its not yet routable.
                let mut batches: Vec<Vec<u64>> = vec![Vec::with_capacity(MAX_SCIDS_PER_REPLY)];
 -              let channels = self.network_graph.channels.read().unwrap();
 +              let mut channels = self.network_graph.channels.write().unwrap();
                for (_, ref chan) in channels.range(inclusive_start_scid.unwrap()..exclusive_end_scid.unwrap()) {
                        if let Some(chan_announcement) = &chan.announcement_message {
                                // Construct a new batch if last one is full
                features.set_gossip_queries_optional();
                features
        }
 +
 +      fn processing_queue_high(&self) -> bool {
 +              self.network_graph.pending_checks.too_many_checks_pending()
 +      }
  }
  
 -impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> MessageSendEventsProvider for P2PGossipSync<G, C, L>
 +impl<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref> MessageSendEventsProvider for P2PGossipSync<G, U, L>
  where
 -      C::Target: chain::Access,
 +      U::Target: UtxoLookup,
        L::Target: Logger,
  {
        fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
@@@ -968,7 -922,7 +968,7 @@@ impl<'a> fmt::Debug for DirectedChannel
  ///
  /// While this may be smaller than the actual channel capacity, amounts greater than
  /// [`Self::as_msat`] should not be routed through the channel.
 -#[derive(Clone, Copy, Debug)]
 +#[derive(Clone, Copy, Debug, PartialEq)]
  pub enum EffectiveCapacity {
        /// The available liquidity in the channel known from being a channel counterparty, and thus a
        /// direct hop.
@@@ -1017,7 -971,7 +1017,7 @@@ impl EffectiveCapacity 
  /// Fees for routing via a given channel or a node
  #[derive(Eq, PartialEq, Copy, Clone, Debug, Hash)]
  pub struct RoutingFees {
-       /// Flat routing fee in satoshis
+       /// Flat routing fee in millisatoshis.
        pub base_msat: u32,
        /// Liquidity-based routing fee in millionths of a routed amount.
        /// In other words, 10000 is 1%.
@@@ -1235,7 -1189,6 +1235,7 @@@ impl<L: Deref> ReadableArgs<L> for Netw
                        last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp),
                        removed_nodes: Mutex::new(HashMap::new()),
                        removed_channels: Mutex::new(HashMap::new()),
 +                      pending_checks: utxo::PendingChecks::new(),
                })
        }
  }
@@@ -1275,7 -1228,6 +1275,7 @@@ impl<L: Deref> NetworkGraph<L> where L:
                        last_rapid_gossip_sync_timestamp: Mutex::new(None),
                        removed_channels: Mutex::new(HashMap::new()),
                        removed_nodes: Mutex::new(HashMap::new()),
 +                      pending_checks: utxo::PendingChecks::new(),
                }
        }
  
        /// routing messages from a source using a protocol other than the lightning P2P protocol.
        pub fn update_node_from_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), LightningError> {
                let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
 -              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id, "node_announcement");
 +              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &get_pubkey_from_node_id!(msg.contents.node_id, "node_announcement"), "node_announcement");
                self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
        }
  
        }
  
        fn update_node_from_announcement_intern(&self, msg: &msgs::UnsignedNodeAnnouncement, full_msg: Option<&msgs::NodeAnnouncement>) -> Result<(), LightningError> {
 -              match self.nodes.write().unwrap().get_mut(&NodeId::from_pubkey(&msg.node_id)) {
 -                      None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
 +              let mut nodes = self.nodes.write().unwrap();
 +              match nodes.get_mut(&msg.node_id) {
 +                      None => {
 +                              core::mem::drop(nodes);
 +                              self.pending_checks.check_hold_pending_node_announcement(msg, full_msg)?;
 +                              Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError})
 +                      },
                        Some(node) => {
                                if let Some(node_info) = node.announcement_info.as_ref() {
                                        // The timestamp field is somewhat of a misnomer - the BOLTs use it to order
        /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
        /// routing messages from a source using a protocol other than the lightning P2P protocol.
        ///
 -      /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
 +      /// If a [`UtxoLookup`] object is provided via `utxo_lookup`, it will be called to verify
        /// the corresponding UTXO exists on chain and is correctly-formatted.
 -      pub fn update_channel_from_announcement<C: Deref>(
 -              &self, msg: &msgs::ChannelAnnouncement, chain_access: &Option<C>,
 +      pub fn update_channel_from_announcement<U: Deref>(
 +              &self, msg: &msgs::ChannelAnnouncement, utxo_lookup: &Option<U>,
        ) -> Result<(), LightningError>
        where
 -              C::Target: chain::Access,
 +              U::Target: UtxoLookup,
        {
                let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
 -              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1, "channel_announcement");
 -              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2, "channel_announcement");
 -              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1, "channel_announcement");
 -              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2, "channel_announcement");
 -              self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), chain_access)
 +              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement"), "channel_announcement");
 +              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement"), "channel_announcement");
 +              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement"), "channel_announcement");
 +              secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_2, "channel_announcement"), "channel_announcement");
 +              self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), utxo_lookup)
        }
  
        /// Store or update channel info from a channel announcement without verifying the associated
        /// signatures. Because we aren't given the associated signatures here we cannot relay the
        /// channel announcement to any of our peers.
        ///
 -      /// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
 +      /// If a [`UtxoLookup`] object is provided via `utxo_lookup`, it will be called to verify
        /// the corresponding UTXO exists on chain and is correctly-formatted.
 -      pub fn update_channel_from_unsigned_announcement<C: Deref>(
 -              &self, msg: &msgs::UnsignedChannelAnnouncement, chain_access: &Option<C>
 +      pub fn update_channel_from_unsigned_announcement<U: Deref>(
 +              &self, msg: &msgs::UnsignedChannelAnnouncement, utxo_lookup: &Option<U>
        ) -> Result<(), LightningError>
        where
 -              C::Target: chain::Access,
 +              U::Target: UtxoLookup,
        {
 -              self.update_channel_from_unsigned_announcement_intern(msg, None, chain_access)
 +              self.update_channel_from_unsigned_announcement_intern(msg, None, utxo_lookup)
        }
  
        /// Update channel from partial announcement data received via rapid gossip sync
                Ok(())
        }
  
 -      fn update_channel_from_unsigned_announcement_intern<C: Deref>(
 -              &self, msg: &msgs::UnsignedChannelAnnouncement, full_msg: Option<&msgs::ChannelAnnouncement>, chain_access: &Option<C>
 +      fn update_channel_from_unsigned_announcement_intern<U: Deref>(
 +              &self, msg: &msgs::UnsignedChannelAnnouncement, full_msg: Option<&msgs::ChannelAnnouncement>, utxo_lookup: &Option<U>
        ) -> Result<(), LightningError>
        where
 -              C::Target: chain::Access,
 +              U::Target: UtxoLookup,
        {
                if msg.node_id_1 == msg.node_id_2 || msg.bitcoin_key_1 == msg.bitcoin_key_2 {
                        return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
                }
  
 -              let node_one = NodeId::from_pubkey(&msg.node_id_1);
 -              let node_two = NodeId::from_pubkey(&msg.node_id_2);
 -
                {
                        let channels = self.channels.read().unwrap();
  
                                        // We use the Node IDs rather than the bitcoin_keys to check for "equivalence"
                                        // as we didn't (necessarily) store the bitcoin keys, and we only really care
                                        // if the peers on the channel changed anyway.
 -                                      if node_one == chan.node_one && node_two == chan.node_two {
 +                                      if msg.node_id_1 == chan.node_one && msg.node_id_2 == chan.node_two {
                                                return Err(LightningError {
                                                        err: "Already have chain-validated channel".to_owned(),
                                                        action: ErrorAction::IgnoreDuplicateGossip
                                                });
                                        }
 -                              } else if chain_access.is_none() {
 +                              } else if utxo_lookup.is_none() {
                                        // Similarly, if we can't check the chain right now anyway, ignore the
                                        // duplicate announcement without bothering to take the channels write lock.
                                        return Err(LightningError {
                        let removed_channels = self.removed_channels.lock().unwrap();
                        let removed_nodes = self.removed_nodes.lock().unwrap();
                        if removed_channels.contains_key(&msg.short_channel_id) ||
 -                              removed_nodes.contains_key(&node_one) ||
 -                              removed_nodes.contains_key(&node_two) {
 +                              removed_nodes.contains_key(&msg.node_id_1) ||
 +                              removed_nodes.contains_key(&msg.node_id_2) {
                                return Err(LightningError{
                                        err: format!("Channel with SCID {} or one of its nodes was removed from our network graph recently", &msg.short_channel_id),
                                        action: ErrorAction::IgnoreAndLog(Level::Gossip)});
                        }
                }
  
 -              let utxo_value = match &chain_access {
 -                      &None => {
 -                              // Tentatively accept, potentially exposing us to DoS attacks
 -                              None
 -                      },
 -                      &Some(ref chain_access) => {
 -                              match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
 -                                      Ok(TxOut { value, script_pubkey }) => {
 -                                              let expected_script =
 -                                                      make_funding_redeemscript(&msg.bitcoin_key_1, &msg.bitcoin_key_2).to_v0_p2wsh();
 -                                              if script_pubkey != expected_script {
 -                                                      return Err(LightningError{err: format!("Channel announcement key ({}) didn't match on-chain script ({})", expected_script.to_hex(), script_pubkey.to_hex()), action: ErrorAction::IgnoreError});
 -                                              }
 -                                              //TODO: Check if value is worth storing, use it to inform routing, and compare it
 -                                              //to the new HTLC max field in channel_update
 -                                              Some(value)
 -                                      },
 -                                      Err(chain::AccessError::UnknownChain) => {
 -                                              return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
 -                                      },
 -                                      Err(chain::AccessError::UnknownTx) => {
 -                                              return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
 -                                      },
 -                              }
 -                      },
 -              };
 +              let utxo_value = self.pending_checks.check_channel_announcement(
 +                      utxo_lookup, msg, full_msg)?;
  
                #[allow(unused_mut, unused_assignments)]
                let mut announcement_received_time = 0;
  
                let chan_info = ChannelInfo {
                        features: msg.features.clone(),
 -                      node_one,
 +                      node_one: msg.node_id_1,
                        one_to_two: None,
 -                      node_two,
 +                      node_two: msg.node_id_2,
                        two_to_one: None,
                        capacity_sats: utxo_value,
                        announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
                        announcement_received_time,
                };
  
 -              self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)
 +              self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)?;
 +
 +              log_gossip!(self.logger, "Added channel_announcement for {}{}", msg.short_channel_id, if !msg.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" });
 +              Ok(())
        }
  
        /// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
  
                let mut channels = self.channels.write().unwrap();
                match channels.get_mut(&msg.short_channel_id) {
 -                      None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),
 +                      None => {
 +                              core::mem::drop(channels);
 +                              self.pending_checks.check_hold_pending_channel_update(msg, full_msg)?;
 +                              return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError});
 +                      },
                        Some(channel) => {
                                if msg.htlc_maximum_msat > MAX_VALUE_MSAT {
                                        return Err(LightningError{err:
@@@ -1923,13 -1890,13 +1923,13 @@@ impl ReadOnlyNetworkGraph<'_> 
  }
  
  #[cfg(test)]
 -mod tests {
 -      use crate::chain;
 +pub(crate) mod tests {
        use crate::ln::channelmanager;
        use crate::ln::chan_utils::make_funding_redeemscript;
        #[cfg(feature = "std")]
        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::routing::utxo::{UtxoLookupError, UtxoResult};
        use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
                UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
                ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
                assert!(!gossip_sync.should_request_full_sync(&node_id));
        }
  
 -      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);
 +      pub(crate) fn get_signed_node_announcement<F: Fn(&mut UnsignedNodeAnnouncement)>(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> NodeAnnouncement {
 +              let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_key));
                let mut unsigned_announcement = UnsignedNodeAnnouncement {
                        features: channelmanager::provided_node_features(&UserConfig::default()),
                        timestamp: 100,
 -                      node_id: node_id,
 +                      node_id,
                        rgb: [0; 3],
                        alias: [0; 32],
                        addresses: Vec::new(),
                }
        }
  
 -      fn get_signed_channel_announcement<F: Fn(&mut UnsignedChannelAnnouncement)>(f: F, node_1_key: &SecretKey, node_2_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> ChannelAnnouncement {
 +      pub(crate) fn get_signed_channel_announcement<F: Fn(&mut UnsignedChannelAnnouncement)>(f: F, node_1_key: &SecretKey, node_2_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> ChannelAnnouncement {
                let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_key);
                let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_key);
                let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap();
                        features: channelmanager::provided_channel_features(&UserConfig::default()),
                        chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                        short_channel_id: 0,
 -                      node_id_1,
 -                      node_id_2,
 -                      bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey),
 -                      bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey),
 +                      node_id_1: NodeId::from_pubkey(&node_id_1),
 +                      node_id_2: NodeId::from_pubkey(&node_id_2),
 +                      bitcoin_key_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey)),
 +                      bitcoin_key_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey)),
                        excess_data: Vec::new(),
                };
                f(&mut unsigned_announcement);
                }
        }
  
 -      fn get_channel_script(secp_ctx: &Secp256k1<secp256k1::All>) -> Script {
 +      pub(crate) fn get_channel_script(secp_ctx: &Secp256k1<secp256k1::All>) -> Script {
                let node_1_btckey = SecretKey::from_slice(&[40; 32]).unwrap();
                let node_2_btckey = SecretKey::from_slice(&[39; 32]).unwrap();
                make_funding_redeemscript(&PublicKey::from_secret_key(secp_ctx, &node_1_btckey),
                        &PublicKey::from_secret_key(secp_ctx, &node_2_btckey)).to_v0_p2wsh()
        }
  
 -      fn get_signed_channel_update<F: Fn(&mut UnsignedChannelUpdate)>(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> ChannelUpdate {
 +      pub(crate) fn get_signed_channel_update<F: Fn(&mut UnsignedChannelUpdate)>(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> ChannelUpdate {
                let mut unsigned_channel_update = UnsignedChannelUpdate {
                        chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                        short_channel_id: 0,
  
                // Test if an associated transaction were not on-chain (or not confirmed).
                let chain_source = test_utils::TestChainSource::new(Network::Testnet);
 -              *chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
 +              *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Sync(Err(UtxoLookupError::UnknownTx));
                let network_graph = NetworkGraph::new(genesis_hash, &logger);
                gossip_sync = P2PGossipSync::new(&network_graph, Some(&chain_source), &logger);
  
                };
  
                // Now test if the transaction is found in the UTXO set and the script is correct.
 -              *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
 +              *chain_source.utxo_ret.lock().unwrap() =
 +                      UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() }));
                let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
                        unsigned_announcement.short_channel_id += 2;
                }, node_1_privkey, node_2_privkey, &secp_ctx);
  
                // If we receive announcement for the same channel, once we've validated it against the
                // chain, we simply ignore all new (duplicate) announcements.
 -              *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
 +              *chain_source.utxo_ret.lock().unwrap() =
 +                      UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script }));
                match gossip_sync.handle_channel_announcement(&valid_announcement) {
                        Ok(_) => panic!(),
                        Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
                {
                        // Announce a channel we will update
                        let good_script = get_channel_script(&secp_ctx);
 -                      *chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
 +                      *chain_source.utxo_ret.lock().unwrap() =
 +                              UtxoResult::Sync(Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() }));
  
                        let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
                        short_channel_id = valid_channel_announcement.contents.short_channel_id;
                let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph);
                let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
                let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
 -              let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
 +              let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
  
                // No nodes yet.
                let next_announcements = gossip_sync.get_next_node_announcement(None);
                // It should ignore if gossip_queries feature is not enabled
                {
                        let init_msg = Init { features: InitFeatures::empty(), remote_network_address: None };
 -                      gossip_sync.peer_connected(&node_id_1, &init_msg).unwrap();
 +                      gossip_sync.peer_connected(&node_id_1, &init_msg, true).unwrap();
                        let events = gossip_sync.get_and_clear_pending_msg_events();
                        assert_eq!(events.len(), 0);
                }
                        let mut features = InitFeatures::empty();
                        features.set_gossip_queries_optional();
                        let init_msg = Init { features, remote_network_address: None };
 -                      gossip_sync.peer_connected(&node_id_1, &init_msg).unwrap();
 +                      gossip_sync.peer_connected(&node_id_1, &init_msg, true).unwrap();
                        let events = gossip_sync.get_and_clear_pending_msg_events();
                        assert_eq!(events.len(), 1);
                        match &events[0] {
index 0aefaf383062b3d8ddcafbeb1465242ccf802e95,2dd764022cd63a2a253910fac4be5be1d8c77ed9..0a5c0b643dd0c315f2782d94b7caa469dcf4e46d
@@@ -378,7 -378,6 +378,6 @@@ macro_rules! decode_tlv_stream_with_cus
  macro_rules! _decode_tlv_stream_range {
        ($stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
         $(, $decode_custom_tlv: expr)?) => { {
-               use core::ops::RangeBounds;
                use $crate::ln::msgs::DecodeError;
                let mut last_seen_type: Option<u64> = None;
                let mut stream_ref = $stream;
                                                }
                                        },
                                        Err(e) => return Err(e),
-                                       Ok(t) => if $range.contains(&t.0) { t } else {
+                                       Ok(t) => if core::ops::RangeBounds::contains(&$range, &t.0) { t } else {
                                                drop(tracking_reader);
  
                                                // Assumes the type id is minimally encoded, which is enforced on read.
        } }
  }
  
 +/// Implements [`Readable`]/[`Writeable`] for a message struct that may include non-TLV and
 +/// TLV-encoded parts.
 +///
 +/// This is useful to implement a [`CustomMessageReader`].
 +///
 +/// Currently `$fieldty` may only be `option`, i.e., `$tlvfield` is optional field.
 +///
 +/// For example,
 +/// ```
 +/// # use lightning::impl_writeable_msg;
 +/// struct MyCustomMessage {
 +///   pub field_1: u32,
 +///   pub field_2: bool,
 +///   pub field_3: String,
 +///   pub tlv_optional_integer: Option<u32>,
 +/// }
 +///
 +/// impl_writeable_msg!(MyCustomMessage, {
 +///   field_1,
 +///   field_2,
 +///   field_3
 +/// }, {
 +///   (1, tlv_optional_integer, option),
 +/// });
 +/// ```
 +///
 +/// [`Readable`]: crate::util::ser::Readable
 +/// [`Writeable`]: crate::util::ser::Writeable
 +/// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader
 +#[macro_export]
  macro_rules! impl_writeable_msg {
        ($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
                impl $crate::util::ser::Writeable for $st {
                        fn write<W: $crate::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
                                $( self.$field.write(w)?; )*
 -                              encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
 +                              $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
                                Ok(())
                        }
                }
                impl $crate::util::ser::Readable for $st {
                        fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
                                $(let $field = $crate::util::ser::Readable::read(r)?;)*
 -                              $(_init_tlv_field_var!($tlvfield, $fieldty);)*
 -                              decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
 +                              $($crate::_init_tlv_field_var!($tlvfield, $fieldty);)*
 +                              $crate::decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
                                Ok(Self {
                                        $($field),*,
                                        $($tlvfield),*
@@@ -675,10 -644,10 +674,10 @@@ macro_rules! _init_and_read_tlv_fields 
  }
  
  /// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
 -/// If `$fieldty` is `required`, then `$field` is a required field that is not an Option nor a Vec.
 +/// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
  /// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
  /// If `$fieldty` is `option`, then `$field` is optional field.
 -/// If `$fieldty` is `vec_type`, then `$field` is a Vec, which needs to have its individual elements serialized.
 +/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
  ///
  /// For example,
  /// ```