@@ -23,7 +23,8 @@ use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
2323use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentSendFailure , BREAKDOWN_TIMEOUT , MIN_CLTV_EXPIRY_DELTA } ;
2424use ln:: channel:: { Channel , ChannelError } ;
2525use ln:: { chan_utils, onion_utils} ;
26- use routing:: router:: { Route , RouteHop , get_route} ;
26+ use routing:: router:: { Route , RouteHop , RouteHint , RouteHintHop , get_route} ;
27+ use routing:: network_graph:: RoutingFees ;
2728use ln:: features:: { ChannelFeatures , InitFeatures , InvoiceFeatures , NodeFeatures } ;
2829use ln:: msgs;
2930use ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler , HTLCFailChannelUpdate , ErrorAction } ;
@@ -7947,6 +7948,151 @@ fn test_announce_disable_channels() {
79477948 }
79487949}
79497950
7951+ #[ test]
7952+ fn test_priv_forwarding_rejection ( ) {
7953+ // If we have a private channel with outbound liquidity, and
7954+ // UserConfig::accept_forwards_to_priv_channels is set to false, we should reject any attempts
7955+ // to forward through that channel.
7956+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
7957+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
7958+ let mut no_announce_cfg = test_default_channel_config ( ) ;
7959+ no_announce_cfg. channel_options . announced_channel = false ;
7960+ no_announce_cfg. accept_forwards_to_priv_channels = false ;
7961+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , Some ( no_announce_cfg) , None ] ) ;
7962+ let persister: test_utils:: TestPersister ;
7963+ let new_chain_monitor: test_utils:: TestChainMonitor ;
7964+ let nodes_1_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
7965+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
7966+
7967+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 500_000_000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
7968+
7969+ nodes[ 1 ] . node . create_channel ( nodes[ 2 ] . node . get_our_node_id ( ) , 1_000_000 , 500_000_000 , 42 , None ) . unwrap ( ) ;
7970+ let open_channel = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendOpenChannel , nodes[ 2 ] . node. get_our_node_id( ) ) ;
7971+ nodes[ 2 ] . node . handle_open_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel) ;
7972+ let accept_channel = get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
7973+ nodes[ 1 ] . node . handle_accept_channel ( & nodes[ 2 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & accept_channel) ;
7974+
7975+ let ( temporary_channel_id, tx, _) = create_funding_transaction ( & nodes[ 1 ] , 1_000_000 , 42 ) ;
7976+ nodes[ 1 ] . node . funding_transaction_generated ( & temporary_channel_id, tx. clone ( ) ) . unwrap ( ) ;
7977+ nodes[ 2 ] . node . handle_funding_created ( & nodes[ 1 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingCreated , nodes[ 2 ] . node. get_our_node_id( ) ) ) ;
7978+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
7979+
7980+ nodes[ 1 ] . node . handle_funding_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendFundingSigned , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
7981+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
7982+
7983+ let conf_height = core:: cmp:: max ( nodes[ 1 ] . best_block_info ( ) . 1 + 1 , nodes[ 2 ] . best_block_info ( ) . 1 + 1 ) ;
7984+ confirm_transaction_at ( & nodes[ 1 ] , & tx, conf_height) ;
7985+ connect_blocks ( & nodes[ 1 ] , CHAN_CONFIRM_DEPTH - 1 ) ;
7986+ confirm_transaction_at ( & nodes[ 2 ] , & tx, conf_height) ;
7987+ connect_blocks ( & nodes[ 2 ] , CHAN_CONFIRM_DEPTH - 1 ) ;
7988+ nodes[ 1 ] . node . handle_funding_locked ( & nodes[ 2 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendFundingLocked , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
7989+ let funding_locked = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingLocked , nodes[ 2 ] . node. get_our_node_id( ) ) ;
7990+ nodes[ 2 ] . node . handle_funding_locked ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_locked) ;
7991+
7992+ assert ! ( nodes[ 0 ] . node. list_usable_channels( ) [ 0 ] . is_public) ;
7993+ assert_eq ! ( nodes[ 1 ] . node. list_usable_channels( ) . len( ) , 2 ) ;
7994+ assert ! ( !nodes[ 2 ] . node. list_usable_channels( ) [ 0 ] . is_public) ;
7995+
7996+ // We should always be able to forward through nodes[1] as long as its out through a public
7997+ // channel:
7998+ send_payment ( & nodes[ 2 ] , & [ & nodes[ 1 ] , & nodes[ 0 ] ] , 10_000 ) ;
7999+
8000+ let ( our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash ! ( nodes[ 2 ] ) ;
8001+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) ,
8002+ & nodes[ 0 ] . net_graph_msg_handler . network_graph . read ( ) . unwrap ( ) ,
8003+ & nodes[ 2 ] . node . get_our_node_id ( ) , Some ( InvoiceFeatures :: known ( ) ) , None ,
8004+ & [ & RouteHint ( vec ! [ RouteHintHop {
8005+ src_node_id: nodes[ 1 ] . node. get_our_node_id( ) ,
8006+ short_channel_id: nodes[ 2 ] . node. list_channels( ) [ 0 ] . short_channel_id. unwrap( ) ,
8007+ fees: RoutingFees { base_msat: 1000 , proportional_millionths: 0 } ,
8008+ cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA ,
8009+ htlc_minimum_msat: None ,
8010+ htlc_maximum_msat: None ,
8011+ } ] ) ] , 10_000 , TEST_FINAL_CLTV , nodes[ 0 ] . logger ) . unwrap ( ) ;
8012+
8013+ nodes[ 0 ] . node . send_payment ( & route, our_payment_hash, & Some ( our_payment_secret) ) . unwrap ( ) ;
8014+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8015+ let payment_event = SendEvent :: from_event ( nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) . remove ( 0 ) ) ;
8016+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
8017+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false , true ) ;
8018+
8019+ let htlc_fail_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
8020+ assert ! ( htlc_fail_updates. update_add_htlcs. is_empty( ) ) ;
8021+ assert_eq ! ( htlc_fail_updates. update_fail_htlcs. len( ) , 1 ) ;
8022+ assert ! ( htlc_fail_updates. update_fail_malformed_htlcs. is_empty( ) ) ;
8023+ assert ! ( htlc_fail_updates. update_fee. is_none( ) ) ;
8024+
8025+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & htlc_fail_updates. update_fail_htlcs [ 0 ] ) ;
8026+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , htlc_fail_updates. commitment_signed, true , true ) ;
8027+ expect_payment_failed ! ( nodes[ 0 ] , our_payment_hash, false ) ;
8028+ expect_payment_failure_chan_update ! ( nodes[ 0 ] , nodes[ 2 ] . node. list_channels( ) [ 0 ] . short_channel_id. unwrap( ) , true ) ;
8029+
8030+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
8031+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
8032+
8033+ let nodes_1_serialized = nodes[ 1 ] . node . encode ( ) ;
8034+ let mut monitor_a_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
8035+ let mut monitor_b_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
8036+ {
8037+ let mons = nodes[ 1 ] . chain_monitor . chain_monitor . monitors . read ( ) . unwrap ( ) ;
8038+ let mut mon_iter = mons. iter ( ) ;
8039+ mon_iter. next ( ) . unwrap ( ) . 1 . write ( & mut monitor_a_serialized) . unwrap ( ) ;
8040+ mon_iter. next ( ) . unwrap ( ) . 1 . write ( & mut monitor_b_serialized) . unwrap ( ) ;
8041+ }
8042+
8043+ persister = test_utils:: TestPersister :: new ( ) ;
8044+ let keys_manager = & chanmon_cfgs[ 1 ] . keys_manager ;
8045+ new_chain_monitor = test_utils:: TestChainMonitor :: new ( Some ( nodes[ 1 ] . chain_source ) , nodes[ 1 ] . tx_broadcaster . clone ( ) , nodes[ 1 ] . logger , node_cfgs[ 1 ] . fee_estimator , & persister, keys_manager) ;
8046+ nodes[ 1 ] . chain_monitor = & new_chain_monitor;
8047+
8048+ let mut monitor_a_read = & monitor_a_serialized. 0 [ ..] ;
8049+ let mut monitor_b_read = & monitor_b_serialized. 0 [ ..] ;
8050+ let ( _, mut monitor_a) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read ( & mut monitor_a_read, keys_manager) . unwrap ( ) ;
8051+ let ( _, mut monitor_b) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read ( & mut monitor_b_read, keys_manager) . unwrap ( ) ;
8052+ assert ! ( monitor_a_read. is_empty( ) ) ;
8053+ assert ! ( monitor_b_read. is_empty( ) ) ;
8054+
8055+ no_announce_cfg. accept_forwards_to_priv_channels = true ;
8056+
8057+ let mut nodes_1_read = & nodes_1_serialized[ ..] ;
8058+ let ( _, nodes_1_deserialized_tmp) = {
8059+ let mut channel_monitors = HashMap :: new ( ) ;
8060+ channel_monitors. insert ( monitor_a. get_funding_txo ( ) . 0 , & mut monitor_a) ;
8061+ channel_monitors. insert ( monitor_b. get_funding_txo ( ) . 0 , & mut monitor_b) ;
8062+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_1_read, ChannelManagerReadArgs {
8063+ default_config : no_announce_cfg,
8064+ keys_manager,
8065+ fee_estimator : node_cfgs[ 1 ] . fee_estimator ,
8066+ chain_monitor : nodes[ 1 ] . chain_monitor ,
8067+ tx_broadcaster : nodes[ 1 ] . tx_broadcaster . clone ( ) ,
8068+ logger : nodes[ 1 ] . logger ,
8069+ channel_monitors,
8070+ } ) . unwrap ( )
8071+ } ;
8072+ assert ! ( nodes_1_read. is_empty( ) ) ;
8073+ nodes_1_deserialized = nodes_1_deserialized_tmp;
8074+
8075+ assert ! ( nodes[ 1 ] . chain_monitor. watch_channel( monitor_a. get_funding_txo( ) . 0 , monitor_a) . is_ok( ) ) ;
8076+ assert ! ( nodes[ 1 ] . chain_monitor. watch_channel( monitor_b. get_funding_txo( ) . 0 , monitor_b) . is_ok( ) ) ;
8077+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
8078+ nodes[ 1 ] . node = & nodes_1_deserialized;
8079+
8080+ nodes[ 0 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: known ( ) } ) ;
8081+ nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
8082+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
8083+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
8084+
8085+ nodes[ 1 ] . node . peer_connected ( & nodes[ 2 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: known ( ) } ) ;
8086+ nodes[ 2 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
8087+ nodes[ 2 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 2 ] . node. get_our_node_id( ) ) ) ;
8088+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 2 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
8089+
8090+ nodes[ 0 ] . node . send_payment ( & route, our_payment_hash, & Some ( our_payment_secret) ) . unwrap ( ) ;
8091+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8092+ pass_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 2 ] ] ] , 10_000 , our_payment_hash, our_payment_secret) ;
8093+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , our_payment_preimage) ;
8094+ }
8095+
79508096#[ test]
79518097fn test_bump_penalty_txn_on_revoked_commitment ( ) {
79528098 // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
0 commit comments