forked from lightningdevkit/ldk-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpending_payment_store.rs
More file actions
93 lines (77 loc) · 2.6 KB
/
pending_payment_store.rs
File metadata and controls
93 lines (77 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use bitcoin::Txid;
use lightning::impl_writeable_tlv_based;
use lightning::ln::channelmanager::PaymentId;
use crate::data_store::{StorableObject, StorableObjectUpdate};
use crate::payment::store::PaymentDetailsUpdate;
use crate::payment::PaymentDetails;
/// Represents a pending payment
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingPaymentDetails {
/// The full payment details
pub details: PaymentDetails,
/// Transaction IDs that have replaced or conflict with this payment.
pub conflicting_txids: Vec<Txid>,
}
impl PendingPaymentDetails {
pub(crate) fn new(details: PaymentDetails, conflicting_txids: Vec<Txid>) -> Self {
Self { details, conflicting_txids }
}
/// Convert to finalized payment for the main payment store
pub fn into_payment_details(self) -> PaymentDetails {
self.details
}
}
impl_writeable_tlv_based!(PendingPaymentDetails, {
(0, details, required),
(2, conflicting_txids, optional_vec),
});
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PendingPaymentDetailsUpdate {
pub id: PaymentId,
pub payment_update: Option<PaymentDetailsUpdate>,
pub conflicting_txids: Option<Vec<Txid>>,
}
impl StorableObject for PendingPaymentDetails {
type Id = PaymentId;
type Update = PendingPaymentDetailsUpdate;
fn id(&self) -> Self::Id {
self.details.id
}
fn update(&mut self, update: Self::Update) -> bool {
let mut updated = false;
// Update the underlying payment details if present
if let Some(payment_update) = update.payment_update {
updated |= self.details.update(payment_update);
}
if let Some(new_conflicting_txids) = update.conflicting_txids {
if self.conflicting_txids != new_conflicting_txids {
self.conflicting_txids = new_conflicting_txids;
updated = true;
}
}
updated
}
fn to_update(&self) -> Self::Update {
self.into()
}
}
impl StorableObjectUpdate<PendingPaymentDetails> for PendingPaymentDetailsUpdate {
fn id(&self) -> <PendingPaymentDetails as StorableObject>::Id {
self.id
}
}
impl From<&PendingPaymentDetails> for PendingPaymentDetailsUpdate {
fn from(value: &PendingPaymentDetails) -> Self {
Self {
id: value.id(),
payment_update: Some(value.details.to_update()),
conflicting_txids: Some(value.conflicting_txids.clone()),
}
}
}