Skip to content

Commit

Permalink
Introduce HTLCDestination enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jurvis committed May 7, 2022
1 parent eac86fa commit 0c07c76
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 43 deletions.
35 changes: 33 additions & 2 deletions lightning/src/ln/channel.rs
Expand Up @@ -46,6 +46,7 @@ use io;
use prelude::*;
use core::{cmp,mem,fmt};
use core::ops::Deref;
use std::io::Error;
#[cfg(any(test, fuzzing, debug_assertions))]
use sync::Mutex;
use bitcoin::hashes::hex::ToHex;
Expand Down Expand Up @@ -799,6 +800,36 @@ impl fmt::Debug for ChannelError {
}
}

/// Used to return destination of where we are forwarding our HTLC to.
#[derive(Clone, Debug)]
pub enum HTLCDestination {
OpenChannel { node_id: PublicKey, channel_id: [u8; 32] },
Unknown { previous_hop_scid: u64 },
Payment { payment_hash: PaymentHash, payment_preimage: Option<PaymentPreimage> },
}

impl Writeable for HTLCDestination {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
match self {
HTLCDestination::OpenChannel { ref node_id, ref channel_id } => {
0u8.write(writer)?;
node_id.write(writer)?;
channel_id.write(writer)?;
},
HTLCDestination::Unknown { ref previous_hop_scid } => {
1u8.write(writer)?;
previous_hop_scid.write(writer)?;
},
HTLCDestination::Payment { ref payment_hash, ref payment_preimage } => {
2u8.write(writer)?;
payment_hash.write(writer)?;
payment_preimage.write(writer)?;
}
}
Ok(())
}
}

macro_rules! secp_check {
($res: expr, $err: expr) => {
match $res {
Expand Down Expand Up @@ -5577,7 +5608,7 @@ impl<Signer: Sign> Channel<Signer> {
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
/// immediately (others we will have to allow to time out).
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash, PublicKey)>) {
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash, HTLCDestination)>) {
// Note that we MUST only generate a monitor update that indicates force-closure - we're
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
// being fully configured in some cases. Thus, its likely any monitor events we generate will
Expand All @@ -5591,7 +5622,7 @@ impl<Signer: Sign> Channel<Signer> {
for htlc_update in self.holding_cell_htlc_updates.drain(..) {
match htlc_update {
HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
dropped_outbound_htlcs.push((source, payment_hash, counterparty_node_id));
dropped_outbound_htlcs.push((source, payment_hash, HTLCDestination::OpenChannel { node_id: counterparty_node_id, channel_id: self.channel_id }));
},
_ => {}
}
Expand Down

0 comments on commit 0c07c76

Please sign in to comment.