Skip to content

Commit

Permalink
Fix unit tests for ChannelReady event
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Oct 28, 2022
1 parent 3dcab29 commit f7d8c41
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,15 @@ mod tests {
nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
let commitment_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap();
confirm_transaction_depth(&mut nodes[0], &commitment_tx, BREAKDOWN_TIMEOUT as u32);

let event = receiver
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
.expect("ChannelReady not handled within deadline");
match event {
Event::ChannelReady{ .. } => {},
_ => panic!("Unexpected event: {:?}", event),
}

let event = receiver
.recv_timeout(Duration::from_secs(EVENT_DEADLINE))
.expect("SpendableOutputs not handled within deadline");
Expand Down
5 changes: 5 additions & 0 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ mod test {
get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready);
get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());

expect_channel_ready_event!(nodes[0], nodes[2].node.get_our_node_id());
expect_channel_ready_event!(nodes[2], nodes[0].node.get_our_node_id());

// As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second
// channel, the channel will never be assigned any `counterparty.forwarding_info`.
Expand Down Expand Up @@ -1257,6 +1260,8 @@ mod test {
get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id());
nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready);
get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[3].node.get_our_node_id());
expect_channel_ready_event!(nodes[3], nodes[1].node.get_our_node_id());

// As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third
// channel, the channel will never be assigned any `counterparty.forwarding_info`.
Expand Down
2 changes: 2 additions & 0 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
node.gossip_sync.handle_channel_update(&as_update).unwrap();
node.gossip_sync.handle_channel_update(&bs_update).unwrap();
}
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());

send_payment(&nodes[0], &[&nodes[1]], 8000000);
close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7839,6 +7839,9 @@ mod tests {
let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
let (announcement, nodes_0_update, nodes_1_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &nodes_0_update, &nodes_1_update);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());


nodes[0].node.close_channel(channel_id, &nodes[1].node.get_our_node_id()).unwrap();
nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &channelmanager::provided_init_features(), &get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id()));
Expand Down
23 changes: 23 additions & 0 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,26 @@ pub fn create_chan_between_nodes<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, n
create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
}

#[macro_export]
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
macro_rules! expect_channel_ready_event {
($node: expr, $expected_counterparty_node_id: expr) => {
let events = $node.node.get_and_clear_pending_events();
assert_eq!(events.len(), 1);
match events[0] {
$crate::util::events::Event::ChannelReady{ ref counterparty_node_id, .. } => {
assert_eq!($expected_counterparty_node_id, *counterparty_node_id);
},
_ => panic!("Unexpected event"),
}
}
}

pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
let (channel_ready, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &channel_ready);
expect_channel_ready_event!(node_a, node_b.node.get_our_node_id());
expect_channel_ready_event!(node_b, node_a.node.get_our_node_id());
(announcement, as_update, bs_update, channel_id, tx)
}

Expand Down Expand Up @@ -741,6 +758,9 @@ pub fn open_zero_conf_channel<'a, 'b, 'c, 'd>(initiator: &'a Node<'b, 'c, 'd>, r
assert_eq!(initiator.node.list_usable_channels().len(), initiator_channels + 1);
assert_eq!(receiver.node.list_usable_channels().len(), receiver_channels + 1);

expect_channel_ready_event!(initiator, receiver.node.get_our_node_id());
expect_channel_ready_event!(receiver, initiator.node.get_our_node_id());

(tx, as_channel_ready.channel_id)
}

Expand Down Expand Up @@ -870,8 +890,10 @@ pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &
connect_blocks(&nodes[b], CHAN_CONFIRM_DEPTH - 1);
let as_channel_ready = get_event_msg!(nodes[a], MessageSendEvent::SendChannelReady, nodes[b].node.get_our_node_id());
nodes[a].node.handle_channel_ready(&nodes[b].node.get_our_node_id(), &get_event_msg!(nodes[b], MessageSendEvent::SendChannelReady, nodes[a].node.get_our_node_id()));
expect_channel_ready_event!(nodes[a], nodes[b].node.get_our_node_id());
let as_update = get_event_msg!(nodes[a], MessageSendEvent::SendChannelUpdate, nodes[b].node.get_our_node_id());
nodes[b].node.handle_channel_ready(&nodes[a].node.get_our_node_id(), &as_channel_ready);
expect_channel_ready_event!(nodes[b], nodes[a].node.get_our_node_id());
let bs_update = get_event_msg!(nodes[b], MessageSendEvent::SendChannelUpdate, nodes[a].node.get_our_node_id());

nodes[a].node.handle_channel_update(&nodes[b].node.get_our_node_id(), &bs_update);
Expand Down Expand Up @@ -2500,6 +2522,7 @@ pub fn reconnect_nodes<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a,
for chan_msgs in resp_2.drain(..) {
if send_channel_ready.1 {
node_b.node.handle_channel_ready(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap());

let announcement_event = node_b.node.get_and_clear_pending_msg_events();
if !announcement_event.is_empty() {
assert_eq!(announcement_event.len(), 1);
Expand Down
38 changes: 33 additions & 5 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id);
let funding_msgs = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &funding_tx);
create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_msgs.0);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());


// nodes[0] should now be able to send the full balance to nodes[1], violating nodes[1]'s
// security model if it ever tries to send funds back to nodes[0] (but that's not our problem).
Expand Down Expand Up @@ -558,6 +561,7 @@ fn do_test_sanity_on_in_flight_opens(steps: u8) {
confirm_transaction_at(&nodes[0], &tx, 2);
connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
}

#[test]
Expand Down Expand Up @@ -3711,11 +3715,24 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken
}

let events_1 = nodes[1].node.get_and_clear_pending_events();
assert_eq!(events_1.len(), 1);
match events_1[0] {
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
if messages_delivered == 0 {
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
assert_eq!(events_1.len(), 2);
match events_1[0] {
Event::ChannelReady { .. } => { },
_ => panic!("Unexpected event"),
};
match events_1[1] {
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
} else {
assert_eq!(events_1.len(), 1);
match events_1[0] {
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
}

nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
Expand Down Expand Up @@ -3952,6 +3969,8 @@ fn test_funding_peer_disconnect() {
},
_ => panic!("Unexpected event {:?}", events_6[0]),
};
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

// When we deliver nodes[1]'s announcement_signatures to nodes[0], nodes[0] should immediately
// broadcast the channel announcement globally, as well as re-send its (now-public)
Expand Down Expand Up @@ -4414,6 +4433,8 @@ fn test_no_txn_manager_serialize_deserialize() {
node.gossip_sync.handle_channel_update(&as_update).unwrap();
node.gossip_sync.handle_channel_update(&bs_update).unwrap();
}
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

send_payment(&nodes[0], &[&nodes[1]], 1000000);
}
Expand Down Expand Up @@ -4535,6 +4556,8 @@ fn test_manager_serialize_deserialize_events() {
node.gossip_sync.handle_channel_update(&as_update).unwrap();
node.gossip_sync.handle_channel_update(&bs_update).unwrap();
}
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

send_payment(&nodes[0], &[&nodes[1]], 1000000);
}
Expand Down Expand Up @@ -9429,6 +9452,9 @@ fn test_duplicate_chan_id() {
let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

send_payment(&nodes[0], &[&nodes[1]], 8000000);
}

Expand Down Expand Up @@ -10356,6 +10382,8 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

let dust_buffer_feerate = {
let chan_lock = nodes[0].node.channel_state.lock().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
check_added_monitors!(nodes[1], 1);
commitment_signed_dance!(nodes[1], nodes[2], htlc_fulfill_updates.commitment_signed, false);
expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, false, false);

if confirm_before_reload {
let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone();
Expand All @@ -499,7 +500,6 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
let bs_htlc_claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
assert_eq!(bs_htlc_claim_txn.len(), 1);
check_spends!(bs_htlc_claim_txn[0], as_commitment_tx);
expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, false, false);

if !confirm_before_reload {
mine_transaction(&nodes[0], &as_commitment_tx);
Expand Down
7 changes: 7 additions & 0 deletions lightning/src/ln/priv_short_conf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) {
} else { panic!("Unexpected event"); }

nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());
let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
assert_eq!(bs_msg_events.len(), 1);
if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] {
Expand Down Expand Up @@ -407,8 +409,10 @@ fn test_inbound_scid_privacy() {
connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
let bs_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id());
nodes[1].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
expect_channel_ready_event!(nodes[1], nodes[2].node.get_our_node_id());
let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready);
expect_channel_ready_event!(nodes[2], nodes[1].node.get_our_node_id());
let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());

nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update);
Expand Down Expand Up @@ -672,6 +676,9 @@ fn test_0conf_channel_with_async_monitor() {
}
_ => panic!("Unexpected event"),
}
expect_channel_ready_event!(nodes[0], nodes[1].node.get_our_node_id());
expect_channel_ready_event!(nodes[1], nodes[0].node.get_our_node_id());

let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());

let as_channel_update = match &as_locked_update[1] {
Expand Down

0 comments on commit f7d8c41

Please sign in to comment.