Merge pull request #1169 from TheBlueMatt/2021-11-fix-update-announcements
[rust-lightning] / lightning / src / ln / msgs.rs
index 122dd7737b996507e2d8fb5135af443172fb65f8..39c00105f8d675e5cf5ce664b2d7416654c40cfb 100644 (file)
@@ -30,7 +30,7 @@ use bitcoin::secp256k1;
 use bitcoin::blockdata::script::Script;
 use bitcoin::hash_types::{Txid, BlockHash};
 
-use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
+use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
 
 use prelude::*;
 use core::{cmp, fmt};
@@ -148,6 +148,10 @@ pub struct OpenChannel {
        pub channel_flags: u8,
        /// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close
        pub shutdown_scriptpubkey: OptionalField<Script>,
+       /// The channel type that this channel will represent. If none is set, we derive the channel
+       /// type from the intersection of our feature bits with our counterparty's feature bits from
+       /// the Init message.
+       pub channel_type: Option<ChannelTypeFeatures>,
 }
 
 /// An accept_channel message to be sent or received from a peer
@@ -390,12 +394,10 @@ pub enum NetAddress {
                port: u16,
        },
        /// An old-style Tor onion address/port on which the peer is listening.
-       OnionV2 {
-               /// The bytes (usually encoded in base32 with ".onion" appended)
-               addr: [u8; 10],
-               /// The port on which the node is listening
-               port: u16,
-       },
+       ///
+       /// This field is deprecated and the Tor network generally no longer supports V2 Onion
+       /// addresses. Thus, the details are not parsed here.
+       OnionV2([u8; 12]),
        /// A new-style Tor onion address/port on which the peer is listening.
        /// To create the human-readable "hostname", concatenate ed25519_pubkey, checksum, and version,
        /// wrap as base32 and append ".onion".
@@ -417,7 +419,7 @@ impl NetAddress {
                match self {
                        &NetAddress::IPv4 {..} => { 1 },
                        &NetAddress::IPv6 {..} => { 2 },
-                       &NetAddress::OnionV2 {..} => { 3 },
+                       &NetAddress::OnionV2(_) => { 3 },
                        &NetAddress::OnionV3 {..} => { 4 },
                }
        }
@@ -427,7 +429,7 @@ impl NetAddress {
                match self {
                        &NetAddress::IPv4 { .. } => { 6 },
                        &NetAddress::IPv6 { .. } => { 18 },
-                       &NetAddress::OnionV2 { .. } => { 12 },
+                       &NetAddress::OnionV2(_) => { 12 },
                        &NetAddress::OnionV3 { .. } => { 37 },
                }
        }
@@ -449,10 +451,9 @@ impl Writeable for NetAddress {
                                addr.write(writer)?;
                                port.write(writer)?;
                        },
-                       &NetAddress::OnionV2 { ref addr, ref port } => {
+                       &NetAddress::OnionV2(bytes) => {
                                3u8.write(writer)?;
-                               addr.write(writer)?;
-                               port.write(writer)?;
+                               bytes.write(writer)?;
                        },
                        &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
                                4u8.write(writer)?;
@@ -482,12 +483,7 @@ impl Readable for Result<NetAddress, u8> {
                                        port: Readable::read(reader)?,
                                }))
                        },
-                       3 => {
-                               Ok(Ok(NetAddress::OnionV2 {
-                                       addr: Readable::read(reader)?,
-                                       port: Readable::read(reader)?,
-                               }))
-                       },
+                       3 => Ok(Ok(NetAddress::OnionV2(Readable::read(reader)?))),
                        4 => {
                                Ok(Ok(NetAddress::OnionV3 {
                                        ed25519_pubkey: Readable::read(reader)?,
@@ -711,6 +707,10 @@ pub enum ErrorAction {
        /// The peer did something harmless that we weren't able to meaningfully process.
        /// If the error is logged, log it at the given level.
        IgnoreAndLog(logger::Level),
+       /// The peer provided us with a gossip message which we'd already seen. In most cases this
+       /// should be ignored, but it may result in the message being forwarded if it is a duplicate of
+       /// our own channel announcements.
+       IgnoreDuplicateGossip,
        /// The peer did something incorrect. Tell them.
        SendErrorMessage {
                /// The message to send.
@@ -1162,7 +1162,9 @@ impl_writeable_msg!(OpenChannel, {
        first_per_commitment_point,
        channel_flags,
        shutdown_scriptpubkey
-}, {});
+}, {
+       (1, channel_type, option),
+});
 
 impl_writeable_msg!(RevokeAndACK, {
        channel_id,
@@ -1747,8 +1749,9 @@ impl_writeable_msg!(GossipTimestampFilter, {
 mod tests {
        use hex;
        use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
+       use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
        use ln::msgs;
-       use ln::msgs::{ChannelFeatures, FinalOnionHopData, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket, OnionHopDataFormat};
+       use ln::msgs::{FinalOnionHopData, OptionalField, OnionErrorPacket, OnionHopDataFormat};
        use util::ser::{Writeable, Readable};
 
        use bitcoin::hashes::hex::FromHex;
@@ -1915,10 +1918,9 @@ mod tests {
                        });
                }
                if onionv2 {
-                       addresses.push(msgs::NetAddress::OnionV2 {
-                               addr: [255, 254, 253, 252, 251, 250, 249, 248, 247, 246],
-                               port: 9735
-                       });
+                       addresses.push(msgs::NetAddress::OnionV2(
+                               [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]
+                       ));
                }
                if onionv3 {
                        addresses.push(msgs::NetAddress::OnionV3 {
@@ -2052,7 +2054,7 @@ mod tests {
                do_encoding_channel_update(true, true, true, true);
        }
 
-       fn do_encoding_open_channel(random_bit: bool, shutdown: bool) {
+       fn do_encoding_open_channel(random_bit: bool, shutdown: bool, incl_chan_type: bool) {
                let secp_ctx = Secp256k1::new();
                let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
                let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
@@ -2079,7 +2081,8 @@ mod tests {
                        htlc_basepoint: pubkey_5,
                        first_per_commitment_point: pubkey_6,
                        channel_flags: if random_bit { 1 << 5 } else { 0 },
-                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+                       shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent },
+                       channel_type: if incl_chan_type { Some(ChannelTypeFeatures::empty()) } else { None },
                };
                let encoded_value = open_channel.encode();
                let mut target_value = Vec::new();
@@ -2093,15 +2096,22 @@ mod tests {
                if shutdown {
                        target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
                }
+               if incl_chan_type {
+                       target_value.append(&mut hex::decode("0100").unwrap());
+               }
                assert_eq!(encoded_value, target_value);
        }
 
        #[test]
        fn encoding_open_channel() {
-               do_encoding_open_channel(false, false);
-               do_encoding_open_channel(true, false);
-               do_encoding_open_channel(false, true);
-               do_encoding_open_channel(true, true);
+               do_encoding_open_channel(false, false, false);
+               do_encoding_open_channel(false, false, true);
+               do_encoding_open_channel(false, true, false);
+               do_encoding_open_channel(false, true, true);
+               do_encoding_open_channel(true, false, false);
+               do_encoding_open_channel(true, false, true);
+               do_encoding_open_channel(true, true, false);
+               do_encoding_open_channel(true, true, true);
        }
 
        fn do_encoding_accept_channel(shutdown: bool) {