Make `DataLossProtect` fields required and remove wrappers
[rust-lightning] / lightning / src / ln / msgs.rs
index 4b2eb9674fa8acefa0afba97245ca4674d49e577..779cf152825c19cfbbdd737ae2e307d81815dbe5 100644 (file)
@@ -458,20 +458,6 @@ pub struct UpdateFee {
        pub feerate_per_kw: u32,
 }
 
-#[derive(Clone, Debug, PartialEq, Eq)]
-/// Proof that the sender knows the per-commitment secret of the previous commitment transaction.
-///
-/// This is used to convince the recipient that the channel is at a certain commitment
-/// number even if they lost that data due to a local failure. Of course, the peer may lie
-/// and even later commitments may have been revoked.
-pub struct DataLossProtect {
-       /// Proof that the sender knows the per-commitment secret of a specific commitment transaction
-       /// belonging to the recipient
-       pub your_last_per_commitment_secret: [u8; 32],
-       /// The sender's per-commitment point for their current commitment transaction
-       pub my_current_per_commitment_point: PublicKey,
-}
-
 /// A [`channel_reestablish`] message to be sent to or received from a peer.
 ///
 /// [`channel_reestablish`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#message-retransmission
@@ -483,8 +469,11 @@ pub struct ChannelReestablish {
        pub next_local_commitment_number: u64,
        /// The next commitment number for the recipient
        pub next_remote_commitment_number: u64,
-       /// Optionally, a field proving that next_remote_commitment_number-1 has been revoked
-       pub data_loss_protect: OptionalField<DataLossProtect>,
+       /// Proof that the sender knows the per-commitment secret of a specific commitment transaction
+       /// belonging to the recipient
+       pub your_last_per_commitment_secret: [u8; 32],
+       /// The sender's per-commitment point for their current commitment transaction
+       pub my_current_per_commitment_point: PublicKey,
 }
 
 /// An [`announcement_signatures`] message to be sent to or received from a peer.
@@ -1362,42 +1351,13 @@ impl_writeable_msg!(AnnouncementSignatures, {
        bitcoin_signature
 }, {});
 
-impl Writeable for ChannelReestablish {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
-               self.channel_id.write(w)?;
-               self.next_local_commitment_number.write(w)?;
-               self.next_remote_commitment_number.write(w)?;
-               match self.data_loss_protect {
-                       OptionalField::Present(ref data_loss_protect) => {
-                               (*data_loss_protect).your_last_per_commitment_secret.write(w)?;
-                               (*data_loss_protect).my_current_per_commitment_point.write(w)?;
-                       },
-                       OptionalField::Absent => {}
-               }
-               Ok(())
-       }
-}
-
-impl Readable for ChannelReestablish{
-       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
-                       channel_id: Readable::read(r)?,
-                       next_local_commitment_number: Readable::read(r)?,
-                       next_remote_commitment_number: Readable::read(r)?,
-                       data_loss_protect: {
-                               match <[u8; 32] as Readable>::read(r) {
-                                       Ok(your_last_per_commitment_secret) =>
-                                               OptionalField::Present(DataLossProtect {
-                                                       your_last_per_commitment_secret,
-                                                       my_current_per_commitment_point: Readable::read(r)?,
-                                               }),
-                                       Err(DecodeError::ShortRead) => OptionalField::Absent,
-                                       Err(e) => return Err(e)
-                               }
-                       }
-               })
-       }
-}
+impl_writeable_msg!(ChannelReestablish, {
+       channel_id,
+       next_local_commitment_number,
+       next_remote_commitment_number,
+       your_last_per_commitment_secret,
+       my_current_per_commitment_point,
+}, {});
 
 impl_writeable_msg!(ClosingSigned,
        { channel_id, fee_satoshis, signature },
@@ -2162,23 +2122,7 @@ mod tests {
        use core::convert::TryFrom;
 
        #[test]
-       fn encoding_channel_reestablish_no_secret() {
-               let cr = msgs::ChannelReestablish {
-                       channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
-                       next_local_commitment_number: 3,
-                       next_remote_commitment_number: 4,
-                       data_loss_protect: OptionalField::Absent,
-               };
-
-               let encoded_value = cr.encode();
-               assert_eq!(
-                       encoded_value,
-                       vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
-               );
-       }
-
-       #[test]
-       fn encoding_channel_reestablish_with_secret() {
+       fn encoding_channel_reestablish() {
                let public_key = {
                        let secp_ctx = Secp256k1::new();
                        PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
@@ -2188,7 +2132,8 @@ mod tests {
                        channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
                        next_local_commitment_number: 3,
                        next_remote_commitment_number: 4,
-                       data_loss_protect: OptionalField::Present(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
+                       your_last_per_commitment_secret: [9;32],
+                       my_current_per_commitment_point: public_key,
                };
 
                let encoded_value = cr.encode();