Skip to content

Commit af915cf

Browse files
committed
ln: box InvoiceContents in {Bolt12,Static}Invoice to reduce sizeof Event
Reduces `mem::size_of::<Event>()` from 1680 B -> 1072 B
1 parent 7b45811 commit af915cf

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

lightning/src/offers/invoice.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice", "signatu
184184
/// [module-level documentation]: self
185185
pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
186186
invreq_bytes: &'a Vec<u8>,
187-
invoice: InvoiceContents,
187+
invoice: Box<InvoiceContents>,
188188
signing_pubkey_strategy: S,
189189
}
190190

@@ -200,7 +200,7 @@ pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
200200
#[cfg(c_bindings)]
201201
pub struct InvoiceWithExplicitSigningPubkeyBuilder<'a> {
202202
invreq_bytes: &'a Vec<u8>,
203-
invoice: InvoiceContents,
203+
invoice: Box<InvoiceContents>,
204204
signing_pubkey_strategy: ExplicitSigningPubkey,
205205
}
206206

@@ -216,7 +216,7 @@ pub struct InvoiceWithExplicitSigningPubkeyBuilder<'a> {
216216
#[cfg(c_bindings)]
217217
pub struct InvoiceWithDerivedSigningPubkeyBuilder<'a> {
218218
invreq_bytes: &'a Vec<u8>,
219-
invoice: InvoiceContents,
219+
invoice: Box<InvoiceContents>,
220220
signing_pubkey_strategy: DerivedSigningPubkey,
221221
}
222222

@@ -246,7 +246,7 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
246246
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey,
247247
) -> Result<Self, Bolt12SemanticError> {
248248
let amount_msats = Self::amount_msats(invoice_request)?;
249-
let contents = InvoiceContents::ForOffer {
249+
let contents = Box::new(InvoiceContents::ForOffer {
250250
invoice_request: invoice_request.contents.clone(),
251251
fields: Self::fields(
252252
payment_paths,
@@ -255,7 +255,7 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
255255
amount_msats,
256256
signing_pubkey,
257257
),
258-
};
258+
});
259259

260260
Self::new(&invoice_request.bytes, contents, ExplicitSigningPubkey {})
261261
}
@@ -266,7 +266,7 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
266266
payment_hash: PaymentHash, signing_pubkey: PublicKey,
267267
) -> Result<Self, Bolt12SemanticError> {
268268
let amount_msats = refund.amount_msats();
269-
let contents = InvoiceContents::ForRefund {
269+
let contents = Box::new(InvoiceContents::ForRefund {
270270
refund: refund.contents.clone(),
271271
fields: Self::fields(
272272
payment_paths,
@@ -275,7 +275,7 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
275275
amount_msats,
276276
signing_pubkey,
277277
),
278-
};
278+
});
279279

280280
Self::new(&refund.bytes, contents, ExplicitSigningPubkey {})
281281
}
@@ -319,7 +319,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
319319
) -> Result<Self, Bolt12SemanticError> {
320320
let amount_msats = Self::amount_msats(invoice_request)?;
321321
let signing_pubkey = keys.public_key();
322-
let contents = InvoiceContents::ForOffer {
322+
let contents = Box::new(InvoiceContents::ForOffer {
323323
invoice_request: invoice_request.contents.clone(),
324324
fields: Self::fields(
325325
payment_paths,
@@ -328,7 +328,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
328328
amount_msats,
329329
signing_pubkey,
330330
),
331-
};
331+
});
332332

333333
Self::new(&invoice_request.bytes, contents, DerivedSigningPubkey(keys))
334334
}
@@ -340,7 +340,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
340340
) -> Result<Self, Bolt12SemanticError> {
341341
let amount_msats = refund.amount_msats();
342342
let signing_pubkey = keys.public_key();
343-
let contents = InvoiceContents::ForRefund {
343+
let contents = Box::new(InvoiceContents::ForRefund {
344344
refund: refund.contents.clone(),
345345
fields: Self::fields(
346346
payment_paths,
@@ -349,7 +349,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
349349
amount_msats,
350350
signing_pubkey,
351351
),
352-
};
352+
});
353353

354354
Self::new(&refund.bytes, contents, DerivedSigningPubkey(keys))
355355
}
@@ -429,7 +429,7 @@ macro_rules! invoice_builder_methods {
429429

430430
#[cfg_attr(c_bindings, allow(dead_code))]
431431
fn new(
432-
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents,
432+
invreq_bytes: &'a Vec<u8>, contents: Box<InvoiceContents>,
433433
signing_pubkey_strategy: $type_param,
434434
) -> Result<Self, Bolt12SemanticError> {
435435
if contents.fields().payment_paths.is_empty() {
@@ -593,7 +593,7 @@ impl<'a> From<InvoiceWithDerivedSigningPubkeyBuilder<'a>>
593593
pub struct UnsignedBolt12Invoice {
594594
bytes: Vec<u8>,
595595
experimental_bytes: Vec<u8>,
596-
contents: InvoiceContents,
596+
contents: Box<InvoiceContents>,
597597
tagged_hash: TaggedHash,
598598
}
599599

@@ -622,7 +622,7 @@ where
622622
}
623623

624624
impl UnsignedBolt12Invoice {
625-
fn new(invreq_bytes: &[u8], contents: InvoiceContents) -> Self {
625+
fn new(invreq_bytes: &[u8], contents: Box<InvoiceContents>) -> Self {
626626
// TLV record ranges applicable to invreq_bytes.
627627
const NON_EXPERIMENTAL_TYPES: core::ops::Range<u64> = 0..INVOICE_REQUEST_TYPES.end;
628628
const EXPERIMENTAL_TYPES: core::ops::Range<u64> =
@@ -731,7 +731,7 @@ impl AsRef<TaggedHash> for UnsignedBolt12Invoice {
731731
#[derive(Clone, Debug)]
732732
pub struct Bolt12Invoice {
733733
bytes: Vec<u8>,
734-
contents: InvoiceContents,
734+
contents: Box<InvoiceContents>,
735735
signature: Signature,
736736
tagged_hash: TaggedHash,
737737
}
@@ -974,7 +974,7 @@ impl Bolt12Invoice {
974974
pub fn verify_using_metadata<T: secp256k1::Signing>(
975975
&self, key: &ExpandedKey, secp_ctx: &Secp256k1<T>,
976976
) -> Result<PaymentId, ()> {
977-
let (metadata, iv_bytes) = match &self.contents {
977+
let (metadata, iv_bytes) = match &*self.contents {
978978
InvoiceContents::ForOffer { invoice_request, .. } => {
979979
(&invoice_request.inner.payer.0, INVOICE_REQUEST_IV_BYTES)
980980
},
@@ -992,7 +992,7 @@ impl Bolt12Invoice {
992992
&self, payment_id: PaymentId, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>,
993993
) -> Result<PaymentId, ()> {
994994
let metadata = Metadata::payer_data(payment_id, nonce, key);
995-
let iv_bytes = match &self.contents {
995+
let iv_bytes = match &*self.contents {
996996
InvoiceContents::ForOffer { .. } => INVOICE_REQUEST_IV_BYTES,
997997
InvoiceContents::ForRefund { .. } => REFUND_IV_BYTES_WITHOUT_METADATA,
998998
};
@@ -1027,7 +1027,7 @@ impl Bolt12Invoice {
10271027
}
10281028

10291029
pub(crate) fn is_for_refund_without_paths(&self) -> bool {
1030-
match self.contents {
1030+
match &*self.contents {
10311031
InvoiceContents::ForOffer { .. } => false,
10321032
InvoiceContents::ForRefund { .. } => self.message_paths().is_empty(),
10331033
}
@@ -1422,7 +1422,7 @@ impl TryFrom<Vec<u8>> for UnsignedBolt12Invoice {
14221422
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
14231423
let invoice = ParsedMessage::<PartialInvoiceTlvStream>::try_from(bytes)?;
14241424
let ParsedMessage { mut bytes, tlv_stream } = invoice;
1425-
let contents = InvoiceContents::try_from(tlv_stream)?;
1425+
let contents = Box::new(InvoiceContents::try_from(tlv_stream)?);
14261426

14271427
let tagged_hash = TaggedHash::from_valid_tlv_stream_bytes(SIGNATURE_TAG, &bytes);
14281428

@@ -1606,15 +1606,15 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
16061606
experimental_invoice_request_tlv_stream,
16071607
experimental_invoice_tlv_stream,
16081608
) = tlv_stream;
1609-
let contents = InvoiceContents::try_from((
1609+
let contents = Box::new(InvoiceContents::try_from((
16101610
payer_tlv_stream,
16111611
offer_tlv_stream,
16121612
invoice_request_tlv_stream,
16131613
invoice_tlv_stream,
16141614
experimental_offer_tlv_stream,
16151615
experimental_invoice_request_tlv_stream,
16161616
experimental_invoice_tlv_stream,
1617-
))?;
1617+
))?);
16181618

16191619
let signature = signature
16201620
.ok_or(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature))?;

lightning/src/offers/static_invoice.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub const SIGNATURE_TAG: &'static str = concat!("lightning", "static_invoice", "
6868
#[derive(Clone, Debug)]
6969
pub struct StaticInvoice {
7070
bytes: Vec<u8>,
71-
contents: InvoiceContents,
71+
contents: Box<InvoiceContents>,
7272
signature: Signature,
7373
}
7474

@@ -104,7 +104,7 @@ struct InvoiceContents {
104104
// TODO: add module-level docs and link here
105105
pub struct StaticInvoiceBuilder<'a> {
106106
offer_bytes: &'a Vec<u8>,
107-
invoice: InvoiceContents,
107+
invoice: Box<InvoiceContents>,
108108
keys: Keypair,
109109
}
110110

@@ -141,7 +141,7 @@ impl<'a> StaticInvoiceBuilder<'a> {
141141
}
142142

143143
let invoice =
144-
InvoiceContents::new(offer, payment_paths, message_paths, created_at, signing_pubkey);
144+
Box::new(InvoiceContents::new(offer, payment_paths, message_paths, created_at, signing_pubkey));
145145

146146
Ok(Self { offer_bytes: &offer.bytes, invoice, keys })
147147
}
@@ -190,7 +190,7 @@ impl<'a> StaticInvoiceBuilder<'a> {
190190
pub struct UnsignedStaticInvoice {
191191
bytes: Vec<u8>,
192192
experimental_bytes: Vec<u8>,
193-
contents: InvoiceContents,
193+
contents: Box<InvoiceContents>,
194194
tagged_hash: TaggedHash,
195195
}
196196

@@ -294,7 +294,7 @@ macro_rules! invoice_accessors_signing_pubkey {
294294
} }
295295

296296
impl UnsignedStaticInvoice {
297-
fn new(offer_bytes: &Vec<u8>, contents: InvoiceContents) -> Self {
297+
fn new(offer_bytes: &Vec<u8>, contents: Box<InvoiceContents>) -> Self {
298298
let (_, invoice_tlv_stream, _, experimental_invoice_tlv_stream) = contents.as_tlv_stream();
299299

300300
const INVOICE_ALLOCATION_SIZE: usize = 1024;
@@ -602,12 +602,12 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for StaticInvoice {
602602
experimental_offer_tlv_stream,
603603
experimental_invoice_tlv_stream,
604604
) = tlv_stream;
605-
let contents = InvoiceContents::try_from((
605+
let contents = Box::new(InvoiceContents::try_from((
606606
offer_tlv_stream,
607607
invoice_tlv_stream,
608608
experimental_offer_tlv_stream,
609609
experimental_invoice_tlv_stream,
610-
))?;
610+
))?);
611611

612612
let signature = match signature {
613613
None => {

0 commit comments

Comments
 (0)