Skip to content

Commit 153f521

Browse files
authored
Merge pull request lightningdevkit#4 from whfuyn/fix-rgb-amount-after-payment-forwarded
fix: rgb amount in channel not updated after payment forwarded
2 parents 80497c4 + d2cea61 commit 153f521

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,14 @@ pub struct HTLCUpdate {
173173
pub(crate) payment_preimage: Option<PaymentPreimage>,
174174
pub(crate) source: HTLCSource,
175175
pub(crate) htlc_value_satoshis: Option<u64>,
176+
pub(crate) htlc_value_rgb: Option<u64>,
176177
}
177178
impl_writeable_tlv_based!(HTLCUpdate, {
178179
(0, payment_hash, required),
179180
(1, htlc_value_satoshis, option),
180181
(2, source, required),
181182
(4, payment_preimage, option),
183+
(6, htlc_value_rgb, option),
182184
});
183185

184186
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
@@ -384,6 +386,7 @@ enum OnchainEvent {
384386
source: HTLCSource,
385387
payment_hash: PaymentHash,
386388
htlc_value_satoshis: Option<u64>,
389+
htlc_value_rgb: Option<u64>,
387390
/// None in the second case, above, ie when there is no relevant output in the commitment
388391
/// transaction which appeared on chain.
389392
commitment_tx_output_idx: Option<u32>,
@@ -465,6 +468,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
465468
(1, htlc_value_satoshis, option),
466469
(2, payment_hash, required),
467470
(3, commitment_tx_output_idx, option),
471+
(4, htlc_value_rgb, option),
468472
},
469473
(1, MaturingOutput) => {
470474
(0, descriptor, required),
@@ -2266,6 +2270,7 @@ macro_rules! fail_unbroadcast_htlcs {
22662270
source: (**source).clone(),
22672271
payment_hash: htlc.payment_hash.clone(),
22682272
htlc_value_satoshis: Some(htlc.amount_msat / 1000),
2273+
htlc_value_rgb: htlc.amount_rgb,
22692274
commitment_tx_output_idx: None,
22702275
},
22712276
};
@@ -3614,7 +3619,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36143619
// Produce actionable events from on-chain events having reached their threshold.
36153620
for entry in onchain_events_reaching_threshold_conf.drain(..) {
36163621
match entry.event {
3617-
OnchainEvent::HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, commitment_tx_output_idx } => {
3622+
OnchainEvent::HTLCUpdate { ref source, payment_hash, htlc_value_satoshis, htlc_value_rgb, commitment_tx_output_idx } => {
36183623
// Check for duplicate HTLC resolutions.
36193624
#[cfg(debug_assertions)]
36203625
{
@@ -3638,6 +3643,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36383643
payment_preimage: None,
36393644
source: source.clone(),
36403645
htlc_value_satoshis,
3646+
htlc_value_rgb,
36413647
}));
36423648
self.htlcs_resolved_on_chain.push(IrrevocablyResolvedHTLC {
36433649
commitment_tx_output_idx,
@@ -3927,7 +3933,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39273933
if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
39283934
if let &Some(ref source) = pending_source {
39293935
log_claim!("revoked counterparty commitment tx", false, pending_htlc, true);
3930-
payment_data = Some(((**source).clone(), $htlc_output.payment_hash, $htlc_output.amount_msat));
3936+
payment_data = Some(((**source).clone(), $htlc_output.payment_hash, $htlc_output.amount_msat, $htlc_output.amount_rgb));
39313937
break;
39323938
}
39333939
}
@@ -3947,7 +3953,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39473953
// transaction. This implies we either learned a preimage, the HTLC
39483954
// has timed out, or we screwed up. In any case, we should now
39493955
// resolve the source HTLC with the original sender.
3950-
payment_data = Some(((*source).clone(), htlc_output.payment_hash, htlc_output.amount_msat));
3956+
payment_data = Some(((*source).clone(), htlc_output.payment_hash, htlc_output.amount_msat, htlc_output.amount_rgb));
39513957
} else if !$holder_tx {
39523958
check_htlc_valid_counterparty!(self.current_counterparty_commitment_txid, htlc_output);
39533959
if payment_data.is_none() {
@@ -3995,7 +4001,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39954001

39964002
// Check that scan_commitment, above, decided there is some source worth relaying an
39974003
// HTLC resolution backwards to and figure out whether we learned a preimage from it.
3998-
if let Some((source, payment_hash, amount_msat)) = payment_data {
4004+
if let Some((source, payment_hash, amount_msat, amount_rgb)) = payment_data {
39994005
if accepted_preimage_claim {
40004006
if !self.pending_monitor_events.iter().any(
40014007
|update| if let &MonitorEvent::HTLCEvent(ref upd) = update { upd.source == source } else { false }) {
@@ -4015,6 +4021,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40154021
payment_preimage: Some(payment_preimage),
40164022
payment_hash,
40174023
htlc_value_satoshis: Some(amount_msat / 1000),
4024+
htlc_value_rgb: amount_rgb,
40184025
}));
40194026
}
40204027
} else if offered_preimage_claim {
@@ -4038,6 +4045,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40384045
payment_preimage: Some(payment_preimage),
40394046
payment_hash,
40404047
htlc_value_satoshis: Some(amount_msat / 1000),
4048+
htlc_value_rgb: amount_rgb,
40414049
}));
40424050
}
40434051
} else {
@@ -4058,6 +4066,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40584066
event: OnchainEvent::HTLCUpdate {
40594067
source, payment_hash,
40604068
htlc_value_satoshis: Some(amount_msat / 1000),
4069+
htlc_value_rgb: amount_rgb,
40614070
commitment_tx_output_idx: Some(input.previous_output.vout),
40624071
},
40634072
};

lightning/src/events/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,8 @@ pub enum Event {
776776
///
777777
/// The caveat described above the `fee_earned_msat` field applies here as well.
778778
outbound_amount_forwarded_msat: Option<u64>,
779+
/// The rgb amount forwarded.
780+
outbound_amount_forwarded_rgb: Option<u64>,
779781
},
780782
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
781783
/// confirmation on-chain.
@@ -1056,7 +1058,7 @@ impl Writeable for Event {
10561058
}
10571059
&Event::PaymentForwarded {
10581060
fee_earned_msat, prev_channel_id, claim_from_onchain_tx,
1059-
next_channel_id, outbound_amount_forwarded_msat
1061+
next_channel_id, outbound_amount_forwarded_msat, outbound_amount_forwarded_rgb,
10601062
} => {
10611063
7u8.write(writer)?;
10621064
write_tlv_fields!(writer, {
@@ -1065,6 +1067,7 @@ impl Writeable for Event {
10651067
(2, claim_from_onchain_tx, required),
10661068
(3, next_channel_id, option),
10671069
(5, outbound_amount_forwarded_msat, option),
1070+
(6, outbound_amount_forwarded_rgb, option),
10681071
});
10691072
},
10701073
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
@@ -1369,16 +1372,18 @@ impl MaybeReadable for Event {
13691372
let mut claim_from_onchain_tx = false;
13701373
let mut next_channel_id = None;
13711374
let mut outbound_amount_forwarded_msat = None;
1375+
let mut outbound_amount_forwarded_rgb = None;
13721376
read_tlv_fields!(reader, {
13731377
(0, fee_earned_msat, option),
13741378
(1, prev_channel_id, option),
13751379
(2, claim_from_onchain_tx, required),
13761380
(3, next_channel_id, option),
13771381
(5, outbound_amount_forwarded_msat, option),
1382+
(6, outbound_amount_forwarded_rgb, option),
13781383
});
13791384
Ok(Some(Event::PaymentForwarded {
13801385
fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id,
1381-
outbound_amount_forwarded_msat
1386+
outbound_amount_forwarded_msat, outbound_amount_forwarded_rgb,
13821387
}))
13831388
};
13841389
f()

lightning/src/ln/channel.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ struct HTLCStats {
475475
on_holder_tx_holding_cell_htlcs_count: u32, // dust HTLCs *non*-included
476476
}
477477

478+
#[derive(Debug)]
478479
/// An enum gathering stats on commitment transaction, either local or remote.
479480
struct CommitmentStats<'a> {
480481
tx: CommitmentTransaction, // the transaction info
@@ -2969,15 +2970,15 @@ impl<SP: Deref> Channel<SP> where
29692970
Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find".to_owned()))
29702971
}
29712972

2972-
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64), ChannelError> {
2973+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(HTLCSource, u64, Option<u64>), ChannelError> {
29732974
if (self.context.channel_state & (ChannelState::ChannelReady as u32)) != (ChannelState::ChannelReady as u32) {
29742975
return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state".to_owned()));
29752976
}
29762977
if self.context.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
29772978
return Err(ChannelError::Close("Peer sent update_fulfill_htlc when we needed a channel_reestablish".to_owned()));
29782979
}
29792980

2980-
self.mark_outbound_htlc_removed(msg.htlc_id, Some(msg.payment_preimage), None).map(|htlc| (htlc.source.clone(), htlc.amount_msat))
2981+
self.mark_outbound_htlc_removed(msg.htlc_id, Some(msg.payment_preimage), None).map(|htlc| (htlc.source.clone(), htlc.amount_msat, htlc.amount_rgb))
29812982
}
29822983

29832984
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,7 +5688,7 @@ where
56885688
}
56895689

56905690
fn claim_funds_internal(&self, source: HTLCSource, payment_preimage: PaymentPreimage,
5691-
forwarded_htlc_value_msat: Option<u64>, from_onchain: bool, startup_replay: bool,
5691+
forwarded_htlc_value_msat: Option<u64>, forwarded_htlc_rgb: Option<u64>, from_onchain: bool, startup_replay: bool,
56925692
next_channel_counterparty_node_id: Option<PublicKey>, next_channel_outpoint: OutPoint
56935693
) {
56945694
match source {
@@ -5794,6 +5794,7 @@ where
57945794
prev_channel_id: Some(prev_outpoint.to_channel_id()),
57955795
next_channel_id: Some(next_channel_outpoint.to_channel_id()),
57965796
outbound_amount_forwarded_msat: forwarded_htlc_value_msat,
5797+
outbound_amount_forwarded_rgb: forwarded_htlc_rgb,
57975798
},
57985799
downstream_counterparty_and_funding_outpoint: chan_to_release,
57995800
})
@@ -6646,7 +6647,7 @@ where
66466647

66476648
fn internal_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<(), MsgHandleErrInternal> {
66486649
let funding_txo;
6649-
let (htlc_source, forwarded_htlc_value) = {
6650+
let (htlc_source, forwarded_htlc_value, forwarded_htlc_rgb) = {
66506651
let per_peer_state = self.per_peer_state.read().unwrap();
66516652
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
66526653
.ok_or_else(|| {
@@ -6683,7 +6684,7 @@ where
66836684
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
66846685
}
66856686
};
6686-
self.claim_funds_internal(htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), false, false, Some(*counterparty_node_id), funding_txo);
6687+
self.claim_funds_internal(htlc_source, msg.payment_preimage.clone(), Some(forwarded_htlc_value), forwarded_htlc_rgb, false, false, Some(*counterparty_node_id), funding_txo);
66876688
Ok(())
66886689
}
66896690

@@ -7177,7 +7178,7 @@ where
71777178
MonitorEvent::HTLCEvent(htlc_update) => {
71787179
if let Some(preimage) = htlc_update.payment_preimage {
71797180
log_trace!(self.logger, "Claiming HTLC with preimage {} from our monitor", preimage);
7180-
self.claim_funds_internal(htlc_update.source, preimage, htlc_update.htlc_value_satoshis.map(|v| v * 1000), true, false, counterparty_node_id, funding_outpoint);
7181+
self.claim_funds_internal(htlc_update.source, preimage, htlc_update.htlc_value_satoshis.map(|v| v * 1000), htlc_update.htlc_value_rgb, true, false, counterparty_node_id, funding_outpoint);
71817182
} else {
71827183
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", &htlc_update.payment_hash);
71837184
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
@@ -10531,7 +10532,7 @@ where
1053110532
.filter_map(|(htlc_source, (htlc, preimage_opt))| {
1053210533
if let HTLCSource::PreviousHopData(_) = htlc_source {
1053310534
if let Some(payment_preimage) = preimage_opt {
10534-
Some((htlc_source, payment_preimage, htlc.amount_msat,
10535+
Some((htlc_source, payment_preimage, htlc.amount_msat, htlc.amount_rgb,
1053510536
// Check if `counterparty_opt.is_none()` to see if the
1053610537
// downstream chan is closed (because we don't have a
1053710538
// channel_id -> peer map entry).
@@ -10828,11 +10829,11 @@ where
1082810829
channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
1082910830
}
1083010831

10831-
for (source, preimage, downstream_value, downstream_closed, downstream_node_id, downstream_funding) in pending_claims_to_replay {
10832+
for (source, preimage, downstream_value, downstream_rgb, downstream_closed, downstream_node_id, downstream_funding) in pending_claims_to_replay {
1083210833
// We use `downstream_closed` in place of `from_onchain` here just as a guess - we
1083310834
// don't remember in the `ChannelMonitor` where we got a preimage from, but if the
1083410835
// channel is closed we just assume that it probably came from an on-chain claim.
10835-
channel_manager.claim_funds_internal(source, preimage, Some(downstream_value),
10836+
channel_manager.claim_funds_internal(source, preimage, Some(downstream_value), downstream_rgb,
1083610837
downstream_closed, true, downstream_node_id, downstream_funding);
1083710838
}
1083810839

0 commit comments

Comments
 (0)