Skip to content

Commit 2e6a533

Browse files
Persist upstream channel ID in HTLCForwardInfo fails
Will be used in an upcoming commit so we can ack a monitor event resolving an HTLC forward in the case that the inbound edge is closed at fail-back time. Without this piece of info, we can't locate the relevant event.
1 parent 15c06fd commit 2e6a533

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,19 @@ impl PendingAddHTLCInfo {
491491
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
492492
pub(super) enum HTLCForwardInfo {
493493
AddHTLC(PendingAddHTLCInfo),
494-
FailHTLC { htlc_id: u64, err_packet: msgs::OnionErrorPacket },
495-
FailMalformedHTLC { htlc_id: u64, failure_code: u16, sha256_of_onion: [u8; 32] },
494+
FailHTLC {
495+
htlc_id: u64,
496+
err_packet: msgs::OnionErrorPacket,
497+
/// Always set in 0.4+
498+
upstream_channel_id: Option<ChannelId>,
499+
},
500+
FailMalformedHTLC {
501+
htlc_id: u64,
502+
failure_code: u16,
503+
sha256_of_onion: [u8; 32],
504+
/// Always set in 0.4+
505+
upstream_channel_id: Option<ChannelId>,
506+
},
496507
}
497508

498509
/// Whether this blinded HTLC is being failed backwards by the introduction node or a blinded node,
@@ -7929,12 +7940,14 @@ impl<
79297940
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
79307941
htlc_id: fail_htlc.htlc_id,
79317942
err_packet: fail_htlc.into(),
7943+
upstream_channel_id: Some(incoming_channel_id),
79327944
},
79337945
HTLCFailureMsg::Malformed(fail_malformed_htlc) => {
79347946
HTLCForwardInfo::FailMalformedHTLC {
79357947
htlc_id: fail_malformed_htlc.htlc_id,
79367948
sha256_of_onion: fail_malformed_htlc.sha256_of_onion,
79377949
failure_code: fail_malformed_htlc.failure_code.into(),
7950+
upstream_channel_id: Some(incoming_channel_id),
79387951
}
79397952
},
79407953
};
@@ -8466,7 +8479,7 @@ impl<
84668479
}
84678480
None
84688481
},
8469-
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet } => {
8482+
HTLCForwardInfo::FailHTLC { htlc_id, ref err_packet, upstream_channel_id: _ } => {
84708483
if let Some(chan) = peer_state
84718484
.channel_by_id
84728485
.get_mut(&forward_chan_id)
@@ -8486,7 +8499,12 @@ impl<
84868499
break;
84878500
}
84888501
},
8489-
HTLCForwardInfo::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
8502+
HTLCForwardInfo::FailMalformedHTLC {
8503+
htlc_id,
8504+
failure_code,
8505+
sha256_of_onion,
8506+
upstream_channel_id: _,
8507+
} => {
84908508
if let Some(chan) = peer_state
84918509
.channel_by_id
84928510
.get_mut(&forward_chan_id)
@@ -9604,6 +9622,7 @@ impl<
96049622
trampoline_shared_secret,
96059623
phantom_shared_secret,
96069624
*htlc_id,
9625+
*channel_id,
96079626
),
96089627
);
96099628

@@ -9666,6 +9685,7 @@ impl<
96669685
&incoming_trampoline_shared_secret,
96679686
&None,
96689687
*htlc_id,
9688+
*channel_id,
96699689
),
96709690
);
96719691
}
@@ -15079,7 +15099,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1507915099
fn get_htlc_forward_failure(
1508015100
blinded_failure: &Option<BlindedFailure>, onion_error: &HTLCFailReason,
1508115101
incoming_packet_shared_secret: &[u8; 32], trampoline_shared_secret: &Option<[u8; 32]>,
15082-
phantom_shared_secret: &Option<[u8; 32]>, htlc_id: u64,
15102+
phantom_shared_secret: &Option<[u8; 32]>, htlc_id: u64, upstream_channel_id: ChannelId,
1508315103
) -> HTLCForwardInfo {
1508415104
// TODO: Correctly wrap the error packet twice if failing back a trampoline + phantom HTLC.
1508515105
let secondary_shared_secret = trampoline_shared_secret.or(*phantom_shared_secret);
@@ -15091,19 +15111,28 @@ fn get_htlc_forward_failure(
1509115111
incoming_packet_shared_secret,
1509215112
&secondary_shared_secret,
1509315113
);
15094-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet }
15114+
HTLCForwardInfo::FailHTLC {
15115+
htlc_id,
15116+
err_packet,
15117+
upstream_channel_id: Some(upstream_channel_id),
15118+
}
1509515119
},
1509615120
Some(BlindedFailure::FromBlindedNode) => HTLCForwardInfo::FailMalformedHTLC {
1509715121
htlc_id,
1509815122
failure_code: LocalHTLCFailureReason::InvalidOnionBlinding.failure_code(),
1509915123
sha256_of_onion: [0; 32],
15124+
upstream_channel_id: Some(upstream_channel_id),
1510015125
},
1510115126
None => {
1510215127
let err_packet = onion_error.get_encrypted_failure_packet(
1510315128
incoming_packet_shared_secret,
1510415129
&secondary_shared_secret,
1510515130
);
15106-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet }
15131+
HTLCForwardInfo::FailHTLC {
15132+
htlc_id,
15133+
err_packet,
15134+
upstream_channel_id: Some(upstream_channel_id),
15135+
}
1510715136
},
1510815137
}
1510915138
}
@@ -18721,15 +18750,21 @@ impl Writeable for HTLCForwardInfo {
1872118750
0u8.write(w)?;
1872218751
info.write(w)?;
1872318752
},
18724-
Self::FailHTLC { htlc_id, err_packet } => {
18753+
Self::FailHTLC { htlc_id, err_packet, upstream_channel_id } => {
1872518754
FAIL_HTLC_VARIANT_ID.write(w)?;
1872618755
write_tlv_fields!(w, {
1872718756
(0, htlc_id, required),
1872818757
(2, err_packet.data, required),
1872918758
(5, err_packet.attribution_data, option),
18759+
(7, upstream_channel_id, option),
1873018760
});
1873118761
},
18732-
Self::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
18762+
Self::FailMalformedHTLC {
18763+
htlc_id,
18764+
failure_code,
18765+
sha256_of_onion,
18766+
upstream_channel_id,
18767+
} => {
1873318768
// Since this variant was added in 0.0.119, write this as `::FailHTLC` with an empty error
1873418769
// packet so older versions have something to fail back with, but serialize the real data as
1873518770
// optional TLVs for the benefit of newer versions.
@@ -18739,6 +18774,7 @@ impl Writeable for HTLCForwardInfo {
1873918774
(1, failure_code, required),
1874018775
(2, Vec::<u8>::new(), required),
1874118776
(3, sha256_of_onion, required),
18777+
(7, upstream_channel_id, option),
1874218778
});
1874318779
},
1874418780
}
@@ -18759,6 +18795,7 @@ impl Readable for HTLCForwardInfo {
1875918795
(2, err_packet, required),
1876018796
(3, sha256_of_onion, option),
1876118797
(5, attribution_data, option),
18798+
(7, upstream_channel_id, option),
1876218799
});
1876318800
if let Some(failure_code) = malformed_htlc_failure_code {
1876418801
if attribution_data.is_some() {
@@ -18768,6 +18805,7 @@ impl Readable for HTLCForwardInfo {
1876818805
htlc_id: _init_tlv_based_struct_field!(htlc_id, required),
1876918806
failure_code,
1877018807
sha256_of_onion: sha256_of_onion.ok_or(DecodeError::InvalidValue)?,
18808+
upstream_channel_id,
1877118809
}
1877218810
} else {
1877318811
Self::FailHTLC {
@@ -18776,6 +18814,7 @@ impl Readable for HTLCForwardInfo {
1877618814
data: _init_tlv_based_struct_field!(err_packet, required),
1877718815
attribution_data: _init_tlv_based_struct_field!(attribution_data, option),
1877818816
},
18817+
upstream_channel_id,
1877918818
}
1878018819
}
1878118820
},

0 commit comments

Comments
 (0)