Skip to content

Commit 4cc3e31

Browse files
committed
Introduce new ChannelId struct
1 parent 7a63ab7 commit 4cc3e31

File tree

17 files changed

+426
-324
lines changed

17 files changed

+426
-324
lines changed

fuzz/src/full_stack.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use lightning::chain::transaction::OutPoint;
3535
use lightning::sign::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
3636
use lightning::events::Event;
3737
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
38+
use lightning::ln::channel::ChannelId;
3839
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId, RecipientOnionFields, Retry};
3940
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
4041
use lightning::ln::msgs::{self, DecodeError};
@@ -466,7 +467,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
466467
let mut should_forward = false;
467468
let mut payments_received: Vec<PaymentHash> = Vec::new();
468469
let mut payments_sent = 0;
469-
let mut pending_funding_generation: Vec<([u8; 32], PublicKey, u64, Script)> = Vec::new();
470+
let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, Script)> = Vec::new();
470471
let mut pending_funding_signatures = HashMap::new();
471472

472473
loop {

fuzz/src/router.rs

Lines changed: 2 additions & 1 deletion
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::channel::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

Lines changed: 9 additions & 9 deletions
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", log_bytes!(channel.channel_id.bytes()[..]));
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+
log_bytes!(channel.channel_id.bytes()[..]));
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+
log_bytes!(channel.channel_id.bytes()[..]), channel.short_channel_id,
685685
channel.inbound_capacity_msat,
686-
log_bytes!(entry.get().channel_id), entry.get().short_channel_id,
686+
log_bytes!(entry.get().channel_id.bytes()[..]), 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+
log_bytes!(entry.get().channel_id.bytes()[..]), entry.get().short_channel_id,
694694
current_max_capacity,
695-
log_bytes!(channel.channel_id), channel.short_channel_id,
695+
log_bytes!(channel.channel_id.bytes()[..]), 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+
log_bytes!(channel.channel_id.bytes()[..]));
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+
log_bytes!(channel.channel_id.bytes()[..]));
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+
log_bytes!(channel.channel_id.bytes()[..]));
742742
}
743743

744744
include_channel

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25252525
}
25262526
} else if !self.holder_tx_signed {
25272527
log_error!(logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast");
2528-
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id()));
2528+
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id().bytes()[..]));
25292529
log_error!(logger, " Read the docs for ChannelMonitor::get_latest_holder_commitment_txn and take manual action!");
25302530
} else {
25312531
// If we generated a MonitorEvent::CommitmentTxConfirmed, the ChannelManager
@@ -3183,7 +3183,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31833183
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
31843184
let mut balance_spendable_csv = None;
31853185
log_info!(logger, "Channel {} closed by funding output spend in txid {}.",
3186-
log_bytes!(self.funding_info.0.to_channel_id()), txid);
3186+
log_bytes!(self.funding_info.0.to_channel_id().bytes()[..]), txid);
31873187
self.funding_spend_seen = true;
31883188
let mut commitment_tx_to_counterparty_output = None;
31893189
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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! Types describing on-chain transactions.
1111
12+
use crate::ln::channel::ChannelId;
1213
use bitcoin::hash_types::Txid;
1314
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
1415
use bitcoin::blockdata::transaction::Transaction;
@@ -56,13 +57,9 @@ pub struct OutPoint {
5657
}
5758

5859
impl OutPoint {
59-
/// 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
60+
/// Convert an `OutPoint` to a lightning channel id. Alternatively use ChannelId::from_funding_outpoint().
61+
pub fn to_channel_id(&self) -> ChannelId {
62+
ChannelId::from_funding_outpoint(&self)
6663
}
6764

6865
/// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
@@ -94,10 +91,10 @@ mod tests {
9491
assert_eq!(&OutPoint {
9592
txid: tx.txid(),
9693
index: 0
97-
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
94+
}.to_channel_id().bytes()[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
9895
assert_eq!(&OutPoint {
9996
txid: tx.txid(),
10097
index: 1
101-
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
98+
}.to_channel_id().bytes()[..], &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
10299
}
103100
}

lightning/src/events/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use bump_transaction::BumpTransactionEvent;
2020

2121
use crate::sign::SpendableOutputDescriptor;
2222
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
23-
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
23+
use crate::ln::channel::{ChannelId, FUNDING_CONF_DEADLINE_BLOCKS};
2424
use crate::ln::features::ChannelTypeFeatures;
2525
use crate::ln::msgs;
2626
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
@@ -215,7 +215,7 @@ pub enum HTLCDestination {
215215
/// counterparty node information.
216216
node_id: Option<PublicKey>,
217217
/// The outgoing `channel_id` between us and the next node.
218-
channel_id: [u8; 32],
218+
channel_id: ChannelId,
219219
},
220220
/// Scenario where we are unsure of the next node to forward the HTLC to.
221221
UnknownNextHop {
@@ -333,7 +333,7 @@ pub enum Event {
333333
/// [`ChannelManager::funding_transaction_generated`].
334334
///
335335
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
336-
temporary_channel_id: [u8; 32],
336+
temporary_channel_id: ChannelId,
337337
/// The counterparty's node_id, which you'll need to pass back into
338338
/// [`ChannelManager::funding_transaction_generated`].
339339
///
@@ -411,7 +411,7 @@ pub enum Event {
411411
/// payment is to pay an invoice or to send a spontaneous payment.
412412
purpose: PaymentPurpose,
413413
/// The `channel_id` indicating over which channel we received the payment.
414-
via_channel_id: Option<[u8; 32]>,
414+
via_channel_id: Option<ChannelId>,
415415
/// The `user_channel_id` indicating over which channel we received the payment.
416416
via_user_channel_id: Option<u128>,
417417
/// The block height at which this payment will be failed back and will no longer be
@@ -663,10 +663,10 @@ pub enum Event {
663663
PaymentForwarded {
664664
/// The incoming channel between the previous node and us. This is only `None` for events
665665
/// generated or serialized by versions prior to 0.0.107.
666-
prev_channel_id: Option<[u8; 32]>,
666+
prev_channel_id: Option<ChannelId>,
667667
/// The outgoing channel between the next node and us. This is only `None` for events
668668
/// generated or serialized by versions prior to 0.0.107.
669-
next_channel_id: Option<[u8; 32]>,
669+
next_channel_id: Option<ChannelId>,
670670
/// The fee, in milli-satoshis, which was earned as a result of the payment.
671671
///
672672
/// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
@@ -697,7 +697,7 @@ pub enum Event {
697697
/// [`Event::ChannelReady`] event.
698698
ChannelPending {
699699
/// The `channel_id` of the channel that is pending confirmation.
700-
channel_id: [u8; 32],
700+
channel_id: ChannelId,
701701
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
702702
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
703703
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -710,7 +710,7 @@ pub enum Event {
710710
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
711711
///
712712
/// Will be `None` for channels created prior to LDK version 0.0.115.
713-
former_temporary_channel_id: Option<[u8; 32]>,
713+
former_temporary_channel_id: Option<ChannelId>,
714714
/// The `node_id` of the channel counterparty.
715715
counterparty_node_id: PublicKey,
716716
/// The outpoint of the channel's funding transaction.
@@ -722,7 +722,7 @@ pub enum Event {
722722
/// establishment.
723723
ChannelReady {
724724
/// The `channel_id` of the channel that is ready.
725-
channel_id: [u8; 32],
725+
channel_id: ChannelId,
726726
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
727727
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
728728
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -742,7 +742,7 @@ pub enum Event {
742742
ChannelClosed {
743743
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
744744
/// resolving the channel are likely still awaiting confirmation.
745-
channel_id: [u8; 32],
745+
channel_id: ChannelId,
746746
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
747747
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
748748
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
@@ -761,7 +761,7 @@ pub enum Event {
761761
/// inputs for another purpose.
762762
DiscardFunding {
763763
/// The channel_id of the channel which has been closed.
764-
channel_id: [u8; 32],
764+
channel_id: ChannelId,
765765
/// The full transaction received from the user
766766
transaction: Transaction
767767
},
@@ -785,7 +785,7 @@ pub enum Event {
785785
///
786786
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
787787
/// [`ChannelManager::force_close_without_broadcasting_txn`]: crate::ln::channelmanager::ChannelManager::force_close_without_broadcasting_txn
788-
temporary_channel_id: [u8; 32],
788+
temporary_channel_id: ChannelId,
789789
/// The node_id of the counterparty requesting to open the channel.
790790
///
791791
/// When responding to the request, the `counterparty_node_id` should be passed
@@ -831,7 +831,7 @@ pub enum Event {
831831
/// requirements (i.e. insufficient fees paid, or a CLTV that is too soon).
832832
HTLCHandlingFailed {
833833
/// The channel over which the HTLC was received.
834-
prev_channel_id: [u8; 32],
834+
prev_channel_id: ChannelId,
835835
/// Destination of the HTLC that failed to be processed.
836836
failed_next_destination: HTLCDestination,
837837
},
@@ -1248,7 +1248,7 @@ impl MaybeReadable for Event {
12481248
},
12491249
9u8 => {
12501250
let f = || {
1251-
let mut channel_id = [0; 32];
1251+
let mut channel_id = ChannelId::new_zero();
12521252
let mut reason = UpgradableRequired(None);
12531253
let mut user_channel_id_low_opt: Option<u64> = None;
12541254
let mut user_channel_id_high_opt: Option<u64> = None;
@@ -1271,7 +1271,7 @@ impl MaybeReadable for Event {
12711271
},
12721272
11u8 => {
12731273
let f = || {
1274-
let mut channel_id = [0; 32];
1274+
let mut channel_id = ChannelId::new_zero();
12751275
let mut transaction = Transaction{ version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
12761276
read_tlv_fields!(reader, {
12771277
(0, channel_id, required),
@@ -1376,7 +1376,7 @@ impl MaybeReadable for Event {
13761376
},
13771377
25u8 => {
13781378
let f = || {
1379-
let mut prev_channel_id = [0; 32];
1379+
let mut prev_channel_id = ChannelId::new_zero();
13801380
let mut failed_next_destination_opt = UpgradableRequired(None);
13811381
read_tlv_fields!(reader, {
13821382
(0, prev_channel_id, required),
@@ -1392,7 +1392,7 @@ impl MaybeReadable for Event {
13921392
27u8 => Ok(None),
13931393
29u8 => {
13941394
let f = || {
1395-
let mut channel_id = [0; 32];
1395+
let mut channel_id = ChannelId::new_zero();
13961396
let mut user_channel_id: u128 = 0;
13971397
let mut counterparty_node_id = RequiredWrapper(None);
13981398
let mut channel_type = RequiredWrapper(None);
@@ -1414,7 +1414,7 @@ impl MaybeReadable for Event {
14141414
},
14151415
31u8 => {
14161416
let f = || {
1417-
let mut channel_id = [0; 32];
1417+
let mut channel_id = ChannelId::new_zero();
14181418
let mut user_channel_id: u128 = 0;
14191419
let mut former_temporary_channel_id = None;
14201420
let mut counterparty_node_id = RequiredWrapper(None);

0 commit comments

Comments
 (0)