Skip to content

Commit 8f8d33f

Browse files
committed
Add test for peer disconnect and async signing
This adds a test that disconnects and reconnects the peers with a commitment signature pending.
1 parent 13bfead commit 8f8d33f

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

lightning/src/ln/async_signer_tests.rs

+72-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn test_async_commitment_signature_for_funding_created() {
5757
assert_eq!(n, 1, "expected one channel, not {}", n);
5858
*chan_ids[0]
5959
};
60-
60+
6161
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true);
6262
nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
6363

@@ -82,7 +82,7 @@ fn test_async_commitment_signature_for_funding_signed() {
8282
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8383

8484
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
85-
85+
8686
// nodes[0] --- open_channel --> nodes[1]
8787
let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8888
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
@@ -121,7 +121,7 @@ fn test_async_commitment_signature_for_funding_signed() {
121121
};
122122
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true);
123123
nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
124-
124+
125125
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
126126

127127
// nodes[0] <-- funding_signed --- nodes[1]
@@ -147,7 +147,7 @@ fn test_async_commitment_signature_for_commitment_signed() {
147147
assert_eq!(n, 1, "expected one channel, not {}", n);
148148
*chan_ids[0]
149149
};
150-
150+
151151
// Send a payment.
152152
let src = &nodes[0];
153153
let dst = &nodes[1];
@@ -174,7 +174,7 @@ fn test_async_commitment_signature_for_commitment_signed() {
174174
check_added_monitors(dst, 1);
175175

176176
get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
177-
177+
178178
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
179179
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
180180
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
@@ -189,3 +189,70 @@ fn test_async_commitment_signature_for_commitment_signed() {
189189
};
190190
}
191191

192+
#[test]
193+
fn test_async_commitment_signature_for_peer_disconnect() {
194+
let chanmon_cfgs = create_chanmon_cfgs(2);
195+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
196+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
197+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
198+
create_announced_chan_between_nodes(&nodes, 0, 1);
199+
200+
let chan_id = {
201+
let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
202+
let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
203+
let chan_ids = chan_lock.channel_by_id.keys().collect::<Vec<_>>();
204+
let n = chan_ids.len();
205+
assert_eq!(n, 1, "expected one channel, not {}", n);
206+
*chan_ids[0]
207+
};
208+
209+
// Send a payment.
210+
let src = &nodes[0];
211+
let dst = &nodes[1];
212+
let (route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(src, dst, 8000000);
213+
src.node.send_payment_with_route(&route, our_payment_hash,
214+
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
215+
check_added_monitors!(src, 1);
216+
217+
// Pass the payment along the route.
218+
let payment_event = {
219+
let mut events = src.node.get_and_clear_pending_msg_events();
220+
assert_eq!(events.len(), 1);
221+
SendEvent::from_event(events.remove(0))
222+
};
223+
assert_eq!(payment_event.node_id, dst.node.get_our_node_id());
224+
assert_eq!(payment_event.msgs.len(), 1);
225+
226+
dst.node.handle_update_add_htlc(&src.node.get_our_node_id(), &payment_event.msgs[0]);
227+
228+
// Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
229+
// `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
230+
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, false);
231+
dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
232+
check_added_monitors(dst, 1);
233+
234+
get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
235+
236+
// Now disconnect and reconnect the peers.
237+
src.node.peer_disconnected(&dst.node.get_our_node_id());
238+
dst.node.peer_disconnected(&src.node.get_our_node_id());
239+
let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
240+
reconnect_args.send_channel_ready = (false, false);
241+
reconnect_args.pending_raa = (true, false);
242+
reconnect_nodes(reconnect_args);
243+
244+
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
245+
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
246+
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
247+
248+
{
249+
let events = dst.node.get_and_clear_pending_msg_events();
250+
let n = events.len();
251+
assert_eq!(n, 1, "expected one message, got {}", n);
252+
if let MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } = events[0] {
253+
assert_eq!(node_id, &src.node.get_our_node_id());
254+
} else {
255+
panic!("expected UpdateHTLCs message, not {:?}", events[0]);
256+
};
257+
}
258+
}

lightning/src/ln/functional_test_utils.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -3142,24 +3142,28 @@ pub fn reconnect_nodes<'a, 'b, 'c, 'd>(args: ReconnectArgs<'a, 'b, 'c, 'd>) {
31423142
// If a expects a channel_ready, it better not think it has received a revoke_and_ack
31433143
// from b
31443144
for reestablish in reestablish_1.iter() {
3145-
assert_eq!(reestablish.next_remote_commitment_number, 0);
3145+
let n = reestablish.next_remote_commitment_number;
3146+
assert_eq!(n, 0, "expected a->b next_remote_commitment_number to be 0, got {}", n);
31463147
}
31473148
}
31483149
if send_channel_ready.1 {
31493150
// If b expects a channel_ready, it better not think it has received a revoke_and_ack
31503151
// from a
31513152
for reestablish in reestablish_2.iter() {
3152-
assert_eq!(reestablish.next_remote_commitment_number, 0);
3153+
let n = reestablish.next_remote_commitment_number;
3154+
assert_eq!(n, 0, "expected b->a next_remote_commitment_number to be 0, got {}", n);
31533155
}
31543156
}
31553157
if send_channel_ready.0 || send_channel_ready.1 {
31563158
// If we expect any channel_ready's, both sides better have set
31573159
// next_holder_commitment_number to 1
31583160
for reestablish in reestablish_1.iter() {
3159-
assert_eq!(reestablish.next_local_commitment_number, 1);
3161+
let n = reestablish.next_local_commitment_number;
3162+
assert_eq!(n, 1, "expected a->b next_local_commitment_number to be 1, got {}", n);
31603163
}
31613164
for reestablish in reestablish_2.iter() {
3162-
assert_eq!(reestablish.next_local_commitment_number, 1);
3165+
let n = reestablish.next_local_commitment_number;
3166+
assert_eq!(n, 1, "expected b->a next_local_commitment_number to be 1, got {}", n);
31633167
}
31643168
}
31653169

0 commit comments

Comments
 (0)