Skip to content

Commit 3653e5e

Browse files
committed
Don't provide RAA or CU with channel_ready pending
When we get a `channel_reestablish` that requires a `channel_ready`, don't respond with the `commitment_signed` or `revoke_and_ack` until we have the `channel_ready` available to send.
1 parent fe6c0e3 commit 3653e5e

File tree

2 files changed

+213
-8
lines changed

2 files changed

+213
-8
lines changed

lightning/src/ln/async_signer_tests.rs

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,3 +1309,207 @@ fn no_stray_channel_reestablish() {
13091309
let events = nodes[0].node.get_and_clear_pending_msg_events();
13101310
assert!(events.is_empty(), "Expected no events from Alice, got {:?}", events);
13111311
}
1312+
1313+
#[test]
1314+
fn dont_elide_channely_ready_from_state_1() {
1315+
// 1. Disable Alice's signer.
1316+
// 2. Send a payment from Alice to Bob.
1317+
// 3. Disconnect Alice and Bob. Reload Alice.
1318+
// 4. Reconnect Alice and Bob.
1319+
// 5. Process messages on Bob, which should generate a `channel_reestablish`.
1320+
// 6. Process messages on Alice, which should *not* send anything, in particular an
1321+
// `update_add_htlc` because Alice must send a `channel_ready` (that she can't create
1322+
// without her signer) first.
1323+
let chanmon_cfgs = create_chanmon_cfgs(2);
1324+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1325+
let persister;
1326+
let new_chain_monitor;
1327+
1328+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1329+
let alice_deserialized;
1330+
1331+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1332+
let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
1333+
1334+
// Turn off Alice's signer.
1335+
eprintln!("disabling alice's signer");
1336+
nodes[0].set_channel_signer_ops_available(
1337+
&nodes[1].node.get_our_node_id(), &channel_id,
1338+
ops::GET_PER_COMMITMENT_POINT | ops::RELEASE_COMMITMENT_SECRET | ops::SIGN_COUNTERPARTY_COMMITMENT,
1339+
false);
1340+
1341+
eprintln!("sending payment from alice to bob");
1342+
let (route, payment_hash, _payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
1343+
nodes[0].node.send_payment_with_route(&route, payment_hash,
1344+
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1345+
1346+
check_added_monitors!(nodes[0], 1);
1347+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1348+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1349+
1350+
// Disconnect Bob and restart Alice
1351+
eprintln!("disconnecting bob");
1352+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
1353+
1354+
eprintln!("restarting alice");
1355+
{
1356+
let alice_serialized = nodes[0].node.encode();
1357+
let alice_monitor_serialized = get_monitor!(nodes[0], channel_id).encode();
1358+
reload_node!(nodes[0], *nodes[0].node.get_current_default_configuration(), &alice_serialized, &[&alice_monitor_serialized], persister, new_chain_monitor, alice_deserialized);
1359+
}
1360+
1361+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1362+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1363+
1364+
eprintln!("unblocking alice's signer with commmitment signed");
1365+
nodes[0].set_channel_signer_ops_available(
1366+
&nodes[1].node.get_our_node_id(), &channel_id,
1367+
ops::SIGN_COUNTERPARTY_COMMITMENT,
1368+
true);
1369+
nodes[0].node.signer_unblocked(None);
1370+
1371+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1372+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1373+
1374+
// Reconnect Alice and Bob.
1375+
eprintln!("reconnecting alice and bob");
1376+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
1377+
features: nodes[1].node.init_features(), networks: None, remote_network_address: None
1378+
}, false).unwrap();
1379+
1380+
nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
1381+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
1382+
}, false).unwrap();
1383+
1384+
// Bob should have sent Alice a channel_reestablish. Alice should not have done anything.
1385+
assert!(get_chan_reestablish_msgs!(nodes[0], nodes[1]).is_empty());
1386+
let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
1387+
assert_eq!(reestablish_2.len(), 1);
1388+
1389+
// When Alice handle's the channel_reestablish, she should _still_ do nothing, in particular,
1390+
// because she doesn't have the channel ready available.
1391+
nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
1392+
match handle_chan_reestablish_msgs!(nodes[0], nodes[1]) {
1393+
(None, None, None, _) => (),
1394+
(channel_ready, revoke_and_ack, commitment_update, order) => {
1395+
panic!("got channel_ready={:?} revoke_and_ack={:?} commitment_update={:?} order={:?}",
1396+
channel_ready, revoke_and_ack, commitment_update, order);
1397+
}
1398+
};
1399+
1400+
// Now provide the commitment point and Alice should send her channel_reestablish.
1401+
nodes[0].set_channel_signer_ops_available(
1402+
&nodes[1].node.get_our_node_id(), &channel_id,
1403+
ops::GET_PER_COMMITMENT_POINT,
1404+
true);
1405+
nodes[0].node.signer_unblocked(None);
1406+
1407+
{
1408+
let events = nodes[0].node.get_and_clear_pending_msg_events();
1409+
assert_eq!(events.len(), 2, "Expected 2 events, got {}: {:?}", events.len(), events);
1410+
match (&events[0], &events[1]) {
1411+
(MessageSendEvent::SendChannelReestablish { .. }, MessageSendEvent::SendChannelReady { .. }) => (),
1412+
(a, b) => panic!("Expected SendChannelReestablish and SendChannelReady, not {:?} and {:?}", a, b)
1413+
}
1414+
}
1415+
}
1416+
1417+
#[test]
1418+
fn dont_lose_commitment_update() {
1419+
// 1. Send a payment from Alice to Bob.
1420+
// 2. Disable signing on Alice.
1421+
// 3. Disconnect Alice and Bob. Reload Alice.
1422+
// 4. Reconnect Alice and Bob.
1423+
// 5. Process messages on Bob, which should generate a `channel_reestablish`.
1424+
let chanmon_cfgs = create_chanmon_cfgs(2);
1425+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1426+
let persister;
1427+
let new_chain_monitor;
1428+
1429+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1430+
let alice_deserialized;
1431+
1432+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1433+
let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
1434+
1435+
eprintln!("sending payment from alice to bob");
1436+
let (route, payment_hash, _payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
1437+
nodes[0].node.send_payment_with_route(&route, payment_hash,
1438+
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1439+
1440+
// Turn off Alice's signer.
1441+
eprintln!("disabling alice's signer");
1442+
nodes[0].set_channel_signer_ops_available(
1443+
&nodes[1].node.get_our_node_id(), &channel_id,
1444+
ops::GET_PER_COMMITMENT_POINT | ops::RELEASE_COMMITMENT_SECRET | ops::SIGN_COUNTERPARTY_COMMITMENT,
1445+
false);
1446+
1447+
check_added_monitors!(nodes[0], 1);
1448+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1449+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
1450+
1451+
// Disconnect Bob and restart Alice
1452+
eprintln!("disconnecting bob");
1453+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
1454+
1455+
eprintln!("restarting alice");
1456+
{
1457+
let alice_serialized = nodes[0].node.encode();
1458+
let alice_monitor_serialized = get_monitor!(nodes[0], channel_id).encode();
1459+
reload_node!(nodes[0], *nodes[0].node.get_current_default_configuration(), &alice_serialized, &[&alice_monitor_serialized], persister, new_chain_monitor, alice_deserialized);
1460+
}
1461+
1462+
// Reconnect Alice and Bob.
1463+
eprintln!("reconnecting alice and bob");
1464+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
1465+
features: nodes[1].node.init_features(), networks: None, remote_network_address: None
1466+
}, false).unwrap();
1467+
1468+
nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
1469+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
1470+
}, false).unwrap();
1471+
1472+
// Bob should have sent Alice a channel_reestablish. Alice should not have done anything.
1473+
assert!(get_chan_reestablish_msgs!(nodes[0], nodes[1]).is_empty());
1474+
let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
1475+
assert_eq!(reestablish_2.len(), 1);
1476+
1477+
// When Alice handle's the channel_reestablish, she should _still_ do nothing, in particular,
1478+
// because she doesn't have the channel ready available.
1479+
nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
1480+
match handle_chan_reestablish_msgs!(nodes[0], nodes[1]) {
1481+
(None, None, None, _) => (),
1482+
(channel_ready, revoke_and_ack, commitment_update, order) => {
1483+
panic!("got channel_ready={:?} revoke_and_ack={:?} commitment_update={:?} order={:?}",
1484+
channel_ready, revoke_and_ack, commitment_update, order);
1485+
}
1486+
};
1487+
1488+
{
1489+
let events = nodes[0].node.get_and_clear_pending_msg_events();
1490+
assert!(events.is_empty(), "expected no events, got {:?}", events);
1491+
}
1492+
1493+
{
1494+
let events = nodes[1].node.get_and_clear_pending_msg_events();
1495+
assert!(events.is_empty(), "expected no events, got {:?}", events);
1496+
}
1497+
1498+
eprintln!("unblocking alice's signer");
1499+
nodes[0].set_channel_signer_ops_available(
1500+
&nodes[1].node.get_our_node_id(), &channel_id,
1501+
ops::GET_PER_COMMITMENT_POINT | ops::RELEASE_COMMITMENT_SECRET | ops::SIGN_COUNTERPARTY_COMMITMENT,
1502+
true);
1503+
nodes[0].node.signer_unblocked(None);
1504+
1505+
{
1506+
let events = nodes[0].node.get_and_clear_pending_msg_events();
1507+
assert_eq!(events.len(), 3, "Expected 3 events, got {}: {:?}", events.len(), events);
1508+
match (&events[0], &events[1], &events[2]) {
1509+
(MessageSendEvent::SendChannelReestablish { .. },
1510+
MessageSendEvent::UpdateHTLCs { .. },
1511+
MessageSendEvent::SendChannelReady { .. }) => (),
1512+
(a, b, c) => panic!("Expected SendChannelReestablish, UpdateHTLCs, SendChannelReady; not {:?}, {:?}, {:?}", a, b, c)
1513+
}
1514+
}
1515+
}

lightning/src/ln/channel.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,17 +4921,18 @@ impl<SP: Deref> Channel<SP> where
49214921
}
49224922
let next_counterparty_commitment_number = INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number + if is_awaiting_remote_revoke { 1 } else { 0 };
49234923

4924-
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
4924+
let mut channel_ready = None;
4925+
if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.cur_holder_commitment_transaction_number == 1 {
49254926
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
49264927
log_debug!(logger, "Reconnecting channel at state 1, (re?)sending channel_ready");
4927-
self.get_channel_ready().or_else(|| {
4928+
channel_ready = self.get_channel_ready();
4929+
if channel_ready.is_none() {
49284930
if !self.context.signer_pending_channel_ready {
49294931
log_trace!(logger, "Unable to generate channel_ready for channel_reestablish; setting signer_pending_channel_ready");
49304932
self.context.signer_pending_channel_ready = true;
49314933
}
4932-
None
4933-
})
4934-
} else { None };
4934+
}
4935+
}
49354936

49364937
if msg.next_local_commitment_number == next_counterparty_commitment_number {
49374938
if raa.is_some() || self.context.signer_pending_revoke_and_ack {
@@ -4942,7 +4943,7 @@ impl<SP: Deref> Channel<SP> where
49424943

49434944
Ok(ReestablishResponses {
49444945
channel_ready, shutdown_msg, announcement_sigs,
4945-
raa,
4946+
raa: if !self.context.signer_pending_channel_ready { raa } else { None },
49464947
commitment_update: None,
49474948
order: self.context.resend_order.clone(),
49484949
})
@@ -4967,8 +4968,8 @@ impl<SP: Deref> Channel<SP> where
49674968
self.context.signer_pending_commitment_update = commitment_update.is_none();
49684969
Ok(ReestablishResponses {
49694970
channel_ready, shutdown_msg, announcement_sigs,
4970-
raa,
4971-
commitment_update,
4971+
raa: if !self.context.signer_pending_channel_ready { raa } else { None },
4972+
commitment_update: if !self.context.signer_pending_channel_ready { commitment_update } else { None },
49724973
order: self.context.resend_order.clone(),
49734974
})
49744975
}

0 commit comments

Comments
 (0)