Skip to content

rustfmt: Run on some smaller files in lightning/ln #3724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lightning/src/ln/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

//! Implementations of extensions on features.

use lightning_types::features::{InitFeatures, NodeFeatures, ChannelFeatures};
use lightning_types::features::{Bolt11InvoiceFeatures, OfferFeatures, InvoiceRequestFeatures};
use lightning_types::features::{Bolt12InvoiceFeatures, BlindedHopFeatures};
use lightning_types::features::ChannelTypeFeatures;
use lightning_types::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
use lightning_types::features::{Bolt11InvoiceFeatures, InvoiceRequestFeatures, OfferFeatures};
use lightning_types::features::{ChannelFeatures, InitFeatures, NodeFeatures};

#[allow(unused_imports)]
use crate::prelude::*;

use crate::{io, io_extras};
use crate::ln::msgs::DecodeError;
use crate::util::ser::{Writer, Readable, Writeable, WithoutLength};
use crate::util::ser::{Readable, WithoutLength, Writeable, Writer};
use crate::{io, io_extras};

fn write_be<W: Writer>(w: &mut W, le_flags: &[u8]) -> Result<(), io::Error> {
for f in le_flags.iter().rev() { // Swap back to big-endian
// Swap back to big-endian
for f in le_flags.iter().rev() {
f.write(w)?;
}
Ok(())
Expand All @@ -42,7 +43,7 @@ macro_rules! impl_feature_len_prefixed_write {
Ok(Self::from_be_bytes(Vec::<u8>::read(r)?))
}
}
}
};
}
impl_feature_len_prefixed_write!(InitFeatures);
impl_feature_len_prefixed_write!(ChannelFeatures);
Expand All @@ -64,7 +65,7 @@ macro_rules! impl_feature_tlv_write {
Ok(WithoutLength::<Self>::read(r)?.0)
}
}
}
};
}

impl_feature_tlv_write!(ChannelTypeFeatures);
Expand All @@ -86,7 +87,7 @@ macro_rules! impl_feature_write_without_length {
Ok(WithoutLength($features::from_be_bytes(v)))
}
}
}
};
}

impl_feature_write_without_length!(Bolt12InvoiceFeatures);
Expand Down
33 changes: 21 additions & 12 deletions lightning/src/ln/script.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//! Abstractions for scripts used in the Lightning Network.

use bitcoin::{WitnessProgram, WPubkeyHash, WScriptHash};
use bitcoin::hashes::Hash;
use bitcoin::opcodes::all::OP_PUSHBYTES_0 as SEGWIT_V0;
use bitcoin::script::{Script, ScriptBuf};
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{WPubkeyHash, WScriptHash, WitnessProgram};

use crate::ln::channelmanager;
use crate::types::features::InitFeatures;
use crate::ln::msgs::DecodeError;
use crate::types::features::InitFeatures;
use crate::util::config::UserConfig;
use crate::util::ser::{Readable, Writeable, Writer};

use crate::io;
Expand All @@ -28,7 +29,7 @@ pub struct InvalidShutdownScript {
/// The script that did not meet the requirements from [BOLT #2].
///
/// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
pub script: ScriptBuf
pub script: ScriptBuf,
}

#[derive(Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -82,7 +83,9 @@ impl ShutdownScript {
/// # Errors
///
/// This function may return an error if `program` is invalid for the segwit `version`.
pub fn new_witness_program(witness_program: &WitnessProgram) -> Result<Self, InvalidShutdownScript> {
pub fn new_witness_program(
witness_program: &WitnessProgram,
) -> Result<Self, InvalidShutdownScript> {
Self::try_from(ScriptBuf::new_witness_program(witness_program))
}

Expand Down Expand Up @@ -128,7 +131,8 @@ impl TryFrom<ScriptBuf> for ShutdownScript {
type Error = InvalidShutdownScript;

fn try_from(script: ScriptBuf) -> Result<Self, Self::Error> {
Self::try_from((script, &channelmanager::provided_init_features(&crate::util::config::UserConfig::default())))
let features = channelmanager::provided_init_features(&UserConfig::default());
Self::try_from((script, &features))
}
}

Expand All @@ -149,14 +153,15 @@ impl TryFrom<(ScriptBuf, &InitFeatures)> for ShutdownScript {
impl Into<ScriptBuf> for ShutdownScript {
fn into(self) -> ScriptBuf {
match self.0 {
ShutdownScriptImpl::Legacy(pubkey) =>
ScriptBuf::new_p2wpkh(&WPubkeyHash::hash(&pubkey.serialize())),
ShutdownScriptImpl::Legacy(pubkey) => {
ScriptBuf::new_p2wpkh(&WPubkeyHash::hash(&pubkey.serialize()))
},
ShutdownScriptImpl::Bolt2(script_pubkey) => script_pubkey,
}
}
}

impl core::fmt::Display for ShutdownScript{
impl core::fmt::Display for ShutdownScript {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match &self.0 {
ShutdownScriptImpl::Legacy(_) => self.clone().into_inner().fmt(f),
Expand All @@ -169,18 +174,22 @@ impl core::fmt::Display for ShutdownScript{
mod shutdown_script_tests {
use super::ShutdownScript;

use bitcoin::{WitnessProgram, WitnessVersion};
use bitcoin::opcodes;
use bitcoin::script::{Builder, ScriptBuf};
use bitcoin::secp256k1::Secp256k1;
use bitcoin::secp256k1::{PublicKey, SecretKey};
use bitcoin::{WitnessProgram, WitnessVersion};

use crate::types::features::InitFeatures;
use crate::prelude::*;
use crate::types::features::InitFeatures;

fn pubkey() -> bitcoin::key::PublicKey {
let secp_ctx = Secp256k1::signing_only();
let secret_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).unwrap();
let secret_key = SecretKey::from_slice(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1,
])
.unwrap();
bitcoin::key::PublicKey::new(PublicKey::from_secret_key(&secp_ctx, &secret_key))
}

Expand Down
3 changes: 0 additions & 3 deletions rustfmt_excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ lightning/src/ln/blinded_payment_tests.rs
lightning/src/ln/chan_utils.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/channel.rs
lightning/src/ln/channel_id.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/features.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/inbound_payment.rs
Expand All @@ -34,7 +32,6 @@ lightning/src/ln/peer_handler.rs
lightning/src/ln/priv_short_conf_tests.rs
lightning/src/ln/reload_tests.rs
lightning/src/ln/reorg_tests.rs
lightning/src/ln/script.rs
lightning/src/ln/shutdown_tests.rs
lightning/src/routing/mod.rs
lightning/src/routing/router.rs
Expand Down
Loading