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