Skip to content

ln: reduce size_of Event from 1856 B -> 720 B, prep for inline storage in offer *Contents #3723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3544,7 +3544,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
package_target_feerate_sat_per_1000_weight,
commitment_tx,
commitment_tx_fee_satoshis,
anchor_descriptor: AnchorDescriptor {
anchor_descriptor: Box::new(AnchorDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
keys_id: self.channel_keys_id,
value_satoshis: channel_parameters.channel_value_satoshis,
Expand All @@ -3554,7 +3554,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
txid: commitment_txid,
vout: anchor_output_idx,
},
},
}),
pending_htlcs: pending_nondust_htlcs,
}));
},
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/events/bump_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub enum BumpTransactionEvent {
commitment_tx_fee_satoshis: u64,
/// The descriptor to sign the anchor input of the anchor transaction constructed as a
/// result of consuming this event.
anchor_descriptor: AnchorDescriptor,
anchor_descriptor: Box<AnchorDescriptor>,
/// The set of pending HTLCs on the commitment transaction that need to be resolved once the
/// commitment transaction confirms.
pending_htlcs: Vec<HTLCOutputInCommitment>,
Expand Down Expand Up @@ -1076,14 +1076,14 @@ mod tests {
package_target_feerate_sat_per_1000_weight: 868,
commitment_tx_fee_satoshis: 930,
commitment_tx,
anchor_descriptor: AnchorDescriptor {
anchor_descriptor: Box::new(AnchorDescriptor {
channel_derivation_parameters: ChannelDerivationParameters {
value_satoshis: 42_000_000,
keys_id: [42; 32],
transaction_parameters,
},
outpoint: OutPoint { txid: Txid::from_byte_array([42; 32]), vout: 0 },
},
}),
pending_htlcs: Vec::new(),
});
}
Expand Down
50 changes: 25 additions & 25 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice", "signatu
/// [module-level documentation]: self
pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
invreq_bytes: &'a Vec<u8>,
invoice: InvoiceContents,
invoice: Box<InvoiceContents>,
signing_pubkey_strategy: S,
}

Expand All @@ -200,7 +200,7 @@ pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
#[cfg(c_bindings)]
pub struct InvoiceWithExplicitSigningPubkeyBuilder<'a> {
invreq_bytes: &'a Vec<u8>,
invoice: InvoiceContents,
invoice: Box<InvoiceContents>,
signing_pubkey_strategy: ExplicitSigningPubkey,
}

Expand All @@ -216,7 +216,7 @@ pub struct InvoiceWithExplicitSigningPubkeyBuilder<'a> {
#[cfg(c_bindings)]
pub struct InvoiceWithDerivedSigningPubkeyBuilder<'a> {
invreq_bytes: &'a Vec<u8>,
invoice: InvoiceContents,
invoice: Box<InvoiceContents>,
signing_pubkey_strategy: DerivedSigningPubkey,
}

Expand Down Expand Up @@ -246,16 +246,16 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey,
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
let contents = Box::new(InvoiceContents::ForOffer {
invoice_request: *invoice_request.contents.clone(),
fields: Self::fields(
payment_paths,
created_at,
payment_hash,
amount_msats,
signing_pubkey,
),
};
});

Self::new(&invoice_request.bytes, contents, ExplicitSigningPubkey {})
}
Expand All @@ -266,16 +266,16 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods {
payment_hash: PaymentHash, signing_pubkey: PublicKey,
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = refund.amount_msats();
let contents = InvoiceContents::ForRefund {
refund: refund.contents.clone(),
let contents = Box::new(InvoiceContents::ForRefund {
refund: *refund.contents.clone(),
fields: Self::fields(
payment_paths,
created_at,
payment_hash,
amount_msats,
signing_pubkey,
),
};
});

Self::new(&refund.bytes, contents, ExplicitSigningPubkey {})
}
Expand Down Expand Up @@ -319,16 +319,16 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
let contents = Box::new(InvoiceContents::ForOffer {
invoice_request: *invoice_request.contents.clone(),
fields: Self::fields(
payment_paths,
created_at,
payment_hash,
amount_msats,
signing_pubkey,
),
};
});

Self::new(&invoice_request.bytes, contents, DerivedSigningPubkey(keys))
}
Expand All @@ -340,16 +340,16 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods {
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = refund.amount_msats();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForRefund {
refund: refund.contents.clone(),
let contents = Box::new(InvoiceContents::ForRefund {
refund: *refund.contents.clone(),
fields: Self::fields(
payment_paths,
created_at,
payment_hash,
amount_msats,
signing_pubkey,
),
};
});

Self::new(&refund.bytes, contents, DerivedSigningPubkey(keys))
}
Expand Down Expand Up @@ -429,7 +429,7 @@ macro_rules! invoice_builder_methods {

#[cfg_attr(c_bindings, allow(dead_code))]
fn new(
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents,
invreq_bytes: &'a Vec<u8>, contents: Box<InvoiceContents>,
signing_pubkey_strategy: $type_param,
) -> Result<Self, Bolt12SemanticError> {
if contents.fields().payment_paths.is_empty() {
Expand Down Expand Up @@ -593,7 +593,7 @@ impl<'a> From<InvoiceWithDerivedSigningPubkeyBuilder<'a>>
pub struct UnsignedBolt12Invoice {
bytes: Vec<u8>,
experimental_bytes: Vec<u8>,
contents: InvoiceContents,
contents: Box<InvoiceContents>,
tagged_hash: TaggedHash,
}

Expand Down Expand Up @@ -622,7 +622,7 @@ where
}

impl UnsignedBolt12Invoice {
fn new(invreq_bytes: &[u8], contents: InvoiceContents) -> Self {
fn new(invreq_bytes: &[u8], contents: Box<InvoiceContents>) -> Self {
// TLV record ranges applicable to invreq_bytes.
const NON_EXPERIMENTAL_TYPES: core::ops::Range<u64> = 0..INVOICE_REQUEST_TYPES.end;
const EXPERIMENTAL_TYPES: core::ops::Range<u64> =
Expand Down Expand Up @@ -731,7 +731,7 @@ impl AsRef<TaggedHash> for UnsignedBolt12Invoice {
#[derive(Clone, Debug)]
pub struct Bolt12Invoice {
bytes: Vec<u8>,
contents: InvoiceContents,
contents: Box<InvoiceContents>,
signature: Signature,
tagged_hash: TaggedHash,
}
Expand Down Expand Up @@ -974,7 +974,7 @@ impl Bolt12Invoice {
pub fn verify_using_metadata<T: secp256k1::Signing>(
&self, key: &ExpandedKey, secp_ctx: &Secp256k1<T>,
) -> Result<PaymentId, ()> {
let (metadata, iv_bytes) = match &self.contents {
let (metadata, iv_bytes) = match &*self.contents {
InvoiceContents::ForOffer { invoice_request, .. } => {
(&invoice_request.inner.payer.0, INVOICE_REQUEST_IV_BYTES)
},
Expand All @@ -992,7 +992,7 @@ impl Bolt12Invoice {
&self, payment_id: PaymentId, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>,
) -> Result<PaymentId, ()> {
let metadata = Metadata::payer_data(payment_id, nonce, key);
let iv_bytes = match &self.contents {
let iv_bytes = match &*self.contents {
InvoiceContents::ForOffer { .. } => INVOICE_REQUEST_IV_BYTES,
InvoiceContents::ForRefund { .. } => REFUND_IV_BYTES_WITHOUT_METADATA,
};
Expand Down Expand Up @@ -1027,7 +1027,7 @@ impl Bolt12Invoice {
}

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

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

Expand Down Expand Up @@ -1606,15 +1606,15 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
experimental_invoice_request_tlv_stream,
experimental_invoice_tlv_stream,
) = tlv_stream;
let contents = InvoiceContents::try_from((
let contents = Box::new(InvoiceContents::try_from((
payer_tlv_stream,
offer_tlv_stream,
invoice_request_tlv_stream,
invoice_tlv_stream,
experimental_offer_tlv_stream,
experimental_invoice_request_tlv_stream,
experimental_invoice_tlv_stream,
))?;
))?);

let signature = signature
.ok_or(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature))?;
Expand Down
36 changes: 17 additions & 19 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Invreq ~~~~~";
/// [module-level documentation]: self
pub struct InvoiceRequestBuilder<'a, 'b, T: secp256k1::Signing> {
offer: &'a Offer,
invoice_request: InvoiceRequestContentsWithoutPayerSigningPubkey,
invoice_request: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
payer_signing_pubkey: Option<PublicKey>,
secp_ctx: Option<&'b Secp256k1<T>>,
}
Expand All @@ -132,7 +132,7 @@ pub struct InvoiceRequestBuilder<'a, 'b, T: secp256k1::Signing> {
#[cfg(c_bindings)]
pub struct InvoiceRequestWithDerivedPayerSigningPubkeyBuilder<'a, 'b> {
offer: &'a Offer,
invoice_request: InvoiceRequestContentsWithoutPayerSigningPubkey,
invoice_request: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
payer_signing_pubkey: Option<PublicKey>,
secp_ctx: Option<&'b Secp256k1<secp256k1::All>>,
}
Expand All @@ -151,7 +151,7 @@ macro_rules! invoice_request_derived_payer_signing_pubkey_builder_methods {
let metadata = Metadata::DerivedSigningPubkey(derivation_material);
Self {
offer,
invoice_request: Self::create_contents(offer, metadata),
invoice_request: Box::new(Self::create_contents(offer, metadata)),
payer_signing_pubkey: None,
secp_ctx: Some(secp_ctx),
}
Expand Down Expand Up @@ -181,7 +181,7 @@ macro_rules! invoice_request_builder_methods { (
) => {
#[cfg_attr(c_bindings, allow(dead_code))]
fn create_contents(offer: &Offer, metadata: Metadata) -> InvoiceRequestContentsWithoutPayerSigningPubkey {
let offer = offer.contents.clone();
let offer = *offer.contents.clone();
InvoiceRequestContentsWithoutPayerSigningPubkey {
payer: PayerContents(metadata), offer, chain: None, amount_msats: None,
features: InvoiceRequestFeatures::empty(), quantity: None, payer_note: None,
Expand Down Expand Up @@ -317,13 +317,13 @@ macro_rules! invoice_request_builder_methods { (
debug_assert!($self.payer_signing_pubkey.is_some());
let payer_signing_pubkey = $self.payer_signing_pubkey.unwrap();

let invoice_request = InvoiceRequestContents {
let invoice_request = Box::new(InvoiceRequestContents {
#[cfg(not(c_bindings))]
inner: $self.invoice_request,
#[cfg(c_bindings)]
inner: $self.invoice_request.clone(),
payer_signing_pubkey,
};
});
let unsigned_invoice_request = UnsignedInvoiceRequest::new($self.offer, invoice_request);

(unsigned_invoice_request, keys, secp_ctx)
Expand Down Expand Up @@ -446,7 +446,7 @@ impl<'a, 'b> From<InvoiceRequestWithDerivedPayerSigningPubkeyBuilder<'a, 'b>>
pub struct UnsignedInvoiceRequest {
bytes: Vec<u8>,
experimental_bytes: Vec<u8>,
contents: InvoiceRequestContents,
contents: Box<InvoiceRequestContents>,
tagged_hash: TaggedHash,
}

Expand Down Expand Up @@ -475,7 +475,7 @@ where
}

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

Expand Down Expand Up @@ -621,7 +621,7 @@ pub struct VerifiedInvoiceRequest {
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub(super) struct InvoiceRequestContents {
pub(super) inner: InvoiceRequestContentsWithoutPayerSigningPubkey,
pub(super) inner: Box<InvoiceRequestContentsWithoutPayerSigningPubkey>,
payer_signing_pubkey: PublicKey,
}

Expand Down Expand Up @@ -997,10 +997,8 @@ impl VerifiedInvoiceRequest {
///
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
pub fn fields(&self) -> InvoiceRequestFields {
let InvoiceRequestContents {
payer_signing_pubkey,
inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
} = &self.inner.contents;
let InvoiceRequestContents { payer_signing_pubkey, inner } = &*self.inner.contents;
let InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. } = &**inner;

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

let contents = InvoiceRequestContents::try_from(tlv_stream)?;
let contents = Box::new(InvoiceRequestContents::try_from(tlv_stream)?);
let tagged_hash = TaggedHash::from_valid_tlv_stream_bytes(SIGNATURE_TAG, &bytes);

let offset = TlvStream::new(&bytes)
Expand All @@ -1298,13 +1296,13 @@ impl TryFrom<Vec<u8>> for InvoiceRequest {
experimental_offer_tlv_stream,
experimental_invoice_request_tlv_stream,
) = tlv_stream;
let contents = InvoiceRequestContents::try_from((
let contents = Box::new(InvoiceRequestContents::try_from((
payer_tlv_stream,
offer_tlv_stream,
invoice_request_tlv_stream,
experimental_offer_tlv_stream,
experimental_invoice_request_tlv_stream,
))?;
))?);

let signature = match signature {
None => {
Expand Down Expand Up @@ -1374,7 +1372,7 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerSigningPubkey {
inner: Box::new(InvoiceRequestContentsWithoutPayerSigningPubkey {
payer,
offer,
chain,
Expand All @@ -1385,7 +1383,7 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
offer_from_hrn,
#[cfg(test)]
experimental_bar,
},
}),
payer_signing_pubkey,
})
}
Expand Down
Loading
Loading