@@ -13,7 +13,7 @@ use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
13
13
use ln:: onion_utils;
14
14
use ln:: router:: { Route , RouteHop } ;
15
15
use ln:: msgs;
16
- use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler , HTLCFailChannelUpdate , LocalFeatures } ;
16
+ use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler , HTLCFailChannelUpdate , LocalFeatures , ErrorAction } ;
17
17
use util:: test_utils;
18
18
use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
19
19
use util:: errors:: APIError ;
@@ -5515,8 +5515,8 @@ fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
5515
5515
// We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
5516
5516
// HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
5517
5517
5518
- let nodes = create_network ( 2 ) ;
5519
- let chan =create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
5518
+ let nodes = create_network ( 2 , & [ None , None ] ) ;
5519
+ let chan =create_announced_chan_between_nodes ( & nodes, 0 , 1 , LocalFeatures :: new ( ) , LocalFeatures :: new ( ) ) ;
5520
5520
5521
5521
let bs_dust_limit = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan. 2 ) . unwrap ( ) . our_dust_limit_satoshis ;
5522
5522
@@ -5604,8 +5604,8 @@ fn test_no_failure_dust_htlc_local_commitment() {
5604
5604
// Transaction filters for failing back dust htlc based on local commitment txn infos has been
5605
5605
// prone to error, we test here that a dummy transaction don't fail them.
5606
5606
5607
- let nodes = create_network ( 2 ) ;
5608
- let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
5607
+ let nodes = create_network ( 2 , & [ None , None ] ) ;
5608
+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 , LocalFeatures :: new ( ) , LocalFeatures :: new ( ) ) ;
5609
5609
5610
5610
// Rebalance a bit
5611
5611
send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 8000000 ) ;
@@ -5658,8 +5658,8 @@ fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
5658
5658
// Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
5659
5659
// Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
5660
5660
5661
- let nodes = create_network ( 3 ) ;
5662
- let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
5661
+ let nodes = create_network ( 3 , & [ None , None , None ] ) ;
5662
+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 , LocalFeatures :: new ( ) , LocalFeatures :: new ( ) ) ;
5663
5663
5664
5664
let bs_dust_limit = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan. 2 ) . unwrap ( ) . our_dust_limit_satoshis ;
5665
5665
@@ -5778,3 +5778,103 @@ fn test_sweep_outbound_htlc_failure_update() {
5778
5778
do_test_sweep_outbound_htlc_failure_update ( false , false ) ;
5779
5779
do_test_sweep_outbound_htlc_failure_update ( true , false ) ;
5780
5780
}
5781
+
5782
+ #[ test]
5783
+ fn test_upfront_shutdown_script ( ) {
5784
+ // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
5785
+ // enforce it at shutdown message
5786
+
5787
+ let mut config = UserConfig :: new ( ) ;
5788
+ config. channel_options . announced_channel = true ;
5789
+ config. peer_channel_config_limits . force_announced_channel_preference = false ;
5790
+ config. channel_options . commit_upfront_shutdown_pubkey = false ;
5791
+ let nodes = create_network ( 3 , & [ None , Some ( config) , None ] ) ;
5792
+
5793
+ // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
5794
+ let flags = LocalFeatures :: new ( ) ;
5795
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 2 , 1000000 , 1000000 , flags. clone ( ) , flags. clone ( ) ) ;
5796
+ nodes[ 0 ] . node . close_channel ( & OutPoint :: new ( chan. 3 . txid ( ) , 0 ) . to_channel_id ( ) ) . unwrap ( ) ;
5797
+ let mut node_0_shutdown = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendShutdown , nodes[ 2 ] . node. get_our_node_id( ) ) ;
5798
+ node_0_shutdown. scriptpubkey = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) . to_p2sh ( ) ;
5799
+ // Test we enforce upfront_scriptpbukey if by providing a diffrent one at closing that we disconnect peer
5800
+ if let Err ( error) = nodes[ 2 ] . node . handle_shutdown ( & nodes[ 0 ] . node . get_our_node_id ( ) , & node_0_shutdown) {
5801
+ if let Some ( error) = error. action {
5802
+ match error {
5803
+ ErrorAction :: SendErrorMessage { msg } => {
5804
+ assert_eq ! ( msg. data, "Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey" ) ;
5805
+ } ,
5806
+ _ => { assert ! ( false ) ; }
5807
+ }
5808
+ } else { assert ! ( false ) ; }
5809
+ } else { assert ! ( false ) ; }
5810
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
5811
+ assert_eq ! ( events. len( ) , 1 ) ;
5812
+ match events[ 0 ] {
5813
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
5814
+ _ => panic ! ( "Unexpected event" ) ,
5815
+ }
5816
+
5817
+ // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
5818
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 2 , 1000000 , 1000000 , flags. clone ( ) , flags. clone ( ) ) ;
5819
+ nodes[ 0 ] . node . close_channel ( & OutPoint :: new ( chan. 3 . txid ( ) , 0 ) . to_channel_id ( ) ) . unwrap ( ) ;
5820
+ let node_0_shutdown = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendShutdown , nodes[ 2 ] . node. get_our_node_id( ) ) ;
5821
+ // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
5822
+ if let Ok ( _) = nodes[ 2 ] . node . handle_shutdown ( & nodes[ 0 ] . node . get_our_node_id ( ) , & node_0_shutdown) { }
5823
+ else { assert ! ( false ) }
5824
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
5825
+ assert_eq ! ( events. len( ) , 1 ) ;
5826
+ match events[ 0 ] {
5827
+ MessageSendEvent :: SendShutdown { node_id, .. } => { assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) }
5828
+ _ => panic ! ( "Unexpected event" ) ,
5829
+ }
5830
+
5831
+ // We test that if case of peer non-signaling we don't enforce committed script at channel opening
5832
+ let mut flags_no = LocalFeatures :: new ( ) ;
5833
+ flags_no. unset_upfront_shutdown_script ( ) ;
5834
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1000000 , 1000000 , flags_no, flags. clone ( ) ) ;
5835
+ nodes[ 0 ] . node . close_channel ( & OutPoint :: new ( chan. 3 . txid ( ) , 0 ) . to_channel_id ( ) ) . unwrap ( ) ;
5836
+ let mut node_1_shutdown = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendShutdown , nodes[ 1 ] . node. get_our_node_id( ) ) ;
5837
+ node_1_shutdown. scriptpubkey = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) . to_p2sh ( ) ;
5838
+ if let Ok ( _) = nodes[ 1 ] . node . handle_shutdown ( & nodes[ 0 ] . node . get_our_node_id ( ) , & node_1_shutdown) { }
5839
+ else { assert ! ( false ) }
5840
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
5841
+ assert_eq ! ( events. len( ) , 1 ) ;
5842
+ match events[ 0 ] {
5843
+ MessageSendEvent :: SendShutdown { node_id, .. } => { assert_eq ! ( node_id, nodes[ 0 ] . node. get_our_node_id( ) ) }
5844
+ _ => panic ! ( "Unexpected event" ) ,
5845
+ }
5846
+
5847
+ // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
5848
+ // channel smoothly, opt-out is from channel initiator here
5849
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 1 , 0 , 1000000 , 1000000 , flags. clone ( ) , flags. clone ( ) ) ;
5850
+ nodes[ 1 ] . node . close_channel ( & OutPoint :: new ( chan. 3 . txid ( ) , 0 ) . to_channel_id ( ) ) . unwrap ( ) ;
5851
+ let mut node_0_shutdown = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendShutdown , nodes[ 0 ] . node. get_our_node_id( ) ) ;
5852
+ node_0_shutdown. scriptpubkey = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) . to_p2sh ( ) ;
5853
+ if let Ok ( _) = nodes[ 0 ] . node . handle_shutdown ( & nodes[ 1 ] . node . get_our_node_id ( ) , & node_0_shutdown) { }
5854
+ else { assert ! ( false ) }
5855
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
5856
+ assert_eq ! ( events. len( ) , 1 ) ;
5857
+ match events[ 0 ] {
5858
+ MessageSendEvent :: SendShutdown { node_id, .. } => { assert_eq ! ( node_id, nodes[ 1 ] . node. get_our_node_id( ) ) }
5859
+ _ => panic ! ( "Unexpected event" ) ,
5860
+ }
5861
+
5862
+ //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
5863
+ //// channel smoothly
5864
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1000000 , 1000000 , flags. clone ( ) , flags. clone ( ) ) ;
5865
+ nodes[ 1 ] . node . close_channel ( & OutPoint :: new ( chan. 3 . txid ( ) , 0 ) . to_channel_id ( ) ) . unwrap ( ) ;
5866
+ let mut node_0_shutdown = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendShutdown , nodes[ 0 ] . node. get_our_node_id( ) ) ;
5867
+ node_0_shutdown. scriptpubkey = Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_RETURN ) . into_script ( ) . to_p2sh ( ) ;
5868
+ if let Ok ( _) = nodes[ 0 ] . node . handle_shutdown ( & nodes[ 1 ] . node . get_our_node_id ( ) , & node_0_shutdown) { }
5869
+ else { assert ! ( false ) }
5870
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
5871
+ assert_eq ! ( events. len( ) , 2 ) ;
5872
+ match events[ 0 ] {
5873
+ MessageSendEvent :: SendShutdown { node_id, .. } => { assert_eq ! ( node_id, nodes[ 1 ] . node. get_our_node_id( ) ) }
5874
+ _ => panic ! ( "Unexpected event" ) ,
5875
+ }
5876
+ match events[ 1 ] {
5877
+ MessageSendEvent :: SendClosingSigned { node_id, .. } => { assert_eq ! ( node_id, nodes[ 1 ] . node. get_our_node_id( ) ) }
5878
+ _ => panic ! ( "Unexpected event" ) ,
5879
+ }
5880
+ }
0 commit comments