-macro_rules! get_chan_reestablish_msgs {
- ($src_node: expr, $dst_node: expr) => {
- {
- let mut res = Vec::with_capacity(1);
- for msg in $src_node.node.get_and_clear_pending_msg_events() {
- if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- res.push(msg.clone());
- } else {
- panic!("Unexpected event")
- }
- }
- res
- }
- }
-}
-
-macro_rules! handle_chan_reestablish_msgs {
- ($src_node: expr, $dst_node: expr) => {
- {
- let msg_events = $src_node.node.get_and_clear_pending_msg_events();
- let mut idx = 0;
- let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
- idx += 1;
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- Some(msg.clone())
- } else {
- None
- };
-
- let mut revoke_and_ack = None;
- let mut commitment_update = None;
- let order = if let Some(ev) = msg_events.get(idx) {
- idx += 1;
- match ev {
- &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- revoke_and_ack = Some(msg.clone());
- RAACommitmentOrder::RevokeAndACKFirst
- },
- &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- commitment_update = Some(updates.clone());
- RAACommitmentOrder::CommitmentFirst
- },
- _ => panic!("Unexpected event"),
- }
- } else {
- RAACommitmentOrder::CommitmentFirst
- };
-
- if let Some(ev) = msg_events.get(idx) {
- match ev {
- &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- assert!(revoke_and_ack.is_none());
- revoke_and_ack = Some(msg.clone());
- },
- &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
- assert_eq!(*node_id, $dst_node.node.get_our_node_id());
- assert!(commitment_update.is_none());
- commitment_update = Some(updates.clone());
- },
- _ => panic!("Unexpected event"),
- }
- }
-
- (funding_locked, revoke_and_ack, commitment_update, order)
- }
- }
-}
-
-/// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
-/// for claims/fails they are separated out.
-fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
- node_a.node.peer_connected(&node_b.node.get_our_node_id());
- let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
- node_b.node.peer_connected(&node_a.node.get_our_node_id());
- let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
-
- if send_funding_locked.0 {
- // If a expects a funding_locked, it better not think it has received a revoke_and_ack
- // from b
- for reestablish in reestablish_1.iter() {
- assert_eq!(reestablish.next_remote_commitment_number, 0);
- }
- }
- if send_funding_locked.1 {
- // If b expects a funding_locked, it better not think it has received a revoke_and_ack
- // from a
- for reestablish in reestablish_2.iter() {
- assert_eq!(reestablish.next_remote_commitment_number, 0);
- }
- }
- if send_funding_locked.0 || send_funding_locked.1 {
- // If we expect any funding_locked's, both sides better have set
- // next_local_commitment_number to 1
- for reestablish in reestablish_1.iter() {
- assert_eq!(reestablish.next_local_commitment_number, 1);
- }
- for reestablish in reestablish_2.iter() {
- assert_eq!(reestablish.next_local_commitment_number, 1);
- }
- }
-
- let mut resp_1 = Vec::new();
- for msg in reestablish_1 {
- node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
- resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
- }
- if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
- check_added_monitors!(node_b, 1);
- } else {
- check_added_monitors!(node_b, 0);
- }
-
- let mut resp_2 = Vec::new();
- for msg in reestablish_2 {
- node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
- resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
- }
- if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
- check_added_monitors!(node_a, 1);
- } else {
- check_added_monitors!(node_a, 0);
- }
-
- // We dont yet support both needing updates, as that would require a different commitment dance:
- assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
- (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
-
- for chan_msgs in resp_1.drain(..) {
- if send_funding_locked.0 {
- node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
- let announcement_event = node_a.node.get_and_clear_pending_msg_events();
- if !announcement_event.is_empty() {
- assert_eq!(announcement_event.len(), 1);
- if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
- //TODO: Test announcement_sigs re-sending
- } else { panic!("Unexpected event!"); }
- }
- } else {
- assert!(chan_msgs.0.is_none());
- }
- if pending_raa.0 {
- assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
- node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
- assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
- check_added_monitors!(node_a, 1);
- } else {
- assert!(chan_msgs.1.is_none());
- }
- if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
- let commitment_update = chan_msgs.2.unwrap();
- if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
- assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
- } else {
- assert!(commitment_update.update_add_htlcs.is_empty());
- }
- assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
- assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
- assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
- for update_add in commitment_update.update_add_htlcs {
- node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
- }
- for update_fulfill in commitment_update.update_fulfill_htlcs {
- node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
- }
- for update_fail in commitment_update.update_fail_htlcs {
- node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
- }
-
- if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
- commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
- } else {
- node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
- check_added_monitors!(node_a, 1);
- let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
- // No commitment_signed so get_event_msg's assert(len == 1) passes
- node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
- assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
- check_added_monitors!(node_b, 1);
- }
- } else {
- assert!(chan_msgs.2.is_none());
- }
- }
-
- for chan_msgs in resp_2.drain(..) {
- if send_funding_locked.1 {
- node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
- let announcement_event = node_b.node.get_and_clear_pending_msg_events();
- if !announcement_event.is_empty() {
- assert_eq!(announcement_event.len(), 1);
- if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
- //TODO: Test announcement_sigs re-sending
- } else { panic!("Unexpected event!"); }
- }
- } else {
- assert!(chan_msgs.0.is_none());
- }
- if pending_raa.1 {
- assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
- node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
- assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
- check_added_monitors!(node_b, 1);
- } else {
- assert!(chan_msgs.1.is_none());
- }
- if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
- let commitment_update = chan_msgs.2.unwrap();
- if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
- assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
- }
- assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
- assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
- assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
- for update_add in commitment_update.update_add_htlcs {
- node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
- }
- for update_fulfill in commitment_update.update_fulfill_htlcs {
- node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
- }
- for update_fail in commitment_update.update_fail_htlcs {
- node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
- }
-
- if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
- commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
- } else {
- node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
- check_added_monitors!(node_b, 1);
- let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
- // No commitment_signed so get_event_msg's assert(len == 1) passes
- node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
- assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
- check_added_monitors!(node_a, 1);
- }
- } else {
- assert!(chan_msgs.2.is_none());
- }
- }
-}
-