@@ -8687,3 +8687,143 @@ fn test_onchain_htlc_settlement_after_close() {
86878687 do_test_onchain_htlc_settlement_after_close ( true , false ) ;
86888688 do_test_onchain_htlc_settlement_after_close ( false , false ) ;
86898689}
8690+
8691+ #[ test]
8692+ fn test_duplicate_chan_id ( ) {
8693+ // Test that if a given peer tries to open a channel with the same channel_id as one that is
8694+ // already open we reject it and keep the old channel.
8695+ //
8696+ // Previously, full_stack_target managed to figure out that if you tried to open two channels
8697+ // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
8698+ // the existing channel when we detect the duplicate new channel, screwing up our monitor
8699+ // updating logic for the existing channel.
8700+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8701+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8702+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8703+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8704+
8705+ // Create an initial channel
8706+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
8707+ let mut open_chan_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8708+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8709+ 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( ) ) ) ;
8710+
8711+ // Try to create a second channel with the same temporary_channel_id as the first and check
8712+ // that it is rejected.
8713+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8714+ {
8715+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8716+ assert_eq ! ( events. len( ) , 1 ) ;
8717+ match events[ 0 ] {
8718+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8719+ // Technically, at this point, nodes[1] would be justified in thinking both the
8720+ // first (valid) and second (invalid) channels are closed, given they both have
8721+ // the same non-temporary channel_id. However, currently we do not, so we just
8722+ // move forward with it.
8723+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
8724+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8725+ } ,
8726+ _ => panic ! ( "Unexpected event" ) ,
8727+ }
8728+ }
8729+
8730+ // Move the first channel through the funding flow...
8731+ let ( temporary_channel_id, tx, funding_output) = create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ;
8732+
8733+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, funding_output) ;
8734+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
8735+
8736+ let mut funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8737+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
8738+ {
8739+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
8740+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
8741+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
8742+ added_monitors. clear ( ) ;
8743+ }
8744+ let funding_signed_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ;
8745+
8746+ let funding_outpoint = :: chain:: transaction:: OutPoint { txid : funding_created_msg. funding_txid , index : funding_created_msg. funding_output_index } ;
8747+ let channel_id = funding_outpoint. to_channel_id ( ) ;
8748+
8749+ // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
8750+ // temporary one).
8751+
8752+ // First try to open a second channel with a temporary channel id equal to the txid-based one.
8753+ // Technically this is allowed by the spec, but we don't support it and there's little reason
8754+ // to. Still, it shouldn't cause any other issues.
8755+ open_chan_msg. temporary_channel_id = channel_id;
8756+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
8757+ {
8758+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8759+ assert_eq ! ( events. len( ) , 1 ) ;
8760+ match events[ 0 ] {
8761+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8762+ // Technically, at this point, nodes[1] would be justified in thinking both
8763+ // channels are closed, but currently we do not, so we just move forward with it.
8764+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
8765+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8766+ } ,
8767+ _ => panic ! ( "Unexpected event" ) ,
8768+ }
8769+ }
8770+
8771+ // Now try to create a second channel which has a duplicate funding output.
8772+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
8773+ let open_chan_2_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8774+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_2_msg) ;
8775+ 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( ) ) ) ;
8776+ create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ; // Get and check the FundingGenerationReady event
8777+
8778+ let funding_created = {
8779+ let mut a_channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
8780+ let mut as_chan = a_channel_lock. by_id . get_mut ( & open_chan_2_msg. temporary_channel_id ) . unwrap ( ) ;
8781+ let logger = test_utils:: TestLogger :: new ( ) ;
8782+ as_chan. get_outbound_funding_created ( funding_outpoint, & & logger) . unwrap ( )
8783+ } ;
8784+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
8785+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created) ;
8786+ // At this point we'll try to add a duplicate channel monitor, which will be rejected, but
8787+ // still needs to be cleared here.
8788+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8789+
8790+ // ...still, nodes[1] will reject the duplicate channel.
8791+ {
8792+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8793+ assert_eq ! ( events. len( ) , 1 ) ;
8794+ match events[ 0 ] {
8795+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
8796+ // Technically, at this point, nodes[1] would be justified in thinking both
8797+ // channels are closed, but currently we do not, so we just move forward with it.
8798+ assert_eq ! ( msg. channel_id, channel_id) ;
8799+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8800+ } ,
8801+ _ => panic ! ( "Unexpected event" ) ,
8802+ }
8803+ }
8804+
8805+ // finally, finish creating the original channel and send a payment over it to make sure
8806+ // everything is functional.
8807+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_signed_msg) ;
8808+ {
8809+ let mut added_monitors = nodes[ 0 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
8810+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
8811+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
8812+ added_monitors. clear ( ) ;
8813+ }
8814+
8815+ let events_4 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
8816+ assert_eq ! ( events_4. len( ) , 1 ) ;
8817+ match events_4[ 0 ] {
8818+ Event :: FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
8819+ assert_eq ! ( user_channel_id, 42 ) ;
8820+ assert_eq ! ( * funding_txo, funding_output) ;
8821+ } ,
8822+ _ => panic ! ( "Unexpected event" ) ,
8823+ } ;
8824+
8825+ let ( funding_locked, _) = create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
8826+ let ( announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_locked) ;
8827+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & as_update, & bs_update) ;
8828+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 8000000 , 8_000_000 ) ;
8829+ }
0 commit comments