Skip to content

Commit

Permalink
feat: re-add last_update
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Mar 16, 2024
1 parent 695fe63 commit d25dcb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ where
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Succeeded,
last_update: 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 {
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
last_update: 0,
};
self.payment_store.insert(payment)?;

Expand All @@ -137,6 +138,7 @@ impl Bolt11Payment {
amount_msat: invoice.amount_milli_satoshis(),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
last_update: 0,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -230,6 +232,7 @@ impl Bolt11Payment {
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Pending,
last_update: 0,
};
self.payment_store.insert(payment)?;

Expand All @@ -254,6 +257,7 @@ impl Bolt11Payment {
amount_msat: Some(amount_msat),
direction: PaymentDirection::Outbound,
status: PaymentStatus::Failed,
last_update: 0,
};
self.payment_store.insert(payment)?;

Expand Down Expand Up @@ -320,6 +324,7 @@ impl Bolt11Payment {
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
last_update: 0,
};

self.payment_store.insert(payment)?;
Expand Down Expand Up @@ -451,6 +456,7 @@ impl Bolt11Payment {
amount_msat,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
last_update: 0,
};

self.payment_store.insert(payment)?;
Expand Down
14 changes: 13 additions & 1 deletion src/payment/payment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::collections::HashMap;
use std::iter::FromIterator;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::time;

/// Represents a payment.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -33,6 +34,8 @@ pub struct PaymentDetails {
pub direction: PaymentDirection,
/// The status of the payment.
pub status: PaymentStatus,
/// Last update timestamp, as seconds since Unix epoch.
pub last_update: u64,
}

impl Writeable for PaymentDetails {
Expand Down Expand Up @@ -67,6 +70,7 @@ impl Readable for PaymentDetails {
(8, direction, required),
(10, status, required),
(131072, bolt11_invoice, option),
(131074, last_update, required),
});

let id: PaymentId = id.0.ok_or(DecodeError::InvalidValue)?;
Expand All @@ -75,6 +79,7 @@ impl Readable for PaymentDetails {
let amount_msat: Option<u64> = amount_msat.0.ok_or(DecodeError::InvalidValue)?;
let direction: PaymentDirection = direction.0.ok_or(DecodeError::InvalidValue)?;
let status: PaymentStatus = status.0.ok_or(DecodeError::InvalidValue)?;
let last_update: u64 = last_update.0.ok_or(DecodeError::InvalidValue)?;

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

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

Expand Down Expand Up @@ -334,6 +339,11 @@ where
payment.status = status;
}

payment.last_update = time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap_or(time::Duration::ZERO)
.as_secs();

self.persist_info(&update.id, payment)?;
updated = true;
}
Expand Down Expand Up @@ -440,6 +450,7 @@ mod tests {
amount_msat: None,
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
last_update: 0,
};

assert_eq!(Ok(false), payment_store.insert(payment.clone()));
Expand Down Expand Up @@ -602,3 +613,4 @@ mod tests {
}
}
}

2 changes: 2 additions & 0 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl SpontaneousPayment {
status: PaymentStatus::Pending,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
last_update: 0,
};
self.payment_store.insert(payment)?;

Expand All @@ -117,6 +118,7 @@ impl SpontaneousPayment {
status: PaymentStatus::Failed,
direction: PaymentDirection::Outbound,
amount_msat: Some(amount_msat),
last_update: 0,
};

self.payment_store.insert(payment)?;
Expand Down

0 comments on commit d25dcb1

Please sign in to comment.