77// You may not use this file except in accordance with one or both of these
88// licenses.
99
10- //! Tests of the onion error messages/codes which are returned when routing a payment fails.
11- //! These tests work by standing up full nodes and route payments across the network, checking the
12- //! returned errors decode to the correct thing.
10+ // General unit tests that cross several files. Basically functions as an overflow for taking
11+ // smaller tests out of functional_tests.rs to avoid making it any larger.
1312
1413use chain:: channelmonitor:: { CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS } ;
1514use ln:: channelmanager:: { HTLCForwardInfo , PaymentPreimage , PaymentHash } ;
1615use ln:: onion_utils;
1716use routing:: router:: { Route , get_route} ;
1817use ln:: features:: InitFeatures ;
1918use ln:: msgs;
20- use ln:: msgs:: { ChannelMessageHandler , HTLCFailChannelUpdate , OptionalField } ;
19+ use ln:: msgs:: { ChannelMessageHandler , ErrorAction , HTLCFailChannelUpdate , OptionalField } ;
2120use util:: test_utils;
2221use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
2322use util:: ser:: { Writeable , Writer } ;
@@ -38,6 +37,11 @@ use std::io;
3837
3938use ln:: functional_test_utils:: * ;
4039
40+
41+ // Tests of the onion error messages/codes which are returned when routing a payment fails.
42+ // These tests work by standing up full nodes and route payments across the network, checking the
43+ // returned errors decode to the correct thing.
44+
4145fn run_onion_failure_test < F1 , F2 > ( _name : & str , test_case : u8 , nodes : & Vec < Node > , route : & Route , payment_hash : & PaymentHash , callback_msg : F1 , callback_node : F2 , expected_retryable : bool , expected_error_code : Option < u16 > , expected_channel_update : Option < HTLCFailChannelUpdate > )
4246 where F1 : for < ' a > FnMut ( & ' a mut msgs:: UpdateAddHTLC ) ,
4347 F2 : FnMut ( ) ,
@@ -511,3 +515,135 @@ fn test_onion_failure() {
511515}
512516
513517
518+ #[ test]
519+ fn test_duplicate_chan_id ( ) {
520+ // Test that if a given peer tries to open a channel with the same channel_id as one that is
521+ // already open we reject it and keep the old channel.
522+ //
523+ // Previously, full_stack_target managed to figure out that if you tried to open two channels
524+ // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
525+ // the existing channel when we detect the duplicate new channel, screwing up our monitor
526+ // updating logic for the existing channel.
527+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
528+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
529+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
530+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
531+
532+ // Create an initial channel
533+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
534+ let mut open_chan_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
535+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
536+ let accept_chan_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
537+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & accept_chan_msg) ;
538+
539+ // Try to create a second channel with the same temporary_channel_id as the first and check
540+ // that it is rejected.
541+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
542+ {
543+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
544+ assert_eq ! ( events. len( ) , 1 ) ;
545+ match events[ 0 ] {
546+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
547+ // Technically, at this point, nodes[1] would be justified in thinking both
548+ // channels are closed, but currently we do not, so we just move forward with it.
549+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
550+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
551+ } ,
552+ _ => panic ! ( "Unexpected event" ) ,
553+ }
554+ }
555+
556+ // Move the first channel through the funding flow...
557+ let ( temporary_channel_id, tx, funding_output) = create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ;
558+
559+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, funding_output) ;
560+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
561+
562+ let mut funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
563+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
564+ {
565+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
566+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
567+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
568+ added_monitors. clear ( ) ;
569+ }
570+ let funding_signed_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ;
571+
572+ let channel_id = :: chain:: transaction:: OutPoint { txid : funding_created_msg. funding_txid , index : funding_created_msg. funding_output_index } . to_channel_id ( ) ;
573+
574+ // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
575+ // temporary one).
576+
577+ // First try to open a second channel with a temporary channel id equal to the txid-based one.
578+ // Technically this is allowed by the spec, but we don't support it and there's little reason
579+ // to. Still, it shouldn't cause any other issues.
580+ open_chan_msg. temporary_channel_id = channel_id;
581+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_msg) ;
582+ {
583+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
584+ assert_eq ! ( events. len( ) , 1 ) ;
585+ match events[ 0 ] {
586+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
587+ // Technically, at this point, nodes[1] would be justified in thinking both
588+ // channels are closed, but currently we do not, so we just move forward with it.
589+ assert_eq ! ( msg. channel_id, open_chan_msg. temporary_channel_id) ;
590+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
591+ } ,
592+ _ => panic ! ( "Unexpected event" ) ,
593+ }
594+ }
595+
596+ // Now try to create a second channel which has a duplicate funding output.
597+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 42 , None ) . unwrap ( ) ;
598+ let open_chan_2_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
599+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_chan_2_msg) ;
600+ 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( ) ) ) ;
601+ create_funding_transaction ( & nodes[ 0 ] , 100000 , 42 ) ; // Get and check the FundingGenerationReady event
602+
603+ nodes[ 0 ] . node . funding_transaction_generated_dup_funding_id ( & open_chan_2_msg. temporary_channel_id , funding_output) ;
604+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
605+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
606+ // At this point we'll try to add a duplicate channel monitor, which will be rejected, but
607+ // still needs to be cleared here.
608+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
609+
610+ // ...still, nodes[1] will reject the duplicate channel.
611+ {
612+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
613+ assert_eq ! ( events. len( ) , 1 ) ;
614+ match events[ 0 ] {
615+ MessageSendEvent :: HandleError { action : ErrorAction :: SendErrorMessage { ref msg } , node_id } => {
616+ // Technically, at this point, nodes[1] would be justified in thinking both
617+ // channels are closed, but currently we do not, so we just move forward with it.
618+ assert_eq ! ( msg. channel_id, channel_id) ;
619+ assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
620+ } ,
621+ _ => panic ! ( "Unexpected event" ) ,
622+ }
623+ }
624+
625+ // finally, finish creating the original channel and send a payment over it to make sure
626+ // everything is functional.
627+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_signed_msg) ;
628+ {
629+ let mut added_monitors = nodes[ 0 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
630+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
631+ assert_eq ! ( added_monitors[ 0 ] . 0 , funding_output) ;
632+ added_monitors. clear ( ) ;
633+ }
634+
635+ let events_4 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
636+ assert_eq ! ( events_4. len( ) , 1 ) ;
637+ match events_4[ 0 ] {
638+ Event :: FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
639+ assert_eq ! ( user_channel_id, 42 ) ;
640+ assert_eq ! ( * funding_txo, funding_output) ;
641+ } ,
642+ _ => panic ! ( "Unexpected event" ) ,
643+ } ;
644+
645+ let ( funding_locked, _) = create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
646+ let ( announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_locked) ;
647+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & as_update, & bs_update) ;
648+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 8000000 , 8_000_000 ) ;
649+ }
0 commit comments