Skip to content

Commit 1141cbb

Browse files
committed
Remove unnecessary byte_utils helpers
Now that to_be_bytes is available under our current MSRV of 1.41, we can use it instead of our own version.
1 parent ce15992 commit 1141cbb

File tree

7 files changed

+37
-85
lines changed

7 files changed

+37
-85
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ struct CounterpartyCommitmentParameters {
291291

292292
impl Writeable for CounterpartyCommitmentParameters {
293293
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
294-
w.write_all(&byte_utils::be64_to_array(0))?;
294+
w.write_all(&(0 as u64).to_be_bytes())?;
295295
write_tlv_fields!(w, {
296296
(0, self.counterparty_delayed_payment_base_key, required),
297297
(2, self.counterparty_htlc_base_key, required),
@@ -945,7 +945,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
945945
self.channel_keys_id.write(writer)?;
946946
self.holder_revocation_basepoint.write(writer)?;
947947
writer.write_all(&self.funding_info.0.txid[..])?;
948-
writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
948+
writer.write_all(&self.funding_info.0.index.to_be_bytes())?;
949949
self.funding_info.1.write(writer)?;
950950
self.current_counterparty_commitment_txid.write(writer)?;
951951
self.prev_counterparty_commitment_txid.write(writer)?;
@@ -972,24 +972,24 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
972972
},
973973
}
974974

975-
writer.write_all(&byte_utils::be16_to_array(self.on_holder_tx_csv))?;
975+
writer.write_all(&self.on_holder_tx_csv.to_be_bytes())?;
976976

977977
self.commitment_secrets.write(writer)?;
978978

979979
macro_rules! serialize_htlc_in_commitment {
980980
($htlc_output: expr) => {
981981
writer.write_all(&[$htlc_output.offered as u8; 1])?;
982-
writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
983-
writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
982+
writer.write_all(&$htlc_output.amount_msat.to_be_bytes())?;
983+
writer.write_all(&$htlc_output.cltv_expiry.to_be_bytes())?;
984984
writer.write_all(&$htlc_output.payment_hash.0[..])?;
985985
$htlc_output.transaction_output_index.write(writer)?;
986986
}
987987
}
988988

989-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_claimable_outpoints.len() as u64))?;
989+
writer.write_all(&(self.counterparty_claimable_outpoints.len() as u64).to_be_bytes())?;
990990
for (ref txid, ref htlc_infos) in self.counterparty_claimable_outpoints.iter() {
991991
writer.write_all(&txid[..])?;
992-
writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
992+
writer.write_all(&(htlc_infos.len() as u64).to_be_bytes())?;
993993
for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
994994
debug_assert!(htlc_source.is_none() || Some(**txid) == self.current_counterparty_commitment_txid
995995
|| Some(**txid) == self.prev_counterparty_commitment_txid,
@@ -999,13 +999,13 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
999999
}
10001000
}
10011001

1002-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_commitment_txn_on_chain.len() as u64))?;
1002+
writer.write_all(&(self.counterparty_commitment_txn_on_chain.len() as u64).to_be_bytes())?;
10031003
for (ref txid, commitment_number) in self.counterparty_commitment_txn_on_chain.iter() {
10041004
writer.write_all(&txid[..])?;
10051005
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
10061006
}
10071007

1008-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_hash_commitment_number.len() as u64))?;
1008+
writer.write_all(&(self.counterparty_hash_commitment_number.len() as u64).to_be_bytes())?;
10091009
for (ref payment_hash, commitment_number) in self.counterparty_hash_commitment_number.iter() {
10101010
writer.write_all(&payment_hash.0[..])?;
10111011
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
@@ -1023,7 +1023,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
10231023
writer.write_all(&byte_utils::be48_to_array(self.current_counterparty_commitment_number))?;
10241024
writer.write_all(&byte_utils::be48_to_array(self.current_holder_commitment_number))?;
10251025

1026-
writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
1026+
writer.write_all(&(self.payment_preimages.len() as u64).to_be_bytes())?;
10271027
for payment_preimage in self.payment_preimages.values() {
10281028
writer.write_all(&payment_preimage.0[..])?;
10291029
}
@@ -1044,15 +1044,15 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
10441044
}
10451045
}
10461046

1047-
writer.write_all(&byte_utils::be64_to_array(self.pending_events.len() as u64))?;
1047+
writer.write_all(&(self.pending_events.len() as u64).to_be_bytes())?;
10481048
for event in self.pending_events.iter() {
10491049
event.write(writer)?;
10501050
}
10511051

10521052
self.best_block.block_hash().write(writer)?;
1053-
writer.write_all(&byte_utils::be32_to_array(self.best_block.height()))?;
1053+
writer.write_all(&self.best_block.height().to_be_bytes())?;
10541054

1055-
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?;
1055+
writer.write_all(&(self.onchain_events_awaiting_threshold_conf.len() as u64).to_be_bytes())?;
10561056
for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
10571057
entry.write(writer)?;
10581058
}

lightning/src/chain/keysinterface.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
3131
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
3232
use bitcoin::{PackedLockTime, secp256k1, Sequence, Witness};
3333

34-
use crate::util::{byte_utils, transaction_utils};
34+
use crate::util::transaction_utils;
3535
use crate::util::crypto::{hkdf_extract_expand_twice, sign};
3636
use crate::util::ser::{Writeable, Writer, Readable, ReadableArgs};
3737

@@ -43,6 +43,7 @@ use crate::ln::msgs::UnsignedChannelAnnouncement;
4343
use crate::ln::script::ShutdownScript;
4444

4545
use crate::prelude::*;
46+
use core::convert::TryInto;
4647
use core::sync::atomic::{AtomicUsize, Ordering};
4748
use crate::io::{self, Error};
4849
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
@@ -973,8 +974,8 @@ impl KeysManager {
973974
inbound_pmt_key_bytes.copy_from_slice(&inbound_payment_key[..]);
974975

975976
let mut rand_bytes_unique_start = Sha256::engine();
976-
rand_bytes_unique_start.input(&byte_utils::be64_to_array(starting_time_secs));
977-
rand_bytes_unique_start.input(&byte_utils::be32_to_array(starting_time_nanos));
977+
rand_bytes_unique_start.input(&starting_time_secs.to_be_bytes());
978+
rand_bytes_unique_start.input(&starting_time_nanos.to_be_bytes());
978979
rand_bytes_unique_start.input(seed);
979980

980981
let mut res = KeysManager {
@@ -1006,7 +1007,7 @@ impl KeysManager {
10061007
}
10071008
/// Derive an old Sign containing per-channel secrets based on a key derivation parameters.
10081009
pub fn derive_channel_keys(&self, channel_value_satoshis: u64, params: &[u8; 32]) -> InMemorySigner {
1009-
let chan_id = byte_utils::slice_to_be64(&params[0..8]);
1010+
let chan_id = u64::from_be_bytes(params[0..8].try_into().unwrap());
10101011
let mut unique_start = Sha256::engine();
10111012
unique_start.input(params);
10121013
unique_start.input(&self.seed);

lightning/src/chain/onchaintx.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::chain::package::PackageSolvingData;
3737
use crate::chain::package::PackageTemplate;
3838
use crate::util::logger::Logger;
3939
use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, VecWriter};
40-
use crate::util::byte_utils;
4140

4241
use crate::io;
4342
use crate::prelude::*;
@@ -272,29 +271,29 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
272271
(key_data.0.len() as u32).write(writer)?;
273272
writer.write_all(&key_data.0[..])?;
274273

275-
writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
274+
writer.write_all(&(self.pending_claim_requests.len() as u64).to_be_bytes())?;
276275
for (ref ancestor_claim_txid, request) in self.pending_claim_requests.iter() {
277276
ancestor_claim_txid.write(writer)?;
278277
request.write(writer)?;
279278
}
280279

281-
writer.write_all(&byte_utils::be64_to_array(self.claimable_outpoints.len() as u64))?;
280+
writer.write_all(&(self.claimable_outpoints.len() as u64).to_be_bytes())?;
282281
for (ref outp, ref claim_and_height) in self.claimable_outpoints.iter() {
283282
outp.write(writer)?;
284283
claim_and_height.0.write(writer)?;
285284
claim_and_height.1.write(writer)?;
286285
}
287286

288-
writer.write_all(&byte_utils::be64_to_array(self.locktimed_packages.len() as u64))?;
287+
writer.write_all(&(self.locktimed_packages.len() as u64).to_be_bytes())?;
289288
for (ref locktime, ref packages) in self.locktimed_packages.iter() {
290289
locktime.write(writer)?;
291-
writer.write_all(&byte_utils::be64_to_array(packages.len() as u64))?;
290+
writer.write_all(&(packages.len() as u64).to_be_bytes())?;
292291
for ref package in packages.iter() {
293292
package.write(writer)?;
294293
}
295294
}
296295

297-
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?;
296+
writer.write_all(&(self.onchain_events_awaiting_threshold_conf.len() as u64).to_be_bytes())?;
298297
for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
299298
entry.write(writer)?;
300299
}

lightning/src/chain/package.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use crate::ln::msgs::DecodeError;
2727
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
2828
use crate::chain::keysinterface::Sign;
2929
use crate::chain::onchaintx::OnchainTxHandler;
30-
use crate::util::byte_utils;
3130
use crate::util::logger::Logger;
3231
use crate::util::ser::{Readable, Writer, Writeable};
3332

@@ -774,7 +773,7 @@ impl PackageTemplate {
774773

775774
impl Writeable for PackageTemplate {
776775
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
777-
writer.write_all(&byte_utils::be64_to_array(self.inputs.len() as u64))?;
776+
writer.write_all(&(self.inputs.len() as u64).to_be_bytes())?;
778777
for (ref outpoint, ref rev_outp) in self.inputs.iter() {
779778
outpoint.write(writer)?;
780779
rev_outp.write(writer)?;

lightning/src/ln/chan_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bitcoin::hash_types::{Txid, PubkeyHash};
2424
use crate::ln::{PaymentHash, PaymentPreimage};
2525
use crate::ln::msgs::DecodeError;
2626
use crate::util::ser::{Readable, Writeable, Writer};
27-
use crate::util::{byte_utils, transaction_utils};
27+
use crate::util::transaction_utils;
2828

2929
use bitcoin::secp256k1::{SecretKey, PublicKey, Scalar};
3030
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature, Message};
@@ -310,7 +310,7 @@ impl Writeable for CounterpartyCommitmentSecrets {
310310
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
311311
for &(ref secret, ref idx) in self.old_secrets.iter() {
312312
writer.write_all(secret)?;
313-
writer.write_all(&byte_utils::be64_to_array(*idx))?;
313+
writer.write_all(&idx.to_be_bytes())?;
314314
}
315315
write_tlv_fields!(writer, {});
316316
Ok(())

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use crate::ln::wire::Encode;
5454
use crate::chain::keysinterface::{Sign, KeysInterface, KeysManager, Recipient};
5555
use crate::util::config::{UserConfig, ChannelConfig};
5656
use crate::util::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
57-
use crate::util::{byte_utils, events};
57+
use crate::util::events;
5858
use crate::util::wakers::{Future, Notifier};
5959
use crate::util::scid_utils::fake_scid;
6060
use crate::util::ser::{BigSize, FixedLengthReader, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
@@ -2041,7 +2041,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
20412041
return Err(ReceiveError {
20422042
msg: "Upstream node set CLTV to the wrong value",
20432043
err_code: 18,
2044-
err_data: byte_utils::be32_to_array(cltv_expiry).to_vec()
2044+
err_data: cltv_expiry.to_be_bytes().to_vec()
20452045
})
20462046
}
20472047
// final_expiry_too_soon
@@ -2060,7 +2060,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
20602060
if hop_data.amt_to_forward > amt_msat {
20612061
return Err(ReceiveError {
20622062
err_code: 19,
2063-
err_data: byte_utils::be64_to_array(amt_msat).to_vec(),
2063+
err_data: amt_msat.to_be_bytes().to_vec(),
20642064
msg: "Upstream node sent less than we were supposed to receive in payment",
20652065
});
20662066
}
@@ -3439,9 +3439,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34393439

34403440
macro_rules! fail_htlc {
34413441
($htlc: expr, $payment_hash: expr) => {
3442-
let mut htlc_msat_height_data = byte_utils::be64_to_array($htlc.value).to_vec();
3442+
let mut htlc_msat_height_data = $htlc.value.to_be_bytes().to_vec();
34433443
htlc_msat_height_data.extend_from_slice(
3444-
&byte_utils::be32_to_array(self.best_block.read().unwrap().height()),
3444+
&self.best_block.read().unwrap().height().to_be_bytes(),
34453445
);
34463446
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
34473447
short_channel_id: $htlc.prev_hop.short_channel_id,
@@ -3895,9 +3895,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38953895
let removed_source = self.claimable_htlcs.lock().unwrap().remove(payment_hash);
38963896
if let Some((_, mut sources)) = removed_source {
38973897
for htlc in sources.drain(..) {
3898-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
3899-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
3900-
self.best_block.read().unwrap().height()));
3898+
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
3899+
htlc_msat_height_data.extend_from_slice(&self.best_block.read().unwrap().height().to_be_bytes());
39013900
self.fail_htlc_backwards_internal(
39023901
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
39033902
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data },
@@ -4291,9 +4290,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42914290
mem::drop(channel_state_lock);
42924291
if !valid_mpp {
42934292
for htlc in sources.drain(..) {
4294-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
4295-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
4296-
self.best_block.read().unwrap().height()));
4293+
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
4294+
htlc_msat_height_data.extend_from_slice(&self.best_block.read().unwrap().height().to_be_bytes());
42974295
self.fail_htlc_backwards_internal(
42984296
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
42994297
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data },
@@ -6267,8 +6265,8 @@ where
62676265
// number of blocks we generally consider it to take to do a commitment update,
62686266
// just give up on it and fail the HTLC.
62696267
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
6270-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
6271-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(height));
6268+
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
6269+
htlc_msat_height_data.extend_from_slice(&height.to_be_bytes());
62726270
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(), HTLCFailReason::Reason {
62736271
failure_code: 0x4000 | 15,
62746272
data: htlc_msat_height_data

lightning/src/util/byte_utils.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,6 @@ pub fn slice_to_be48(v: &[u8]) -> u64 {
1717
((v[5] as u64) << 8*0)
1818
}
1919
#[inline]
20-
pub fn slice_to_be64(v: &[u8]) -> u64 {
21-
((v[0] as u64) << 8*7) |
22-
((v[1] as u64) << 8*6) |
23-
((v[2] as u64) << 8*5) |
24-
((v[3] as u64) << 8*4) |
25-
((v[4] as u64) << 8*3) |
26-
((v[5] as u64) << 8*2) |
27-
((v[6] as u64) << 8*1) |
28-
((v[7] as u64) << 8*0)
29-
}
30-
31-
#[inline]
32-
pub fn be16_to_array(u: u16) -> [u8; 2] {
33-
let mut v = [0; 2];
34-
v[0] = ((u >> 8*1) & 0xff) as u8;
35-
v[1] = ((u >> 8*0) & 0xff) as u8;
36-
v
37-
}
38-
#[inline]
39-
pub fn be32_to_array(u: u32) -> [u8; 4] {
40-
let mut v = [0; 4];
41-
v[0] = ((u >> 8*3) & 0xff) as u8;
42-
v[1] = ((u >> 8*2) & 0xff) as u8;
43-
v[2] = ((u >> 8*1) & 0xff) as u8;
44-
v[3] = ((u >> 8*0) & 0xff) as u8;
45-
v
46-
}
47-
#[inline]
4820
pub fn be48_to_array(u: u64) -> [u8; 6] {
4921
assert!(u & 0xffff_0000_0000_0000 == 0);
5022
let mut v = [0; 6];
@@ -56,19 +28,6 @@ pub fn be48_to_array(u: u64) -> [u8; 6] {
5628
v[5] = ((u >> 8*0) & 0xff) as u8;
5729
v
5830
}
59-
#[inline]
60-
pub fn be64_to_array(u: u64) -> [u8; 8] {
61-
let mut v = [0; 8];
62-
v[0] = ((u >> 8*7) & 0xff) as u8;
63-
v[1] = ((u >> 8*6) & 0xff) as u8;
64-
v[2] = ((u >> 8*5) & 0xff) as u8;
65-
v[3] = ((u >> 8*4) & 0xff) as u8;
66-
v[4] = ((u >> 8*3) & 0xff) as u8;
67-
v[5] = ((u >> 8*2) & 0xff) as u8;
68-
v[6] = ((u >> 8*1) & 0xff) as u8;
69-
v[7] = ((u >> 8*0) & 0xff) as u8;
70-
v
71-
}
7231

7332
#[cfg(test)]
7433
mod tests {
@@ -77,10 +36,6 @@ mod tests {
7736
#[test]
7837
fn test_all() {
7938
assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
80-
assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea);
81-
assert_eq!(be16_to_array(0xdead), [0xde, 0xad]);
82-
assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
8339
assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
84-
assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]);
8540
}
8641
}

0 commit comments

Comments
 (0)