From 095fca9293536e97538b5c157452f7453b9fcc9c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 22 Sep 2023 01:44:57 +0000 Subject: [PATCH] Avoid ref to a `Vec` when accessing custom onion TLVs The bindings can't easily return a reference to a `Vec`, so we instead split the implementation into vec/slice depending on the `c_bindings` flag. --- lightning/src/ln/outbound_payment.rs | 16 ++++++++++++++++ lightning/src/ln/payment_tests.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index 1642f28e..b806b138 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -594,10 +594,26 @@ impl RecipientOnionFields { /// Note that if this field is non-empty, it will contain strictly increasing TLVs, each /// represented by a `(u64, Vec)` for its type number and serialized value respectively. /// This is validated when setting this field using [`Self::with_custom_tlvs`]. + #[cfg(not(c_bindings))] pub fn custom_tlvs(&self) -> &Vec<(u64, Vec)> { &self.custom_tlvs } + /// Gets the custom TLVs that will be sent or have been received. + /// + /// Custom TLVs allow sending extra application-specific data with a payment. They provide + /// additional flexibility on top of payment metadata, as while other implementations may + /// require `payment_metadata` to reflect metadata provided in an invoice, custom TLVs + /// do not have this restriction. + /// + /// Note that if this field is non-empty, it will contain strictly increasing TLVs, each + /// represented by a `(u64, Vec)` for its type number and serialized value respectively. + /// This is validated when setting this field using [`Self::with_custom_tlvs`]. + #[cfg(c_bindings)] + pub fn custom_tlvs(&self) -> Vec<(u64, Vec)> { + self.custom_tlvs.clone() + } + /// When we have received some HTLC(s) towards an MPP payment, as we receive further HTLC(s) we /// have to make sure that some fields match exactly across the parts. For those that aren't /// required to match, if they don't match we should remove them so as to not expose data diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index 72176760..26ecbb0b 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -3781,7 +3781,7 @@ fn test_retry_custom_tlvs() { payment_hash, Some(payment_secret), events.pop().unwrap(), true, None).unwrap(); match payment_claimable { Event::PaymentClaimable { onion_fields, .. } => { - assert_eq!(onion_fields.unwrap().custom_tlvs(), &custom_tlvs); + assert_eq!(&onion_fields.unwrap().custom_tlvs()[..], &custom_tlvs[..]); }, _ => panic!("Unexpected event"), }; -- 2.30.2