Skip to content

Commit 0644621

Browse files
Add ChannelManager:id_to_peer map coverage test
1 parent 9654bfe commit 0644621

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

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

0 commit comments

Comments
 (0)