Skip to content

Commit 9136597

Browse files
committed
Return owned Strings for onion message message types
Returning a reference from a trait method is relatively difficult to map in bindings and is currently handled by storing the object in the trait instance, returning a reference to the local field. This is fine when the object we're returning only needs to live as long as the trait, but when it needs to be `'static` (as is the case for onion message `msg_type`s), there's not really a good way to map them at all. Instead, here, condition on `#[cfg(c_bindings)]` we return a fully owned `String`. This is obviously relatively less effecient, but the extra allocation and `memcpy` isn't the end of the world, especially given it should be released relatively quickly.
1 parent ff0874a commit 9136597

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ impl CustomOnionMessageHandler for IgnoringMessageHandler {
170170

171171
impl OnionMessageContents for Infallible {
172172
fn tlv_type(&self) -> u64 { unreachable!(); }
173+
#[cfg(c_bindings)]
174+
fn msg_type(&self) -> String { unreachable!(); }
175+
#[cfg(not(c_bindings))]
173176
fn msg_type(&self) -> &'static str { unreachable!(); }
174177
}
175178

lightning/src/onion_message/async_payments.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ impl OnionMessageContents for ReleaseHeldHtlc {
7777
fn tlv_type(&self) -> u64 {
7878
RELEASE_HELD_HTLC_TLV_TYPE
7979
}
80+
#[cfg(c_bindings)]
81+
fn msg_type(&self) -> String {
82+
"Release Held HTLC".to_string()
83+
}
84+
#[cfg(not(c_bindings))]
8085
fn msg_type(&self) -> &'static str {
8186
"Release Held HTLC"
8287
}
@@ -107,6 +112,14 @@ impl OnionMessageContents for AsyncPaymentsMessage {
107112
Self::ReleaseHeldHtlc(msg) => msg.tlv_type(),
108113
}
109114
}
115+
#[cfg(c_bindings)]
116+
fn msg_type(&self) -> String {
117+
match &self {
118+
Self::HeldHtlcAvailable(_) => "Held HTLC Available".to_string(),
119+
Self::ReleaseHeldHtlc(msg) => msg.msg_type(),
120+
}
121+
}
122+
#[cfg(not(c_bindings))]
110123
fn msg_type(&self) -> &'static str {
111124
match &self {
112125
Self::HeldHtlcAvailable(_) => "Held HTLC Available",

lightning/src/onion_message/offers.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ impl OffersMessage {
9999
_ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)),
100100
}
101101
}
102+
103+
fn get_msg_type(&self) -> &'static str {
104+
match &self {
105+
OffersMessage::InvoiceRequest(_) => "Invoice Request",
106+
OffersMessage::Invoice(_) => "Invoice",
107+
#[cfg(async_payments)]
108+
OffersMessage::StaticInvoice(_) => "Static Invoice",
109+
OffersMessage::InvoiceError(_) => "Invoice Error",
110+
}
111+
}
102112
}
103113

104114
impl fmt::Debug for OffersMessage {
@@ -131,14 +141,13 @@ impl OnionMessageContents for OffersMessage {
131141
OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
132142
}
133143
}
144+
#[cfg(c_bindings)]
145+
fn msg_type(&self) -> String {
146+
self.get_msg_type().to_string()
147+
}
148+
#[cfg(not(c_bindings))]
134149
fn msg_type(&self) -> &'static str {
135-
match &self {
136-
OffersMessage::InvoiceRequest(_) => "Invoice Request",
137-
OffersMessage::Invoice(_) => "Invoice",
138-
#[cfg(async_payments)]
139-
OffersMessage::StaticInvoice(_) => "Static Invoice",
140-
OffersMessage::InvoiceError(_) => "Invoice Error",
141-
}
150+
self.get_msg_type()
142151
}
143152
}
144153

lightning/src/onion_message/packet.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ impl<T: OnionMessageContents> OnionMessageContents for ParsedOnionMessageContent
148148
&ParsedOnionMessageContents::Custom(ref msg) => msg.tlv_type(),
149149
}
150150
}
151+
#[cfg(c_bindings)]
152+
fn msg_type(&self) -> String {
153+
match self {
154+
ParsedOnionMessageContents::Offers(ref msg) => msg.msg_type(),
155+
#[cfg(async_payments)]
156+
ParsedOnionMessageContents::AsyncPayments(ref msg) => msg.msg_type(),
157+
ParsedOnionMessageContents::Custom(ref msg) => msg.msg_type(),
158+
}
159+
}
160+
#[cfg(not(c_bindings))]
151161
fn msg_type(&self) -> &'static str {
152162
match self {
153163
ParsedOnionMessageContents::Offers(ref msg) => msg.msg_type(),
@@ -174,6 +184,11 @@ pub trait OnionMessageContents: Writeable + core::fmt::Debug {
174184
/// Returns the TLV type identifying the message contents. MUST be >= 64.
175185
fn tlv_type(&self) -> u64;
176186

187+
#[cfg(c_bindings)]
188+
/// Returns the message type
189+
fn msg_type(&self) -> String;
190+
191+
#[cfg(not(c_bindings))]
177192
/// Returns the message type
178193
fn msg_type(&self) -> &'static str;
179194
}

0 commit comments

Comments
 (0)