Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 11, 2024
1 parent 5cc0192 commit 3b9c315
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 178 deletions.
26 changes: 14 additions & 12 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ interface Node {
void update_channel_config([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id, ChannelConfig channel_config);
[Throws=NodeError]
void sync_wallets();
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
PaymentDetails? payment([ByRef]PaymentId payment_id);
[Throws=NodeError]
void remove_payment([ByRef]PaymentHash payment_hash);
void remove_payment([ByRef]PaymentId payment_id);
BalanceDetails list_balances();
sequence<PaymentDetails> list_payments();
sequence<PeerDetails> list_peers();
Expand All @@ -82,9 +82,9 @@ interface Node {

interface Bolt11Payment {
[Throws=NodeError]
PaymentHash send([ByRef]Bolt11Invoice invoice);
PaymentId send([ByRef]Bolt11Invoice invoice);
[Throws=NodeError]
PaymentHash send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
[Throws=NodeError]
void send_probes([ByRef]Bolt11Invoice invoice);
[Throws=NodeError]
Expand All @@ -101,7 +101,7 @@ interface Bolt11Payment {

interface SpontaneousPayment {
[Throws=NodeError]
PaymentHash send(u64 amount_msat, PublicKey node_id);
PaymentId send(u64 amount_msat, PublicKey node_id);
[Throws=NodeError]
void send_probes(u64 amount_msat, PublicKey node_id);
};
Expand Down Expand Up @@ -139,6 +139,7 @@ enum NodeError {
"InvalidSocketAddress",
"InvalidPublicKey",
"InvalidSecretKey",
"InvalidPaymentId",
"InvalidPaymentHash",
"InvalidPaymentPreimage",
"InvalidPaymentSecret",
Expand Down Expand Up @@ -221,9 +222,9 @@ interface ClosureReason {
[Enum]
interface PaymentKind {
Onchain();
Bolt11();
Bolt11Jit(LSPFeeLimits lsp_fee_limits);
Spontaneous();
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret);
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
Spontaneous(PaymentHash hash, PaymentPreimage? preimage);
};

enum PaymentDirection {
Expand All @@ -243,10 +244,8 @@ dictionary LSPFeeLimits {
};

dictionary PaymentDetails {
PaymentKind? kind;
PaymentHash hash;
PaymentPreimage? preimage;
PaymentSecret? secret;
PaymentId id;
PaymentKind kind;
u64? amount_msat;
PaymentDirection direction;
PaymentStatus status;
Expand Down Expand Up @@ -371,6 +370,9 @@ typedef string Address;
[Custom]
typedef string Bolt11Invoice;

[Custom]
typedef string PaymentId;

[Custom]
typedef string PaymentHash;

Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub enum Error {
InvalidPublicKey,
/// The given secret key is invalid.
InvalidSecretKey,
/// The given payment id is invalid.
InvalidPaymentId,
/// The given payment hash is invalid.
InvalidPaymentHash,
/// The given payment pre-image is invalid.
Expand Down Expand Up @@ -100,6 +102,7 @@ impl fmt::Display for Error {
Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
Self::InvalidPaymentId => write!(f, "The given payment id is invalid."),
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),
Expand Down
52 changes: 33 additions & 19 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use lightning::chain::chaininterface::ConfirmationTarget;
use lightning::events::{ClosureReason, PaymentPurpose};
use lightning::events::{Event as LdkEvent, PaymentFailureReason};
use lightning::impl_writeable_tlv_based_enum;
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::{ChannelId, PaymentHash};
use lightning::routing::gossip::NodeId;
use lightning::util::errors::APIError;
Expand Down Expand Up @@ -411,7 +412,8 @@ where
onion_fields: _,
counterparty_skimmed_fee_msat,
} => {
if let Some(info) = self.payment_store.get(&payment_hash) {
let payment_id = PaymentId(payment_hash.0);
if let Some(info) = self.payment_store.get(&payment_id) {
if info.status == PaymentStatus::Succeeded {
log_info!(
self.logger,
Expand All @@ -423,7 +425,7 @@ where

let update = PaymentDetailsUpdate {
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_hash)
..PaymentDetailsUpdate::new(payment_id)
};
self.payment_store.update(&update).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
Expand All @@ -433,7 +435,7 @@ where
}

let max_total_opening_fee_msat = match info.kind {
Some(PaymentKind::Bolt11Jit { lsp_fee_limits }) => {
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
lsp_fee_limits
.max_total_opening_fee_msat
.or_else(|| {
Expand Down Expand Up @@ -461,7 +463,7 @@ where

let update = PaymentDetailsUpdate {
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_hash)
..PaymentDetailsUpdate::new(payment_id)
};
self.payment_store.update(&update).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
Expand Down Expand Up @@ -502,7 +504,7 @@ where

let update = PaymentDetailsUpdate {
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_hash)
..PaymentDetailsUpdate::new(payment_id)
};
self.payment_store.update(&update).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
Expand All @@ -524,14 +526,15 @@ where
hex_utils::to_string(&payment_hash.0),
amount_msat,
);
let payment_id = PaymentId(payment_hash.0);
match purpose {
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
let update = PaymentDetailsUpdate {
preimage: Some(payment_preimage),
secret: Some(Some(payment_secret)),
amount_msat: Some(Some(amount_msat)),
status: Some(PaymentStatus::Succeeded),
..PaymentDetailsUpdate::new(payment_hash)
..PaymentDetailsUpdate::new(payment_id)
};
match self.payment_store.update(&update) {
Ok(true) => (),
Expand All @@ -555,11 +558,14 @@ where
}
},
PaymentPurpose::SpontaneousPayment(preimage) => {
let payment = PaymentDetails {
kind: Some(PaymentKind::Spontaneous),
preimage: Some(preimage),
let id = PaymentId(payment_hash.0);
let kind = PaymentKind::Spontaneous {
hash: payment_hash,
secret: None,
preimage: Some(preimage),
};
let payment = PaymentDetails {
id,
kind,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Succeeded,
Expand Down Expand Up @@ -596,13 +602,19 @@ where
});
},
LdkEvent::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
if let Some(mut payment) = self.payment_store.get(&payment_hash) {
payment.preimage = Some(payment_preimage);
payment.status = PaymentStatus::Succeeded;
self.payment_store.insert(payment.clone()).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
panic!("Failed to access payment store");
});
let payment_id = PaymentId(payment_hash.0);
let update = PaymentDetailsUpdate {
preimage: Some(Some(payment_preimage)),
status: Some(PaymentStatus::Succeeded),
..PaymentDetailsUpdate::new(payment_id)
};

self.payment_store.update(&update).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
panic!("Failed to access payment store");
});

self.payment_store.get(&payment_id).map(|payment| {
log_info!(
self.logger,
"Successfully sent payment of {}msat{} from \
Expand All @@ -616,7 +628,8 @@ where
hex_utils::to_string(&payment_hash.0),
hex_utils::to_string(&payment_preimage.0)
);
}
});

self.event_queue
.add_event(Event::PaymentSuccessful { payment_hash, fee_paid_msat })
.unwrap_or_else(|e| {
Expand All @@ -632,9 +645,10 @@ where
reason
);

let payment_id = PaymentId(payment_hash.0);
let update = PaymentDetailsUpdate {
status: Some(PaymentStatus::Failed),
..PaymentDetailsUpdate::new(payment_hash)
..PaymentDetailsUpdate::new(payment_id)
};
self.payment_store.update(&update).unwrap_or_else(|e| {
log_error!(self.logger, "Failed to access payment store: {}", e);
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ pub use types::{ChannelDetails, PeerDetails, UserChannelId};
use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};

use lightning::chain::{BestBlock, Confirm};
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::msgs::SocketAddress;
use lightning::ln::PaymentHash;

use lightning::util::config::{ChannelHandshakeConfig, UserConfig};
pub use lightning::util::logger::Level as LogLevel;
Expand Down Expand Up @@ -1143,16 +1143,16 @@ impl Node {
}
}

/// Retrieve the details of a specific payment with the given hash.
/// Retrieve the details of a specific payment with the given id.
///
/// Returns `Some` if the payment was known and `None` otherwise.
pub fn payment(&self, payment_hash: &PaymentHash) -> Option<PaymentDetails> {
self.payment_store.get(payment_hash)
pub fn payment(&self, payment_id: &PaymentId) -> Option<PaymentDetails> {
self.payment_store.get(payment_id)
}

/// Remove the payment with the given hash from the store.
pub fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
self.payment_store.remove(&payment_hash)
/// Remove the payment with the given id from the store.
pub fn remove_payment(&self, payment_id: &PaymentId) -> Result<(), Error> {
self.payment_store.remove(&payment_id)
}

/// Retrieves an overview of all known balances.
Expand Down
Loading

0 comments on commit 3b9c315

Please sign in to comment.