Skip to content

Commit 4e770af

Browse files
committed
offers: box *Contents. reduce size_of Event 1856 -> 1072
Prep for inline storage for `description`, `issuer`, `payer_note` * invoice: box `InvoiceContents` reduces `mem::size_of::<Event>()` : 1856 -> 1072 * invoice_request: box contents * offer: box `OfferContents` * refund: box `RefundContents` * static invoice: box `InvoiceContents`
1 parent 18eb30b commit 4e770af

File tree

5 files changed

+73
-70
lines changed

5 files changed

+73
-70
lines changed

lightning/src/offers/invoice.rs

Lines changed: 25 additions & 25 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,16 +246,16 @@ 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 {
250-
invoice_request: invoice_request.contents.clone(),
249+
let contents = Box::new(InvoiceContents::ForOffer {
250+
invoice_request: *invoice_request.contents.clone(),
251251
fields: Self::fields(
252252
payment_paths,
253253
created_at,
254254
payment_hash,
255255
amount_msats,
256256
signing_pubkey,
257257
),
258-
};
258+
});
259259

260260
Self::new(&invoice_request.bytes, contents, ExplicitSigningPubkey {})
261261
}
@@ -266,16 +266,16 @@ 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 {
270-
refund: refund.contents.clone(),
269+
let contents = Box::new(InvoiceContents::ForRefund {
270+
refund: *refund.contents.clone(),
271271
fields: Self::fields(
272272
payment_paths,
273273
created_at,
274274
payment_hash,
275275
amount_msats,
276276
signing_pubkey,
277277
),
278-
};
278+
});
279279

280280
Self::new(&refund.bytes, contents, ExplicitSigningPubkey {})
281281
}
@@ -319,16 +319,16 @@ 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 {
323-
invoice_request: invoice_request.contents.clone(),
322+
let contents = Box::new(InvoiceContents::ForOffer {
323+
invoice_request: *invoice_request.contents.clone(),
324324
fields: Self::fields(
325325
payment_paths,
326326
created_at,
327327
payment_hash,
328328
amount_msats,
329329
signing_pubkey,
330330
),
331-
};
331+
});
332332

333333
Self::new(&invoice_request.bytes, contents, DerivedSigningPubkey(keys))
334334
}
@@ -340,16 +340,16 @@ 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 {
344-
refund: refund.contents.clone(),
343+
let contents = Box::new(InvoiceContents::ForRefund {
344+
refund: *refund.contents.clone(),
345345
fields: Self::fields(
346346
payment_paths,
347347
created_at,
348348
payment_hash,
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/invoice_request.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Invreq ~~~~~";
119119
/// [module-level documentation]: self
120120
pub struct InvoiceRequestBuilder<'a, 'b, T: secp256k1::Signing> {
121121
offer: &'a Offer,
122-
invoice_request: InvoiceRequestContentsWithoutPayerSigningPubkey,
122+
invoice_request: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
123123
payer_signing_pubkey: Option<PublicKey>,
124124
secp_ctx: Option<&'b Secp256k1<T>>,
125125
}
@@ -132,7 +132,7 @@ pub struct InvoiceRequestBuilder<'a, 'b, T: secp256k1::Signing> {
132132
#[cfg(c_bindings)]
133133
pub struct InvoiceRequestWithDerivedPayerSigningPubkeyBuilder<'a, 'b> {
134134
offer: &'a Offer,
135-
invoice_request: InvoiceRequestContentsWithoutPayerSigningPubkey,
135+
invoice_request: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
136136
payer_signing_pubkey: Option<PublicKey>,
137137
secp_ctx: Option<&'b Secp256k1<secp256k1::All>>,
138138
}
@@ -151,7 +151,7 @@ macro_rules! invoice_request_derived_payer_signing_pubkey_builder_methods {
151151
let metadata = Metadata::DerivedSigningPubkey(derivation_material);
152152
Self {
153153
offer,
154-
invoice_request: Self::create_contents(offer, metadata),
154+
invoice_request: Box::new(Self::create_contents(offer, metadata)),
155155
payer_signing_pubkey: None,
156156
secp_ctx: Some(secp_ctx),
157157
}
@@ -181,7 +181,7 @@ macro_rules! invoice_request_builder_methods { (
181181
) => {
182182
#[cfg_attr(c_bindings, allow(dead_code))]
183183
fn create_contents(offer: &Offer, metadata: Metadata) -> InvoiceRequestContentsWithoutPayerSigningPubkey {
184-
let offer = offer.contents.clone();
184+
let offer = *offer.contents.clone();
185185
InvoiceRequestContentsWithoutPayerSigningPubkey {
186186
payer: PayerContents(metadata), offer, chain: None, amount_msats: None,
187187
features: InvoiceRequestFeatures::empty(), quantity: None, payer_note: None,
@@ -317,13 +317,13 @@ macro_rules! invoice_request_builder_methods { (
317317
debug_assert!($self.payer_signing_pubkey.is_some());
318318
let payer_signing_pubkey = $self.payer_signing_pubkey.unwrap();
319319

320-
let invoice_request = InvoiceRequestContents {
320+
let invoice_request = Box::new(InvoiceRequestContents {
321321
#[cfg(not(c_bindings))]
322322
inner: $self.invoice_request,
323323
#[cfg(c_bindings)]
324324
inner: $self.invoice_request.clone(),
325325
payer_signing_pubkey,
326-
};
326+
});
327327
let unsigned_invoice_request = UnsignedInvoiceRequest::new($self.offer, invoice_request);
328328

329329
(unsigned_invoice_request, keys, secp_ctx)
@@ -446,7 +446,7 @@ impl<'a, 'b> From<InvoiceRequestWithDerivedPayerSigningPubkeyBuilder<'a, 'b>>
446446
pub struct UnsignedInvoiceRequest {
447447
bytes: Vec<u8>,
448448
experimental_bytes: Vec<u8>,
449-
contents: InvoiceRequestContents,
449+
contents: Box<InvoiceRequestContents>,
450450
tagged_hash: TaggedHash,
451451
}
452452

@@ -475,7 +475,7 @@ where
475475
}
476476

477477
impl UnsignedInvoiceRequest {
478-
fn new(offer: &Offer, contents: InvoiceRequestContents) -> Self {
478+
fn new(offer: &Offer, contents: Box<InvoiceRequestContents>) -> Self {
479479
// Use the offer bytes instead of the offer TLV stream as the offer may have contained
480480
// unknown TLV records, which are not stored in `OfferContents`.
481481
let (
@@ -584,7 +584,7 @@ impl AsRef<TaggedHash> for UnsignedInvoiceRequest {
584584
#[cfg_attr(test, derive(PartialEq))]
585585
pub struct InvoiceRequest {
586586
pub(super) bytes: Vec<u8>,
587-
pub(super) contents: InvoiceRequestContents,
587+
pub(super) contents: Box<InvoiceRequestContents>,
588588
signature: Signature,
589589
}
590590

@@ -621,7 +621,7 @@ pub struct VerifiedInvoiceRequest {
621621
#[derive(Clone, Debug)]
622622
#[cfg_attr(test, derive(PartialEq))]
623623
pub(super) struct InvoiceRequestContents {
624-
pub(super) inner: InvoiceRequestContentsWithoutPayerSigningPubkey,
624+
pub(super) inner: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
625625
payer_signing_pubkey: PublicKey,
626626
}
627627

@@ -997,10 +997,8 @@ impl VerifiedInvoiceRequest {
997997
///
998998
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
999999
pub fn fields(&self) -> InvoiceRequestFields {
1000-
let InvoiceRequestContents {
1001-
payer_signing_pubkey,
1002-
inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
1003-
} = &self.inner.contents;
1000+
let InvoiceRequestContents { payer_signing_pubkey, inner } = &*self.inner.contents;
1001+
let InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. } = &**inner;
10041002

10051003
InvoiceRequestFields {
10061004
payer_signing_pubkey: *payer_signing_pubkey,
@@ -1271,7 +1269,7 @@ impl TryFrom<Vec<u8>> for UnsignedInvoiceRequest {
12711269
let invoice_request = ParsedMessage::<PartialInvoiceRequestTlvStream>::try_from(bytes)?;
12721270
let ParsedMessage { mut bytes, tlv_stream } = invoice_request;
12731271

1274-
let contents = InvoiceRequestContents::try_from(tlv_stream)?;
1272+
let contents = Box::new(InvoiceRequestContents::try_from(tlv_stream)?);
12751273
let tagged_hash = TaggedHash::from_valid_tlv_stream_bytes(SIGNATURE_TAG, &bytes);
12761274

12771275
let offset = TlvStream::new(&bytes)
@@ -1298,13 +1296,13 @@ impl TryFrom<Vec<u8>> for InvoiceRequest {
12981296
experimental_offer_tlv_stream,
12991297
experimental_invoice_request_tlv_stream,
13001298
) = tlv_stream;
1301-
let contents = InvoiceRequestContents::try_from((
1299+
let contents = Box::new(InvoiceRequestContents::try_from((
13021300
payer_tlv_stream,
13031301
offer_tlv_stream,
13041302
invoice_request_tlv_stream,
13051303
experimental_offer_tlv_stream,
13061304
experimental_invoice_request_tlv_stream,
1307-
))?;
1305+
))?);
13081306

13091307
let signature = match signature {
13101308
None => {
@@ -1374,7 +1372,7 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
13741372
}
13751373

13761374
Ok(InvoiceRequestContents {
1377-
inner: InvoiceRequestContentsWithoutPayerSigningPubkey {
1375+
inner: Box::new(InvoiceRequestContentsWithoutPayerSigningPubkey {
13781376
payer,
13791377
offer,
13801378
chain,
@@ -1385,7 +1383,7 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
13851383
offer_from_hrn,
13861384
#[cfg(test)]
13871385
experimental_bar,
1388-
},
1386+
}),
13891387
payer_signing_pubkey,
13901388
})
13911389
}

0 commit comments

Comments
 (0)