Skip to content

Commit 6e1ccc7

Browse files
author
Antoine Riard
committed
Track HTLCSource in ChannelMonitor
Insert it in current_local_signed_tx, prev_local_signed_tx, remote_claimable_outpoints. For so get it provided by Channel calls to provide_latest_{local,remote}_tx
1 parent d8e8496 commit 6e1ccc7

File tree

4 files changed

+175
-47
lines changed

4 files changed

+175
-47
lines changed

src/ln/channel.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl Channel {
751751
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
752752
/// which peer generated this transaction and "to whom" this transaction flows.
753753
#[inline]
754-
fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<HTLCOutputInCommitment>) {
754+
fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>) {
755755
let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
756756

757757
let txins = {
@@ -765,30 +765,30 @@ impl Channel {
765765
ins
766766
};
767767

768-
let mut txouts: Vec<(TxOut, Option<HTLCOutputInCommitment>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
768+
let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
769769

770770
let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
771771
let mut remote_htlc_total_msat = 0;
772772
let mut local_htlc_total_msat = 0;
773773
let mut value_to_self_msat_offset = 0;
774774

775775
macro_rules! add_htlc_output {
776-
($htlc: expr, $outbound: expr) => {
776+
($htlc: expr, $outbound: expr, $source: expr) => {
777777
if $outbound == local { // "offered HTLC output"
778778
if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
779779
let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
780780
txouts.push((TxOut {
781781
script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
782782
value: $htlc.amount_msat / 1000
783-
}, Some(htlc_in_tx)));
783+
}, Some((htlc_in_tx, $source))));
784784
}
785785
} else {
786786
if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
787787
let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
788788
txouts.push((TxOut { // "received HTLC output"
789789
script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
790790
value: $htlc.amount_msat / 1000
791-
}, Some(htlc_in_tx)));
791+
}, Some((htlc_in_tx, $source))));
792792
}
793793
}
794794
}
@@ -804,7 +804,7 @@ impl Channel {
804804
};
805805

806806
if include {
807-
add_htlc_output!(htlc, false);
807+
add_htlc_output!(htlc, false, None);
808808
remote_htlc_total_msat += htlc.amount_msat;
809809
} else {
810810
match &htlc.state {
@@ -830,7 +830,7 @@ impl Channel {
830830
};
831831

832832
if include {
833-
add_htlc_output!(htlc, true);
833+
add_htlc_output!(htlc, true, Some(htlc.source.clone()));
834834
local_htlc_total_msat += htlc.amount_msat;
835835
} else {
836836
match htlc.state {
@@ -901,12 +901,12 @@ impl Channel {
901901
transaction_utils::sort_outputs(&mut txouts);
902902

903903
let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
904-
let mut htlcs_used: Vec<HTLCOutputInCommitment> = Vec::with_capacity(txouts.len());
904+
let mut htlcs_used: Vec<(HTLCOutputInCommitment, Option<HTLCSource>)> = Vec::with_capacity(txouts.len());
905905
for (idx, out) in txouts.drain(..).enumerate() {
906906
outputs.push(out.0);
907-
if let Some(out_htlc) = out.1 {
908-
htlcs_used.push(out_htlc);
909-
htlcs_used.last_mut().unwrap().transaction_output_index = idx as u32;
907+
if let Some(mut out_htlc) = out.1 {
908+
out_htlc.0.transaction_output_index = idx as u32;
909+
htlcs_used.push((out_htlc.0, out_htlc.1));
910910
}
911911
}
912912

@@ -1692,7 +1692,7 @@ impl Channel {
16921692
new_local_commitment_txn.push(local_commitment_tx.0.clone());
16931693

16941694
let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.1.len());
1695-
for (idx, ref htlc) in local_commitment_tx.1.iter().enumerate() {
1695+
for (idx, &(ref htlc, ref htlc_source)) in local_commitment_tx.1.iter().enumerate() {
16961696
let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys, feerate_per_kw);
16971697
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
16981698
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
@@ -1704,7 +1704,7 @@ impl Channel {
17041704
} else {
17051705
self.create_htlc_tx_signature(&htlc_tx, htlc, &local_keys)?.1
17061706
};
1707-
htlcs_and_sigs.push(((*htlc).clone(), msg.htlc_signatures[idx], htlc_sig));
1707+
htlcs_and_sigs.push(((*htlc).clone(), msg.htlc_signatures[idx], htlc_sig, (*htlc_source).clone()));
17081708
}
17091709

17101710
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
@@ -3221,7 +3221,7 @@ impl Channel {
32213221

32223222
/// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
32233223
/// when we shouldn't change HTLC/channel state.
3224-
fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>)), ChannelError> {
3224+
fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>)), ChannelError> {
32253225
let funding_script = self.get_funding_redeemscript();
32263226

32273227
let mut feerate_per_kw = self.feerate_per_kw;
@@ -3239,7 +3239,7 @@ impl Channel {
32393239

32403240
let mut htlc_sigs = Vec::new();
32413241

3242-
for ref htlc in remote_commitment_tx.1.iter() {
3242+
for &(ref htlc, _) in remote_commitment_tx.1.iter() {
32433243
let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys, feerate_per_kw);
32443244
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
32453245
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
@@ -3960,7 +3960,7 @@ mod tests {
39603960
let htlc_basepoint = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.htlc_base_key);
39613961
let keys = TxCreationKeys::new(&secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &chan.their_revocation_basepoint.unwrap(), &chan.their_payment_basepoint.unwrap(), &chan.their_htlc_basepoint.unwrap()).unwrap();
39623962

3963-
let mut unsigned_tx: (Transaction, Vec<HTLCOutputInCommitment>);
3963+
let mut unsigned_tx: (Transaction, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>);
39643964

39653965
macro_rules! test_commitment {
39663966
( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
@@ -3980,7 +3980,7 @@ mod tests {
39803980
( $htlc_idx: expr, $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr ) => {
39813981
let remote_signature = Signature::from_der(&secp_ctx, &hex::decode($their_sig_hex).unwrap()[..]).unwrap();
39823982

3983-
let ref htlc = unsigned_tx.1[$htlc_idx];
3983+
let (ref htlc, _) = unsigned_tx.1[$htlc_idx];
39843984
let mut htlc_tx = chan.build_htlc_transaction(&unsigned_tx.0.txid(), &htlc, true, &keys, chan.feerate_per_kw);
39853985
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
39863986
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();

src/ln/channelmanager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ mod channel_held_info {
9090
}
9191

9292
/// Tracks the inbound corresponding to an outbound HTLC
93-
#[derive(Clone)]
93+
#[derive(Clone, PartialEq)]
9494
pub struct HTLCPreviousHopData {
95-
pub(super) short_channel_id: u64,
96-
pub(super) htlc_id: u64,
97-
pub(super) incoming_packet_shared_secret: [u8; 32],
95+
pub(crate) short_channel_id: u64,
96+
pub(crate) htlc_id: u64,
97+
pub(crate) incoming_packet_shared_secret: [u8; 32],
9898
}
9999

100100
/// Tracks the inbound corresponding to an outbound HTLC
101-
#[derive(Clone)]
101+
#[derive(Clone, PartialEq)]
102102
pub enum HTLCSource {
103103
PreviousHopData(HTLCPreviousHopData),
104104
OutboundRoute {
@@ -131,7 +131,7 @@ mod channel_held_info {
131131
}
132132
}
133133
}
134-
pub(super) use self::channel_held_info::*;
134+
pub(crate) use self::channel_held_info::*;
135135

136136
type ShutdownResult = (Vec<Transaction>, Vec<(HTLCSource, [u8; 32])>);
137137

0 commit comments

Comments
 (0)