Skip to content

Commit b959830

Browse files
committed
Add and emit OnchainPaymentSuccessful and OnchainPaymentReceived events
.. two new events that we're emitting when we sent or received an onchain payment, i.e., the transactions reached ANTI_REORG_DELAY confirmations.
1 parent 31beaf2 commit b959830

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

bindings/ldk_node.udl

+2
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ interface Event {
291291
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
292292
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
293293
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
294+
OnchainPaymentSuccessful(PaymentId payment_id, Txid txid, u64 amount_msat, BlockHash block_hash, u32 block_height);
295+
OnchainPaymentReceived(PaymentId payment_id, Txid txid, u64 amount_msat, BlockHash block_hash, u32 block_height);
294296
};
295297

296298
enum PaymentFailureReason {

src/event.rs

+51-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use lightning_liquidity::lsps2::utils::compute_opening_fee;
4242

4343
use bitcoin::blockdata::locktime::absolute::LockTime;
4444
use bitcoin::secp256k1::PublicKey;
45-
use bitcoin::{Amount, OutPoint};
45+
use bitcoin::{Amount, BlockHash, OutPoint, Txid};
4646

4747
use rand::{thread_rng, Rng};
4848

@@ -221,6 +221,41 @@ pub enum Event {
221221
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
222222
reason: Option<ClosureReason>,
223223
},
224+
/// A sent onchain payment was successful.
225+
///
226+
/// It's guaranteed to have reached at least [`ANTI_REORG_DELAY`] delay confirmations.
227+
///
228+
///
229+
/// [`ANTI_REORG_DELAY`]: lightning::chain::channelmonitor::ANTI_REORG_DELAY
230+
OnchainPaymentSuccessful {
231+
/// A local identifier used to track the payment.
232+
payment_id: PaymentId,
233+
/// The transaction identifier.
234+
txid: Txid,
235+
/// The value, in thousandths of a satoshi, that has been received.
236+
amount_msat: u64,
237+
/// The hash of the block in which the transaction was confirmed.
238+
block_hash: BlockHash,
239+
/// The height under which the block was confirmed.
240+
block_height: u32,
241+
},
242+
/// An onchain payment has been received.
243+
///
244+
/// It's guaranteed to have reached at least [`ANTI_REORG_DELAY`] delay confirmations.
245+
///
246+
/// [`ANTI_REORG_DELAY`]: lightning::chain::channelmonitor::ANTI_REORG_DELAY
247+
OnchainPaymentReceived {
248+
/// A local identifier used to track the payment.
249+
payment_id: PaymentId,
250+
/// The transaction identifier.
251+
txid: Txid,
252+
/// The value, in thousandths of a satoshi, that has been received.
253+
amount_msat: u64,
254+
/// The hash of the block in which the transaction was confirmed.
255+
block_hash: BlockHash,
256+
/// The height under which the block was confirmed.
257+
block_height: u32,
258+
},
224259
}
225260

226261
impl_writeable_tlv_based_enum!(Event,
@@ -277,7 +312,21 @@ impl_writeable_tlv_based_enum!(Event,
277312
(10, skimmed_fee_msat, option),
278313
(12, claim_from_onchain_tx, required),
279314
(14, outbound_amount_forwarded_msat, option),
280-
}
315+
},
316+
(8, OnchainPaymentSuccessful) => {
317+
(0, payment_id, required),
318+
(2, txid, required),
319+
(4, amount_msat, required),
320+
(6, block_hash, required),
321+
(8, block_height, required),
322+
},
323+
(9, OnchainPaymentReceived) => {
324+
(0, payment_id, required),
325+
(2, txid, required),
326+
(4, amount_msat, required),
327+
(6, block_hash, required),
328+
(8, block_height, required),
329+
},
281330
);
282331

283332
pub struct EventQueue<L: Deref>

src/wallet/mod.rs

+40-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use persist::KVStoreWalletPersister;
99

1010
use crate::logger::{log_debug, log_error, log_info, log_trace, FilesystemLogger, Logger};
1111

12-
use crate::event::EventQueue;
12+
use crate::event::{Event, EventQueue};
1313
use crate::fee_estimator::{ConfirmationTarget, FeeEstimator};
1414
use crate::payment::store::{ConfirmationStatus, PaymentStore};
1515
use crate::payment::{PaymentDetails, PaymentDirection, PaymentStatus};
@@ -192,24 +192,59 @@ where
192192
let (sent, received) = locked_wallet.sent_and_received(&wtx.tx_node.tx);
193193
let (direction, amount_msat) = if sent > received {
194194
let direction = PaymentDirection::Outbound;
195-
let amount_msat = Some(sent.to_sat().saturating_sub(received.to_sat()) * 1000);
195+
let amount_msat = sent.to_sat().saturating_sub(received.to_sat()) * 1000;
196196
(direction, amount_msat)
197197
} else {
198198
let direction = PaymentDirection::Inbound;
199-
let amount_msat = Some(received.to_sat().saturating_sub(sent.to_sat()) * 1000);
199+
let amount_msat = received.to_sat().saturating_sub(sent.to_sat()) * 1000;
200200
(direction, amount_msat)
201201
};
202202

203203
let payment = PaymentDetails {
204204
id,
205205
kind,
206-
amount_msat,
206+
amount_msat: Some(amount_msat),
207207
direction,
208208
status: payment_status,
209209
latest_update_timestamp,
210210
};
211211

212-
self.payment_store.insert_or_update(&payment)?;
212+
let updated = self.payment_store.insert_or_update(&payment)?;
213+
214+
// If we just updated the entry and we deem the transaction successful (i.e., has seen
215+
// at least ANTI_REORG_DELAY confirmations), issue an event.
216+
if updated && payment_status == PaymentStatus::Succeeded {
217+
match confirmation_status {
218+
ConfirmationStatus::Confirmed { block_hash, height, .. } => {
219+
let event;
220+
if direction == PaymentDirection::Outbound {
221+
event = Event::OnchainPaymentSuccessful {
222+
payment_id: id,
223+
txid,
224+
block_hash,
225+
block_height: height,
226+
amount_msat,
227+
};
228+
} else {
229+
event = Event::OnchainPaymentReceived {
230+
payment_id: id,
231+
txid,
232+
block_hash,
233+
block_height: height,
234+
amount_msat,
235+
};
236+
}
237+
self.event_queue.add_event(event).map_err(|e| {
238+
log_error!(self.logger, "Failed to push to event queue: {}", e);
239+
Error::PersistenceFailed
240+
})?;
241+
},
242+
_ => {
243+
// We only issue events for transactions that have seen ANTI_REORG_DELAY
244+
// confirmations.
245+
},
246+
}
247+
}
213248
}
214249

215250
Ok(())

0 commit comments

Comments
 (0)