Skip to content

Commit fa7f170

Browse files
Add ChannelManager:id_to_peer map coverage test
1 parent 4058861 commit fa7f170

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7233,7 +7233,7 @@ mod tests {
72337233
use ln::msgs::ChannelMessageHandler;
72347234
use routing::router::{PaymentParameters, RouteParameters, find_route};
72357235
use util::errors::APIError;
7236-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
7236+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
72377237
use util::test_utils;
72387238
use chain::keysinterface::KeysInterface;
72397239

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

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

0 commit comments

Comments
 (0)