Skip to content
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

[WIP] ChannelId as a wrapper type around [u8; 32] #2447

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let rnid = node_pks.iter().skip(u16::from_be_bytes(get_slice!(2).try_into().unwrap()) as usize % node_pks.len()).next().unwrap();
let capacity = u64::from_be_bytes(get_slice!(8).try_into().unwrap());
first_hops_vec.push(ChannelDetails {
channel_id: [0; 32],
channel_id: ChannelId([0; 32]),
counterparty: ChannelCounterparty {
node_id: *rnid,
features: channelmanager::provided_init_features(&UserConfig::default()),
Expand Down
18 changes: 9 additions & 9 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ where
log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id));
log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id.0));
continue;
}

Expand All @@ -641,7 +641,7 @@ where
// If any public channel exists, return no hints and let the sender
// look at the public channels instead.
log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}",
log_bytes!(channel.channel_id));
log_bytes!(channel.channel_id.0));
return vec![].into_iter().take(MAX_CHANNEL_HINTS).map(route_hint_from_channel);
}
}
Expand Down Expand Up @@ -681,18 +681,18 @@ where
log_trace!(logger,
"Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints",
log_pubkey!(channel.counterparty.node_id),
log_bytes!(channel.channel_id), channel.short_channel_id,
log_bytes!(channel.channel_id.0), channel.short_channel_id,
channel.inbound_capacity_msat,
log_bytes!(entry.get().channel_id), entry.get().short_channel_id,
log_bytes!(entry.get().channel_id.0), entry.get().short_channel_id,
current_max_capacity);
entry.insert(channel);
} else {
log_trace!(logger,
"Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints",
log_pubkey!(channel.counterparty.node_id),
log_bytes!(entry.get().channel_id), entry.get().short_channel_id,
log_bytes!(entry.get().channel_id.0), entry.get().short_channel_id,
current_max_capacity,
log_bytes!(channel.channel_id), channel.short_channel_id,
log_bytes!(channel.channel_id.0), channel.short_channel_id,
channel.inbound_capacity_msat);
}
}
Expand Down Expand Up @@ -731,14 +731,14 @@ where

if include_channel {
log_trace!(logger, "Including channel {} in invoice route hints",
log_bytes!(channel.channel_id));
log_bytes!(channel.channel_id.0));
} else if !has_enough_capacity {
log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints",
log_bytes!(channel.channel_id));
log_bytes!(channel.channel_id.0));
} else {
debug_assert!(!channel.is_usable || (has_pub_unconf_chan && !channel.is_public));
log_trace!(logger, "Ignoring channel {} with disconnected peer",
log_bytes!(channel.channel_id));
log_bytes!(channel.channel_id.0));
}

include_channel
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
}
} else if !self.holder_tx_signed {
log_error!(logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast");
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id()));
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id().0));
log_error!(logger, " Read the docs for ChannelMonitor::get_latest_holder_commitment_txn and take manual action!");
} else {
// If we generated a MonitorEvent::CommitmentTxConfirmed, the ChannelManager
Expand Down Expand Up @@ -3181,7 +3181,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
let mut balance_spendable_csv = None;
log_info!(logger, "Channel {} closed by funding output spend in txid {}.",
log_bytes!(self.funding_info.0.to_channel_id()), txid);
log_bytes!(self.funding_info.0.to_channel_id().0), txid);
self.funding_spend_seen = true;
let mut commitment_tx_to_counterparty_output = None;
if (tx.input[0].sequence.0 >> 8*3) as u8 == 0x80 && (tx.lock_time.0 >> 8*3) as u8 == 0x20 {
Expand Down
9 changes: 5 additions & 4 deletions lightning/src/chain/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

//! Types describing on-chain transactions.

use crate::ln::channel::ChannelId;
use bitcoin::hash_types::Txid;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
use bitcoin::blockdata::transaction::Transaction;
Expand Down Expand Up @@ -57,12 +58,12 @@ pub struct OutPoint {

impl OutPoint {
/// Convert an `OutPoint` to a lightning channel id.
pub fn to_channel_id(&self) -> [u8; 32] {
pub fn to_channel_id(&self) -> ChannelId {
let mut res = [0; 32];
res[..].copy_from_slice(&self.txid[..]);
res[30] ^= ((self.index >> 8) & 0xff) as u8;
res[31] ^= ((self.index >> 0) & 0xff) as u8;
res
ChannelId(res)
}

/// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
Expand Down Expand Up @@ -94,10 +95,10 @@ mod tests {
assert_eq!(&OutPoint {
txid: tx.txid(),
index: 0
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
}.to_channel_id().0, &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
assert_eq!(&OutPoint {
txid: tx.txid(),
index: 1
}.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
}.to_channel_id().0, &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
}
}
36 changes: 18 additions & 18 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use bump_transaction::BumpTransactionEvent;

use crate::sign::SpendableOutputDescriptor;
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
use crate::ln::channel::{ChannelId, FUNDING_CONF_DEADLINE_BLOCKS};
use crate::ln::features::ChannelTypeFeatures;
use crate::ln::msgs;
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
Expand Down Expand Up @@ -210,7 +210,7 @@ pub enum HTLCDestination {
/// counterparty node information.
node_id: Option<PublicKey>,
/// The outgoing `channel_id` between us and the next node.
channel_id: [u8; 32],
channel_id: ChannelId,
},
/// Scenario where we are unsure of the next node to forward the HTLC to.
UnknownNextHop {
Expand Down Expand Up @@ -328,7 +328,7 @@ pub enum Event {
/// [`ChannelManager::funding_transaction_generated`].
///
/// [`ChannelManager::funding_transaction_generated`]: crate::ln::channelmanager::ChannelManager::funding_transaction_generated
temporary_channel_id: [u8; 32],
temporary_channel_id: ChannelId,
/// The counterparty's node_id, which you'll need to pass back into
/// [`ChannelManager::funding_transaction_generated`].
///
Expand Down Expand Up @@ -406,7 +406,7 @@ pub enum Event {
/// payment is to pay an invoice or to send a spontaneous payment.
purpose: PaymentPurpose,
/// The `channel_id` indicating over which channel we received the payment.
via_channel_id: Option<[u8; 32]>,
via_channel_id: Option<ChannelId>,
/// The `user_channel_id` indicating over which channel we received the payment.
via_user_channel_id: Option<u128>,
/// The block height at which this payment will be failed back and will no longer be
Expand Down Expand Up @@ -658,10 +658,10 @@ pub enum Event {
PaymentForwarded {
/// The incoming channel between the previous node and us. This is only `None` for events
/// generated or serialized by versions prior to 0.0.107.
prev_channel_id: Option<[u8; 32]>,
prev_channel_id: Option<ChannelId>,
/// The outgoing channel between the next node and us. This is only `None` for events
/// generated or serialized by versions prior to 0.0.107.
next_channel_id: Option<[u8; 32]>,
next_channel_id: Option<ChannelId>,
/// The fee, in milli-satoshis, which was earned as a result of the payment.
///
/// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
Expand Down Expand Up @@ -692,7 +692,7 @@ pub enum Event {
/// [`Event::ChannelReady`] event.
ChannelPending {
/// The `channel_id` of the channel that is pending confirmation.
channel_id: [u8; 32],
channel_id: ChannelId,
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
Expand All @@ -705,7 +705,7 @@ pub enum Event {
/// The `temporary_channel_id` this channel used to be known by during channel establishment.
///
/// Will be `None` for channels created prior to LDK version 0.0.115.
former_temporary_channel_id: Option<[u8; 32]>,
former_temporary_channel_id: Option<ChannelId>,
/// The `node_id` of the channel counterparty.
counterparty_node_id: PublicKey,
/// The outpoint of the channel's funding transaction.
Expand All @@ -717,7 +717,7 @@ pub enum Event {
/// establishment.
ChannelReady {
/// The `channel_id` of the channel that is ready.
channel_id: [u8; 32],
channel_id: ChannelId,
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
Expand All @@ -737,7 +737,7 @@ pub enum Event {
ChannelClosed {
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
/// resolving the channel are likely still awaiting confirmation.
channel_id: [u8; 32],
channel_id: ChannelId,
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
Expand All @@ -756,7 +756,7 @@ pub enum Event {
/// inputs for another purpose.
DiscardFunding {
/// The channel_id of the channel which has been closed.
channel_id: [u8; 32],
channel_id: ChannelId,
/// The full transaction received from the user
transaction: Transaction
},
Expand All @@ -780,7 +780,7 @@ pub enum Event {
///
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
/// [`ChannelManager::force_close_without_broadcasting_txn`]: crate::ln::channelmanager::ChannelManager::force_close_without_broadcasting_txn
temporary_channel_id: [u8; 32],
temporary_channel_id: ChannelId,
/// The node_id of the counterparty requesting to open the channel.
///
/// When responding to the request, the `counterparty_node_id` should be passed
Expand Down Expand Up @@ -826,7 +826,7 @@ pub enum Event {
/// requirements (i.e. insufficient fees paid, or a CLTV that is too soon).
HTLCHandlingFailed {
/// The channel over which the HTLC was received.
prev_channel_id: [u8; 32],
prev_channel_id: ChannelId,
/// Destination of the HTLC that failed to be processed.
failed_next_destination: HTLCDestination,
},
Expand Down Expand Up @@ -1243,7 +1243,7 @@ impl MaybeReadable for Event {
},
9u8 => {
let f = || {
let mut channel_id = [0; 32];
let mut channel_id = ChannelId([0; 32]);
let mut reason = UpgradableRequired(None);
let mut user_channel_id_low_opt: Option<u64> = None;
let mut user_channel_id_high_opt: Option<u64> = None;
Expand All @@ -1266,7 +1266,7 @@ impl MaybeReadable for Event {
},
11u8 => {
let f = || {
let mut channel_id = [0; 32];
let mut channel_id = ChannelId([0; 32]);
let mut transaction = Transaction{ version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
read_tlv_fields!(reader, {
(0, channel_id, required),
Expand Down Expand Up @@ -1371,7 +1371,7 @@ impl MaybeReadable for Event {
},
25u8 => {
let f = || {
let mut prev_channel_id = [0; 32];
let mut prev_channel_id = ChannelId([0; 32]);
let mut failed_next_destination_opt = UpgradableRequired(None);
read_tlv_fields!(reader, {
(0, prev_channel_id, required),
Expand All @@ -1387,7 +1387,7 @@ impl MaybeReadable for Event {
27u8 => Ok(None),
29u8 => {
let f = || {
let mut channel_id = [0; 32];
let mut channel_id = ChannelId([0; 32]);
let mut user_channel_id: u128 = 0;
let mut counterparty_node_id = RequiredWrapper(None);
let mut channel_type = RequiredWrapper(None);
Expand All @@ -1409,7 +1409,7 @@ impl MaybeReadable for Event {
},
31u8 => {
let f = || {
let mut channel_id = [0; 32];
let mut channel_id = ChannelId([0; 32]);
let mut user_channel_id: u128 = 0;
let mut former_temporary_channel_id = None;
let mut counterparty_node_id = RequiredWrapper(None);
Expand Down
Loading