@@ -122,7 +122,7 @@ use crate::offers::invoice_macros::{invoice_accessors_common, invoice_builder_me
122
122
#[ cfg( test) ]
123
123
use crate :: offers:: invoice_macros:: invoice_builder_methods_test_common;
124
124
use 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 } ;
126
126
use crate :: offers:: nonce:: Nonce ;
127
127
use crate :: offers:: offer:: { Amount , EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , OfferTlvStream , OfferTlvStreamRef , Quantity } ;
128
128
use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError , ParsedMessage } ;
@@ -520,19 +520,8 @@ impl UnsignedBolt12Invoice {
520
520
let ( _, _, _, invoice_tlv_stream, _, _, experimental_invoice_tlv_stream) =
521
521
contents. as_tlv_stream ( ) ;
522
522
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 ) ;
536
525
537
526
// Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
538
527
// have contained unknown TLV records, which are not stored in `InvoiceRequestContents` or
@@ -545,23 +534,16 @@ impl UnsignedBolt12Invoice {
545
534
546
535
invoice_tlv_stream. write ( & mut bytes) . unwrap ( ) ;
547
536
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 ) ;
558
539
540
+ let experimental_tlv_stream = TlvStream :: new ( remaining_bytes)
541
+ . range ( EXPERIMENTAL_TYPES ) ;
559
542
for record in experimental_tlv_stream {
560
543
record. write ( & mut experimental_bytes) . unwrap ( ) ;
561
544
}
562
545
563
546
experimental_invoice_tlv_stream. write ( & mut experimental_bytes) . unwrap ( ) ;
564
- debug_assert_eq ! ( experimental_bytes. len( ) , experimental_bytes. capacity( ) ) ;
565
547
566
548
let tlv_stream = TlvStream :: new ( & bytes) . chain ( TlvStream :: new ( & experimental_bytes) ) ;
567
549
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
592
574
signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
593
575
594
576
// 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
- ) ;
603
577
$self. bytes. extend_from_slice( & $self. experimental_bytes) ;
604
578
605
579
Ok ( Bolt12Invoice {
@@ -965,13 +939,6 @@ impl Hash for Bolt12Invoice {
965
939
}
966
940
967
941
impl InvoiceContents {
968
- fn is_for_offer ( & self ) -> bool {
969
- match self {
970
- InvoiceContents :: ForOffer { .. } => true ,
971
- InvoiceContents :: ForRefund { .. } => false ,
972
- }
973
- }
974
-
975
942
/// Whether the original offer or refund has expired.
976
943
#[ cfg( feature = "std" ) ]
977
944
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
1362
1329
1363
1330
#[ cfg( not( test) ) ]
1364
1331
tlv_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
+ }
1366
1337
) ;
1367
1338
1368
1339
#[ cfg( test) ]
@@ -2880,9 +2851,6 @@ mod tests {
2880
2851
BigSize ( 32 ) . write ( & mut unknown_bytes) . unwrap ( ) ;
2881
2852
[ 42u8 ; 32 ] . write ( & mut unknown_bytes) . unwrap ( ) ;
2882
2853
2883
- unsigned_invoice. bytes . reserve_exact (
2884
- unsigned_invoice. bytes . capacity ( ) - unsigned_invoice. bytes . len ( ) + unknown_bytes. len ( ) ,
2885
- ) ;
2886
2854
unsigned_invoice. bytes . extend_from_slice ( & unknown_bytes) ;
2887
2855
unsigned_invoice. tagged_hash =
2888
2856
TaggedHash :: from_valid_tlv_stream_bytes ( SIGNATURE_TAG , & unsigned_invoice. bytes ) ;
@@ -2917,9 +2885,6 @@ mod tests {
2917
2885
BigSize ( 32 ) . write ( & mut unknown_bytes) . unwrap ( ) ;
2918
2886
[ 42u8 ; 32 ] . write ( & mut unknown_bytes) . unwrap ( ) ;
2919
2887
2920
- unsigned_invoice. bytes . reserve_exact (
2921
- unsigned_invoice. bytes . capacity ( ) - unsigned_invoice. bytes . len ( ) + unknown_bytes. len ( ) ,
2922
- ) ;
2923
2888
unsigned_invoice. bytes . extend_from_slice ( & unknown_bytes) ;
2924
2889
unsigned_invoice. tagged_hash =
2925
2890
TaggedHash :: from_valid_tlv_stream_bytes ( SIGNATURE_TAG , & unsigned_invoice. bytes ) ;
@@ -2982,9 +2947,6 @@ mod tests {
2982
2947
BigSize ( 32 ) . write ( & mut unknown_bytes) . unwrap ( ) ;
2983
2948
[ 42u8 ; 32 ] . write ( & mut unknown_bytes) . unwrap ( ) ;
2984
2949
2985
- unsigned_invoice. bytes . reserve_exact (
2986
- unsigned_invoice. bytes . capacity ( ) - unsigned_invoice. bytes . len ( ) + unknown_bytes. len ( ) ,
2987
- ) ;
2988
2950
unsigned_invoice. experimental_bytes . extend_from_slice ( & unknown_bytes) ;
2989
2951
2990
2952
let tlv_stream = TlvStream :: new ( & unsigned_invoice. bytes )
@@ -3021,9 +2983,6 @@ mod tests {
3021
2983
BigSize ( 32 ) . write ( & mut unknown_bytes) . unwrap ( ) ;
3022
2984
[ 42u8 ; 32 ] . write ( & mut unknown_bytes) . unwrap ( ) ;
3023
2985
3024
- unsigned_invoice. bytes . reserve_exact (
3025
- unsigned_invoice. bytes . capacity ( ) - unsigned_invoice. bytes . len ( ) + unknown_bytes. len ( ) ,
3026
- ) ;
3027
2986
unsigned_invoice. experimental_bytes . extend_from_slice ( & unknown_bytes) ;
3028
2987
3029
2988
let tlv_stream = TlvStream :: new ( & unsigned_invoice. bytes )
0 commit comments