@@ -7715,6 +7715,121 @@ mod tests {
77157715 // Check that using the original payment hash succeeds.
77167716 assert ! ( inbound_payment:: verify( payment_hash, & payment_data, nodes[ 0 ] . node. highest_seen_timestamp. load( Ordering :: Acquire ) as u64 , & nodes[ 0 ] . node. inbound_payment_key, & nodes[ 0 ] . logger) . is_ok( ) ) ;
77177717 }
7718+
7719+ #[ test]
7720+ fn test_id_to_peer_coverage ( ) {
7721+ // Test that the `ChannelManager:id_to_peer` contains channels which have been assigned
7722+ // a `channel_id` (i.e. have had the funding tx created), and that they are removed once
7723+ // the channel is successfully closed.
7724+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
7725+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
7726+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
7727+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
7728+
7729+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 1_000_000 , 500_000_000 , 42 , None ) . unwrap ( ) ;
7730+ let open_channel = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
7731+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel) ;
7732+ let accept_channel = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7733+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & accept_channel) ;
7734+
7735+ let ( temporary_channel_id, tx, _funding_output) = create_funding_transaction ( & nodes[ 0 ] , & nodes[ 1 ] . node . get_our_node_id ( ) , 1_000_000 , 42 ) ;
7736+ let channel_id = & tx. txid ( ) . into_inner ( ) ;
7737+ {
7738+ // Ensure that the `id_to_peer` map is empty until either party has received the
7739+ // funding transaction, and have the real `channel_id`.
7740+ assert_eq ! ( nodes[ 0 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7741+ assert_eq ! ( nodes[ 1 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7742+ }
7743+
7744+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, & nodes[ 1 ] . node . get_our_node_id ( ) , tx. clone ( ) ) . unwrap ( ) ;
7745+ {
7746+ // Assert that `nodes[0]`'s `id_to_peer` map is populated with the channel as soon as
7747+ // as it has the funding transaction.
7748+ let nodes_0_lock = nodes[ 0 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7749+ assert_eq ! ( nodes_0_lock. len( ) , 1 ) ;
7750+ assert ! ( nodes_0_lock. contains_key( channel_id) ) ;
7751+
7752+ assert_eq ! ( nodes[ 1 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7753+ }
7754+
7755+ let funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
7756+
7757+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
7758+ {
7759+ let nodes_0_lock = nodes[ 0 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7760+ assert_eq ! ( nodes_0_lock. len( ) , 1 ) ;
7761+ assert ! ( nodes_0_lock. contains_key( channel_id) ) ;
7762+
7763+ // Assert that `nodes[1]`'s `id_to_peer` map is populated with the channel as soon as
7764+ // as it has the funding transaction.
7765+ let nodes_1_lock = nodes[ 1 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7766+ assert_eq ! ( nodes_1_lock. len( ) , 1 ) ;
7767+ assert ! ( nodes_1_lock. contains_key( channel_id) ) ;
7768+ }
7769+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
7770+ let funding_signed = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7771+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_signed) ;
7772+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7773+ let ( channel_ready, _) = create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
7774+ let ( announcement, nodes_0_update, nodes_1_update) = create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & channel_ready) ;
7775+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & nodes_0_update, & nodes_1_update) ;
7776+
7777+ nodes[ 0 ] . node . close_channel ( channel_id, & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
7778+ nodes[ 1 ] . node . handle_shutdown ( & nodes[ 0 ] . node . get_our_node_id ( ) , & InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendShutdown , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
7779+ let nodes_1_shutdown = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendShutdown , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7780+ nodes[ 0 ] . node . handle_shutdown ( & nodes[ 1 ] . node . get_our_node_id ( ) , & InitFeatures :: known ( ) , & nodes_1_shutdown) ;
7781+
7782+ let closing_signed_node_0 = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendClosingSigned , nodes[ 1 ] . node. get_our_node_id( ) ) ;
7783+ nodes[ 1 ] . node . handle_closing_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & closing_signed_node_0) ;
7784+ {
7785+ // Assert that the channel is kept in the `id_to_peer` map for both nodes until the
7786+ // channel can be fully closed by both parties (i.e. no outstanding htlcs exists, the
7787+ // fee for the closing transaction has been negotiated and the parties has the other
7788+ // party's signature for the fee negotiated closing transaction.)
7789+ let nodes_0_lock = nodes[ 0 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7790+ assert_eq ! ( nodes_0_lock. len( ) , 1 ) ;
7791+ assert ! ( nodes_0_lock. contains_key( channel_id) ) ;
7792+
7793+ // At this stage, `nodes[1]` has proposed a fee for the closing transaction in the
7794+ // `handle_closing_signed` call above. As `nodes[1]` has not yet received the signature
7795+ // from `nodes[0]` for the closing transaction with the proposed fee, the channel is
7796+ // kept in the `nodes[1]`'s `id_to_peer` map.
7797+ let nodes_1_lock = nodes[ 1 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7798+ assert_eq ! ( nodes_1_lock. len( ) , 1 ) ;
7799+ assert ! ( nodes_1_lock. contains_key( channel_id) ) ;
7800+ }
7801+
7802+ nodes[ 0 ] . node . handle_closing_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendClosingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
7803+ {
7804+ // `nodes[0]` accepts `nodes[1]`'s proposed fee for the closing transaction, and
7805+ // therefore has all it needs to fully close the channel (both signatures for the
7806+ // closing transaction).
7807+ // Assert that the channel is removed from `nodes[0]`'s `id_to_peer` map as it can be
7808+ // fully closed by `nodes[0]`.
7809+ assert_eq ! ( nodes[ 0 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7810+
7811+ // Assert that the channel is still in `nodes[1]`'s `id_to_peer` map, as `nodes[1]`
7812+ // doesn't have `nodes[0]`'s signature for the closing transaction yet.
7813+ let nodes_1_lock = nodes[ 1 ] . node . id_to_peer . lock ( ) . unwrap ( ) ;
7814+ assert_eq ! ( nodes_1_lock. len( ) , 1 ) ;
7815+ assert ! ( nodes_1_lock. contains_key( channel_id) ) ;
7816+ }
7817+
7818+ let ( _nodes_0_update, closing_signed_node_0) = get_closing_signed_broadcast ! ( nodes[ 0 ] . node, nodes[ 1 ] . node. get_our_node_id( ) ) ;
7819+
7820+ nodes[ 1 ] . node . handle_closing_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & closing_signed_node_0. unwrap ( ) ) ;
7821+ {
7822+ // Assert that the channel is removed from both parties `id_to_peer` map once they both
7823+ // have everything required to fully close the channel.
7824+ assert_eq ! ( nodes[ 0 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7825+ assert_eq ! ( nodes[ 1 ] . node. id_to_peer. lock( ) . unwrap( ) . len( ) , 0 ) ;
7826+ }
7827+ let ( _nodes_1_update, _none) = get_closing_signed_broadcast ! ( nodes[ 1 ] . node, nodes[ 0 ] . node. get_our_node_id( ) ) ;
7828+
7829+ // Clear the `ChannelClosed` event for the nodes.
7830+ nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
7831+ nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
7832+ }
77187833}
77197834
77207835#[ cfg( all( any( test, feature = "_test_utils" ) , feature = "_bench_unstable" ) ) ]
0 commit comments