@@ -8457,6 +8457,43 @@ fn test_concurrent_monitor_claim() {
84578457 }
84588458}
84598459
8460+ #[ test]
8461+ fn test_pre_lockin_no_chan_closed_update ( ) {
8462+ // Test that if a peer closes a channel in response to a funding_created message we don't
8463+ // generate a channel update (as the channel cannot appear on chain without a funding_signed
8464+ // message).
8465+ //
8466+ // Doing so would imply a channel monitor update before the initial channel monitor
8467+ // registration, violating our API guarantees.
8468+ //
8469+ // Previously, full_stack_target managed to hit this case by opening then closing a channel,
8470+ // then opening a second channel with the same funding output as the first (which is not
8471+ // rejected because the first channel does not exist in the ChannelManager) and closing it
8472+ // before receiving funding_signed.
8473+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8474+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8475+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8476+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8477+
8478+ // Create an initial channel
8479+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
8480+ let mut open_chan_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8481+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8482+ let accept_chan_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
8483+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & accept_chan_msg) ;
8484+
8485+ // Move the first channel through the funding flow...
8486+ let ( temporary_channel_id, _tx, funding_output) = create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ;
8487+
8488+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, funding_output) ;
8489+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
8490+
8491+ let funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8492+ let channel_id = :: chain:: transaction:: OutPoint { txid : funding_created_msg. funding_txid , index : funding_created_msg. funding_output_index } . to_channel_id ( ) ;
8493+ nodes[ 0 ] . node . handle_error ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msgs:: ErrorMessage { channel_id, data : "Hi" . to_owned ( ) } ) ;
8494+ assert ! ( nodes[ 0 ] . chain_monitor. added_monitors. lock( ) . unwrap( ) . is_empty( ) ) ;
8495+ }
8496+
84608497#[ test]
84618498fn test_htlc_no_detection ( ) {
84628499 // This test is a mutation to underscore the detection logic bug we had
@@ -8687,3 +8724,143 @@ fn test_onchain_htlc_settlement_after_close() {
86878724 do_test_onchain_htlc_settlement_after_close ( true , false ) ;
86888725 do_test_onchain_htlc_settlement_after_close ( false , false ) ;
86898726}
8727+
8728+ #[ test]
8729+ fn test_duplicate_chan_id ( ) {
8730+ // Test that if a given peer tries to open a channel with the same channel_id as one that is
8731+ // already open we reject it and keep the old channel.
8732+ //
8733+ // Previously, full_stack_target managed to figure out that if you tried to open two channels
8734+ // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
8735+ // the existing channel when we detect the duplicate new channel, screwing up our monitor
8736+ // updating logic for the existing channel.
8737+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8738+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8739+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8740+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8741+
8742+ // Create an initial channel
8743+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
8744+ let mut open_chan_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8745+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8746+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
8747+
8748+ // Try to create a second channel with the same temporary_channel_id as the first and check
8749+ // that it is rejected.
8750+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8751+ {
8752+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8753+ assert_eq ! ( events. len( ) , 1 ) ;
8754+ match events[ 0 ] {
8755+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8756+ // Technically, at this point, nodes[1] would be justified in thinking both the
8757+ // first (valid) and second (invalid) channels are closed, given they both have
8758+ // the same non-temporary channel_id. However, currently we do not, so we just
8759+ // move forward with it.
8760+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
8761+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8762+ } ,
8763+ _ => panic ! ( "Unexpected event" ) ,
8764+ }
8765+ }
8766+
8767+ // Move the first channel through the funding flow...
8768+ let ( temporary_channel_id, tx, funding_output) = create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ;
8769+
8770+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, funding_output) ;
8771+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
8772+
8773+ let mut funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8774+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
8775+ {
8776+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
8777+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
8778+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
8779+ added_monitors. clear ( ) ;
8780+ }
8781+ let funding_signed_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ;
8782+
8783+ let funding_outpoint = :: chain:: transaction:: OutPoint { txid : funding_created_msg. funding_txid , index : funding_created_msg. funding_output_index } ;
8784+ let channel_id = funding_outpoint. to_channel_id ( ) ;
8785+
8786+ // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
8787+ // temporary one).
8788+
8789+ // First try to open a second channel with a temporary channel id equal to the txid-based one.
8790+ // Technically this is allowed by the spec, but we don't support it and there's little reason
8791+ // to. Still, it shouldn't cause any other issues.
8792+ open_chan_msg. temporary_channel_id = channel_id;
8793+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8794+ {
8795+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8796+ assert_eq ! ( events. len( ) , 1 ) ;
8797+ match events[ 0 ] {
8798+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8799+ // Technically, at this point, nodes[1] would be justified in thinking both
8800+ // channels are closed, but currently we do not, so we just move forward with it.
8801+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
8802+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8803+ } ,
8804+ _ => panic ! ( "Unexpected event" ) ,
8805+ }
8806+ }
8807+
8808+ // Now try to create a second channel which has a duplicate funding output.
8809+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
8810+ let open_chan_2_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8811+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_2_msg) ;
8812+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
8813+ create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ; // Get and check the FundingGenerationReady event
8814+
8815+ let funding_created = {
8816+ let mut a_channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
8817+ let mut as_chan = a_channel_lock. by_id . get_mut ( & open_chan_2_msg. temporary_channel_id ) . unwrap ( ) ;
8818+ let logger = test_utils:: TestLogger :: new ( ) ;
8819+ as_chan. get_outbound_funding_created ( funding_outpoint, & & logger) . unwrap ( )
8820+ } ;
8821+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
8822+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created) ;
8823+ // At this point we'll try to add a duplicate channel monitor, which will be rejected, but
8824+ // still needs to be cleared here.
8825+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8826+
8827+ // ...still, nodes[1] will reject the duplicate channel.
8828+ {
8829+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8830+ assert_eq ! ( events. len( ) , 1 ) ;
8831+ match events[ 0 ] {
8832+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8833+ // Technically, at this point, nodes[1] would be justified in thinking both
8834+ // channels are closed, but currently we do not, so we just move forward with it.
8835+ assert_eq ! ( msg. channel_id, channel_id) ;
8836+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8837+ } ,
8838+ _ => panic ! ( "Unexpected event" ) ,
8839+ }
8840+ }
8841+
8842+ // finally, finish creating the original channel and send a payment over it to make sure
8843+ // everything is functional.
8844+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_signed_msg) ;
8845+ {
8846+ let mut added_monitors = nodes[ 0 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
8847+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
8848+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
8849+ added_monitors. clear ( ) ;
8850+ }
8851+
8852+ let events_4 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
8853+ assert_eq ! ( events_4. len( ) , 1 ) ;
8854+ match events_4[ 0 ] {
8855+ Event :: FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
8856+ assert_eq ! ( user_channel_id, 42 ) ;
8857+ assert_eq ! ( * funding_txo, funding_output) ;
8858+ } ,
8859+ _ => panic ! ( "Unexpected event" ) ,
8860+ } ;
8861+
8862+ let ( funding_locked, _) = create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
8863+ let ( announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_locked) ;
8864+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & as_update, & bs_update) ;
8865+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 8000000 , 8_000_000 ) ;
8866+ }
0 commit comments