Merge pull request #1710 from TheBlueMatt/2022-09-compile-warn
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Sun, 11 Sep 2022 14:54:05 +0000 (14:54 +0000)
committerGitHub <noreply@github.com>
Sun, 11 Sep 2022 14:54:05 +0000 (14:54 +0000)
Fix several compile warnings added in some of my recent commits

12 files changed:
lightning-net-tokio/src/lib.rs
lightning/src/chain/channelmonitor.rs
lightning/src/chain/mod.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/features.rs
lightning/src/ln/msgs.rs
lightning/src/ln/peer_handler.rs
lightning/src/ln/priv_short_conf_tests.rs
lightning/src/onion_message/messenger.rs
lightning/src/routing/gossip.rs
lightning/src/util/test_utils.rs

index c50e3e69bd272083d8ceed17015a62d311f7f32c..d5feb9936527ce156b2bec62ccec6c2f241245eb 100644 (file)
@@ -582,6 +582,7 @@ mod tests {
                fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
                fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
                fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
+               fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::known() }
                fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { InitFeatures::known() }
        }
        impl ChannelMessageHandler for MsgHandler {
index ed84750590a1c95ac220ce9408c40135b5aca0ec..e79e50172f9f3f1c5c8a2e98d378f118e6841001 100644 (file)
@@ -1630,7 +1630,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
        /// confirmations on the claim transaction.
        ///
        /// Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
-       /// LDK prior to 0.0.108, balances may not be fully captured if our counterparty broadcasted
+       /// LDK prior to 0.0.111, balances may not be fully captured if our counterparty broadcasted
        /// a revoked state.
        ///
        /// See [`Balance`] for additional details on the types of claimable balances which
index 42508569575f988bf508ae69ba64ab40abb453e7..f0544679817db3434a679662901240b99d68da54 100644 (file)
@@ -61,7 +61,7 @@ impl BestBlock {
 }
 
 /// An error when accessing the chain via [`Access`].
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub enum AccessError {
        /// The requested chain is unknown.
        UnknownChain,
index 624f4d6b688b0e2d81964d39e2deb77e1233af8a..b37550b0d2a91eb909bb997dc3dda39a91de887b 100644 (file)
@@ -4771,6 +4771,9 @@ impl<Signer: Sign> Channel<Signer> {
        }
 
        fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
+               // Called:
+               //  * always when a new block/transactions are confirmed with the new height
+               //  * when funding is signed with a height of 0
                if self.funding_tx_confirmation_height == 0 && self.minimum_depth != Some(0) {
                        return None;
                }
@@ -4796,7 +4799,7 @@ impl<Signer: Sign> Channel<Signer> {
                        // We got a reorg but not enough to trigger a force close, just ignore.
                        false
                } else {
-                       if self.channel_state < ChannelState::ChannelFunded as u32 {
+                       if self.funding_tx_confirmation_height != 0 && self.channel_state < ChannelState::ChannelFunded as u32 {
                                // We should never see a funding transaction on-chain until we've received
                                // funding_signed (if we're an outbound channel), or seen funding_generated (if we're
                                // an inbound channel - before that we have no known funding TXID). The fuzzer,
index c79a18ca8dd40e0690450ca51fe8ca0b038e879e..a51eacc9d3f0a69f64882d19161ae90334aae61e 100644 (file)
@@ -6121,7 +6121,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
        }
 
        fn provided_node_features(&self) -> NodeFeatures {
-               NodeFeatures::known()
+               NodeFeatures::known_channel_features()
        }
 
        fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
index 91922f1a45f26748f9fdaf8c7ba009a068f6d6b9..642298f56ed52265952eb2b4642163a06f5b0474 100644 (file)
 //!     (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md) for more information).
 //! - `BasicMPP` - requires/supports that a node can receive basic multi-part payments
 //!     (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md#basic-multi-part-payments) for more information).
+//! - `Wumbo` - requires/supports that a node create large channels. Called `option_support_large_channel` in the spec.
+//!     (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message) for more information).
 //! - `ShutdownAnySegwit` - requires/supports that future segwit versions are allowed in `shutdown`
 //!     (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md) for more information).
+//! - `OnionMessages` - requires/supports forwarding onion messages
+//!     (see [BOLT-7](https://github.com/lightning/bolts/pull/759/files) for more information).
+//!     TODO: update link
 //! - `ChannelType` - node supports the channel_type field in open/accept
 //!     (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md) for more information).
 //! - `SCIDPrivacy` - supply channel aliases for routing
@@ -164,7 +169,8 @@ mod sealed {
                ],
                optional_features: [
                        // Note that if new "non-channel-related" flags are added here they should be
-                       // explicitly cleared in InitFeatures::known_channel_features.
+                       // explicitly cleared in InitFeatures::known_channel_features and
+                       // NodeFeatures::known_channel_features.
                        // Byte 0
                        DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
                        // Byte 1
@@ -174,7 +180,7 @@ mod sealed {
                        // Byte 3
                        ShutdownAnySegwit,
                        // Byte 4
-                       ,
+                       OnionMessages,
                        // Byte 5
                        ChannelType | SCIDPrivacy,
                        // Byte 6
@@ -208,7 +214,7 @@ mod sealed {
                        // Byte 3
                        ShutdownAnySegwit,
                        // Byte 4
-                       ,
+                       OnionMessages,
                        // Byte 5
                        ChannelType | SCIDPrivacy,
                        // Byte 6
@@ -435,8 +441,6 @@ mod sealed {
        define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
                "Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
                set_shutdown_any_segwit_required, supports_shutdown_anysegwit, requires_shutdown_anysegwit);
-       // We do not yet advertise the onion messages feature bit, but we need to detect when peers
-       // support it.
        define_feature!(39, OnionMessages, [InitContext, NodeContext],
                "Feature flags for `option_onion_messages`.", set_onion_messages_optional,
                set_onion_messages_required, supports_onion_messages, requires_onion_messages);
@@ -470,6 +474,17 @@ pub struct Features<T: sealed::Context> {
        mark: PhantomData<T>,
 }
 
+impl <T: sealed::Context> Features<T> {
+       pub(crate) fn or(mut self, o: Self) -> Self {
+               let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
+               self.flags.resize(total_feature_len, 0u8);
+               for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
+                       *byte |= *o_byte;
+               }
+               self
+       }
+}
+
 impl<T: sealed::Context> Clone for Features<T> {
        fn clone(&self) -> Self {
                Self {
@@ -532,16 +547,6 @@ impl InitFeatures {
                Ok(())
        }
 
-       /// or's another InitFeatures into this one.
-       pub(crate) fn or(mut self, o: InitFeatures) -> InitFeatures {
-               let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
-               self.flags.resize(total_feature_len, 0u8);
-               for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
-                       *byte |= *o_byte;
-               }
-               self
-       }
-
        /// Converts `InitFeatures` to `Features<C>`. Only known `InitFeatures` relevant to context `C`
        /// are included in the result.
        pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
@@ -554,6 +559,16 @@ impl InitFeatures {
                Self::known()
                        .clear_initial_routing_sync()
                        .clear_gossip_queries()
+                       .clear_onion_messages()
+       }
+}
+
+impl NodeFeatures {
+       /// Returns the set of known node features that are related to channels.
+       pub fn known_channel_features() -> NodeFeatures {
+               Self::known()
+                       .clear_gossip_queries()
+                       .clear_onion_messages()
        }
 }
 
@@ -787,6 +802,13 @@ impl<T: sealed::InitialRoutingSync> Features<T> {
        }
 }
 
+impl<T: sealed::OnionMessages> Features<T> {
+       pub(crate) fn clear_onion_messages(mut self) -> Self {
+               <T as sealed::OnionMessages>::clear_bits(&mut self.flags);
+               self
+       }
+}
+
 impl<T: sealed::ShutdownAnySegwit> Features<T> {
        #[cfg(test)]
        pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
@@ -913,6 +935,11 @@ mod tests {
                assert!(!InitFeatures::known().requires_wumbo());
                assert!(!NodeFeatures::known().requires_wumbo());
 
+               assert!(InitFeatures::known().supports_onion_messages());
+               assert!(NodeFeatures::known().supports_onion_messages());
+               assert!(!InitFeatures::known().requires_onion_messages());
+               assert!(!NodeFeatures::known().requires_onion_messages());
+
                assert!(InitFeatures::known().supports_zero_conf());
                assert!(!InitFeatures::known().requires_zero_conf());
                assert!(NodeFeatures::known().supports_zero_conf());
@@ -957,7 +984,7 @@ mod tests {
                        // - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
                        // - basic_mpp | wumbo
                        // - opt_shutdown_anysegwit
-                       // -
+                       // - onion_messages
                        // - option_channel_type | option_scid_alias
                        // - option_zeroconf
                        assert_eq!(node_features.flags.len(), 7);
@@ -965,7 +992,7 @@ mod tests {
                        assert_eq!(node_features.flags[1], 0b01010001);
                        assert_eq!(node_features.flags[2], 0b00001010);
                        assert_eq!(node_features.flags[3], 0b00001000);
-                       assert_eq!(node_features.flags[4], 0b00000000);
+                       assert_eq!(node_features.flags[4], 0b10000000);
                        assert_eq!(node_features.flags[5], 0b10100000);
                        assert_eq!(node_features.flags[6], 0b00001000);
                }
index e063c31464fc89a7f93b57c16e18f6f3dc62b8eb..747107c08221c41b04a5283a91804031d981325f 100644 (file)
@@ -900,7 +900,7 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
        // Handler information:
        /// Gets the node feature flags which this handler itself supports. All available handlers are
        /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
-       /// which are broadcasted in our node_announcement message.
+       /// which are broadcasted in our [`NodeAnnouncement`] message.
        fn provided_node_features(&self) -> NodeFeatures;
 
        /// Gets the init feature flags which should be sent to the given peer. All available handlers
@@ -958,6 +958,10 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
        fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
 
        // Handler information:
+       /// Gets the node feature flags which this handler itself supports. All available handlers are
+       /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
+       /// which are broadcasted in our [`NodeAnnouncement`] message.
+       fn provided_node_features(&self) -> NodeFeatures;
        /// Gets the init feature flags which should be sent to the given peer. All available handlers
        /// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
        /// which are sent in our [`Init`] message.
@@ -976,6 +980,19 @@ pub trait OnionMessageHandler : OnionMessageProvider {
        /// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
        /// drop and refuse to forward onion messages to this peer.
        fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
+
+       // Handler information:
+       /// Gets the node feature flags which this handler itself supports. All available handlers are
+       /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
+       /// which are broadcasted in our [`NodeAnnouncement`] message.
+       fn provided_node_features(&self) -> NodeFeatures;
+
+       /// Gets the init feature flags which should be sent to the given peer. All available handlers
+       /// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
+       /// which are sent in our [`Init`] message.
+       ///
+       /// Note that this method is called before [`Self::peer_connected`].
+       fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
 }
 
 mod fuzzy_internal_msgs {
index 417b14d15b2c9637d64d78997cbae2d2b56de201..838927cb2367ac0e0af1fab435edb7a1170e27da 100644 (file)
@@ -77,6 +77,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
        fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
        fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
        fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: msgs::QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
+       fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
        fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
                InitFeatures::empty()
        }
@@ -88,6 +89,10 @@ impl OnionMessageHandler for IgnoringMessageHandler {
        fn handle_onion_message(&self, _their_node_id: &PublicKey, _msg: &msgs::OnionMessage) {}
        fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
        fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
+       fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
+       fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
+               InitFeatures::empty()
+       }
 }
 impl Deref for IgnoringMessageHandler {
        type Target = IgnoringMessageHandler;
@@ -1061,7 +1066,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                peer.their_node_id = Some(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
-                                                                       .or(self.message_handler.route_handler.provided_init_features(&their_node_id));
+                                                                       .or(self.message_handler.route_handler.provided_init_features(&their_node_id))
+                                                                       .or(self.message_handler.onion_message_handler.provided_init_features(&their_node_id));
                                                                let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
                                                                self.enqueue_message(peer, &resp);
                                                                peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1074,7 +1080,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                                                peer.their_node_id = Some(their_node_id);
                                                                insert_node_id!();
                                                                let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
-                                                                       .or(self.message_handler.route_handler.provided_init_features(&their_node_id));
+                                                                       .or(self.message_handler.route_handler.provided_init_features(&their_node_id))
+                                                                       .or(self.message_handler.onion_message_handler.provided_init_features(&their_node_id));
                                                                let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
                                                                self.enqueue_message(peer, &resp);
                                                                peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1969,8 +1976,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                // addresses be sorted for future compatibility.
                addresses.sort_by_key(|addr| addr.get_id());
 
+               let features = self.message_handler.chan_handler.provided_node_features()
+                       .or(self.message_handler.route_handler.provided_node_features())
+                       .or(self.message_handler.onion_message_handler.provided_node_features());
                let announcement = msgs::UnsignedNodeAnnouncement {
-                       features: self.message_handler.chan_handler.provided_node_features(),
+                       features,
                        timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,
                        node_id: PublicKey::from_secret_key(&self.secp_ctx, &self.our_node_secret),
                        rgb, alias, addresses,
index acceb43953e896e76364da8cf20c49eac6448a8b..0977d54c6a828997333f54ef16f95828806ca7a3 100644 (file)
@@ -958,3 +958,45 @@ fn test_zero_conf_accept_reject() {
                _ => panic!(),
        }
 }
+
+#[test]
+fn test_connect_before_funding() {
+       // Tests for a particularly dumb explicit panic that existed prior to 0.0.111 for 0conf
+       // channels. If we received a block while awaiting funding for 0-conf channels we'd hit an
+       // explicit panic when deciding if we should broadcast our channel_ready message.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+
+       let mut manually_accept_conf = test_default_channel_config();
+       manually_accept_conf.manually_accept_inbound_channels = true;
+
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf)]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_001, 42, None).unwrap();
+       let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+       assert_eq!(accept_channel.minimum_depth, 0);
+       nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
+
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::FundingGenerationReady { .. } => {},
+               _ => panic!("Unexpected event"),
+       }
+
+       connect_blocks(&nodes[0], 1);
+       connect_blocks(&nodes[1], 1);
+}
index e38a0d10f6aef42a840c9781beaa2bc540775133..2df01c2ba103dc118d644779aabbcf45489d6ba2 100644 (file)
@@ -16,6 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
 
 use chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient, Sign};
+use ln::features::{InitFeatures, NodeFeatures};
 use ln::msgs::{self, OnionMessageHandler};
 use ln::onion_utils;
 use super::blinded_route::{BlindedRoute, ForwardTlvs, ReceiveTlvs};
@@ -345,6 +346,18 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
                let mut pending_msgs = self.pending_messages.lock().unwrap();
                pending_msgs.remove(their_node_id);
        }
+
+       fn provided_node_features(&self) -> NodeFeatures {
+               let mut features = NodeFeatures::empty();
+               features.set_onion_messages_optional();
+               features
+       }
+
+       fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
+               let mut features = InitFeatures::empty();
+               features.set_onion_messages_optional();
+               features
+       }
 }
 
 impl<Signer: Sign, K: Deref, L: Deref> OnionMessageProvider for OnionMessenger<Signer, K, L>
index cd366f92a29f82ff9cdeb86ea139e124f5d29f2e..e265c6322ec56ad1cb3aee3bae696596d5ff02a5 100644 (file)
@@ -573,6 +573,12 @@ where C::Target: chain::Access, L::Target: Logger
                })
        }
 
+       fn provided_node_features(&self) -> NodeFeatures {
+               let mut features = NodeFeatures::empty();
+               features.set_gossip_queries_optional();
+               features
+       }
+
        fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
                let mut features = InitFeatures::empty();
                features.set_gossip_queries_optional();
index 2892af41c62d11fad77229af2dd27ade4dd20d93..f0e264b4dd19ba9ad129167bc35f8349e18aaf54 100644 (file)
@@ -358,7 +358,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
                self.received_msg(wire::Message::Error(msg.clone()));
        }
        fn provided_node_features(&self) -> NodeFeatures {
-               NodeFeatures::empty()
+               NodeFeatures::known_channel_features()
        }
        fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
                InitFeatures::known_channel_features()
@@ -509,6 +509,12 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
                Ok(())
        }
 
+       fn provided_node_features(&self) -> NodeFeatures {
+               let mut features = NodeFeatures::empty();
+               features.set_gossip_queries_optional();
+               features
+       }
+
        fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
                let mut features = InitFeatures::empty();
                features.set_gossip_queries_optional();