Skip to content

Commit 8a860dc

Browse files
Add skimmed fee to monitor updates
We're moving towards having the ChannelMonitor be responsible for resolving HTLCs, via MonitorEvents, so we need to start tracking more payment information in monitor updates so the monitor later has access to it. Here we add tracking for the skimmed_fee field for claims.
1 parent bd2d404 commit 8a860dc

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,21 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
642642
},
643643
);
644644

645+
/// Information about an HTLC forward that was claimed via preimage on the outbound edge, included
646+
/// in [`ChannelMonitorUpdateStep`] so the monitor can generate events with the full claim context.
647+
#[derive(Clone, Debug, PartialEq, Eq)]
648+
pub(crate) struct OutboundHTLCClaim {
649+
pub htlc_id: SentHTLCId,
650+
pub preimage: PaymentPreimage,
651+
pub skimmed_fee_msat: Option<u64>,
652+
}
653+
654+
impl_writeable_tlv_based!(OutboundHTLCClaim, {
655+
(1, htlc_id, required),
656+
(3, preimage, required),
657+
(5, skimmed_fee_msat, option),
658+
});
659+
645660
#[derive(Clone, Debug, PartialEq, Eq)]
646661
pub(crate) enum ChannelMonitorUpdateStep {
647662
LatestHolderCommitmentTXInfo {
@@ -653,13 +668,13 @@ pub(crate) enum ChannelMonitorUpdateStep {
653668
/// `htlc_outputs` will only include dust HTLCs. We still have to track the
654669
/// `Option<Signature>` for backwards compatibility.
655670
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
656-
claimed_htlcs: Vec<(SentHTLCId, PaymentPreimage)>,
671+
claimed_htlcs: Vec<OutboundHTLCClaim>,
657672
nondust_htlc_sources: Vec<HTLCSource>,
658673
},
659674
LatestHolderCommitment {
660675
commitment_txs: Vec<HolderCommitmentTransaction>,
661676
htlc_data: CommitmentHTLCData,
662-
claimed_htlcs: Vec<(SentHTLCId, PaymentPreimage)>,
677+
claimed_htlcs: Vec<OutboundHTLCClaim>,
663678
},
664679
LatestCounterpartyCommitmentTXInfo {
665680
commitment_txid: Txid,
@@ -740,9 +755,29 @@ impl ChannelMonitorUpdateStep {
740755
impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
741756
(0, LatestHolderCommitmentTXInfo) => {
742757
(0, commitment_tx, required),
743-
(1, claimed_htlcs, optional_vec),
758+
(1, _legacy_claimed_htlcs, (legacy, Vec<(SentHTLCId, PaymentPreimage)>,
759+
|v: Option<&Vec<(SentHTLCId, PaymentPreimage)>>| {
760+
// Only populate from legacy if the new-format tag 5 wasn't read.
761+
if let Some(legacy) = v {
762+
if claimed_htlcs.as_ref().map_or(true, |v| v.is_empty()) {
763+
claimed_htlcs = Some(legacy.iter().map(|(htlc_id, preimage)| OutboundHTLCClaim {
764+
htlc_id: *htlc_id, preimage: *preimage, skimmed_fee_msat: None,
765+
}).collect());
766+
}
767+
}
768+
Ok(())
769+
},
770+
|us: &ChannelMonitorUpdateStep| match us {
771+
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { claimed_htlcs, .. } => {
772+
let claims: Vec<(SentHTLCId, PaymentPreimage)> =
773+
claimed_htlcs.iter().map(|claim| (claim.htlc_id, claim.preimage)).collect();
774+
Some(claims)
775+
}
776+
_ => None
777+
})),
744778
(2, htlc_outputs, required_vec),
745779
(4, nondust_htlc_sources, optional_vec),
780+
(5, claimed_htlcs, optional_vec), // Added in 0.4
746781
},
747782
(1, LatestCounterpartyCommitmentTXInfo) => {
748783
(0, commitment_txid, required),
@@ -777,7 +812,27 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
777812
(8, LatestHolderCommitment) => {
778813
(1, commitment_txs, required_vec),
779814
(3, htlc_data, required),
780-
(5, claimed_htlcs, required_vec),
815+
(5, _legacy_claimed_htlcs, (legacy, Vec<(SentHTLCId, PaymentPreimage)>,
816+
|v: Option<&Vec<(SentHTLCId, PaymentPreimage)>>| {
817+
// Only populate from legacy if the new-format tag 7 wasn't read.
818+
if let Some(legacy) = v {
819+
if claimed_htlcs.as_ref().map_or(true, |v| v.is_empty()) {
820+
claimed_htlcs = Some(legacy.iter().map(|(htlc_id, preimage)| OutboundHTLCClaim {
821+
htlc_id: *htlc_id, preimage: *preimage, skimmed_fee_msat: None,
822+
}).collect());
823+
}
824+
}
825+
Ok(())
826+
},
827+
|us: &ChannelMonitorUpdateStep| match us {
828+
ChannelMonitorUpdateStep::LatestHolderCommitment { claimed_htlcs, .. } => {
829+
let claims: Vec<(SentHTLCId, PaymentPreimage)> =
830+
claimed_htlcs.iter().map(|claim| (claim.htlc_id, claim.preimage)).collect();
831+
Some(claims)
832+
}
833+
_ => None
834+
})),
835+
(7, claimed_htlcs, optional_vec), // Added in 0.4
781836
},
782837
(10, RenegotiatedFunding) => {
783838
(1, channel_parameters, (required: ReadableArgs, None)),
@@ -3691,7 +3746,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36913746
fn provide_latest_holder_commitment_tx(
36923747
&mut self, holder_commitment_tx: HolderCommitmentTransaction,
36933748
htlc_outputs: &[(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)],
3694-
claimed_htlcs: &[(SentHTLCId, PaymentPreimage)], mut nondust_htlc_sources: Vec<HTLCSource>,
3749+
claimed_htlcs: &[OutboundHTLCClaim], mut nondust_htlc_sources: Vec<HTLCSource>,
36953750
) -> Result<(), &'static str> {
36963751
let dust_htlcs = if htlc_outputs.iter().any(|(_, s, _)| s.is_some()) {
36973752
// If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
@@ -3812,7 +3867,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38123867

38133868
fn update_holder_commitment_data(
38143869
&mut self, commitment_txs: Vec<HolderCommitmentTransaction>,
3815-
mut htlc_data: CommitmentHTLCData, claimed_htlcs: &[(SentHTLCId, PaymentPreimage)],
3870+
mut htlc_data: CommitmentHTLCData, claimed_htlcs: &[OutboundHTLCClaim],
38163871
) -> Result<(), &'static str> {
38173872
self.verify_matching_commitment_transactions(
38183873
commitment_txs.iter().map(|holder_commitment_tx| holder_commitment_tx.deref()),
@@ -3832,7 +3887,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38323887
mem::swap(&mut htlc_data, &mut self.current_holder_htlc_data);
38333888
self.prev_holder_htlc_data = Some(htlc_data);
38343889

3835-
for (claimed_htlc_id, claimed_preimage) in claimed_htlcs {
3890+
for claim in claimed_htlcs {
38363891
let htlc_opt = self
38373892
.funding
38383893
.counterparty_claimable_outpoints
@@ -3841,7 +3896,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38413896
.iter()
38423897
.find_map(|(htlc, source_opt)| {
38433898
if let Some(source) = source_opt {
3844-
if SentHTLCId::from_source(source) == *claimed_htlc_id {
3899+
if SentHTLCId::from_source(source) == claim.htlc_id {
38453900
return Some((htlc, source));
38463901
}
38473902
}
@@ -3855,14 +3910,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38553910
// startup until it is explicitly acked.
38563911
self.push_monitor_event(MonitorEvent::HTLCEvent(HTLCUpdate {
38573912
payment_hash: htlc.payment_hash,
3858-
payment_preimage: Some(*claimed_preimage),
3913+
payment_preimage: Some(claim.preimage),
38593914
source: *source.clone(),
38603915
htlc_value_msat: Some(htlc.amount_msat),
38613916
user_channel_id: self.user_channel_id,
38623917
}));
38633918
}
38643919
}
3865-
self.counterparty_fulfilled_htlcs.insert(*claimed_htlc_id, *claimed_preimage);
3920+
self.counterparty_fulfilled_htlcs.insert(claim.htlc_id, claim.preimage);
38663921
}
38673922

38683923
Ok(())

lightning/src/ln/channel.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::chain::chaininterface::{
3333
use crate::chain::chainmonitor::MonitorEventSource;
3434
use crate::chain::channelmonitor::{
3535
ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, CommitmentHTLCData,
36-
LATENCY_GRACE_PERIOD_BLOCKS,
36+
OutboundHTLCClaim, LATENCY_GRACE_PERIOD_BLOCKS,
3737
};
3838
use crate::chain::transaction::{OutPoint, TransactionData};
3939
use crate::chain::BestBlock;
@@ -8632,7 +8632,11 @@ where
86328632
// claims, but such an upgrade is unlikely and including claimed HTLCs here
86338633
// fixes a bug which the user was exposed to on 0.0.104 when they started the
86348634
// claim anyway.
8635-
claimed_htlcs.push((SentHTLCId::from_source(&htlc.source), preimage));
8635+
claimed_htlcs.push(OutboundHTLCClaim {
8636+
htlc_id: SentHTLCId::from_source(&htlc.source),
8637+
preimage,
8638+
skimmed_fee_msat: htlc.skimmed_fee_msat,
8639+
});
86368640
}
86378641
htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(reason);
86388642
need_commitment = true;

lightning/src/util/ser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ impl_writeable_for_vec_with_element_length_prefix!(&crate::ln::msgs::UpdateAddHT
11131113
impl_for_vec!(u32);
11141114
impl_for_vec!(crate::events::HTLCLocator);
11151115
impl_for_vec!(crate::ln::types::ChannelId);
1116+
impl_for_vec!(crate::chain::channelmonitor::OutboundHTLCClaim);
11161117

11171118
impl Writeable for Vec<Witness> {
11181119
#[inline]

0 commit comments

Comments
 (0)