Skip to content

Commit b0975eb

Browse files
Add ChannelManager:id_to_peer map coverage test
1 parent 7833589 commit b0975eb

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7619,6 +7619,121 @@ mod tests {
76197619
// Check that using the original payment hash succeeds.
76207620
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());
76217621
}
7622+
7623+
#[test]
7624+
fn test_id_to_peer_coverage() {
7625+
// Test that the `ChannelManager:id_to_peer` contains channels which have been assigned
7626+
// an `channel_id` (i.e. have had the funding tx created), and that they are removed once
7627+
// the channel is successfully closed.
7628+
let chanmon_cfgs = create_chanmon_cfgs(2);
7629+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7630+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7631+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7632+
7633+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
7634+
let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7635+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
7636+
let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7637+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
7638+
7639+
let (temporary_channel_id, tx, _funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
7640+
let channel_id = &tx.txid().into_inner();
7641+
{
7642+
// Ensure that the `id_to_peer` map is empty until either party has recieved the
7643+
// funding transaction, and have the real `channel_id`.
7644+
assert_eq!(nodes[0].node.id_to_peer.lock().unwrap().len(), 0);
7645+
assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
7646+
}
7647+
7648+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
7649+
{
7650+
// Assert that `nodes[0]`'s `id_to_peer` map is populated with the channel as soon as
7651+
// as it has the funding transaction.
7652+
let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
7653+
assert_eq!(nodes_0_lock.len(), 1);
7654+
assert!(nodes_0_lock.contains_key(channel_id));
7655+
7656+
assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
7657+
}
7658+
7659+
let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
7660+
7661+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
7662+
{
7663+
let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
7664+
assert_eq!(nodes_0_lock.len(), 1);
7665+
assert!(nodes_0_lock.contains_key(channel_id));
7666+
7667+
// Assert that `nodes[1]`'s `id_to_peer` map is populated with the channel as soon as
7668+
// as it has the funding transaction.
7669+
let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
7670+
assert_eq!(nodes_1_lock.len(), 1);
7671+
assert!(nodes_1_lock.contains_key(channel_id));
7672+
}
7673+
check_added_monitors!(nodes[1], 1);
7674+
let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
7675+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
7676+
check_added_monitors!(nodes[0], 1);
7677+
let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
7678+
let (announcement, nodes_0_update, nodes_1_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
7679+
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &nodes_0_update, &nodes_1_update);
7680+
7681+
nodes[0].node.close_channel(channel_id, &nodes[1].node.get_our_node_id()).unwrap();
7682+
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()));
7683+
let nodes_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7684+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &nodes_1_shutdown);
7685+
7686+
let closing_signed_node_0 = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
7687+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &closing_signed_node_0);
7688+
{
7689+
// Assert that the channel is kept in the `id_to_peer` map for both nodes until the
7690+
// channel can be fully closed by both parties (i.e. no outstanding htlcs exists, the
7691+
// fee for the closing transaction has been negotiated and the parties has the other
7692+
// party's signature for the fee negotiated closing transaction.)
7693+
let nodes_0_lock = nodes[0].node.id_to_peer.lock().unwrap();
7694+
assert_eq!(nodes_0_lock.len(), 1);
7695+
assert!(nodes_0_lock.contains_key(channel_id));
7696+
7697+
// At this stage, `nodes[1]` has proposed a fee for the closing transaction in the
7698+
// `handle_closing_signed` call above. As `nodes[1]` has not yet received the signature
7699+
// from `nodes[0]` for the closing transaction with the proposed fee, the channel is
7700+
// kept in the `nodes[1]`'s `id_to_peer` map.
7701+
let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
7702+
assert_eq!(nodes_1_lock.len(), 1);
7703+
assert!(nodes_1_lock.contains_key(channel_id));
7704+
}
7705+
7706+
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()));
7707+
{
7708+
// `nodes[0]` accepts `nodes[1]`'s proposed fee for the closing transaction, and
7709+
// therefore has all it needs to fully close the channel (both signatures for the
7710+
// closing transaction).
7711+
// Assert that the channel is removed from `nodes[0]`'s `id_to_peer` map as it can be
7712+
// fully closed by `nodes[0]`.
7713+
assert_eq!(nodes[0].node.id_to_peer.lock().unwrap().len(), 0);
7714+
7715+
// Assert that the channel is still in `nodes[1]`'s `id_to_peer` map, as `nodes[1]`
7716+
// doesn't have `nodes[0]`'s signature for the closing transaction yet.
7717+
let nodes_1_lock = nodes[1].node.id_to_peer.lock().unwrap();
7718+
assert_eq!(nodes_1_lock.len(), 1);
7719+
assert!(nodes_1_lock.contains_key(channel_id));
7720+
}
7721+
7722+
let (_nodes_0_update, closing_signed_node_0) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
7723+
7724+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &closing_signed_node_0.unwrap());
7725+
{
7726+
// Assert that the channel is removed from both parties `id_to_peer` map once they both
7727+
// have everything required to fully close the channel.
7728+
assert_eq!(nodes[0].node.id_to_peer.lock().unwrap().len(), 0);
7729+
assert_eq!(nodes[1].node.id_to_peer.lock().unwrap().len(), 0);
7730+
}
7731+
let (_nodes_1_update, _none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
7732+
7733+
// Clear the `ChannelClosed` event for the nodes.
7734+
nodes[0].node.get_and_clear_pending_events();
7735+
nodes[1].node.get_and_clear_pending_events();
7736+
}
76227737
}
76237738

76247739
#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]

0 commit comments

Comments
 (0)