@@ -177,23 +177,30 @@ pub(crate) struct CounterpartyOfferedHTLCOutput {
177
177
counterparty_delayed_payment_base_key : PublicKey ,
178
178
counterparty_htlc_base_key : PublicKey ,
179
179
preimage : PaymentPreimage ,
180
- htlc : HTLCOutputInCommitment
180
+ htlc : HTLCOutputInCommitment ,
181
+ opt_anchors : Option < ( ) > ,
181
182
}
182
183
183
184
impl CounterpartyOfferedHTLCOutput {
184
- pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , preimage : PaymentPreimage , htlc : HTLCOutputInCommitment ) -> Self {
185
+ pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , preimage : PaymentPreimage , htlc : HTLCOutputInCommitment , opt_anchors : bool ) -> Self {
185
186
CounterpartyOfferedHTLCOutput {
186
187
per_commitment_point,
187
188
counterparty_delayed_payment_base_key,
188
189
counterparty_htlc_base_key,
189
190
preimage,
190
- htlc
191
+ htlc,
192
+ opt_anchors : if opt_anchors { Some ( ( ) ) } else { None } ,
191
193
}
192
194
}
195
+
196
+ fn opt_anchors ( & self ) -> bool {
197
+ self . opt_anchors . is_some ( )
198
+ }
193
199
}
194
200
195
201
impl_writeable_tlv_based ! ( CounterpartyOfferedHTLCOutput , {
196
202
( 0 , per_commitment_point, required) ,
203
+ ( 1 , opt_anchors, option) ,
197
204
( 2 , counterparty_delayed_payment_base_key, required) ,
198
205
( 4 , counterparty_htlc_base_key, required) ,
199
206
( 6 , preimage, required) ,
@@ -209,22 +216,29 @@ pub(crate) struct CounterpartyReceivedHTLCOutput {
209
216
per_commitment_point : PublicKey ,
210
217
counterparty_delayed_payment_base_key : PublicKey ,
211
218
counterparty_htlc_base_key : PublicKey ,
212
- htlc : HTLCOutputInCommitment
219
+ htlc : HTLCOutputInCommitment ,
220
+ opt_anchors : Option < ( ) > ,
213
221
}
214
222
215
223
impl CounterpartyReceivedHTLCOutput {
216
- pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , htlc : HTLCOutputInCommitment ) -> Self {
224
+ pub ( crate ) fn build ( per_commitment_point : PublicKey , counterparty_delayed_payment_base_key : PublicKey , counterparty_htlc_base_key : PublicKey , htlc : HTLCOutputInCommitment , opt_anchors : bool ) -> Self {
217
225
CounterpartyReceivedHTLCOutput {
218
226
per_commitment_point,
219
227
counterparty_delayed_payment_base_key,
220
228
counterparty_htlc_base_key,
221
- htlc
229
+ htlc,
230
+ opt_anchors : if opt_anchors { Some ( ( ) ) } else { None } ,
222
231
}
223
232
}
233
+
234
+ fn opt_anchors ( & self ) -> bool {
235
+ self . opt_anchors . is_some ( )
236
+ }
224
237
}
225
238
226
239
impl_writeable_tlv_based ! ( CounterpartyReceivedHTLCOutput , {
227
240
( 0 , per_commitment_point, required) ,
241
+ ( 1 , opt_anchors, option) ,
228
242
( 2 , counterparty_delayed_payment_base_key, required) ,
229
243
( 4 , counterparty_htlc_base_key, required) ,
230
244
( 6 , htlc, required) ,
@@ -315,12 +329,12 @@ impl PackageSolvingData {
315
329
} ;
316
330
amt
317
331
}
318
- fn weight ( & self , opt_anchors : bool ) -> usize {
332
+ fn weight ( & self ) -> usize {
319
333
let weight = match self {
320
334
PackageSolvingData :: RevokedOutput ( ref outp) => { outp. weight as usize } ,
321
335
PackageSolvingData :: RevokedHTLCOutput ( ref outp) => { outp. weight as usize } ,
322
- PackageSolvingData :: CounterpartyOfferedHTLCOutput ( .. ) => { weight_offered_htlc ( opt_anchors) as usize } ,
323
- PackageSolvingData :: CounterpartyReceivedHTLCOutput ( .. ) => { weight_received_htlc ( opt_anchors) as usize } ,
336
+ PackageSolvingData :: CounterpartyOfferedHTLCOutput ( ref outp ) => { weight_offered_htlc ( outp . opt_anchors ( ) ) as usize } ,
337
+ PackageSolvingData :: CounterpartyReceivedHTLCOutput ( ref outp ) => { weight_received_htlc ( outp . opt_anchors ( ) ) as usize } ,
324
338
// Note: Currently, weights of holder outputs spending witnesses aren't used
325
339
// as we can't malleate spending package to increase their feerate. This
326
340
// should change with the remaining anchor output patchset.
@@ -594,13 +608,13 @@ impl PackageTemplate {
594
608
self . inputs . iter ( ) . map ( |( _, outp) | outp. absolute_tx_timelock ( self . height_original ) )
595
609
. max ( ) . expect ( "There must always be at least one output to spend in a PackageTemplate" )
596
610
}
597
- pub ( crate ) fn package_weight ( & self , destination_script : & Script , opt_anchors : bool ) -> usize {
611
+ pub ( crate ) fn package_weight ( & self , destination_script : & Script ) -> usize {
598
612
let mut inputs_weight = 0 ;
599
613
let mut witnesses_weight = 2 ; // count segwit flags
600
614
for ( _, outp) in self . inputs . iter ( ) {
601
615
// previous_out_point: 36 bytes ; var_int: 1 byte ; sequence: 4 bytes
602
616
inputs_weight += 41 * WITNESS_SCALE_FACTOR ;
603
- witnesses_weight += outp. weight ( opt_anchors ) ;
617
+ witnesses_weight += outp. weight ( ) ;
604
618
}
605
619
// version: 4 bytes ; count_tx_in: 1 byte ; count_tx_out: 1 byte ; lock_time: 4 bytes
606
620
let transaction_weight = 10 * WITNESS_SCALE_FACTOR ;
@@ -873,26 +887,26 @@ mod tests {
873
887
}
874
888
875
889
macro_rules! dumb_counterparty_output {
876
- ( $secp_ctx: expr, $amt: expr) => {
890
+ ( $secp_ctx: expr, $amt: expr, $opt_anchors : expr ) => {
877
891
{
878
892
let dumb_scalar = SecretKey :: from_slice( & hex:: decode( "0101010101010101010101010101010101010101010101010101010101010101" ) . unwrap( ) [ ..] ) . unwrap( ) ;
879
893
let dumb_point = PublicKey :: from_secret_key( & $secp_ctx, & dumb_scalar) ;
880
894
let hash = PaymentHash ( [ 1 ; 32 ] ) ;
881
895
let htlc = HTLCOutputInCommitment { offered: true , amount_msat: $amt, cltv_expiry: 0 , payment_hash: hash, transaction_output_index: None } ;
882
- PackageSolvingData :: CounterpartyReceivedHTLCOutput ( CounterpartyReceivedHTLCOutput :: build( dumb_point, dumb_point, dumb_point, htlc) )
896
+ PackageSolvingData :: CounterpartyReceivedHTLCOutput ( CounterpartyReceivedHTLCOutput :: build( dumb_point, dumb_point, dumb_point, htlc, $opt_anchors ) )
883
897
}
884
898
}
885
899
}
886
900
887
901
macro_rules! dumb_counterparty_offered_output {
888
- ( $secp_ctx: expr, $amt: expr) => {
902
+ ( $secp_ctx: expr, $amt: expr, $opt_anchors : expr ) => {
889
903
{
890
904
let dumb_scalar = SecretKey :: from_slice( & hex:: decode( "0101010101010101010101010101010101010101010101010101010101010101" ) . unwrap( ) [ ..] ) . unwrap( ) ;
891
905
let dumb_point = PublicKey :: from_secret_key( & $secp_ctx, & dumb_scalar) ;
892
906
let hash = PaymentHash ( [ 1 ; 32 ] ) ;
893
907
let preimage = PaymentPreimage ( [ 2 ; 32 ] ) ;
894
908
let htlc = HTLCOutputInCommitment { offered: false , amount_msat: $amt, cltv_expiry: 1000 , payment_hash: hash, transaction_output_index: None } ;
895
- PackageSolvingData :: CounterpartyOfferedHTLCOutput ( CounterpartyOfferedHTLCOutput :: build( dumb_point, dumb_point, dumb_point, preimage, htlc) )
909
+ PackageSolvingData :: CounterpartyOfferedHTLCOutput ( CounterpartyOfferedHTLCOutput :: build( dumb_point, dumb_point, dumb_point, preimage, htlc, $opt_anchors ) )
896
910
}
897
911
}
898
912
}
@@ -987,7 +1001,7 @@ mod tests {
987
1001
let txid = Txid :: from_hex ( "c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e" ) . unwrap ( ) ;
988
1002
let secp_ctx = Secp256k1 :: new ( ) ;
989
1003
let revk_outp = dumb_revk_output ! ( secp_ctx) ;
990
- let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 0 ) ;
1004
+ let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 0 , false ) ;
991
1005
992
1006
let mut revoked_package = PackageTemplate :: build_package ( txid, 0 , revk_outp, 1000 , true , 100 ) ;
993
1007
let counterparty_package = PackageTemplate :: build_package ( txid, 1 , counterparty_outp, 1000 , true , 100 ) ;
@@ -1051,7 +1065,7 @@ mod tests {
1051
1065
fn test_package_amounts ( ) {
1052
1066
let txid = Txid :: from_hex ( "c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e" ) . unwrap ( ) ;
1053
1067
let secp_ctx = Secp256k1 :: new ( ) ;
1054
- let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 1_000_000 ) ;
1068
+ let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 1_000_000 , false ) ;
1055
1069
1056
1070
let package = PackageTemplate :: build_package ( txid, 0 , counterparty_outp, 1000 , true , 100 ) ;
1057
1071
assert_eq ! ( package. package_amount( ) , 1000 ) ;
@@ -1068,24 +1082,22 @@ mod tests {
1068
1082
{
1069
1083
let revk_outp = dumb_revk_output ! ( secp_ctx) ;
1070
1084
let package = PackageTemplate :: build_package ( txid, 0 , revk_outp, 0 , true , 100 ) ;
1071
- for & opt_anchors in [ false , true ] . iter ( ) {
1072
- assert_eq ! ( package. package_weight( & Script :: new( ) , opt_anchors) , weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize ) ;
1073
- }
1085
+ assert_eq ! ( package. package_weight( & Script :: new( ) ) , weight_sans_output + WEIGHT_REVOKED_OUTPUT as usize ) ;
1074
1086
}
1075
1087
1076
1088
{
1077
- let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 1_000_000 ) ;
1078
- let package = PackageTemplate :: build_package ( txid, 0 , counterparty_outp, 1000 , true , 100 ) ;
1079
1089
for & opt_anchors in [ false , true ] . iter ( ) {
1080
- assert_eq ! ( package. package_weight( & Script :: new( ) , opt_anchors) , weight_sans_output + weight_received_htlc( opt_anchors) as usize ) ;
1090
+ let counterparty_outp = dumb_counterparty_output ! ( secp_ctx, 1_000_000 , opt_anchors) ;
1091
+ let package = PackageTemplate :: build_package ( txid, 0 , counterparty_outp, 1000 , true , 100 ) ;
1092
+ assert_eq ! ( package. package_weight( & Script :: new( ) ) , weight_sans_output + weight_received_htlc( opt_anchors) as usize ) ;
1081
1093
}
1082
1094
}
1083
1095
1084
1096
{
1085
- let counterparty_outp = dumb_counterparty_offered_output ! ( secp_ctx, 1_000_000 ) ;
1086
- let package = PackageTemplate :: build_package ( txid, 0 , counterparty_outp, 1000 , true , 100 ) ;
1087
1097
for & opt_anchors in [ false , true ] . iter ( ) {
1088
- assert_eq ! ( package. package_weight( & Script :: new( ) , opt_anchors) , weight_sans_output + weight_offered_htlc( opt_anchors) as usize ) ;
1098
+ let counterparty_outp = dumb_counterparty_offered_output ! ( secp_ctx, 1_000_000 , opt_anchors) ;
1099
+ let package = PackageTemplate :: build_package ( txid, 0 , counterparty_outp, 1000 , true , 100 ) ;
1100
+ assert_eq ! ( package. package_weight( & Script :: new( ) ) , weight_sans_output + weight_offered_htlc( opt_anchors) as usize ) ;
1089
1101
}
1090
1102
}
1091
1103
}
0 commit comments