@@ -20,6 +20,7 @@ use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureRe
20
20
use bitcoin:: blockdata:: script:: Builder ;
21
21
use bitcoin:: blockdata:: opcodes;
22
22
use bitcoin:: secp256k1:: Secp256k1 ;
23
+ use bitcoin:: Transaction ;
23
24
24
25
use prelude:: * ;
25
26
@@ -82,6 +83,17 @@ fn chanmon_fail_from_stale_commitment() {
82
83
expect_payment_failed_with_update ! ( nodes[ 0 ] , payment_hash, false , update_a. contents. short_channel_id, true ) ;
83
84
}
84
85
86
+ fn test_spendable_output < ' a , ' b , ' c , ' d > ( node : & ' a Node < ' b , ' c , ' d > , spendable_tx : & Transaction ) {
87
+ let mut spendable = node. chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
88
+ assert_eq ! ( spendable. len( ) , 1 ) ;
89
+ if let Event :: SpendableOutputs { outputs } = spendable. pop ( ) . unwrap ( ) {
90
+ assert_eq ! ( outputs. len( ) , 1 ) ;
91
+ let spend_tx = node. keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
92
+ Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
93
+ check_spends ! ( spend_tx, spendable_tx) ;
94
+ } else { panic ! ( ) ; }
95
+ }
96
+
85
97
#[ test]
86
98
fn chanmon_claim_value_coop_close ( ) {
87
99
// Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
@@ -155,23 +167,9 @@ fn chanmon_claim_value_coop_close() {
155
167
assert_eq ! ( Vec :: <Balance >:: new( ) ,
156
168
nodes[ 1 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ;
157
169
158
- let mut node_a_spendable = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
159
- assert_eq ! ( node_a_spendable. len( ) , 1 ) ;
160
- if let Event :: SpendableOutputs { outputs } = node_a_spendable. pop ( ) . unwrap ( ) {
161
- assert_eq ! ( outputs. len( ) , 1 ) ;
162
- let spend_tx = nodes[ 0 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
163
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
164
- check_spends ! ( spend_tx, shutdown_tx[ 0 ] ) ;
165
- }
170
+ test_spendable_output ( & nodes[ 0 ] , & shutdown_tx[ 0 ] ) ;
171
+ test_spendable_output ( & nodes[ 1 ] , & shutdown_tx[ 0 ] ) ;
166
172
167
- let mut node_b_spendable = nodes[ 1 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
168
- assert_eq ! ( node_b_spendable. len( ) , 1 ) ;
169
- if let Event :: SpendableOutputs { outputs } = node_b_spendable. pop ( ) . unwrap ( ) {
170
- assert_eq ! ( outputs. len( ) , 1 ) ;
171
- let spend_tx = nodes[ 1 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
172
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
173
- check_spends ! ( spend_tx, shutdown_tx[ 0 ] ) ;
174
- }
175
173
check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: CooperativeClosure ) ;
176
174
check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: CooperativeClosure ) ;
177
175
}
@@ -384,15 +382,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
384
382
} ] ) ,
385
383
sorted_vec( nodes[ 1 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ) ;
386
384
387
- let mut node_a_spendable = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
388
- assert_eq ! ( node_a_spendable. len( ) , 1 ) ;
389
- if let Event :: SpendableOutputs { outputs } = node_a_spendable. pop ( ) . unwrap ( ) {
390
- assert_eq ! ( outputs. len( ) , 1 ) ;
391
- let spend_tx = nodes[ 0 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
392
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
393
- check_spends ! ( spend_tx, remote_txn[ 0 ] ) ;
394
- }
395
-
385
+ test_spendable_output ( & nodes[ 0 ] , & remote_txn[ 0 ] ) ;
396
386
assert ! ( nodes[ 1 ] . chain_monitor. chain_monitor. get_and_clear_pending_events( ) . is_empty( ) ) ;
397
387
398
388
// After broadcasting the HTLC claim transaction, node A will still consider the HTLC
@@ -449,14 +439,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
449
439
nodes[ 0 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ;
450
440
expect_payment_failed ! ( nodes[ 0 ] , timeout_payment_hash, true ) ;
451
441
452
- let mut node_a_spendable = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
453
- assert_eq ! ( node_a_spendable. len( ) , 1 ) ;
454
- if let Event :: SpendableOutputs { outputs } = node_a_spendable. pop ( ) . unwrap ( ) {
455
- assert_eq ! ( outputs. len( ) , 1 ) ;
456
- let spend_tx = nodes[ 0 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
457
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
458
- check_spends ! ( spend_tx, a_broadcast_txn[ 2 ] ) ;
459
- } else { panic ! ( ) ; }
442
+ test_spendable_output ( & nodes[ 0 ] , & a_broadcast_txn[ 2 ] ) ;
460
443
461
444
// Node B will no longer consider the HTLC "contentious" after the HTLC claim transaction
462
445
// confirms, and consider it simply "awaiting confirmations". Note that it has to wait for the
@@ -479,15 +462,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
479
462
// After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
480
463
// only the HTLCs claimable on node B.
481
464
connect_blocks ( & nodes[ 1 ] , node_b_commitment_claimable - nodes[ 1 ] . best_block_info ( ) . 1 ) ;
482
-
483
- let mut node_b_spendable = nodes[ 1 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
484
- assert_eq ! ( node_b_spendable. len( ) , 1 ) ;
485
- if let Event :: SpendableOutputs { outputs } = node_b_spendable. pop ( ) . unwrap ( ) {
486
- assert_eq ! ( outputs. len( ) , 1 ) ;
487
- let spend_tx = nodes[ 1 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
488
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
489
- check_spends ! ( spend_tx, remote_txn[ 0 ] ) ;
490
- }
465
+ test_spendable_output ( & nodes[ 1 ] , & remote_txn[ 0 ] ) ;
491
466
492
467
assert_eq ! ( sorted_vec( vec![ Balance :: ClaimableAwaitingConfirmations {
493
468
claimable_amount_satoshis: 3_000 ,
@@ -501,15 +476,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
501
476
// After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
502
477
// have only one HTLC output left spendable.
503
478
connect_blocks ( & nodes[ 1 ] , node_b_htlc_claimable - nodes[ 1 ] . best_block_info ( ) . 1 ) ;
504
-
505
- let mut node_b_spendable = nodes[ 1 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
506
- assert_eq ! ( node_b_spendable. len( ) , 1 ) ;
507
- if let Event :: SpendableOutputs { outputs } = node_b_spendable. pop ( ) . unwrap ( ) {
508
- assert_eq ! ( outputs. len( ) , 1 ) ;
509
- let spend_tx = nodes[ 1 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
510
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
511
- check_spends ! ( spend_tx, b_broadcast_txn[ 0 ] ) ;
512
- } else { panic ! ( ) ; }
479
+ test_spendable_output ( & nodes[ 1 ] , & b_broadcast_txn[ 0 ] ) ;
513
480
514
481
assert_eq ! ( vec![ Balance :: ContentiousClaimable {
515
482
claimable_amount_satoshis: 4_000 ,
@@ -704,25 +671,11 @@ fn test_balances_on_local_commitment_htlcs() {
704
671
confirmation_height: node_a_htlc_claimable,
705
672
} ] ,
706
673
nodes[ 0 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) ) ;
707
- let mut node_a_spendable = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
708
- assert_eq ! ( node_a_spendable. len( ) , 1 ) ;
709
- if let Event :: SpendableOutputs { outputs } = node_a_spendable. pop ( ) . unwrap ( ) {
710
- assert_eq ! ( outputs. len( ) , 1 ) ;
711
- let spend_tx = nodes[ 0 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
712
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
713
- check_spends ! ( spend_tx, as_txn[ 0 ] ) ;
714
- }
674
+ test_spendable_output ( & nodes[ 0 ] , & as_txn[ 0 ] ) ;
715
675
716
676
// Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
717
677
// `SpendableOutputs` event and removing the claimable balance entry.
718
678
connect_blocks ( & nodes[ 0 ] , node_a_htlc_claimable - nodes[ 0 ] . best_block_info ( ) . 1 ) ;
719
679
assert ! ( nodes[ 0 ] . chain_monitor. chain_monitor. get_monitor( funding_outpoint) . unwrap( ) . get_claimable_balances( ) . is_empty( ) ) ;
720
- let mut node_a_spendable = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
721
- assert_eq ! ( node_a_spendable. len( ) , 1 ) ;
722
- if let Event :: SpendableOutputs { outputs } = node_a_spendable. pop ( ) . unwrap ( ) {
723
- assert_eq ! ( outputs. len( ) , 1 ) ;
724
- let spend_tx = nodes[ 0 ] . keys_manager . backing . spend_spendable_outputs ( & [ & outputs[ 0 ] ] , Vec :: new ( ) ,
725
- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) , 253 , & Secp256k1 :: new ( ) ) . unwrap ( ) ;
726
- check_spends ! ( spend_tx, as_txn[ 1 ] ) ;
727
- }
680
+ test_spendable_output ( & nodes[ 0 ] , & as_txn[ 1 ] ) ;
728
681
}
0 commit comments