Skip to content

Commit a295722

Browse files
committed
Make channel_id optional in PaymentForwarded event
1 parent 970bbfb commit a295722

8 files changed

+29
-31
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ fn test_monitor_update_fail_reestablish() {
11021102
assert!(updates.update_fee.is_none());
11031103
assert_eq!(updates.update_fulfill_htlcs.len(), 1);
11041104
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1105-
expect_payment_forwarded!(nodes[1], chan_1.2, Some(1000), false);
1105+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), false);
11061106
check_added_monitors!(nodes[1], 1);
11071107
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
11081108
commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
@@ -2087,7 +2087,7 @@ fn test_fail_htlc_on_broadcast_after_claim() {
20872087
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
20882088
let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
20892089
check_added_monitors!(nodes[1], 1);
2090-
expect_payment_forwarded!(nodes[1], chan_id_1, Some(1000), false);
2090+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), false);
20912091

20922092
mine_transaction(&nodes[1], &bs_txn[0]);
20932093
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
@@ -2468,7 +2468,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
24682468
assert_eq!(fulfill_msg, cs_updates.update_fulfill_htlcs[0]);
24692469
}
24702470
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &fulfill_msg);
2471-
expect_payment_forwarded!(nodes[1], chan_id_1, Some(1000), false);
2471+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), false);
24722472
check_added_monitors!(nodes[1], 1);
24732473

24742474
let mut bs_updates = None;

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4291,7 +4291,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42914291

42924292
let mut pending_events = self.pending_events.lock().unwrap();
42934293

4294-
let channel_id = prev_outpoint.to_channel_id();
4294+
let channel_id = if fee_earned_msat.is_some() { Some(prev_outpoint.to_channel_id()) } else { None };
42954295
pending_events.push(events::Event::PaymentForwarded {
42964296
channel_id,
42974297
fee_earned_msat,

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,12 +1328,17 @@ macro_rules! expect_payment_path_successful {
13281328
}
13291329

13301330
macro_rules! expect_payment_forwarded {
1331-
($node: expr, $channel_id: expr, $expected_fee: expr, $upstream_force_closed: expr) => {
1331+
($node: expr, $source_node: expr, $expected_fee: expr, $upstream_force_closed: expr) => {
13321332
let events = $node.node.get_and_clear_pending_events();
13331333
assert_eq!(events.len(), 1);
13341334
match events[0] {
13351335
Event::PaymentForwarded { channel_id, fee_earned_msat, claim_from_onchain_tx } => {
1336-
assert_eq!(channel_id, $channel_id);
1336+
if fee_earned_msat.is_some() {
1337+
// Is the event channel_id in one of the channels between the two nodes?
1338+
assert!($node.node.list_channels().iter().any(|x| x.counterparty.node_id == $source_node.node.get_our_node_id() && x.channel_id == channel_id.unwrap()));
1339+
} else {
1340+
assert!(channel_id.is_none());
1341+
}
13371342
assert_eq!(fee_earned_msat, $expected_fee);
13381343
assert_eq!(claim_from_onchain_tx, $upstream_force_closed);
13391344
},
@@ -1574,11 +1579,11 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
15741579
}
15751580
}
15761581
macro_rules! mid_update_fulfill_dance {
1577-
($node: expr, $prev_node: expr, $next_channel_id: expr, $new_msgs: expr) => {
1582+
($node: expr, $prev_node: expr, $next_node: expr, $new_msgs: expr) => {
15781583
{
15791584
$node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
15801585
let fee = $node.node.channel_state.lock().unwrap().by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap().config.forwarding_fee_base_msat;
1581-
expect_payment_forwarded!($node, $next_channel_id, Some(fee as u64), false);
1586+
expect_payment_forwarded!($node, $next_node, Some(fee as u64), false);
15821587
expected_total_fee_msat += fee as u64;
15831588
check_added_monitors!($node, 1);
15841589
let new_next_msgs = if $new_msgs {
@@ -1607,13 +1612,9 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
16071612
if idx == expected_route.len() - 1 {
16081613
next_node = origin_node;
16091614
} else {
1610-
next_node = expected_route[idx - 1];
1615+
next_node = expected_route[expected_route.len() - 1 - idx - 1];
16111616
}
1612-
let next_channel_id = match node.node.list_channels().iter().find(|&x| x.counterparty.node_id == next_node.node.get_our_node_id()) {
1613-
Some(channel) => channel.channel_id,
1614-
None => panic!("Uh oh! Coudn't find the channel")
1615-
};
1616-
mid_update_fulfill_dance!(node, prev_node, next_channel_id, update_next_msgs);
1617+
mid_update_fulfill_dance!(node, prev_node, next_node, update_next_msgs);
16171618
} else {
16181619
assert!(!update_next_msgs);
16191620
assert!(node.node.get_and_clear_pending_msg_events().is_empty());

lightning/src/ln/functional_tests.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,7 +2684,7 @@ fn test_htlc_on_chain_success() {
26842684
Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
26852685
_ => panic!("Unexpected event"),
26862686
}
2687-
let chan_id = chan_1.2;
2687+
let chan_id = Some(chan_1.2);
26882688
match forwarded_events[1] {
26892689
Event::PaymentForwarded { channel_id, fee_earned_msat, claim_from_onchain_tx } => {
26902690
assert_eq!(channel_id, chan_id);
@@ -5068,10 +5068,6 @@ fn test_onchain_to_onchain_claim() {
50685068
// Create some initial channels
50695069
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
50705070
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
5071-
assert_ne!(chan_1.2, chan_2.2);
5072-
println!("chan_ab_id: {:?}\nchan_bc_id: {:?}", chan_1.2, chan_2.2);
5073-
// chan_ab_id: [54, 1, 27, 147, 208, 68, 174, 120, 153, 166, 147, 131, 196, 197, 137, 125, 20, 169, 146, 130, 47, 96, 90, 106, 226, 118, 1, 231, 93, 113, 103, 164]
5074-
// chan_bc_id: [62, 94, 18, 107, 70, 218, 72, 107, 73, 188, 156, 196, 17, 42, 11, 32, 206, 143, 17, 229, 106, 81, 201, 215, 188, 141, 176, 146, 55, 122, 100, 36]
50755071

50765072
// Ensure all nodes are at the same height
50775073
let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
@@ -5122,7 +5118,7 @@ fn test_onchain_to_onchain_claim() {
51225118
}
51235119
match events[1] {
51245120
Event::PaymentForwarded { channel_id, fee_earned_msat, claim_from_onchain_tx } => {
5125-
assert_eq!(channel_id, chan_1.2);
5121+
assert_eq!(channel_id, Some(chan_1.2));
51265122
assert_eq!(fee_earned_msat, Some(1000));
51275123
assert_eq!(claim_from_onchain_tx, true);
51285124
},
@@ -5289,7 +5285,7 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
52895285
// Note that the fee paid is effectively double as the HTLC value (including the nodes[1] fee
52905286
// and nodes[2] fee) is rounded down and then claimed in full.
52915287
mine_transaction(&nodes[1], &htlc_success_txn[0]);
5292-
expect_payment_forwarded!(nodes[1], chan_1.2 ,Some(196*2), true);
5288+
expect_payment_forwarded!(nodes[1], nodes[0], Some(196*2), true);
52935289
let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
52945290
assert!(updates.update_add_htlcs.is_empty());
52955291
assert!(updates.update_fail_htlcs.is_empty());
@@ -8867,7 +8863,7 @@ fn do_test_onchain_htlc_settlement_after_close(broadcast_alice: bool, go_onchain
88678863
assert_eq!(carol_updates.update_fulfill_htlcs.len(), 1);
88688864

88698865
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &carol_updates.update_fulfill_htlcs[0]);
8870-
expect_payment_forwarded!(nodes[1], chan_ab.2, if go_onchain_before_fulfill || force_closing_node == 1 { None } else { Some(1000) }, false);
8866+
expect_payment_forwarded!(nodes[1], nodes[0], if go_onchain_before_fulfill || force_closing_node == 1 { None } else { Some(1000) }, false);
88718867
// If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
88728868
if !go_onchain_before_fulfill && broadcast_alice {
88738869
let events = nodes[1].node.get_and_clear_pending_msg_events();

lightning/src/ln/payment_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
495495
let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
496496
assert_eq!(bs_htlc_claim_txn.len(), 1);
497497
check_spends!(bs_htlc_claim_txn[0], as_commitment_tx);
498-
expect_payment_forwarded!(nodes[1], chan_id, None, false);
498+
expect_payment_forwarded!(nodes[1], nodes[0], None, false);
499499

500500
if !confirm_before_reload {
501501
mine_transaction(&nodes[0], &as_commitment_tx);

lightning/src/ln/reorg_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) {
138138
// ChannelManager only polls chain::Watch::release_pending_monitor_events when we
139139
// probe it for events, so we probe non-message events here (which should just be the
140140
// PaymentForwarded event).
141-
expect_payment_forwarded!(nodes[1], chan_1.2, Some(1000), true);
141+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), true);
142142
} else {
143143
// Confirm the timeout tx and check that we fail the HTLC backwards
144144
let block = Block {

lightning/src/ln/shutdown_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn updates_shutdown_wait() {
110110
assert!(updates.update_fee.is_none());
111111
assert_eq!(updates.update_fulfill_htlcs.len(), 1);
112112
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
113-
expect_payment_forwarded!(nodes[1], chan_1.2, Some(1000), false);
113+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), false);
114114
check_added_monitors!(nodes[1], 1);
115115
let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
116116
commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
@@ -279,7 +279,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
279279
assert!(updates.update_fee.is_none());
280280
assert_eq!(updates.update_fulfill_htlcs.len(), 1);
281281
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
282-
expect_payment_forwarded!(nodes[1], chan_1.2, Some(1000), false);
282+
expect_payment_forwarded!(nodes[1], nodes[0], Some(1000), false);
283283
check_added_monitors!(nodes[1], 1);
284284
let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
285285
commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);

lightning/src/util/events.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ pub enum Event {
343343
/// This event is generated when a payment has been successfully forwarded through us and a
344344
/// forwarding fee earned.
345345
PaymentForwarded {
346-
/// The channel between the source node and the node emitting this event.
347-
channel_id: [u8; 32],
346+
/// The channel between the source node and us. Optional because PaymentForwarded event can
347+
/// be emitted even after the channel has been closed.
348+
channel_id: Option<[u8; 32]>,
348349
/// The fee, in milli-satoshis, which was earned as a result of the payment.
349350
///
350351
/// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
@@ -525,7 +526,7 @@ impl Writeable for Event {
525526
&Event::PaymentForwarded { channel_id, fee_earned_msat, claim_from_onchain_tx } => {
526527
7u8.write(writer)?;
527528
write_tlv_fields!(writer, {
528-
(0, channel_id, required),
529+
(0, channel_id, option),
529530
(4, fee_earned_msat, option),
530531
(6, claim_from_onchain_tx, required),
531532
});
@@ -686,11 +687,11 @@ impl MaybeReadable for Event {
686687
},
687688
7u8 => {
688689
let f = || {
689-
let mut channel_id = [0; 32];
690+
let mut channel_id = None;
690691
let mut fee_earned_msat = None;
691692
let mut claim_from_onchain_tx = false;
692693
read_tlv_fields!(reader, {
693-
(0, channel_id, required),
694+
(0, channel_id, option),
694695
(2, fee_earned_msat, option),
695696
(4, claim_from_onchain_tx, required),
696697
});

0 commit comments

Comments
 (0)