From 91a60c82567d95e982adab4e19e8df5666b41904 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 9 Sep 2024 15:30:09 +0000 Subject: [PATCH 1/3] impl `Borrow<[u8]>` for `ChannelId` We do this for `Payment*` in `lightning-types` and its needed for the `hex_conservaitve` `impl_fmt_traits` macro which we'll use in the next commit. --- lightning/src/ln/channelmanager.rs | 2 +- lightning/src/ln/types.rs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index b3047930f61..a1dc6fd4cfc 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -7135,7 +7135,7 @@ where chan } else { let update_actions = peer_state.monitor_update_blocked_actions - .remove(&channel_id).unwrap_or(Vec::new()); + .remove(channel_id).unwrap_or(Vec::new()); mem::drop(peer_state_lock); mem::drop(per_peer_state); self.handle_monitor_update_completion_actions(update_actions); diff --git a/lightning/src/ln/types.rs b/lightning/src/ln/types.rs index 659c7e4caba..9d56ad46d85 100644 --- a/lightning/src/ln/types.rs +++ b/lightning/src/ln/types.rs @@ -30,6 +30,7 @@ use bitcoin::hashes::{ HashEngine as _, sha256::Hash as Sha256, }; +use core::borrow::Borrow; use core::fmt; use core::ops::Deref; @@ -127,6 +128,12 @@ impl fmt::Display for ChannelId { } } +impl Borrow<[u8]> for ChannelId { + fn borrow(&self) -> &[u8] { + &self.0[..] + } +} + pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; #[cfg(test)] From cc6e2a0f7af60b0809dd9ff849bd3a41d3a78d3f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 9 Sep 2024 15:30:54 +0000 Subject: [PATCH 2/3] Write `ChannelId`s out as hex in `Debug` output `ChannelId`s are almost always referenced as hex, so having debug output print the raw bytes is somewhat annoying. Instead, we should dump them as hex the same way we do for `Display`. This uses the `hex_conservative` `impl_fmt_macros` which does all the work for us, like we use for `lightning_types`. --- lightning/src/ln/types.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lightning/src/ln/types.rs b/lightning/src/ln/types.rs index 9d56ad46d85..dbe1e216b90 100644 --- a/lightning/src/ln/types.rs +++ b/lightning/src/ln/types.rs @@ -30,8 +30,8 @@ use bitcoin::hashes::{ HashEngine as _, sha256::Hash as Sha256, }; +use bitcoin::hex::display::impl_fmt_traits; use core::borrow::Borrow; -use core::fmt; use core::ops::Deref; /// A unique 32-byte identifier for a channel. @@ -42,7 +42,7 @@ use core::ops::Deref; /// A _temporary_ ID is generated randomly. /// (Later revocation-point-based _v2_ is a possibility.) /// The variety (context) is not stored, it is relevant only at creation. -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct ChannelId(pub [u8; 32]); impl ChannelId { @@ -122,18 +122,18 @@ impl Readable for ChannelId { } } -impl fmt::Display for ChannelId { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - crate::util::logger::DebugBytes(&self.0).fmt(f) - } -} - impl Borrow<[u8]> for ChannelId { fn borrow(&self) -> &[u8] { &self.0[..] } } +impl_fmt_traits! { + impl fmt_traits for ChannelId { + const LENGTH: usize = 32; + } +} + pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; #[cfg(test)] From 98d15f2c9945af75a99ca40df0a94b4d6e9b6ef1 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 9 Sep 2024 15:32:12 +0000 Subject: [PATCH 3/3] Clean up imports in `lightning-types` To remove the now-redundant `hex_conservative` explicit dependency. --- lightning-types/Cargo.toml | 2 -- lightning-types/src/payment.rs | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lightning-types/Cargo.toml b/lightning-types/Cargo.toml index 9e8feedf6ef..f3eeec0f1a9 100644 --- a/lightning-types/Cargo.toml +++ b/lightning-types/Cargo.toml @@ -17,8 +17,6 @@ _test_utils = [] [dependencies] bitcoin = { version = "0.32.2", default-features = false } -# TODO: Once we switch to bitcoin 0.32 drop this explicit dep: -hex-conservative = { version = "0.2", default-features = false } bech32 = { version = "0.9", default-features = false } [lints] diff --git a/lightning-types/src/payment.rs b/lightning-types/src/payment.rs index 6b8854ac5f8..a2832c6b559 100644 --- a/lightning-types/src/payment.rs +++ b/lightning-types/src/payment.rs @@ -14,9 +14,7 @@ use alloc::vec::Vec; use core::borrow::Borrow; use bitcoin::hashes::{sha256::Hash as Sha256, Hash as _}; - -// TODO: Once we switch to rust-bitcoin 0.32, import this as bitcoin::hex -use hex_conservative::display::impl_fmt_traits; +use bitcoin::hex::display::impl_fmt_traits; /// The payment hash is the hash of the [`PaymentPreimage`] which is the value used to lock funds /// in HTLCs while they transit the lightning network.