Skip to content

Commit 0bc0f4b

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 228a249 commit 0bc0f4b

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

lightning/src/ln/async_signer_tests.rs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn test_async_commitment_signature_for_funding_created() {
5454
assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
5555
channels[0].channel_id
5656
};
57-
57+
5858
nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true);
5959
nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id)));
6060

@@ -79,7 +79,7 @@ fn test_async_commitment_signature_for_funding_signed() {
7979
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8080

8181
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
82-
82+
8383
// nodes[0] --- open_channel --> nodes[1]
8484
let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8585
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
@@ -115,7 +115,7 @@ fn test_async_commitment_signature_for_funding_signed() {
115115
};
116116
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true);
117117
nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
118-
118+
119119
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
120120

121121
// nodes[0] <-- funding_signed --- nodes[1]
@@ -141,7 +141,7 @@ fn test_async_commitment_signature_for_commitment_signed() {
141141
assert_eq!(n, 1, "expected one channel, not {}", n);
142142
*chan_ids[0]
143143
};
144-
144+
145145
// Send a payment.
146146
let src = &nodes[0];
147147
let dst = &nodes[1];
@@ -168,7 +168,7 @@ fn test_async_commitment_signature_for_commitment_signed() {
168168
check_added_monitors(dst, 1);
169169

170170
get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());
171-
171+
172172
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
173173
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
174174
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));
@@ -183,3 +183,70 @@ fn test_async_commitment_signature_for_commitment_signed() {
183183
};
184184
}
185185

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

lightning/src/ln/functional_test_utils.rs

Lines changed: 8 additions & 4 deletions
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)