From: Matt Corallo Date: Tue, 15 Mar 2022 23:53:01 +0000 (+0000) Subject: Drop the `Writeable::encode_with_len` method in non-test buidls X-Git-Tag: v0.0.106~4^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=c47acd76e8e60b65335a1bd1b9bbb53ddd43e496;p=rust-lightning Drop the `Writeable::encode_with_len` method in non-test buidls 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. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index a483421d..6cf07ab4 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -2475,21 +2475,22 @@ impl 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[..]); } } } diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 6a52e5e1..de12d850 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -174,6 +174,7 @@ pub trait Writeable { } /// Writes self out to a Vec + #[cfg(test)] fn encode_with_len(&self) -> Vec { let mut msg = VecWriter(Vec::new()); 0u16.write(&mut msg).unwrap();