Skip to content

Commit

Permalink
fix: handled TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmitr committed Jun 13, 2024
1 parent ed911bb commit 9b8dea5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 33 deletions.
3 changes: 1 addition & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ interface PaymentKind {
Onchain();
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, string? bolt11_invoice);
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
// TODO: add custom_tlvs here as sequence<TlvEntry> custom_tlvs
Spontaneous(PaymentHash hash, PaymentPreimage? preimage);
Spontaneous(PaymentHash hash, PaymentPreimage? preimage, sequence<TlvEntry> custom_tlvs);
};

enum PaymentDirection {
Expand Down
86 changes: 59 additions & 27 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::types::{DynStore, Sweeper, Wallet};
use crate::{
hex_utils, BumpTransactionEventHandler, ChannelManager, Config, Error, Graph, PeerInfo,
PeerStore, UserChannelId,
PeerStore, TlvEntry, UserChannelId,
};

use crate::payment::store::{
Expand Down Expand Up @@ -553,17 +553,61 @@ where
panic!("Failed to access payment store");
});
}

if let PaymentPurpose::SpontaneousPayment(preimage) = purpose {
// TODO: fix unwrap to have proper error handling and log error
let custom_tlvs = onion_fields.unwrap().custom_tlvs();
let custom_tlvs = onion_fields
.map(|of| {
of.custom_tlvs()
.iter()
.map(|(t, v)| TlvEntry { r#type: *t, value: v.clone() })
.collect()
})
.unwrap_or_default();
log_info!(
self.logger,
"Saving spontaneous payment with custom TLVs for payment hash {} of {}msat",
// custom_tlvs, // TODO: log the custom TLVs
"Saving spontaneous payment with custom TLVs {:?} for payment hash {} of {}msat",
custom_tlvs,
hex_utils::to_string(&payment_hash.0),
amount_msat,
);
// TODO: create payment in pending status and save TLVs as a new field on spontaneous payment in PaymentKind::Spontaneous

let payment = PaymentDetails {
id: payment_id,
kind: PaymentKind::Spontaneous {
hash: payment_hash,
preimage: Some(preimage),
custom_tlvs,
},
amount_msat: Some(amount_msat),
direction: PaymentDirection::Inbound,
status: PaymentStatus::Pending,
last_update: time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap_or(time::Duration::ZERO)
.as_secs(),
fee_msat: None,
};

match self.payment_store.insert(payment) {
Ok(false) => (),
Ok(true) => {
log_error!(
self.logger,
"Spontaneous payment with hash {} was previously known",
hex_utils::to_string(&payment_hash.0)
);
debug_assert!(false);
},
Err(e) => {
log_error!(
self.logger,
"Failed to insert payment with hash {}: {}",
hex_utils::to_string(&payment_hash.0),
e
);
debug_assert!(false);
},
};
}
},
LdkEvent::PaymentClaimed {
Expand Down Expand Up @@ -634,38 +678,26 @@ where
return;
},
PaymentPurpose::SpontaneousPayment(preimage) => {
// TODO: change to updating the payment instead of creating it
// since it now has to be created when checking if the payment is claimable in order to store the custom tlvs
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::Succeeded,
last_update: time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap_or(time::Duration::ZERO)
.as_secs(),
fee_msat: None,
let update = PaymentDetailsUpdate {
preimage: Some(Some(preimage)),
status: Some(PaymentStatus::Succeeded),
..PaymentDetailsUpdate::new(payment_id)
};

match self.payment_store.insert(payment) {
Ok(false) => (),
Ok(true) => {
match self.payment_store.update(&update) {
Ok(true) => (),
Ok(false) => {
log_error!(
self.logger,
"Spontaneous payment with hash {} was previously known",
"Payment with hash {} couldn't be found in store",
hex_utils::to_string(&payment_hash.0)
);
debug_assert!(false);
},
Err(e) => {
log_error!(
self.logger,
"Failed to insert payment with hash {}: {}",
"Failed to update payment with hash {}: {}",
hex_utils::to_string(&payment_hash.0),
e
);
Expand Down
7 changes: 5 additions & 2 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ impl SpontaneousPayment {
amount_msat,
);
let recipient_fields = RecipientOnionFields::spontaneous_empty()
.with_custom_tlvs(custom_tlvs.into_iter().map(|tlv| (tlv.r#type, tlv.value)).collect())
.with_custom_tlvs(
custom_tlvs.iter().map(|tlv| (tlv.r#type, tlv.value.clone())).collect(),
)
.map_err(|_| {
log_error!(self.logger, "Payment error: invalid custom TLVs.");
Error::InvalidCustomTlv
Expand All @@ -89,7 +91,7 @@ impl SpontaneousPayment {
kind: PaymentKind::Spontaneous {
hash: payment_hash,
preimage: Some(payment_preimage),
// TODO: save custom TLVs here from recipient_fields.custom_tlvs()
custom_tlvs,
},
status: PaymentStatus::Pending,
direction: PaymentDirection::Outbound,
Expand All @@ -112,6 +114,7 @@ impl SpontaneousPayment {
kind: PaymentKind::Spontaneous {
hash: payment_hash,
preimage: Some(payment_preimage),
custom_tlvs,
},

status: PaymentStatus::Failed,
Expand Down
7 changes: 5 additions & 2 deletions src/payment/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::io::{
PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
};
use crate::logger::{log_error, Logger};
use crate::types::DynStore;
use crate::types::{DynStore, TlvEntry};
use crate::Error;

use lightning::ln::channelmanager::PaymentId;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Readable for PaymentDetails {
PaymentKind::Bolt11 { hash, preimage, secret, bolt11_invoice: None }
}
} else {
PaymentKind::Spontaneous { hash, preimage }
PaymentKind::Spontaneous { hash, preimage, custom_tlvs: Vec::new() }
}
};

Expand Down Expand Up @@ -194,6 +194,8 @@ pub enum PaymentKind {
hash: PaymentHash,
/// The pre-image used by the payment.
preimage: Option<PaymentPreimage>,
/// Custom TLVs.
custom_tlvs: Vec<TlvEntry>,
},
}

Expand All @@ -214,6 +216,7 @@ impl_writeable_tlv_based_enum!(PaymentKind,
(8, Spontaneous) => {
(0, hash, required),
(2, preimage, option),
(131072, custom_tlvs, optional_vec),
};
);

Expand Down
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::logger::FilesystemLogger;
use crate::message_handler::NodeCustomMessageHandler;

use lightning::chain::chainmonitor;
use lightning::impl_writeable_tlv_based;
use lightning::ln::channelmanager::ChannelDetails as LdkChannelDetails;
use lightning::ln::msgs::RoutingMessageHandler;
use lightning::ln::msgs::SocketAddress;
Expand Down Expand Up @@ -478,10 +479,16 @@ impl Default for ChannelConfig {
}

/// Custom TLV entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TlvEntry {
/// Type number.
pub r#type: u64,

/// Serialized value.
pub value: Vec<u8>,
}

impl_writeable_tlv_based!(TlvEntry, {
(0, r#type, required),
(1, value, required),
});

0 comments on commit 9b8dea5

Please sign in to comment.