Skip to content

Commit c605781

Browse files
authored
Merge pull request #404 from G8XSU/payment-forwarded-event
Start publishing PaymentForwarded events.
2 parents b86e87d + d8d9002 commit c605781

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ interface Event {
277277
PaymentFailed(PaymentId? payment_id, PaymentHash? payment_hash, PaymentFailureReason? reason);
278278
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat);
279279
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline);
280+
PaymentForwarded(ChannelId prev_channel_id, ChannelId next_channel_id, UserChannelId? prev_user_channel_id, UserChannelId? next_user_channel_id, u64? total_fee_earned_msat, u64? skimmed_fee_msat, boolean claim_from_onchain_tx, u64? outbound_amount_forwarded_msat);
280281
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
281282
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
282283
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);

src/event.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,50 @@ pub enum Event {
103103
/// The value, in thousandths of a satoshi, that has been received.
104104
amount_msat: u64,
105105
},
106+
/// A payment has been forwarded.
107+
PaymentForwarded {
108+
/// The channel id of the incoming channel between the previous node and us.
109+
prev_channel_id: ChannelId,
110+
/// The channel id of the outgoing channel between the next node and us.
111+
next_channel_id: ChannelId,
112+
/// The `user_channel_id` of the incoming channel between the previous node and us.
113+
///
114+
/// Will only be `None` for events serialized with LDK Node v0.3.0 or prior.
115+
prev_user_channel_id: Option<UserChannelId>,
116+
/// The `user_channel_id` of the outgoing channel between the next node and us.
117+
///
118+
/// This will be `None` if the payment was settled via an on-chain transaction. See the
119+
/// caveat described for the `total_fee_earned_msat` field.
120+
next_user_channel_id: Option<UserChannelId>,
121+
/// The total fee, in milli-satoshis, which was earned as a result of the payment.
122+
///
123+
/// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
124+
/// was pending, the amount the next hop claimed will have been rounded down to the nearest
125+
/// whole satoshi. Thus, the fee calculated here may be higher than expected as we still
126+
/// claimed the full value in millisatoshis from the source. In this case,
127+
/// `claim_from_onchain_tx` will be set.
128+
///
129+
/// If the channel which sent us the payment has been force-closed, we will claim the funds
130+
/// via an on-chain transaction. In that case we do not yet know the on-chain transaction
131+
/// fees which we will spend and will instead set this to `None`.
132+
total_fee_earned_msat: Option<u64>,
133+
/// The share of the total fee, in milli-satoshis, which was withheld in addition to the
134+
/// forwarding fee.
135+
///
136+
/// This will only be `Some` if we forwarded an intercepted HTLC with less than the
137+
/// expected amount. This means our counterparty accepted to receive less than the invoice
138+
/// amount.
139+
///
140+
/// The caveat described above the `total_fee_earned_msat` field applies here as well.
141+
skimmed_fee_msat: Option<u64>,
142+
/// If this is `true`, the forwarded HTLC was claimed by our counterparty via an on-chain
143+
/// transaction.
144+
claim_from_onchain_tx: bool,
145+
/// The final amount forwarded, in milli-satoshis, after the fee is deducted.
146+
///
147+
/// The caveat described above the `total_fee_earned_msat` field applies here as well.
148+
outbound_amount_forwarded_msat: Option<u64>,
149+
},
106150
/// A payment for a previously-registered payment hash has been received.
107151
///
108152
/// This needs to be manually claimed by supplying the correct preimage to [`claim_for_hash`].
@@ -204,6 +248,16 @@ impl_writeable_tlv_based_enum!(Event,
204248
(2, payment_id, required),
205249
(4, claimable_amount_msat, required),
206250
(6, claim_deadline, option),
251+
},
252+
(7, PaymentForwarded) => {
253+
(0, prev_channel_id, required),
254+
(2, next_channel_id, required),
255+
(4, prev_user_channel_id, option),
256+
(6, next_user_channel_id, option),
257+
(8, total_fee_earned_msat, option),
258+
(10, skimmed_fee_msat, option),
259+
(12, claim_from_onchain_tx, required),
260+
(14, outbound_amount_forwarded_msat, option),
207261
}
208262
);
209263

@@ -1068,11 +1122,28 @@ where
10681122
LdkEvent::PaymentForwarded {
10691123
prev_channel_id,
10701124
next_channel_id,
1125+
prev_user_channel_id,
1126+
next_user_channel_id,
10711127
total_fee_earned_msat,
1128+
skimmed_fee_msat,
10721129
claim_from_onchain_tx,
10731130
outbound_amount_forwarded_msat,
1074-
..
10751131
} => {
1132+
let event = Event::PaymentForwarded {
1133+
prev_channel_id: prev_channel_id.expect("prev_channel_id expected for events generated by LDK versions greater than 0.0.107."),
1134+
next_channel_id: next_channel_id.expect("next_channel_id expected for events generated by LDK versions greater than 0.0.107."),
1135+
prev_user_channel_id: prev_user_channel_id.map(UserChannelId),
1136+
next_user_channel_id: next_user_channel_id.map(UserChannelId),
1137+
total_fee_earned_msat,
1138+
skimmed_fee_msat,
1139+
claim_from_onchain_tx,
1140+
outbound_amount_forwarded_msat,
1141+
};
1142+
self.event_queue.add_event(event).map_err(|e| {
1143+
log_error!(self.logger, "Failed to push to event queue: {}", e);
1144+
ReplayEvent()
1145+
})?;
1146+
10761147
let read_only_network_graph = self.network_graph.read_only();
10771148
let nodes = read_only_network_graph.nodes();
10781149
let channels = self.channel_manager.list_channels();

tests/integration_tests_rust.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ fn multi_hop_sending() {
192192
let invoice = nodes[4].bolt11_payment().receive(2_500_000, &"asdf", 9217).unwrap();
193193
nodes[0].bolt11_payment().send(&invoice, Some(sending_params)).unwrap();
194194

195+
expect_event!(nodes[1], PaymentForwarded);
196+
expect_event!(nodes[2], PaymentForwarded);
197+
195198
let payment_id = expect_payment_received_event!(&nodes[4], 2_500_000);
196199
let fee_paid_msat = Some(2000);
197200
expect_payment_successful_event!(nodes[0], payment_id, Some(fee_paid_msat));

0 commit comments

Comments
 (0)