Skip to content

Commit 70c8f68

Browse files
committed
Fix AwaitingRAA on RAA receipt when monitor updating had failed
This fixes a rather subtle case handling RAAs when we don't generate a response due to a previous monitor update failure, but would otherwise send a CS response. We need to still set AwaitingRemoteRevoke on the channl in question, but previously did not. Found by chanmon_fail_consistency fuzz test with the failing test converted and added manually.
1 parent 480de08 commit 70c8f68

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/ln/chanmon_update_fail_tests.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,4 +988,113 @@ fn test_monitor_update_fail_reestablish() {
988988
}
989989
}
990990

991+
#[test]
992+
fn raa_no_response_awaiting_raa_state() {
993+
// This is a rather convoluted test which ensures that if handling of an RAA does not happen
994+
// due to a previous monitor update failure, we still set AwaitingRemoteRevoke on the channel
995+
// in question (assuming it intends to respond with a CS after monitor updating is restored).
996+
// Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
997+
let mut nodes = create_network(2);
998+
create_announced_chan_between_nodes(&nodes, 0, 1);
999+
1000+
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
1001+
let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
1002+
let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
1003+
let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
1004+
1005+
// Queue up two payments - one will be delivered right away, one immediately goes into the
1006+
// holding cell as nodes[0] is AwaitingRAA. Ultimately this allows us to deliver an RAA
1007+
// immediately after a CS. By setting failing the monitor update failure from the CS (which
1008+
// requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
1009+
// generation during RAA while in monitor-update-failed state.
1010+
nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
1011+
check_added_monitors!(nodes[0], 1);
1012+
nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
1013+
check_added_monitors!(nodes[0], 0);
1014+
1015+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1016+
assert_eq!(events.len(), 1);
1017+
let payment_event = SendEvent::from_event(events.pop().unwrap());
1018+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1019+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
1020+
check_added_monitors!(nodes[1], 1);
9911021

1022+
let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1023+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
1024+
check_added_monitors!(nodes[0], 1);
1025+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1026+
assert_eq!(events.len(), 1);
1027+
let payment_event = SendEvent::from_event(events.pop().unwrap());
1028+
1029+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
1030+
check_added_monitors!(nodes[0], 1);
1031+
let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1032+
1033+
// Now we have a CS queued up which adds a new HTLC (which will need a RAA/CS response from
1034+
// nodes[1]) followed by an RAA. Fail the monitor updating prior to the CS, deliver the RAA,
1035+
// then restore channel monitor updates.
1036+
*nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
1037+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1038+
if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap_err() {
1039+
assert_eq!(err, "Failed to update ChannelMonitor");
1040+
} else { panic!(); }
1041+
check_added_monitors!(nodes[1], 1);
1042+
1043+
if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap_err() {
1044+
assert_eq!(err, "Previous monitor update failure prevented responses to RAA");
1045+
} else { panic!(); }
1046+
check_added_monitors!(nodes[1], 1);
1047+
1048+
*nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
1049+
nodes[1].node.test_restore_channel_monitor();
1050+
// nodes[1] should be AwaitingRAA here!
1051+
check_added_monitors!(nodes[1], 1);
1052+
let bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1053+
expect_pending_htlcs_forwardable!(nodes[1]);
1054+
expect_payment_received!(nodes[1], payment_hash_1);
1055+
1056+
// We send a third payment here, which is somewhat of a redundant test, but the
1057+
// chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
1058+
// commitment transaction states) whereas here we can explicitly check for it.
1059+
nodes[0].node.send_payment(route.clone(), payment_hash_3).unwrap();
1060+
check_added_monitors!(nodes[0], 0);
1061+
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1062+
1063+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0).unwrap();
1064+
check_added_monitors!(nodes[0], 1);
1065+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1066+
assert_eq!(events.len(), 1);
1067+
let payment_event = SendEvent::from_event(events.pop().unwrap());
1068+
1069+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_responses.1).unwrap();
1070+
check_added_monitors!(nodes[0], 1);
1071+
let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1072+
1073+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1074+
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
1075+
check_added_monitors!(nodes[1], 1);
1076+
let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1077+
1078+
// Finally deliver the RAA to nodes[1] which results in a CS response to the last update
1079+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
1080+
check_added_monitors!(nodes[1], 1);
1081+
expect_pending_htlcs_forwardable!(nodes[1]);
1082+
expect_payment_received!(nodes[1], payment_hash_2);
1083+
let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1084+
1085+
nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
1086+
check_added_monitors!(nodes[0], 1);
1087+
1088+
nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap();
1089+
check_added_monitors!(nodes[0], 1);
1090+
let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1091+
1092+
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
1093+
check_added_monitors!(nodes[1], 1);
1094+
expect_pending_htlcs_forwardable!(nodes[1]);
1095+
expect_payment_received!(nodes[1], payment_hash_3);
1096+
1097+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
1098+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
1099+
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
1100+
}

src/ln/channel.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,10 @@ impl Channel {
20452045
// cells) while we can't update the monitor, so we just return what we have.
20462046
if require_commitment {
20472047
self.monitor_pending_commitment_signed = true;
2048+
// When the monitor updating is restored we'll call get_last_commitment_update(),
2049+
// which does not update state, but we're definitely now awaiting a remote revoke
2050+
// before we can step forward any more, so set it here.
2051+
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
20482052
}
20492053
self.monitor_pending_forwards.append(&mut to_forward_infos);
20502054
self.monitor_pending_failures.append(&mut revoked_htlcs);

0 commit comments

Comments
 (0)