Skip to content

Commit

Permalink
Add a latest_update_timestamp field to PaymentDetails
Browse files Browse the repository at this point in the history
.. which allows to filter and sort payment based on how recent they are.
  • Loading branch information
tnull committed Jun 18, 2024
1 parent df67c81 commit 32fb2a8
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 150 deletions.
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ dictionary PaymentDetails {
u64? amount_msat;
PaymentDirection direction;
PaymentStatus status;
u64 latest_update_timestamp;
};

[NonExhaustive]
Expand Down
44 changes: 24 additions & 20 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,19 +598,21 @@ where
..
} => {
let offer_id = payment_context.offer_id;
let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt12Offer {
hash: Some(payment_hash),
preimage: payment_preimage,
secret: Some(payment_secret),
offer_id,
},
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
let kind = PaymentKind::Bolt12Offer {
hash: Some(payment_hash),
preimage: payment_preimage,
secret: Some(payment_secret),
offer_id,
};

let payment = PaymentDetails::new(
payment_id,
kind,
Some(amount_msat),
PaymentDirection::Inbound,
PaymentStatus::Pending,
);

match self.payment_store.insert(payment) {
Ok(false) => (),
Ok(true) => {
Expand Down Expand Up @@ -638,17 +640,19 @@ where
},
PaymentPurpose::SpontaneousPayment(preimage) => {
// Since it's spontaneous, we insert it now into our store.
let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Spontaneous {
hash: payment_hash,
preimage: Some(preimage),
},
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
let kind = PaymentKind::Spontaneous {
hash: payment_hash,
preimage: Some(preimage),
};

let payment = PaymentDetails::new(
payment_id,
kind,
Some(amount_msat),
PaymentDirection::Inbound,
PaymentStatus::Pending,
);

match self.payment_store.insert(payment) {
Ok(false) => (),
Ok(true) => {
Expand Down
128 changes: 66 additions & 62 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ impl Bolt11Payment {
let amt_msat = invoice.amount_milli_satoshis().unwrap();
log_info!(self.logger, "Initiated sending {}msat to {}", amt_msat, payee_pubkey);

let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: payment_secret,
},
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
let kind = PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: payment_secret,
};
let payment = PaymentDetails::new(
payment_id,
kind,
invoice.amount_milli_satoshis(),
PaymentDirection::Outbound,
PaymentStatus::Pending,
);

self.payment_store.insert(payment)?;

Expand All @@ -126,17 +127,18 @@ impl Bolt11Payment {
match e {
RetryableSendFailure::DuplicatePayment => Err(Error::DuplicatePayment),
_ => {
let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: payment_secret,
},
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
let kind = PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: payment_secret,
};
let payment = PaymentDetails::new(
payment_id,
kind,
invoice.amount_milli_satoshis(),
PaymentDirection::Outbound,
PaymentStatus::Failed,
);

self.payment_store.insert(payment)?;
Err(Error::PaymentSendingFailed)
Expand Down Expand Up @@ -216,17 +218,19 @@ impl Bolt11Payment {
payee_pubkey
);

let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: Some(*payment_secret),
},
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
let kind = PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: Some(*payment_secret),
};

let payment = PaymentDetails::new(
payment_id,
kind,
Some(amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Pending,
);
self.payment_store.insert(payment)?;

Ok(payment_id)
Expand All @@ -237,17 +241,18 @@ impl Bolt11Payment {
match e {
RetryableSendFailure::DuplicatePayment => Err(Error::DuplicatePayment),
_ => {
let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: Some(*payment_secret),
},
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
let kind = PaymentKind::Bolt11 {
hash: payment_hash,
preimage: None,
secret: Some(*payment_secret),
};
let payment = PaymentDetails::new(
payment_id,
kind,
Some(amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Failed,
);
self.payment_store.insert(payment)?;

Err(Error::PaymentSendingFailed)
Expand Down Expand Up @@ -472,19 +477,18 @@ impl Bolt11Payment {
} else {
None
};
let payment = PaymentDetails {
let kind = PaymentKind::Bolt11 {
hash: payment_hash,
preimage,
secret: Some(payment_secret.clone()),
};
let payment = PaymentDetails::new(
id,
kind: PaymentKind::Bolt11 {
hash: payment_hash,
preimage,
secret: Some(payment_secret.clone()),
},

kind,
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
};

PaymentDirection::Inbound,
PaymentStatus::Pending,
);
self.payment_store.insert(payment)?;

Ok(invoice)
Expand Down Expand Up @@ -605,19 +609,19 @@ impl Bolt11Payment {
let id = PaymentId(payment_hash.0);
let preimage =
self.channel_manager.get_payment_preimage(payment_hash, payment_secret.clone()).ok();
let payment = PaymentDetails {
let kind = PaymentKind::Bolt11Jit {
hash: payment_hash,
preimage,
secret: Some(payment_secret.clone()),
lsp_fee_limits,
};
let payment = PaymentDetails::new(
id,
kind: PaymentKind::Bolt11Jit {
hash: payment_hash,
preimage,
secret: Some(payment_secret.clone()),
lsp_fee_limits,
},
kind,
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
};

PaymentDirection::Inbound,
PaymentStatus::Pending,
);
self.payment_store.insert(payment)?;

// Persist LSP peer to make sure we reconnect on restart.
Expand Down
81 changes: 40 additions & 41 deletions src/payment/bolt12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ impl Bolt12Payment {
secret: None,
offer_id: offer.id(),
};
let payment = PaymentDetails {
id: payment_id,
let payment = PaymentDetails::new(
payment_id,
kind,
amount_msat: Some(*offer_amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
};
Some(*offer_amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Pending,
);
self.payment_store.insert(payment)?;

Ok(payment_id)
Expand All @@ -118,13 +118,13 @@ impl Bolt12Payment {
secret: None,
offer_id: offer.id(),
};
let payment = PaymentDetails {
id: payment_id,
let payment = PaymentDetails::new(
payment_id,
kind,
amount_msat: Some(*offer_amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
};
Some(*offer_amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Failed,
);
self.payment_store.insert(payment)?;
Err(Error::InvoiceRequestCreationFailed)
},
Expand Down Expand Up @@ -197,13 +197,13 @@ impl Bolt12Payment {
secret: None,
offer_id: offer.id(),
};
let payment = PaymentDetails {
id: payment_id,
let payment = PaymentDetails::new(
payment_id,
kind,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
};
Some(amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Pending,
);
self.payment_store.insert(payment)?;

Ok(payment_id)
Expand All @@ -219,13 +219,13 @@ impl Bolt12Payment {
secret: None,
offer_id: offer.id(),
};
let payment = PaymentDetails {
id: payment_id,
let payment = PaymentDetails::new(
payment_id,
kind,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
};
Some(amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Failed,
);
self.payment_store.insert(payment)?;
Err(Error::PaymentSendingFailed)
},
Expand Down Expand Up @@ -281,17 +281,16 @@ impl Bolt12Payment {
let payment_hash = invoice.payment_hash();
let payment_id = PaymentId(payment_hash.0);

let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Bolt12Refund {
hash: Some(payment_hash),
preimage: None,
secret: None,
},
amount_msat: Some(refund.amount_msats()),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
};
let kind =
PaymentKind::Bolt12Refund { hash: Some(payment_hash), preimage: None, secret: None };

let payment = PaymentDetails::new(
payment_id,
kind,
Some(refund.amount_msats()),
PaymentDirection::Inbound,
PaymentStatus::Pending,
);

self.payment_store.insert(payment)?;

Expand Down Expand Up @@ -333,13 +332,13 @@ impl Bolt12Payment {

let kind = PaymentKind::Bolt12Refund { hash: None, preimage: None, secret: None };

let payment = PaymentDetails {
id: payment_id,
let payment = PaymentDetails::new(
payment_id,
kind,
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
};
Some(amount_msat),
PaymentDirection::Outbound,
PaymentStatus::Pending,
);

self.payment_store.insert(payment)?;

Expand Down
Loading

0 comments on commit 32fb2a8

Please sign in to comment.