Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose onchain transactions in store and events #432

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ interface Event {
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
OnchainPaymentSuccessful(PaymentId payment_id, Txid txid, u64 amount_msat, BlockHash block_hash, u32 block_height);
OnchainPaymentReceived(PaymentId payment_id, Txid txid, u64 amount_msat, BlockHash block_hash, u32 block_height);
};

enum PaymentFailureReason {
Expand Down Expand Up @@ -326,7 +328,7 @@ interface ClosureReason {

[Enum]
interface PaymentKind {
Onchain();
Onchain(Txid txid, ConfirmationStatus status);
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret);
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
Bolt12Offer(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret, OfferId offer_id, UntrustedString? payer_note, u64? quantity);
Expand Down Expand Up @@ -357,6 +359,12 @@ dictionary LSPFeeLimits {
u64? max_proportional_opening_fee_ppm_msat;
};

[Enum]
interface ConfirmationStatus {
Confirmed (BlockHash block_hash, u32 height, u64 timestamp);
Unconfirmed ();
};

dictionary PaymentDetails {
PaymentId id;
PaymentKind kind;
Expand Down
46 changes: 24 additions & 22 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,35 @@ fn build_with_store_internal(

let tx_broadcaster = Arc::new(TransactionBroadcaster::new(Arc::clone(&logger)));
let fee_estimator = Arc::new(OnchainFeeEstimator::new());

let payment_store = match io::utils::read_payments(Arc::clone(&kv_store), Arc::clone(&logger)) {
Ok(payments) => {
Arc::new(PaymentStore::new(payments, Arc::clone(&kv_store), Arc::clone(&logger)))
},
Err(_) => {
return Err(BuildError::ReadFailed);
},
};

let event_queue = match io::utils::read_event_queue(Arc::clone(&kv_store), Arc::clone(&logger))
{
Ok(event_queue) => Arc::new(event_queue),
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Arc::new(EventQueue::new(Arc::clone(&kv_store), Arc::clone(&logger)))
} else {
return Err(BuildError::ReadFailed);
}
},
};

let wallet = Arc::new(Wallet::new(
bdk_wallet,
wallet_persister,
Arc::clone(&tx_broadcaster),
Arc::clone(&fee_estimator),
Arc::clone(&payment_store),
Arc::clone(&event_queue),
Arc::clone(&logger),
));

Expand Down Expand Up @@ -1176,28 +1200,6 @@ fn build_with_store_internal(
},
}

// Init payment info storage
let payment_store = match io::utils::read_payments(Arc::clone(&kv_store), Arc::clone(&logger)) {
Ok(payments) => {
Arc::new(PaymentStore::new(payments, Arc::clone(&kv_store), Arc::clone(&logger)))
},
Err(_) => {
return Err(BuildError::ReadFailed);
},
};

let event_queue = match io::utils::read_event_queue(Arc::clone(&kv_store), Arc::clone(&logger))
{
Ok(event_queue) => Arc::new(event_queue),
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
Arc::new(EventQueue::new(Arc::clone(&kv_store), Arc::clone(&logger)))
} else {
return Err(BuildError::ReadFailed);
}
},
};

let peer_store = match io::utils::read_peer_info(Arc::clone(&kv_store), Arc::clone(&logger)) {
Ok(peer_store) => Arc::new(peer_store),
Err(e) => {
Expand Down
53 changes: 51 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use lightning_liquidity::lsps2::utils::compute_opening_fee;

use bitcoin::blockdata::locktime::absolute::LockTime;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Amount, OutPoint};
use bitcoin::{Amount, BlockHash, OutPoint, Txid};

use rand::{thread_rng, Rng};

Expand Down Expand Up @@ -221,6 +221,41 @@ pub enum Event {
/// This will be `None` for events serialized by LDK Node v0.2.1 and prior.
reason: Option<ClosureReason>,
},
/// A sent onchain payment was successful.
///
/// It's guaranteed to have reached at least [`ANTI_REORG_DELAY`] delay confirmations.
///
///
/// [`ANTI_REORG_DELAY`]: lightning::chain::channelmonitor::ANTI_REORG_DELAY
OnchainPaymentSuccessful {
/// A local identifier used to track the payment.
payment_id: PaymentId,
/// The transaction identifier.
txid: Txid,
/// The value, in thousandths of a satoshi, that has been received.
amount_msat: u64,
/// The hash of the block in which the transaction was confirmed.
block_hash: BlockHash,
/// The height under which the block was confirmed.
block_height: u32,
},
/// An onchain payment has been received.
///
/// It's guaranteed to have reached at least [`ANTI_REORG_DELAY`] delay confirmations.
///
/// [`ANTI_REORG_DELAY`]: lightning::chain::channelmonitor::ANTI_REORG_DELAY
OnchainPaymentReceived {
/// A local identifier used to track the payment.
payment_id: PaymentId,
/// The transaction identifier.
txid: Txid,
/// The value, in thousandths of a satoshi, that has been received.
amount_msat: u64,
/// The hash of the block in which the transaction was confirmed.
block_hash: BlockHash,
/// The height under which the block was confirmed.
block_height: u32,
},
}

impl_writeable_tlv_based_enum!(Event,
Expand Down Expand Up @@ -277,7 +312,21 @@ impl_writeable_tlv_based_enum!(Event,
(10, skimmed_fee_msat, option),
(12, claim_from_onchain_tx, required),
(14, outbound_amount_forwarded_msat, option),
}
},
(8, OnchainPaymentSuccessful) => {
(0, payment_id, required),
(2, txid, required),
(4, amount_msat, required),
(6, block_hash, required),
(8, block_height, required),
},
(9, OnchainPaymentReceived) => {
(0, payment_id, required),
(2, txid, required),
(4, amount_msat, required),
(6, block_hash, required),
(8, block_height, required),
},
);

pub struct EventQueue<L: Deref>
Expand Down
Loading
Loading