@@ -724,10 +724,16 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
724
724
725
725
#[ cfg( test) ]
726
726
mod tests {
727
- use :: { check_added_monitors, get_local_commitment_txn} ;
727
+ use bitcoin:: BlockHeader ;
728
+ use :: { check_added_monitors, check_closed_broadcast, check_closed_event, expect_payment_sent} ;
729
+ use :: { get_local_commitment_txn, get_route_and_payment_hash, unwrap_send_err} ;
730
+ use chain:: { ChannelMonitorUpdateErr , Confirm , Watch } ;
731
+ use chain:: channelmonitor:: LATENCY_GRACE_PERIOD_BLOCKS ;
732
+ use ln:: channelmanager:: PaymentSendFailure ;
728
733
use ln:: features:: InitFeatures ;
729
734
use ln:: functional_test_utils:: * ;
730
- use util:: events:: MessageSendEventsProvider ;
735
+ use util:: errors:: APIError ;
736
+ use util:: events:: { ClosureReason , MessageSendEventsProvider } ;
731
737
use util:: test_utils:: { OnRegisterOutput , TxOutReference } ;
732
738
733
739
/// Tests that in-block dependent transactions are processed by `block_connected` when not
@@ -772,4 +778,78 @@ mod tests {
772
778
nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
773
779
nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
774
780
}
781
+
782
+ fn do_chainsync_pauses_events ( block_timeout : bool ) {
783
+ // When a chainsync monitor update occurs, any MonitorUpdates should be held before being
784
+ // passed upstream. This tests that behavior, as well as some ways it might go wrong.
785
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
786
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
787
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
788
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
789
+ let channel = create_announced_chan_between_nodes (
790
+ & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
791
+
792
+ // Get a route for later and rebalance the channel somewhat
793
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 10_000_000 ) ;
794
+ let ( route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 1 ] , 100_000 ) ;
795
+
796
+ // First route a payment that we will claim on chain and give the recipient the preimage.
797
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1_000_000 ) . 0 ;
798
+ nodes[ 1 ] . node . claim_funds ( payment_preimage) ;
799
+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
800
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
801
+ let remote_txn = get_local_commitment_txn ! ( nodes[ 1 ] , channel. 2 ) ;
802
+ assert_eq ! ( remote_txn. len( ) , 2 ) ;
803
+
804
+ // Temp-fail the block connection which will hold the channel-closed event
805
+ chanmon_cfgs[ 0 ] . persister . chain_sync_monitor_persistences . lock ( ) . unwrap ( ) . clear ( ) ;
806
+ chanmon_cfgs[ 0 ] . persister . set_update_ret ( Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ) ;
807
+
808
+ // Connect B's commitment transaction, but only to the ChainMonitor/ChannelMonitor. The
809
+ // channel is now closed, but the ChannelManager doesn't know that yet.
810
+ let new_header = BlockHeader {
811
+ version : 2 , time : 0 , bits : 0 , nonce : 0 ,
812
+ prev_blockhash : nodes[ 0 ] . best_block_info ( ) . 0 ,
813
+ merkle_root : Default :: default ( ) } ;
814
+ nodes[ 0 ] . chain_monitor . chain_monitor . transactions_confirmed ( & new_header,
815
+ & [ ( 0 , & remote_txn[ 0 ] ) , ( 1 , & remote_txn[ 1 ] ) ] , nodes[ 0 ] . best_block_info ( ) . 1 + 1 ) ;
816
+ assert ! ( nodes[ 0 ] . chain_monitor. release_pending_monitor_events( ) . is_empty( ) ) ;
817
+
818
+ // If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
819
+ // the update through to the ChannelMonitor which will refuse it (as the channel is closed).
820
+ chanmon_cfgs[ 0 ] . persister . set_update_ret ( Ok ( ( ) ) ) ;
821
+ unwrap_send_err ! ( nodes[ 0 ] . node. send_payment( & route, second_payment_hash, & Some ( second_payment_secret) ) ,
822
+ true , APIError :: ChannelUnavailable { ref err } ,
823
+ assert!( err. contains( "ChannelMonitor storage failure" ) ) ) ;
824
+ check_added_monitors ! ( nodes[ 0 ] , 2 ) ; // After the failure we generate a close-channel monitor update
825
+ check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
826
+ check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: ProcessingError { err: "ChannelMonitor storage failure" . to_string( ) } ) ;
827
+
828
+ // However, as the ChainMonitor is still waiting for the original persistence to complete,
829
+ // it won't yet release the MonitorEvents.
830
+ assert ! ( nodes[ 0 ] . chain_monitor. release_pending_monitor_events( ) . is_empty( ) ) ;
831
+
832
+ if block_timeout {
833
+ // After three blocks, pending MontiorEvents should be released either way.
834
+ let latest_header = BlockHeader {
835
+ version : 2 , time : 0 , bits : 0 , nonce : 0 ,
836
+ prev_blockhash : nodes[ 0 ] . best_block_info ( ) . 0 ,
837
+ merkle_root : Default :: default ( ) } ;
838
+ nodes[ 0 ] . chain_monitor . chain_monitor . best_block_updated ( & latest_header, nodes[ 0 ] . best_block_info ( ) . 1 + LATENCY_GRACE_PERIOD_BLOCKS ) ;
839
+ } else {
840
+ for ( funding_outpoint, update_ids) in chanmon_cfgs[ 0 ] . persister . chain_sync_monitor_persistences . lock ( ) . unwrap ( ) . iter ( ) {
841
+ for update_id in update_ids {
842
+ nodes[ 0 ] . chain_monitor . chain_monitor . channel_monitor_updated ( * funding_outpoint, * update_id) . unwrap ( ) ;
843
+ }
844
+ }
845
+ }
846
+
847
+ expect_payment_sent ! ( nodes[ 0 ] , payment_preimage) ;
848
+ }
849
+
850
+ #[ test]
851
+ fn chainsync_pauses_events ( ) {
852
+ do_chainsync_pauses_events ( false ) ;
853
+ do_chainsync_pauses_events ( true ) ;
854
+ }
775
855
}
0 commit comments