Skip to content

Commit fa5732e

Browse files
committed
use optional_vec for Vec<CustomTlvRecord>
1 parent 9fb2ae3 commit fa5732e

File tree

8 files changed

+36
-79
lines changed

8 files changed

+36
-79
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ interface SpontaneousPayment {
152152
[Throws=NodeError]
153153
PaymentId send(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters);
154154
[Throws=NodeError]
155-
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<TlvEntry> custom_tlvs);
155+
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<CustomTlvRecord> custom_tlvs);
156156
[Throws=NodeError]
157157
void send_probes(u64 amount_msat, PublicKey node_id);
158158
};
@@ -278,8 +278,8 @@ enum VssHeaderProviderError {
278278
interface Event {
279279
PaymentSuccessful(PaymentId? payment_id, PaymentHash payment_hash, PaymentPreimage? payment_preimage, u64? fee_paid_msat);
280280
PaymentFailed(PaymentId? payment_id, PaymentHash? payment_hash, PaymentFailureReason? reason);
281-
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat, TlvEntries? custom_records);
282-
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline, TlvEntries? custom_records);
281+
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat, sequence<CustomTlvRecord>? custom_records);
282+
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline, sequence<CustomTlvRecord>? custom_records);
283283
PaymentForwarded(ChannelId prev_channel_id, ChannelId next_channel_id, UserChannelId? prev_user_channel_id, UserChannelId? next_user_channel_id, u64? total_fee_earned_msat, u64? skimmed_fee_msat, boolean claim_from_onchain_tx, u64? outbound_amount_forwarded_msat);
284284
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
285285
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
@@ -365,8 +365,8 @@ dictionary SendingParameters {
365365
u8? max_channel_saturation_power_of_half;
366366
};
367367

368-
dictionary TlvEntry {
369-
u64 type;
368+
dictionary CustomTlvRecord {
369+
u64 kind;
370370
sequence<u8> value;
371371
};
372372

@@ -622,6 +622,3 @@ typedef string UntrustedString;
622622

623623
[Custom]
624624
typedef string NodeAlias;
625-
626-
[Custom]
627-
typedef sequence<TlvEntry> TlvEntries;

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum Error {
3434
RefundCreationFailed,
3535
/// Sending a payment has failed.
3636
PaymentSendingFailed,
37-
/// Construction of spontaneous payment with custom TLVs failed.
37+
/// Sending of spontaneous payment with custom TLVs failed.
3838
InvalidCustomTlvs,
3939
/// Sending a payment probe has failed.
4040
ProbeSendingFailed,

src/event.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use crate::types::{DynStore, Sweeper, TlvEntries, Wallet};
8+
use crate::types::{CustomTlvRecord, DynStore, Sweeper, Wallet};
99

1010
use crate::{
1111
hex_utils, BumpTransactionEventHandler, ChannelManager, Config, Error, Graph, PeerInfo,
@@ -103,7 +103,7 @@ pub enum Event {
103103
/// The value, in thousandths of a satoshi, that has been received.
104104
amount_msat: u64,
105105
/// Custom TLV records received on the payment
106-
custom_records: Option<TlvEntries>,
106+
custom_records: Vec<CustomTlvRecord>,
107107
},
108108
/// A payment has been forwarded.
109109
PaymentForwarded {
@@ -171,7 +171,7 @@ pub enum Event {
171171
/// eligible for claiming.
172172
claim_deadline: Option<u32>,
173173
/// Custom TLV records attached to the payment
174-
custom_records: Option<TlvEntries>,
174+
custom_records: Vec<CustomTlvRecord>,
175175
},
176176
/// A channel has been created and is pending confirmation on-chain.
177177
ChannelPending {
@@ -228,7 +228,7 @@ impl_writeable_tlv_based_enum!(Event,
228228
(0, payment_hash, required),
229229
(1, payment_id, option),
230230
(2, amount_msat, required),
231-
(3, custom_records, option),
231+
(3, custom_records, optional_vec),
232232
},
233233
(3, ChannelReady) => {
234234
(0, channel_id, required),
@@ -253,7 +253,7 @@ impl_writeable_tlv_based_enum!(Event,
253253
(2, payment_id, required),
254254
(4, claimable_amount_msat, required),
255255
(6, claim_deadline, option),
256-
(8, custom_records, option),
256+
(8, custom_records, optional_vec),
257257
},
258258
(7, PaymentForwarded) => {
259259
(0, prev_channel_id, required),
@@ -650,19 +650,17 @@ where
650650
"We would have registered the preimage if we knew"
651651
);
652652

653+
let custom_records = onion_fields
654+
.map(|cf| {
655+
cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect()
656+
})
657+
.unwrap_or_default();
653658
let event = Event::PaymentClaimable {
654659
payment_id,
655660
payment_hash,
656661
claimable_amount_msat: amount_msat,
657662
claim_deadline,
658-
custom_records: onion_fields.map(|cf| {
659-
TlvEntries(
660-
cf.custom_tlvs()
661-
.into_iter()
662-
.map(|tlv| tlv.into())
663-
.collect(),
664-
)
665-
}),
663+
custom_records,
666664
};
667665
match self.event_queue.add_event(event) {
668666
Ok(_) => return Ok(()),
@@ -889,9 +887,9 @@ where
889887
payment_id: Some(payment_id),
890888
payment_hash,
891889
amount_msat,
892-
custom_records: onion_fields.map(|cf| {
893-
TlvEntries(cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
894-
}),
890+
custom_records: onion_fields
891+
.map(|cf| cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
892+
.unwrap_or_default(),
895893
};
896894
match self.event_queue.add_event(event) {
897895
Ok(_) => return Ok(()),

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ use types::{
140140
Broadcaster, BumpTransactionEventHandler, ChainMonitor, ChannelManager, DynStore, Graph,
141141
KeysManager, OnionMessenger, PeerManager, Router, Scorer, Sweeper, Wallet,
142142
};
143-
pub use types::{ChannelDetails, PeerDetails, TlvEntry, UserChannelId};
143+
pub use types::{ChannelDetails, PeerDetails, CustomTlvRecord, UserChannelId};
144144

145145
use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};
146146

src/payment/spontaneous.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::payment::store::{
1414
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
1515
};
1616
use crate::payment::SendingParameters;
17-
use crate::types::{ChannelManager, KeysManager, TlvEntry};
17+
use crate::types::{ChannelManager, CustomTlvRecord, KeysManager};
1818

1919
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry, RetryableSendFailure};
2020
use lightning::ln::{PaymentHash, PaymentPreimage};
@@ -65,14 +65,14 @@ impl SpontaneousPayment {
6565
/// Send a spontaneous payment including a list of custom TLVs.
6666
pub fn send_with_custom_tlvs(
6767
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
68-
custom_tlvs: Vec<TlvEntry>,
68+
custom_tlvs: Vec<CustomTlvRecord>,
6969
) -> Result<PaymentId, Error> {
7070
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs))
7171
}
7272

7373
fn send_inner(
7474
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
75-
custom_tlvs: Option<Vec<TlvEntry>>,
75+
custom_tlvs: Option<Vec<CustomTlvRecord>>,
7676
) -> Result<PaymentId, Error> {
7777
let rt_lock = self.runtime.read().unwrap();
7878
if rt_lock.is_none() {
@@ -114,7 +114,7 @@ impl SpontaneousPayment {
114114

115115
let recipient_fields = match custom_tlvs {
116116
Some(tlvs) => RecipientOnionFields::spontaneous_empty()
117-
.with_custom_tlvs(tlvs.into_iter().map(|tlv| (tlv.r#type, tlv.value)).collect())
117+
.with_custom_tlvs(tlvs.into_iter().map(|tlv| (tlv.kind, tlv.value)).collect())
118118
.map_err(|e| {
119119
log_error!(self.logger, "Failed to send payment with custom TLVs: {:?}", e);
120120
Error::InvalidCustomTlvs

src/types.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -350,49 +350,22 @@ pub struct PeerDetails {
350350
pub is_connected: bool,
351351
}
352352

353-
/// List of Custom TLV entries.
354-
#[derive(Debug, Clone, PartialEq, Eq)]
355-
pub struct TlvEntries(pub Vec<TlvEntry>);
356-
357353
/// Custom TLV entry.
358354
#[derive(Debug, Clone, PartialEq, Eq)]
359-
pub struct TlvEntry {
355+
pub struct CustomTlvRecord {
360356
/// Type number.
361-
pub r#type: u64,
357+
pub kind: u64,
362358
/// Serialized value.
363359
pub value: Vec<u8>,
364360
}
365361

366-
impl_writeable_tlv_based!(TlvEntry, {
367-
(0, r#type, required),
362+
impl_writeable_tlv_based!(CustomTlvRecord, {
363+
(0, kind, required),
368364
(1, value, required),
369365
});
370366

371-
impl From<&(u64, Vec<u8>)> for TlvEntry {
367+
impl From<&(u64, Vec<u8>)> for CustomTlvRecord {
372368
fn from(tlv: &(u64, Vec<u8>)) -> Self {
373-
TlvEntry { r#type: tlv.0, value: tlv.1.clone() }
374-
}
375-
}
376-
377-
impl Readable for TlvEntries {
378-
fn read<R: lightning::io::Read>(
379-
reader: &mut R,
380-
) -> Result<Self, lightning::ln::msgs::DecodeError> {
381-
let len: u16 = Readable::read(reader)?;
382-
let mut queue = Vec::with_capacity(len as usize);
383-
for _ in 0..len {
384-
queue.push(Readable::read(reader)?);
385-
}
386-
Ok(Self(queue))
387-
}
388-
}
389-
390-
impl Writeable for TlvEntries {
391-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
392-
(self.0.len() as u16).write(writer)?;
393-
for e in self.0.iter() {
394-
e.write(writer)?;
395-
}
396-
Ok(())
369+
CustomTlvRecord { kind: tlv.0, value: tlv.1.clone() }
397370
}
398371
}

src/uniffi_types.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use crate::config::{
1616
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
1717
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
1818
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
19-
pub use crate::types::{TlvEntries, TlvEntry};
19+
pub use crate::types::CustomTlvRecord;
2020

2121
pub use lightning::chain::channelmonitor::BalanceSource;
2222
pub use lightning::events::{ClosureReason, PaymentFailureReason};
@@ -344,15 +344,3 @@ impl UniffiCustomTypeConverter for NodeAlias {
344344
obj.to_string()
345345
}
346346
}
347-
348-
impl UniffiCustomTypeConverter for TlvEntries {
349-
type Builtin = Vec<TlvEntry>;
350-
351-
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
352-
Ok(TlvEntries(val))
353-
}
354-
355-
fn from_custom(obj: Self) -> Self::Builtin {
356-
obj.0
357-
}
358-
}

tests/common/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use ldk_node::config::{Config, EsploraSyncConfig};
1212
use ldk_node::io::sqlite_store::SqliteStore;
1313
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
1414
use ldk_node::{
15-
Builder, Event, LightningBalance, LogLevel, Node, NodeError, PendingSweepBalance, TlvEntry,
15+
Builder, CustomTlvRecord, Event, LightningBalance, LogLevel, Node, NodeError,
16+
PendingSweepBalance,
1617
};
1718

1819
use lightning::ln::msgs::SocketAddress;
@@ -753,7 +754,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
753754
// Test spontaneous/keysend payments
754755
println!("\nA send_spontaneous_payment");
755756
let keysend_amount_msat = 2500_000;
756-
let custom_tlvs = vec![TlvEntry { r#type: 13377331, value: vec![1, 2, 3] }];
757+
let custom_tlvs = vec![CustomTlvRecord { kind: 13377331, value: vec![1, 2, 3] }];
757758
let keysend_payment_id = node_a
758759
.spontaneous_payment()
759760
.send_with_custom_tlvs(keysend_amount_msat, node_b.node_id(), None, custom_tlvs.clone())
@@ -778,7 +779,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
778779
node_a.payment(&keysend_payment_id).unwrap().kind,
779780
PaymentKind::Spontaneous { .. }
780781
));
781-
assert_eq!(received_custom_records.as_ref().unwrap().0, custom_tlvs);
782+
assert_eq!(received_custom_records, &custom_tlvs);
782783
assert_eq!(node_b.payment(&keysend_payment_id).unwrap().status, PaymentStatus::Succeeded);
783784
assert_eq!(node_b.payment(&keysend_payment_id).unwrap().direction, PaymentDirection::Inbound);
784785
assert_eq!(node_b.payment(&keysend_payment_id).unwrap().amount_msat, Some(keysend_amount_msat));

0 commit comments

Comments
 (0)