Skip to content

Commit

Permalink
Merge pull request #33 from getAlby/payment-created-at
Browse files Browse the repository at this point in the history
feat: store created_at timestamp for payments
  • Loading branch information
rolznz authored Jun 23, 2024
2 parents 27a7d2f + ac2ffa0 commit 2c5998a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ dictionary PaymentDetails {
PaymentStatus status;
u64 last_update;
u64? fee_msat;
u64 created_at;
};

// [NonExhaustive]
Expand Down
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ where
.unwrap_or(time::Duration::ZERO)
.as_secs(),
fee_msat: None,
created_at: 0,
};

match self.payment_store.insert(payment) {
Expand Down
6 changes: 6 additions & 0 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Bolt11Payment {
status: PaymentStatus::Pending,
last_update: 0,
fee_msat: None,
created_at: 0,
};

self.payment_store.insert(payment)?;
Expand All @@ -139,6 +140,7 @@ impl Bolt11Payment {
status: PaymentStatus::Failed,
last_update: 0,
fee_msat: None,
created_at: 0,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -232,6 +234,7 @@ impl Bolt11Payment {
status: PaymentStatus::Pending,
last_update: 0,
fee_msat: None,
created_at: 0,
};
self.payment_store.insert(payment)?;

Expand All @@ -256,6 +259,7 @@ impl Bolt11Payment {
status: PaymentStatus::Failed,
last_update: 0,
fee_msat: None,
created_at: 0,
};
self.payment_store.insert(payment)?;

Expand Down Expand Up @@ -323,6 +327,7 @@ impl Bolt11Payment {
status: PaymentStatus::Pending,
last_update: 0,
fee_msat: None,
created_at: 0,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -455,6 +460,7 @@ impl Bolt11Payment {
status: PaymentStatus::Pending,
last_update: 0,
fee_msat: None,
created_at: 0,
};

self.payment_store.insert(payment)?;
Expand Down
2 changes: 2 additions & 0 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl SpontaneousPayment {
amount_msat: Some(amount_msat),
last_update: 0,
fee_msat: None,
created_at: 0,
};
self.payment_store.insert(payment)?;

Expand All @@ -122,6 +123,7 @@ impl SpontaneousPayment {
amount_msat: Some(amount_msat),
last_update: 0,
fee_msat: None,
created_at: 0,
};

self.payment_store.insert(payment)?;
Expand Down
30 changes: 29 additions & 1 deletion src/payment/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct PaymentDetails {
pub last_update: u64,
/// Fee paid.
pub fee_msat: Option<u64>,
/// Payment creation timestamp, as seconds since Unix epoch.
pub created_at: u64,
}

impl Writeable for PaymentDetails {
Expand All @@ -57,6 +59,7 @@ impl Writeable for PaymentDetails {
(10, self.status, required),
(131074, Some(self.last_update), option),
(131076, self.fee_msat, option),
(131078, Some(self.created_at), option),
});
Ok(())
}
Expand All @@ -75,6 +78,7 @@ impl Readable for PaymentDetails {
(10, status, required),
(131074, last_update, option),
(131076, fee_msat, option),
(131078, created_at, option),
});

let id: PaymentId = id.0.ok_or(DecodeError::InvalidValue)?;
Expand All @@ -84,6 +88,7 @@ impl Readable for PaymentDetails {
let direction: PaymentDirection = direction.0.ok_or(DecodeError::InvalidValue)?;
let status: PaymentStatus = status.0.ok_or(DecodeError::InvalidValue)?;
let last_update: u64 = last_update.unwrap_or(0);
let created_at: u64 = created_at.unwrap_or(0);

let kind = if let Some(kind) = kind_opt {
// If we serialized the payment kind, use it.
Expand Down Expand Up @@ -112,7 +117,16 @@ impl Readable for PaymentDetails {
}
};

Ok(PaymentDetails { id, kind, amount_msat, direction, status, last_update, fee_msat })
Ok(PaymentDetails {
id,
kind,
amount_msat,
direction,
status,
last_update,
fee_msat,
created_at,
})
}
}

Expand Down Expand Up @@ -288,6 +302,19 @@ where
pub(crate) fn insert(&self, payment: PaymentDetails) -> Result<bool, Error> {
let mut locked_payments = self.payments.lock().unwrap();

// If the payment already exists, reuse its timestamp instead of overwriting it.
let created_at = locked_payments.get(&payment.id).map_or_else(
|| {
time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap_or(time::Duration::ZERO)
.as_secs()
},
|p| p.created_at,
);

let payment = PaymentDetails { created_at, ..payment };

let updated = locked_payments.insert(payment.id, payment.clone()).is_some();
self.persist_info(&payment.id, &payment)?;
Ok(updated)
Expand Down Expand Up @@ -464,6 +491,7 @@ mod tests {
status: PaymentStatus::Pending,
last_update: 0,
fee_msat: None,
created_at: 0,
};

assert_eq!(Ok(false), payment_store.insert(payment.clone()));
Expand Down

0 comments on commit 2c5998a

Please sign in to comment.