Merge pull request #1730 from TheBlueMatt/2022-09-111-bindings-and-backports
[rust-lightning] / lightning / src / ln / msgs.rs
index 4db1525718998bab0c29556f382810c4418b1f0b..08ad3afcf498aee5b93612f0aa8093e2c8902083 100644 (file)
@@ -40,9 +40,9 @@ use core::fmt::Debug;
 use io::{self, Read};
 use io_extras::read_to_end;
 
-use util::events::MessageSendEventsProvider;
+use util::events::{MessageSendEventsProvider, OnionMessageProvider};
 use util::logger;
-use util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt, Hostname};
+use util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
 
 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 
@@ -311,7 +311,7 @@ pub struct UpdateAddHTLC {
 pub struct OnionMessage {
        /// Used in decrypting the onion packet's payload.
        pub blinding_point: PublicKey,
-       pub(crate) onion_routing_packet: onion_message::Packet,
+       pub(crate) onion_routing_packet: onion_message::packet::Packet,
 }
 
 /// An update_fulfill_htlc message to be sent or received from a peer
@@ -896,6 +896,19 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
        // Error:
        /// Handle an incoming error message from the given peer.
        fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);
+
+       // 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;
 }
 
 /// A trait to describe an object which can receive routing messages.
@@ -915,15 +928,15 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
        /// Handle an incoming channel_update message, returning true if it should be forwarded on,
        /// false or returning an Err otherwise.
        fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, LightningError>;
-       /// Gets a subset of the channel announcements and updates required to dump our routing table
-       /// to a remote node, starting at the short_channel_id indicated by starting_point and
-       /// including the batch_amount entries immediately higher in numerical value than starting_point.
-       fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)>;
-       /// Gets a subset of the node announcements required to dump our routing table to a remote node,
-       /// starting at the node *after* the provided publickey and including batch_amount entries
-       /// immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
+       /// Gets channel announcements and updates required to dump our routing table to a remote node,
+       /// starting at the short_channel_id indicated by starting_point and including announcements
+       /// for a single channel.
+       fn get_next_channel_announcement(&self, starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)>;
+       /// Gets a node announcement required to dump our routing table to a remote node, starting at
+       /// the node *after* the provided pubkey and including up to one announcement immediately
+       /// higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
        /// If None is provided for starting_point, we start at the first node.
-       fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
+       fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement>;
        /// Called when a connection is established with a peer. This can be used to
        /// perform routing table synchronization using a strategy defined by the
        /// implementor.
@@ -943,6 +956,43 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
        /// Handles when a peer asks us to send routing gossip messages for a
        /// list of short_channel_ids.
        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.
+       ///
+       /// Note that this method is called before [`Self::peer_connected`].
+       fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
+}
+
+/// A trait to describe an object that can receive onion messages.
+pub trait OnionMessageHandler : OnionMessageProvider {
+       /// Handle an incoming onion_message message from the given peer.
+       fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage);
+       /// Called when a connection is established with a peer. Can be used to track which peers
+       /// advertise onion message support and are online.
+       fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
+       /// 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 {
@@ -1354,7 +1404,7 @@ impl Readable for OnionMessage {
                let blinding_point: PublicKey = Readable::read(r)?;
                let len: u16 = Readable::read(r)?;
                let mut packet_reader = FixedLengthReader::new(r, len as u64);
-               let onion_routing_packet: onion_message::Packet = <onion_message::Packet as LengthReadable>::read(&mut packet_reader)?;
+               let onion_routing_packet = <onion_message::packet::Packet as LengthReadable>::read(&mut packet_reader)?;
                Ok(Self {
                        blinding_point,
                        onion_routing_packet,
@@ -1375,14 +1425,14 @@ impl Writeable for OnionMessage {
 impl Writeable for FinalOnionHopData {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                self.payment_secret.0.write(w)?;
-               HighZeroBytesDroppedVarInt(self.total_msat).write(w)
+               HighZeroBytesDroppedBigSize(self.total_msat).write(w)
        }
 }
 
 impl Readable for FinalOnionHopData {
        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
                let secret: [u8; 32] = Readable::read(r)?;
-               let amt: HighZeroBytesDroppedVarInt<u64> = Readable::read(r)?;
+               let amt: HighZeroBytesDroppedBigSize<u64> = Readable::read(r)?;
                Ok(Self { payment_secret: PaymentSecret(secret), total_msat: amt.0 })
        }
 }
@@ -1399,15 +1449,15 @@ impl Writeable for OnionHopData {
                        },
                        OnionHopDataFormat::NonFinalNode { short_channel_id } => {
                                encode_varint_length_prefixed_tlv!(w, {
-                                       (2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
-                                       (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
+                                       (2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
+                                       (4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
                                        (6, short_channel_id, required)
                                });
                        },
                        OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
                                encode_varint_length_prefixed_tlv!(w, {
-                                       (2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
-                                       (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
+                                       (2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
+                                       (4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
                                        (8, payment_data, option),
                                        (5482373484, keysend_preimage, option)
                                });
@@ -1423,8 +1473,8 @@ impl Readable for OnionHopData {
                const LEGACY_ONION_HOP_FLAG: u64 = 0;
                let (format, amt, cltv_value) = if b.0 != LEGACY_ONION_HOP_FLAG {
                        let mut rd = FixedLengthReader::new(r, b.0);
-                       let mut amt = HighZeroBytesDroppedVarInt(0u64);
-                       let mut cltv_value = HighZeroBytesDroppedVarInt(0u32);
+                       let mut amt = HighZeroBytesDroppedBigSize(0u64);
+                       let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
                        let mut short_id: Option<u64> = None;
                        let mut payment_data: Option<FinalOnionHopData> = None;
                        let mut keysend_preimage: Option<PaymentPreimage> = None;
@@ -2835,7 +2885,7 @@ mod tests {
        }
        // see above test, needs to be a separate method for use of the serialization macros.
        fn encode_big_payload() -> Result<Vec<u8>, io::Error> {
-               use util::ser::HighZeroBytesDroppedVarInt;
+               use util::ser::HighZeroBytesDroppedBigSize;
                let payload = msgs::OnionHopData {
                        format: OnionHopDataFormat::NonFinalNode {
                                short_channel_id: 0xdeadbeef1bad1dea,
@@ -2848,8 +2898,8 @@ mod tests {
                if let OnionHopDataFormat::NonFinalNode { short_channel_id } = payload.format {
                        encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
                                (1, test_bytes, vec_type),
-                               (2, HighZeroBytesDroppedVarInt(payload.amt_to_forward), required),
-                               (4, HighZeroBytesDroppedVarInt(payload.outgoing_cltv_value), required),
+                               (2, HighZeroBytesDroppedBigSize(payload.amt_to_forward), required),
+                               (4, HighZeroBytesDroppedBigSize(payload.outgoing_cltv_value), required),
                                (6, short_channel_id, required)
                        });
                }