@@ -182,12 +182,18 @@ fn sorted_vec<T: Ord>(mut v: Vec<T>) -> Vec<T> {
182
182
v
183
183
}
184
184
185
- #[ test]
186
- fn test_claim_value_force_close ( ) {
185
+ fn do_test_claim_value_force_close ( prev_commitment_tx : bool ) {
187
186
// Tests `get_claimable_balances` with an HTLC across a force-close.
188
187
// We build a channel with an HTLC pending, then force close the channel and check that the
189
188
// `get_claimable_balances` return value is correct as transactions confirm on-chain.
190
- let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
189
+ let mut chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
190
+ if prev_commitment_tx {
191
+ // We broadcast a second-to-latest commitment transaction, without providing the revocation
192
+ // secret to the counterparty. However, because we always immediately take the revocation
193
+ // secret from the keys_manager, we would panic at broadcast as we're trying to sign a
194
+ // transaction which, from the point of view of our keys_manager, is revoked.
195
+ chanmon_cfgs[ 1 ] . keys_manager . disable_revocation_policy_check = true ;
196
+ }
191
197
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
192
198
let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
193
199
let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
@@ -230,31 +236,52 @@ fn test_claim_value_force_close() {
230
236
231
237
nodes[ 1 ] . node . claim_funds ( payment_preimage) ;
232
238
check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
233
- get_htlc_update_msgs ! ( & nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
239
+ let b_htlc_msgs = get_htlc_update_msgs ! ( & nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
234
240
// We claim the dust payment here as well, but it won't impact our claimable balances as its
235
241
// dust and thus doesn't appear on chain at all.
236
242
nodes[ 1 ] . node . claim_funds ( dust_payment_preimage) ;
237
243
check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
238
244
nodes[ 1 ] . node . claim_funds ( timeout_payment_preimage) ;
239
245
check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
240
246
247
+ if prev_commitment_tx {
248
+ // To build a previous commitment transaction, deliver one round of commitment messages.
249
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & b_htlc_msgs. update_fulfill_htlcs [ 0 ] ) ;
250
+ expect_payment_sent ! ( nodes[ 0 ] , payment_preimage) ;
251
+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & b_htlc_msgs. commitment_signed ) ;
252
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
253
+ let ( as_raa, as_cs) = get_revoke_commit_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
254
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_raa) ;
255
+ let _htlc_updates = get_htlc_update_msgs ! ( & nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
256
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
257
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_cs) ;
258
+ let _bs_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 0 ] . node. get_our_node_id( ) ) ;
259
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
260
+ }
261
+
241
262
// Once B has received the payment preimage, it includes the value of the HTLC in its
242
263
// "claimable if you were to close the channel" balance.
243
- assert_eq ! ( sorted_vec ( vec![ ClaimableBalance :: ClaimableOnChannelClose {
264
+ let mut a_expected_balances = vec ! [ ClaimableBalance :: ClaimableOnChannelClose {
244
265
claimable_amount_satoshis: 1_000_000 - // Channel funding value in satoshis
245
266
4_000 - // The to-be-failed HTLC value in satoshis
246
267
3_000 - // The claimed HTLC value in satoshis
247
268
1_000 - // The push_msat value in satoshis
248
269
3 - // The dust HTLC value in satoshis
249
270
// The commitment transaction fee with two HTLC outputs:
250
- chan_feerate * ( channel:: COMMITMENT_TX_BASE_WEIGHT + 2 * channel:: COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ,
251
- } , ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
252
- claimable_amount_satoshis: 3_000 ,
253
- claimable_height: htlc_cltv_timeout,
271
+ chan_feerate * ( channel:: COMMITMENT_TX_BASE_WEIGHT +
272
+ if prev_commitment_tx { 1 } else { 2 } *
273
+ channel:: COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ,
254
274
} , ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
255
275
claimable_amount_satoshis: 4_000 ,
256
276
claimable_height: htlc_cltv_timeout,
257
- } ] ) ,
277
+ } ] ;
278
+ if !prev_commitment_tx {
279
+ a_expected_balances. push ( ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
280
+ claimable_amount_satoshis : 3_000 ,
281
+ claimable_height : htlc_cltv_timeout,
282
+ } ) ;
283
+ }
284
+ assert_eq ! ( sorted_vec( a_expected_balances) ,
258
285
sorted_vec( nodes[ 0 ] . chain_monitor. chain_monitor. monitors. read( ) . unwrap( ) . get( & funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ) ;
259
286
assert_eq ! ( vec![ ClaimableBalance :: ClaimableOnChannelClose {
260
287
claimable_amount_satoshis: 1_000 + 3_000 + 4_000 ,
@@ -268,9 +295,13 @@ fn test_claim_value_force_close() {
268
295
mine_transaction ( & nodes[ 1 ] , & remote_txn[ 0 ] ) ;
269
296
270
297
let b_broadcast_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
271
- assert_eq ! ( b_broadcast_txn. len( ) , 5 ) ;
272
- assert_eq ! ( b_broadcast_txn[ 0 ] , b_broadcast_txn[ 3 ] ) ;
273
- assert_eq ! ( b_broadcast_txn[ 1 ] , b_broadcast_txn[ 4 ] ) ;
298
+ assert_eq ! ( b_broadcast_txn. len( ) , if prev_commitment_tx { 4 } else { 5 } ) ;
299
+ if prev_commitment_tx {
300
+ check_spends ! ( b_broadcast_txn[ 3 ] , b_broadcast_txn[ 2 ] ) ;
301
+ } else {
302
+ assert_eq ! ( b_broadcast_txn[ 0 ] , b_broadcast_txn[ 3 ] ) ;
303
+ assert_eq ! ( b_broadcast_txn[ 1 ] , b_broadcast_txn[ 4 ] ) ;
304
+ }
274
305
// b_broadcast_txn[0] should spend the HTLC output of the commitment tx for 3_000 sats
275
306
check_spends ! ( b_broadcast_txn[ 0 ] , remote_txn[ 0 ] ) ;
276
307
check_spends ! ( b_broadcast_txn[ 1 ] , remote_txn[ 0 ] ) ;
@@ -365,7 +396,9 @@ fn test_claim_value_force_close() {
365
396
// After broadcasting the HTLC claim transaction, node A will still consider the HTLC
366
397
// possibly-claimable up to ANTI_REORG_DELAY, at which point it will drop it.
367
398
mine_transaction ( & nodes[ 0 ] , & b_broadcast_txn[ 0 ] ) ;
368
- expect_payment_sent ! ( nodes[ 0 ] , payment_preimage) ;
399
+ if !prev_commitment_tx {
400
+ expect_payment_sent ! ( nodes[ 0 ] , payment_preimage) ;
401
+ }
369
402
assert_eq ! ( sorted_vec( vec![ ClaimableBalance :: MaybeClaimableHTLCAwaitingTimeout {
370
403
claimable_amount_satoshis: 3_000 ,
371
404
claimable_height: htlc_cltv_timeout,
@@ -493,3 +526,9 @@ fn test_claim_value_force_close() {
493
526
assert_eq ! ( Vec :: <ClaimableBalance >:: new( ) ,
494
527
nodes[ 1 ] . chain_monitor. chain_monitor. monitors. read( ) . unwrap( ) . get( & funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ;
495
528
}
529
+
530
+ #[ test]
531
+ fn test_claim_value_force_close ( ) {
532
+ do_test_claim_value_force_close ( true ) ;
533
+ do_test_claim_value_force_close ( false ) ;
534
+ }
0 commit comments