@@ -40,14 +40,34 @@ use core::ops::Deref;
4040const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
4141
4242
43- // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
44- pub ( crate ) const WEIGHT_REVOKED_OFFERED_HTLC : u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133 ;
45- // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
46- pub ( crate ) const WEIGHT_REVOKED_RECEIVED_HTLC : u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139 ;
47- // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
48- pub ( crate ) const WEIGHT_OFFERED_HTLC : u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133 ;
49- // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
50- pub ( crate ) const WEIGHT_RECEIVED_HTLC : u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139 ;
43+ pub ( crate ) fn weight_revoked_offered_htlc ( opt_anchors : bool ) -> u64 {
44+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
45+ const WEIGHT_REVOKED_OFFERED_HTLC : u64 = 1 + 1 + 73 + 1 + 33 + 1 + 133 ;
46+ const WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS : u64 = WEIGHT_REVOKED_OFFERED_HTLC + 3 ; // + OP_1 + OP_CSV + OP_DROP
47+ if opt_anchors { WEIGHT_REVOKED_OFFERED_HTLC_ANCHORS } else { WEIGHT_REVOKED_OFFERED_HTLC }
48+ }
49+
50+ pub ( crate ) fn weight_revoked_received_htlc ( opt_anchors : bool ) -> u64 {
51+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
52+ const WEIGHT_REVOKED_RECEIVED_HTLC : u64 = 1 + 1 + 73 + 1 + 33 + 1 + 139 ;
53+ const WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS : u64 = WEIGHT_REVOKED_RECEIVED_HTLC + 3 ; // + OP_1 + OP_CSV + OP_DROP
54+ if opt_anchors { WEIGHT_REVOKED_RECEIVED_HTLC_ANCHORS } else { WEIGHT_REVOKED_RECEIVED_HTLC }
55+ }
56+
57+ pub ( crate ) fn weight_offered_htlc ( opt_anchors : bool ) -> u64 {
58+ // number_of_witness_elements + sig_length + counterpartyhtlc_sig + preimage_length + preimage + witness_script_length + witness_script
59+ const WEIGHT_OFFERED_HTLC : u64 = 1 + 1 + 73 + 1 + 32 + 1 + 133 ;
60+ const WEIGHT_OFFERED_HTLC_ANCHORS : u64 = WEIGHT_OFFERED_HTLC + 3 ; // + OP_1 + OP_CSV + OP_DROP
61+ if opt_anchors { WEIGHT_OFFERED_HTLC_ANCHORS } else { WEIGHT_OFFERED_HTLC }
62+ }
63+
64+ pub ( crate ) fn weight_received_htlc ( opt_anchors : bool ) -> u64 {
65+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
66+ const WEIGHT_RECEIVED_HTLC : u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139 ;
67+ const WEIGHT_RECEIVED_HTLC_ANCHORS : u64 = WEIGHT_RECEIVED_HTLC + 3 ; // + OP_1 + OP_CSV + OP_DROP
68+ if opt_anchors { WEIGHT_RECEIVED_HTLC_ANCHORS } else { WEIGHT_RECEIVED_HTLC }
69+ }
70+
5171// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
5272pub ( crate ) const WEIGHT_REVOKED_OUTPUT : u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77 ;
5373
@@ -118,8 +138,8 @@ pub(crate) struct RevokedHTLCOutput {
118138}
119139
120140impl RevokedHTLCOutput {
121- pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , per_commitment_key : SecretKey , amount : u64 , htlc : HTLCOutputInCommitment ) -> Self {
122- let weight = if htlc. offered { WEIGHT_REVOKED_OFFERED_HTLC } else { WEIGHT_REVOKED_RECEIVED_HTLC } ;
141+ pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , per_commitment_key : SecretKey , amount : u64 , htlc : HTLCOutputInCommitment , opt_anchors : bool ) -> Self {
142+ let weight = if htlc. offered { weight_revoked_offered_htlc ( opt_anchors ) } else { weight_revoked_received_htlc ( opt_anchors ) } ;
123143 RevokedHTLCOutput {
124144 per_commitment_point,
125145 counterparty_delayed_payment_base_key,
@@ -292,12 +312,12 @@ impl PackageSolvingData {
292312 } ;
293313 amt
294314 }
295- fn weight ( & self ) -> usize {
315+ fn weight ( & self , opt_anchors : bool ) -> usize {
296316 let weight = match self {
297317 PackageSolvingData :: RevokedOutput ( ref outp) => { outp. weight as usize } ,
298318 PackageSolvingData :: RevokedHTLCOutput ( ref outp) => { outp. weight as usize } ,
299- PackageSolvingData :: CounterpartyOfferedHTLCOutput ( ..) => { WEIGHT_OFFERED_HTLC as usize } ,
300- PackageSolvingData :: CounterpartyReceivedHTLCOutput ( ..) => { WEIGHT_RECEIVED_HTLC as usize } ,
319+ PackageSolvingData :: CounterpartyOfferedHTLCOutput ( ..) => { weight_offered_htlc ( opt_anchors ) as usize } ,
320+ PackageSolvingData :: CounterpartyReceivedHTLCOutput ( ..) => { weight_received_htlc ( opt_anchors ) as usize } ,
301321 // Note: Currently, weights of holder outputs spending witnesses aren't used
302322 // as we can't malleate spending package to increase their feerate. This
303323 // should change with the remaining anchor output patchset.
@@ -567,13 +587,13 @@ impl PackageTemplate {
567587 self . inputs . iter ( ) . map ( |( _, outp) | outp. absolute_tx_timelock ( self . height_original ) )
568588 . max ( ) . expect ( "There must always be at least one output to spend in a PackageTemplate" )
569589 }
570- pub ( crate ) fn package_weight ( & self , destination_script : & Script ) -> usize {
590+ pub ( crate ) fn package_weight ( & self , destination_script : & Script , opt_anchors : bool ) -> usize {
571591 let mut inputs_weight = 0 ;
572592 let mut witnesses_weight = 2 ; // count segwit flags
573593 for ( _, outp) in self . inputs . iter ( ) {
574594 // previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
575595 inputs_weight += 41 * WITNESS_SCALE_FACTOR ;
576- witnesses_weight += outp. weight ( ) ;
596+ witnesses_weight += outp. weight ( opt_anchors ) ;
577597 }
578598 // version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
579599 let transaction_weight = 10 * WITNESS_SCALE_FACTOR ;
@@ -1026,6 +1046,8 @@ mod tests {
10261046 let package = PackageTemplate :: build_package ( txid, 0 , revk_outp, 0 , true , 100 ) ;
10271047 // (nVersion (4) + nLocktime (4) + count_tx_in (1) + prevout (36) + sequence (4) + script_length (1) + count_tx_out (1) + value (8) + var_int (1)) * WITNESS_SCALE_FACTOR
10281048 // + witness marker (2) + WEIGHT_REVOKED_OUTPUT
1029- assert_eq ! ( package. package_weight( & Script :: new( ) ) , ( 4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1 ) * WITNESS_SCALE_FACTOR + 2 + WEIGHT_REVOKED_OUTPUT as usize ) ;
1049+ for & opt_anchors in [ false , true ] . iter ( ) {
1050+ assert_eq ! ( package. package_weight( & Script :: new( ) , opt_anchors) , ( 4 + 4 + 1 + 36 + 4 + 1 + 1 + 8 + 1 ) * WITNESS_SCALE_FACTOR + 2 + WEIGHT_REVOKED_OUTPUT as usize ) ;
1051+ }
10301052 }
10311053}
0 commit comments