@@ -122,7 +122,7 @@ use crate::offers::invoice_macros::{invoice_accessors_common, invoice_builder_me
122122#[ cfg( test) ]
123123use crate :: offers:: invoice_macros:: invoice_builder_methods_test_common;
124124use crate :: offers:: invoice_request:: { EXPERIMENTAL_INVOICE_REQUEST_TYPES , ExperimentalInvoiceRequestTlvStream , ExperimentalInvoiceRequestTlvStreamRef , INVOICE_REQUEST_PAYER_ID_TYPE , INVOICE_REQUEST_TYPES , IV_BYTES as INVOICE_REQUEST_IV_BYTES , InvoiceRequest , InvoiceRequestContents , InvoiceRequestTlvStream , InvoiceRequestTlvStreamRef } ;
125- use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self , SIGNATURE_TLV_RECORD_SIZE } ;
125+ use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self } ;
126126use crate :: offers:: nonce:: Nonce ;
127127use crate :: offers:: offer:: { Amount , EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , OfferTlvStream , OfferTlvStreamRef , Quantity } ;
128128use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError , ParsedMessage } ;
@@ -520,19 +520,8 @@ impl UnsignedBolt12Invoice {
520520 let ( _, _, _, invoice_tlv_stream, _, _, experimental_invoice_tlv_stream) =
521521 contents. as_tlv_stream ( ) ;
522522
523- // Allocate enough space for the invoice, which will include:
524- // - all TLV records from `invreq_bytes` except signatures,
525- // - all invoice-specific TLV records, and
526- // - a signature TLV record once the invoice is signed.
527- //
528- // This assumes both the invoice request and the invoice will each only have one signature
529- // using SIGNATURE_TYPES.start as the TLV record. Thus, it is accounted for by invreq_bytes.
530- let mut bytes = Vec :: with_capacity (
531- invreq_bytes. len ( )
532- + invoice_tlv_stream. serialized_length ( )
533- + if contents. is_for_offer ( ) { 0 } else { SIGNATURE_TLV_RECORD_SIZE }
534- + experimental_invoice_tlv_stream. serialized_length ( ) ,
535- ) ;
523+ const INVOICE_ALLOCATION_SIZE : usize = 1024 ;
524+ let mut bytes = Vec :: with_capacity ( INVOICE_ALLOCATION_SIZE ) ;
536525
537526 // Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
538527 // have contained unknown TLV records, which are not stored in `InvoiceRequestContents` or
@@ -545,23 +534,16 @@ impl UnsignedBolt12Invoice {
545534
546535 invoice_tlv_stream. write ( & mut bytes) . unwrap ( ) ;
547536
548- let mut experimental_tlv_stream = TlvStream :: new ( remaining_bytes)
549- . range ( EXPERIMENTAL_TYPES )
550- . peekable ( ) ;
551- let mut experimental_bytes = Vec :: with_capacity (
552- remaining_bytes. len ( )
553- - experimental_tlv_stream
554- . peek ( )
555- . map_or ( remaining_bytes. len ( ) , |first_record| first_record. start )
556- + experimental_invoice_tlv_stream. serialized_length ( ) ,
557- ) ;
537+ const EXPERIMENTAL_TLV_ALLOCATION_SIZE : usize = 0 ;
538+ let mut experimental_bytes = Vec :: with_capacity ( EXPERIMENTAL_TLV_ALLOCATION_SIZE ) ;
558539
540+ let experimental_tlv_stream = TlvStream :: new ( remaining_bytes)
541+ . range ( EXPERIMENTAL_TYPES ) ;
559542 for record in experimental_tlv_stream {
560543 record. write ( & mut experimental_bytes) . unwrap ( ) ;
561544 }
562545
563546 experimental_invoice_tlv_stream. write ( & mut experimental_bytes) . unwrap ( ) ;
564- debug_assert_eq ! ( experimental_bytes. len( ) , experimental_bytes. capacity( ) ) ;
565547
566548 let tlv_stream = TlvStream :: new ( & bytes) . chain ( TlvStream :: new ( & experimental_bytes) ) ;
567549 let tagged_hash = TaggedHash :: from_tlv_stream ( SIGNATURE_TAG , tlv_stream) ;
@@ -592,14 +574,6 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
592574 signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
593575
594576 // Append the experimental bytes after the signature.
595- debug_assert_eq!(
596- // The two-byte overallocation results from SIGNATURE_TLV_RECORD_SIZE accommodating TLV
597- // records with types >= 253.
598- $self. bytes. len( )
599- + $self. experimental_bytes. len( )
600- + if $self. contents. is_for_offer( ) { 0 } else { 2 } ,
601- $self. bytes. capacity( ) ,
602- ) ;
603577 $self. bytes. extend_from_slice( & $self. experimental_bytes) ;
604578
605579 Ok ( Bolt12Invoice {
@@ -965,13 +939,6 @@ impl Hash for Bolt12Invoice {
965939}
966940
967941impl InvoiceContents {
968- fn is_for_offer ( & self ) -> bool {
969- match self {
970- InvoiceContents :: ForOffer { .. } => true ,
971- InvoiceContents :: ForRefund { .. } => false ,
972- }
973- }
974-
975942 /// Whether the original offer or refund has expired.
976943 #[ cfg( feature = "std" ) ]
977944 fn is_offer_or_refund_expired ( & self ) -> bool {
@@ -1362,7 +1329,11 @@ pub(super) const EXPERIMENTAL_INVOICE_TYPES: core::ops::RangeFrom<u64> = 3_000_0
13621329
13631330#[ cfg( not( test) ) ]
13641331tlv_stream ! (
1365- ExperimentalInvoiceTlvStream , ExperimentalInvoiceTlvStreamRef , EXPERIMENTAL_INVOICE_TYPES , { }
1332+ ExperimentalInvoiceTlvStream , ExperimentalInvoiceTlvStreamRef , EXPERIMENTAL_INVOICE_TYPES , {
1333+ // When adding experimental TLVs, update EXPERIMENTAL_TLV_ALLOCATION_SIZE accordingly in
1334+ // both UnsignedBolt12Invoice:new and UnsignedStaticInvoice::new to avoid unnecessary
1335+ // allocations.
1336+ }
13661337) ;
13671338
13681339#[ cfg( test) ]
0 commit comments