Drop the `Writeable::encode_with_len` method in non-test buidls
authorMatt Corallo <git@bluematt.me>
Tue, 15 Mar 2022 23:53:01 +0000 (23:53 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 27 Mar 2022 17:12:17 +0000 (17:12 +0000)
There's not a lot of reason to keep it given its used in one place
outside of tests, and this lets us clean up some of the byte_utils
calls that are still lying around.

lightning/src/ln/channelmanager.rs
lightning/src/util/ser.rs

index a483421db422910c57ffcaa5116f87ca20f7b3e1..6cf07ab4c86fa20c77ee0c75f6d4a2a2eea39c3b 100644 (file)
@@ -2475,21 +2475,22 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                                        break None;
                                }
                                {
-                                       let mut res = Vec::with_capacity(8 + 128);
+                                       let mut res = VecWriter(Vec::with_capacity(chan_update.serialized_length() + 8 + 2));
                                        if let Some(chan_update) = chan_update {
                                                if code == 0x1000 | 11 || code == 0x1000 | 12 {
-                                                       res.extend_from_slice(&byte_utils::be64_to_array(msg.amount_msat));
+                                                       msg.amount_msat.write(&mut res).expect("Writes cannot fail");
                                                }
                                                else if code == 0x1000 | 13 {
-                                                       res.extend_from_slice(&byte_utils::be32_to_array(msg.cltv_expiry));
+                                                       msg.cltv_expiry.write(&mut res).expect("Writes cannot fail");
                                                }
                                                else if code == 0x1000 | 20 {
                                                        // TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
-                                                       res.extend_from_slice(&byte_utils::be16_to_array(0));
+                                                       0u16.write(&mut res).expect("Writes cannot fail");
                                                }
-                                               res.extend_from_slice(&chan_update.encode_with_len()[..]);
+                                               (chan_update.serialized_length() as u16).write(&mut res).expect("Writes cannot fail");
+                                               chan_update.write(&mut res).expect("Writes cannot fail");
                                        }
-                                       return_err!(err, code, &res[..]);
+                                       return_err!(err, code, &res.0[..]);
                                }
                        }
                }
index 6a52e5e1c898230c0d31a4cb9ce20c6acc236076..de12d8506f8d2da87becade08ac82514e30dc6d0 100644 (file)
@@ -174,6 +174,7 @@ pub trait Writeable {
        }
 
        /// Writes self out to a Vec<u8>
+       #[cfg(test)]
        fn encode_with_len(&self) -> Vec<u8> {
                let mut msg = VecWriter(Vec::new());
                0u16.write(&mut msg).unwrap();