Skip to content

Commit c89c64e

Browse files
committed
Use the new ChannelId struct
1 parent 43a9264 commit c89c64e

18 files changed

+392
-342
lines changed

fuzz/src/full_stack.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use lightning::chain::chainmonitor;
3434
use lightning::chain::transaction::OutPoint;
3535
use lightning::sign::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
3636
use lightning::events::Event;
37-
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
37+
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
3838
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId, RecipientOnionFields, Retry};
3939
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
4040
use lightning::ln::msgs::{self, DecodeError};
@@ -481,7 +481,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
481481
let mut should_forward = false;
482482
let mut payments_received: Vec<PaymentHash> = Vec::new();
483483
let mut payments_sent = 0;
484-
let mut pending_funding_generation: Vec<([u8; 32], PublicKey, u64, Script)> = Vec::new();
484+
let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, Script)> = Vec::new();
485485
let mut pending_funding_signatures = HashMap::new();
486486

487487
loop {

fuzz/src/router.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use bitcoin::hash_types::BlockHash;
1313

1414
use lightning::blinded_path::{BlindedHop, BlindedPath};
1515
use lightning::chain::transaction::OutPoint;
16+
use lightning::ln::ChannelId;
1617
use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
1718
use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
1819
use lightning::ln::msgs;
@@ -210,7 +211,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
210211
let rnid = node_pks.iter().skip(u16::from_be_bytes(get_slice!(2).try_into().unwrap()) as usize % node_pks.len()).next().unwrap();
211212
let capacity = u64::from_be_bytes(get_slice!(8).try_into().unwrap());
212213
$first_hops_vec.push(ChannelDetails {
213-
channel_id: [0; 32],
214+
channel_id: ChannelId::new_zero(),
214215
counterparty: ChannelCounterparty {
215216
node_id: *rnid,
216217
features: channelmanager::provided_init_features(&UserConfig::default()),

lightning-invoice/src/utils.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ where
627627
log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
628628
for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
629629
if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
630-
log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id));
630+
log_trace!(logger, "Ignoring channel {} for invoice route hints", &channel.channel_id);
631631
continue;
632632
}
633633

@@ -641,7 +641,7 @@ where
641641
// If any public channel exists, return no hints and let the sender
642642
// look at the public channels instead.
643643
log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
644-
log_bytes!(channel.channel_id));
644+
&channel.channel_id);
645645
return vec![].into_iter().take(MAX_CHANNEL_HINTS).map(route_hint_from_channel);
646646
}
647647
}
@@ -681,18 +681,18 @@ where
681681
log_trace!(logger,
682682
"Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints",
683683
log_pubkey!(channel.counterparty.node_id),
684-
log_bytes!(channel.channel_id), channel.short_channel_id,
684+
&channel.channel_id, channel.short_channel_id,
685685
channel.inbound_capacity_msat,
686-
log_bytes!(entry.get().channel_id), entry.get().short_channel_id,
686+
&entry.get().channel_id, entry.get().short_channel_id,
687687
current_max_capacity);
688688
entry.insert(channel);
689689
} else {
690690
log_trace!(logger,
691691
"Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints",
692692
log_pubkey!(channel.counterparty.node_id),
693-
log_bytes!(entry.get().channel_id), entry.get().short_channel_id,
693+
&entry.get().channel_id, entry.get().short_channel_id,
694694
current_max_capacity,
695-
log_bytes!(channel.channel_id), channel.short_channel_id,
695+
&channel.channel_id, channel.short_channel_id,
696696
channel.inbound_capacity_msat);
697697
}
698698
}
@@ -731,14 +731,14 @@ where
731731

732732
if include_channel {
733733
log_trace!(logger, "Including channel {} in invoice route hints",
734-
log_bytes!(channel.channel_id));
734+
&channel.channel_id);
735735
} else if !has_enough_capacity {
736736
log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints",
737-
log_bytes!(channel.channel_id));
737+
&channel.channel_id);
738738
} else {
739739
debug_assert!(!channel.is_usable || (has_pub_unconf_chan && !channel.is_public));
740740
log_trace!(logger, "Ignoring channel {} with disconnected peer",
741-
log_bytes!(channel.channel_id));
741+
&channel.channel_id);
742742
}
743743

744744
include_channel

lightning/src/chain/channelmonitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2642,7 +2642,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26422642
}
26432643
} else if !self.holder_tx_signed {
26442644
log_error!(logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast");
2645-
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id()));
2645+
log_error!(logger, " in channel monitor for channel {}!", &self.funding_info.0.to_channel_id());
26462646
log_error!(logger, " Read the docs for ChannelMonitor::get_latest_holder_commitment_txn and take manual action!");
26472647
} else {
26482648
// If we generated a MonitorEvent::CommitmentTxConfirmed, the ChannelManager
@@ -3389,7 +3389,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33893389
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
33903390
let mut balance_spendable_csv = None;
33913391
log_info!(logger, "Channel {} closed by funding output spend in txid {}.",
3392-
log_bytes!(self.funding_info.0.to_channel_id()), txid);
3392+
&self.funding_info.0.to_channel_id(), txid);
33933393
self.funding_spend_seen = true;
33943394
let mut commitment_tx_to_counterparty_output = None;
33953395
if (tx.input[0].sequence.0 >> 8*3) as u8 == 0x80 && (tx.lock_time.0 >> 8*3) as u8 == 0x20 {

lightning/src/chain/transaction.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
//! Types describing on-chain transactions.
1111
12+
use crate::ln::ChannelId;
1213
use bitcoin::hash_types::Txid;
14+
use bitcoin::hashes::Hash;
1315
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
1416
use bitcoin::blockdata::transaction::Transaction;
1517

@@ -57,12 +59,8 @@ pub struct OutPoint {
5759

5860
impl OutPoint {
5961
/// Convert an `OutPoint` to a lightning channel id.
60-
pub fn to_channel_id(&self) -> [u8; 32] {
61-
let mut res = [0; 32];
62-
res[..].copy_from_slice(&self.txid[..]);
63-
res[30] ^= ((self.index >> 8) & 0xff) as u8;
64-
res[31] ^= ((self.index >> 0) & 0xff) as u8;
65-
res
62+
pub fn to_channel_id(&self) -> ChannelId {
63+
ChannelId::v1_from_funding_txid(&self.txid.as_inner(), self.index)
6664
}
6765

6866
/// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
@@ -94,10 +92,10 @@ mod tests {
9492
assert_eq!(&OutPoint {
9593
txid: tx.txid(),
9694
index: 0
97-
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
95+
}.to_channel_id().bytes()[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
9896
assert_eq!(&OutPoint {
9997
txid: tx.txid(),
10098
index: 1
101-
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
99+
}.to_channel_id().bytes()[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
102100
}
103101
}

lightning/src/events/mod.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
2323
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
2424
use crate::ln::features::ChannelTypeFeatures;
2525
use crate::ln::msgs;
26-
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
26+
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
2727
use crate::routing::gossip::NetworkUpdate;
2828
use crate::util::errors::APIError;
2929
use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, RequiredWrapper, UpgradableRequired, WithoutLength};
@@ -83,7 +83,7 @@ impl_writeable_tlv_based_enum!(PaymentPurpose,
8383
#[derive(Clone, Debug, PartialEq, Eq)]
8484
pub struct ClaimedHTLC {
8585
/// The `channel_id` of the channel over which the HTLC was received.
86-
pub channel_id: [u8; 32],
86+
pub channel_id: ChannelId,
8787
/// The `user_channel_id` of the channel over which the HTLC was received. This is the value
8888
/// passed in to [`ChannelManager::create_channel`] for outbound channels, or to
8989
/// [`ChannelManager::accept_inbound_channel`] for inbound channels if
@@ -246,7 +246,7 @@ pub enum HTLCDestination {
246246
/// counterparty node information.
247247
node_id: Option<PublicKey>,
248248
/// The outgoing `channel_id` between us and the next node.
249-
channel_id: [u8; 32],
249+
channel_id: ChannelId,
250250
},
251251
/// Scenario where we are unsure of the next node to forward the HTLC to.
252252
UnknownNextHop {
@@ -364,7 +364,7 @@ pub enum Event {
364364
/// [`ChannelManager::funding_transaction_generated`].
365365
///
366366
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
367-
temporary_channel_id: [u8; 32],
367+
temporary_channel_id: ChannelId,
368368
/// The counterparty's node_id, which you'll need to pass back into
369369
/// [`ChannelManager::funding_transaction_generated`].
370370
///
@@ -458,7 +458,7 @@ pub enum Event {
458458
/// payment is to pay an invoice or to send a spontaneous payment.
459459
purpose: PaymentPurpose,
460460
/// The `channel_id` indicating over which channel we received the payment.
461-
via_channel_id: Option<[u8; 32]>,
461+
via_channel_id: Option<ChannelId>,
462462
/// The `user_channel_id` indicating over which channel we received the payment.
463463
via_user_channel_id: Option<u128>,
464464
/// The block height at which this payment will be failed back and will no longer be
@@ -718,17 +718,17 @@ pub enum Event {
718718
/// The `channel_id` indicating which channel the spendable outputs belong to.
719719
///
720720
/// This will always be `Some` for events generated by LDK versions 0.0.117 and above.
721-
channel_id: Option<[u8; 32]>,
721+
channel_id: Option<ChannelId>,
722722
},
723723
/// This event is generated when a payment has been successfully forwarded through us and a
724724
/// forwarding fee earned.
725725
PaymentForwarded {
726726
/// The incoming channel between the previous node and us. This is only `None` for events
727727
/// generated or serialized by versions prior to 0.0.107.
728-
prev_channel_id: Option<[u8; 32]>,
728+
prev_channel_id: Option<ChannelId>,
729729
/// The outgoing channel between the next node and us. This is only `None` for events
730730
/// generated or serialized by versions prior to 0.0.107.
731-
next_channel_id: Option<[u8; 32]>,
731+
next_channel_id: Option<ChannelId>,
732732
/// The fee, in milli-satoshis, which was earned as a result of the payment.
733733
///
734734
/// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
@@ -759,7 +759,7 @@ pub enum Event {
759759
/// [`Event::ChannelReady`] event.
760760
ChannelPending {
761761
/// The `channel_id` of the channel that is pending confirmation.
762-
channel_id: [u8; 32],
762+
channel_id: ChannelId,
763763
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
764764
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
765765
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -772,7 +772,7 @@ pub enum Event {
772772
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
773773
///
774774
/// Will be `None` for channels created prior to LDK version 0.0.115.
775-
former_temporary_channel_id: Option<[u8; 32]>,
775+
former_temporary_channel_id: Option<ChannelId>,
776776
/// The `node_id` of the channel counterparty.
777777
counterparty_node_id: PublicKey,
778778
/// The outpoint of the channel's funding transaction.
@@ -784,7 +784,7 @@ pub enum Event {
784784
/// establishment.
785785
ChannelReady {
786786
/// The `channel_id` of the channel that is ready.
787-
channel_id: [u8; 32],
787+
channel_id: ChannelId,
788788
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
789789
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
790790
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -811,7 +811,7 @@ pub enum Event {
811811
ChannelClosed {
812812
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
813813
/// resolving the channel are likely still awaiting confirmation.
814-
channel_id: [u8; 32],
814+
channel_id: ChannelId,
815815
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
816816
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
817817
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -838,7 +838,7 @@ pub enum Event {
838838
/// inputs for another purpose.
839839
DiscardFunding {
840840
/// The channel_id of the channel which has been closed.
841-
channel_id: [u8; 32],
841+
channel_id: ChannelId,
842842
/// The full transaction received from the user
843843
transaction: Transaction
844844
},
@@ -863,7 +863,7 @@ pub enum Event {
863863
///
864864
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
865865
/// [`ChannelManager::force_close_without_broadcasting_txn`]: crate::ln::channelmanager::ChannelManager::force_close_without_broadcasting_txn
866-
temporary_channel_id: [u8; 32],
866+
temporary_channel_id: ChannelId,
867867
/// The node_id of the counterparty requesting to open the channel.
868868
///
869869
/// When responding to the request, the `counterparty_node_id` should be passed
@@ -909,7 +909,7 @@ pub enum Event {
909909
/// requirements (i.e. insufficient fees paid, or a CLTV that is too soon).
910910
HTLCHandlingFailed {
911911
/// The channel over which the HTLC was received.
912-
prev_channel_id: [u8; 32],
912+
prev_channel_id: ChannelId,
913913
/// Destination of the HTLC that failed to be processed.
914914
failed_next_destination: HTLCDestination,
915915
},
@@ -1279,7 +1279,7 @@ impl MaybeReadable for Event {
12791279
5u8 => {
12801280
let f = || {
12811281
let mut outputs = WithoutLength(Vec::new());
1282-
let mut channel_id: Option<[u8; 32]> = None;
1282+
let mut channel_id: Option<ChannelId> = None;
12831283
read_tlv_fields!(reader, {
12841284
(0, outputs, required),
12851285
(1, channel_id, option),
@@ -1335,7 +1335,7 @@ impl MaybeReadable for Event {
13351335
},
13361336
9u8 => {
13371337
let f = || {
1338-
let mut channel_id = [0; 32];
1338+
let mut channel_id = ChannelId::new_zero();
13391339
let mut reason = UpgradableRequired(None);
13401340
let mut user_channel_id_low_opt: Option<u64> = None;
13411341
let mut user_channel_id_high_opt: Option<u64> = None;
@@ -1363,7 +1363,7 @@ impl MaybeReadable for Event {
13631363
},
13641364
11u8 => {
13651365
let f = || {
1366-
let mut channel_id = [0; 32];
1366+
let mut channel_id = ChannelId::new_zero();
13671367
let mut transaction = Transaction{ version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
13681368
read_tlv_fields!(reader, {
13691369
(0, channel_id, required),
@@ -1474,7 +1474,7 @@ impl MaybeReadable for Event {
14741474
},
14751475
25u8 => {
14761476
let f = || {
1477-
let mut prev_channel_id = [0; 32];
1477+
let mut prev_channel_id = ChannelId::new_zero();
14781478
let mut failed_next_destination_opt = UpgradableRequired(None);
14791479
read_tlv_fields!(reader, {
14801480
(0, prev_channel_id, required),
@@ -1490,7 +1490,7 @@ impl MaybeReadable for Event {
14901490
27u8 => Ok(None),
14911491
29u8 => {
14921492
let f = || {
1493-
let mut channel_id = [0; 32];
1493+
let mut channel_id = ChannelId::new_zero();
14941494
let mut user_channel_id: u128 = 0;
14951495
let mut counterparty_node_id = RequiredWrapper(None);
14961496
let mut channel_type = RequiredWrapper(None);
@@ -1512,7 +1512,7 @@ impl MaybeReadable for Event {
15121512
},
15131513
31u8 => {
15141514
let f = || {
1515-
let mut channel_id = [0; 32];
1515+
let mut channel_id = ChannelId::new_zero();
15161516
let mut user_channel_id: u128 = 0;
15171517
let mut former_temporary_channel_id = None;
15181518
let mut counterparty_node_id = RequiredWrapper(None);

0 commit comments

Comments
 (0)