Section MatchIndexAllEntries.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Context {rri : raft_refinement_interface}.
Context {naetli : no_append_entries_to_leader_interface}.
Context {naetsi : no_append_entries_to_self_interface}.
Context {taifoli : terms_and_indices_from_one_log_interface}.
Context {rlmli : refined_log_matching_lemmas_interface}.
Context {laei : log_all_entries_interface}.
Context {aelli : append_entries_leaderLogs_interface}.
Context {lsi : leader_sublog_interface}.
Context {lhllsi : leaders_have_leaderLogs_strong_interface}.
Context {ollpti : one_leaderLog_per_term_interface}.
Context {mili : match_index_leader_interface}.
Context {matchisi : match_index_sanity_interface}.
Context {aersi : append_entries_reply_sublog_interface}.
Context {cei : candidate_entries_interface}.
Context {vci : votes_correct_interface}.
Context {cci : cronies_correct_interface}.
Definition match_index_all_entries_nw (net : network) : Prop :=
forall p t es e,
In p (nwPackets net) ->
pBody p = AppendEntriesReply t es true ->
currentTerm (snd (nwState net (pDst p))) = t ->
In e (log (snd (nwState net (pDst p)))) ->
eTerm e = t ->
eIndex e <= maxIndex es ->
type (snd (nwState net (pDst p))) = Leader ->
In (t, e) (allEntries (fst (nwState net (pSrc p)))).
Definition match_index_all_entries_inv (net : network) : Prop :=
match_index_all_entries net /\ match_index_all_entries_nw net.
Proof using.
unfold refined_raft_net_invariant_init,
match_index_all_entries_inv,
match_index_all_entries_nw,
match_index_all_entries.
simpl. intros.
intuition.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; intuition.
simpl in *. eauto 10.
Qed.
Proof using mili rri.
intros.
pose proof lift_prop _ match_index_leader_invariant _ ltac:(eauto) leader.
find_rewrite_lem deghost_spec. concludes. auto.
Qed.
Proof using matchisi rri.
intros.
pose proof lift_prop _ match_index_sanity_invariant _ ltac:(eauto) leader h.
find_rewrite_lem deghost_spec. concludes. auto.
Qed.
Proof using aersi rri.
intros.
pose proof lift_prop _ append_entries_reply_sublog_invariant _ ltac:(eauto).
unfold append_entries_reply_sublog in *.
find_apply_lem_hyp ghost_packet.
eapply_prop_hyp In In; eauto; try rewrite deghost_spec; eauto.
find_rewrite_lem deghost_spec. auto.
Qed.
Proof using aersi matchisi mili rlmli rri.
unfold refined_raft_net_invariant_client_request, match_index_all_entries_inv.
simpl. intros. break_and. split.
- unfold match_index_all_entries in *. simpl in *. intros.
repeat find_higher_order_rewrite. update_destruct_simplify_hyp.
+ find_copy_apply_lem_hyp handleClientRequest_type.
find_copy_apply_lem_hyp handleClientRequest_matchIndex_log. intuition.
* repeat find_rewrite.
{ update_destruct_simplify_hyp.
- apply update_elections_data_clientRequest_allEntries_old'.
find_apply_hyp_hyp. repeat find_rewrite. auto.
- find_apply_hyp_hyp. repeat find_rewrite. auto.
}
* break_exists. break_and. repeat find_rewrite.
{ update_destruct_simplify_hyp.
- unfold update_elections_data_client_request. find_rewrite.
break_if.
+ repeat find_rewrite. simpl. break_or_hyp.
* auto.
* right.
find_copy_apply_lem_hyp maxIndex_is_max; [|solve[apply entries_sorted_invariant; auto]].
rewrite <- lifted_match_index_leader in * by auto.
eapply_prop_hyp In In; eauto. repeat find_rewrite. auto.
+ do_bool. find_rewrite. simpl length in *. lia.
- find_erewrite_lem get_set_diff_default.
pose proof lifted_match_index_sanity _ leader h0 ltac:(eauto) ltac:(auto).
break_or_hyp.
+ simpl in *. lia.
+ find_apply_hyp_hyp. repeat find_rewrite. auto.
}
+ find_apply_hyp_hyp. update_destruct_simplify_hyp.
* apply update_elections_data_clientRequest_allEntries_old'.
repeat find_rewrite. auto.
* repeat find_rewrite. auto.
- unfold match_index_all_entries_nw in *.
simpl. intros.
find_apply_hyp_hyp. break_or_hyp.
+ repeat find_higher_order_rewrite. update_destruct_simplify_hyp.
* find_copy_apply_lem_hyp handleClientRequest_type. break_and. repeat find_rewrite.
find_copy_apply_lem_hyp handleClientRequest_log.
{ intuition.
- repeat find_rewrite.
eapply_prop_hyp In In; eauto.
update_destruct_simplify_hyp.
+ apply update_elections_data_clientRequest_allEntries_old'.
repeat find_rewrite. auto.
+ repeat find_rewrite. auto.
- break_exists. break_and. repeat find_rewrite.
assert (es <> nil).
{
apply maxIndex_gt_0_nonempty.
eapply lt_le_trans; [|eauto].
simpl in *. break_or_hyp.
- repeat find_rewrite. lia.
- eapply entries_gt_0_invariant; eauto.
}
pose proof maxIndex_non_empty es. concludes.
break_exists_name max_e. intuition.
find_eapply_lem_hyp lifted_append_entries_reply_sublog; repeat find_rewrite; eauto.
simpl In in *. break_or_hyp.
+ find_apply_lem_hyp maxIndex_is_max; [|solve[apply entries_sorted_invariant; auto]].
lia.
+ eapply_prop_hyp In In; eauto; [|solve[repeat find_rewrite; auto]].
update_destruct_simplify_hyp.
* apply update_elections_data_clientRequest_allEntries_old'.
repeat find_rewrite. auto.
* repeat find_rewrite. auto.
}
* eapply_prop_hyp In In; eauto.
{ update_destruct_simplify_hyp.
- apply update_elections_data_clientRequest_allEntries_old'.
repeat find_rewrite. auto.
- repeat find_rewrite. auto.
}
+ find_apply_lem_hyp handleClientRequest_packets. subst. simpl in *. intuition.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto.
Qed.
Proof using.
intros.
update_destruct_simplify_hyp.
- unfold update_elections_data_timeout. repeat break_match; auto.
- auto.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; repeat find_inversion.
- do_in_map. subst. eauto 10.
- do_in_map. subst. eauto 10.
- simpl in *. intuition.
Qed.
Proof using.
unfold refined_raft_net_invariant_timeout, match_index_all_entries_inv.
simpl. intros. break_and. split.
- unfold match_index_all_entries in *. simpl. intros.
repeat find_higher_order_rewrite.
apply allEntries_update_timeout.
update_destruct_simplify_hyp.
+ find_erewrite_lem handleTimeout_log_same.
find_copy_apply_lem_hyp handleTimeout_type. intuition; try congruence.
find_erewrite_lem handleTimeout_matchIndex.
repeat find_rewrite.
eapply_prop_hyp In In; eauto. congruence.
+ eapply_prop_hyp In In; eauto. congruence.
- unfold match_index_all_entries_nw in *.
simpl. intros.
find_apply_hyp_hyp. break_or_hyp.
+ repeat find_higher_order_rewrite.
apply allEntries_update_timeout.
update_destruct_simplify_hyp.
* find_erewrite_lem handleTimeout_log_same.
find_copy_apply_lem_hyp handleTimeout_type.
intuition; try congruence.
eapply_prop_hyp In In; eauto; try congruence.
* eapply_prop_hyp In In; eauto; congruence.
+ do_in_map. find_eapply_lem_hyp handleTimeout_sends_RV; eauto.
break_exists. subst. simpl in *. discriminate.
Qed.
Proof using.
unfold handleAppendEntries.
intros.
repeat break_match; repeat find_inversion; auto; try discriminate.
Qed.
Proof using.
unfold handleAppendEntries.
intros.
repeat break_match; repeat find_inversion; auto; try discriminate.
Qed.
Proof using naetli rri.
intros.
pose proof (lift_prop _ no_append_entries_to_leader_invariant _ ltac:(eauto)).
unfold no_append_entries_to_leader in *.
find_apply_lem_hyp ghost_packet.
match goal with
| [ H : forall _ _ _ , _, H' : In _ _ |- _ ] => eapply H in H'; eauto
end;
rewrite deghost_spec;
rewrite pDst_deghost_packet; auto.
Qed.
Proof using naetsi rri.
intros.
pose proof (lift_prop _ no_append_entries_to_self_invariant _ ltac:(eauto)).
unfold no_append_entries_to_self in *.
find_apply_lem_hyp ghost_packet.
match goal with
| [ H : forall _ _ _ , _, H' : In _ _ |- _ ] => eapply H in H'; eauto
end.
Qed.
Proof using.
unfold handleAppendEntries, advanceCurrentTerm.
intros. repeat break_match; repeat find_inversion; simpl in *; repeat do_bool; eauto;
eexists; f_equal; eauto using NPeano.Nat.le_antisymm.
Qed.
Proof using.
unfold not_empty.
intros. break_match; congruence.
Qed.
Proof using.
unfold not_empty.
intros. break_match; congruence.
Qed.
Proof using.
unfold handleAppendEntries, haveNewEntries.
intros.
break_if; try find_inversion.
break_if.
- break_if; find_inversion; simpl;
repeat (do_bool; repeat break_and).
+ find_apply_lem_hyp not_empty_true_elim.
pose proof maxIndex_non_empty es ltac:(auto).
break_exists_exists. intuition.
+ break_or_hyp.
* find_apply_lem_hyp not_empty_false_elim. congruence.
* break_match; try discriminate.
do_bool. rewrite advanceCurrentTerm_log.
find_apply_lem_hyp findAtIndex_elim. break_and.
pose proof maxIndex_non_empty es ltac:(auto).
break_exists_name e'. break_and.
match goal with
| [ H : forall _ _ _, In _ _ -> _ |- _ ] =>
specialize (H e' e e')
end.
repeat find_rewrite. repeat concludes. intuition.
assert (e = e').
{ eapply uniqueIndices_elim_eq; eauto.
auto using sorted_uniqueIndices.
}
subst. eauto.
- break_match; try find_inversion.
break_if; try find_inversion.
break_if; find_inversion; simpl;
repeat (do_bool; repeat break_and).
+ find_apply_lem_hyp not_empty_true_elim.
pose proof maxIndex_non_empty es ltac:(auto).
break_exists_exists. intuition.
+ break_or_hyp.
* find_apply_lem_hyp not_empty_false_elim. congruence.
* break_match; try discriminate.
do_bool. rewrite advanceCurrentTerm_log.
find_apply_lem_hyp findAtIndex_elim. break_and.
pose proof maxIndex_non_empty es ltac:(auto).
break_exists_name e'. break_and.
match goal with
| [ H : forall _ _ _, In _ _ -> _ |- _ ] =>
specialize (H e' e0 e')
end.
repeat find_rewrite. repeat concludes. intuition.
assert (e0 = e').
{ eapply uniqueIndices_elim_eq; eauto.
auto using sorted_uniqueIndices.
}
subst. eauto.
Qed.
Proof using.
unfold handleAppendEntries, advanceCurrentTerm.
intros. repeat break_match; repeat find_inversion; simpl; auto; repeat do_bool;
eauto using Nat.le_antisymm.
Qed.
Proof using taifoli rri.
intros net0;intros.
pose proof (lift_prop _ terms_and_indices_from_one_log_invariant).
unfold terms_and_indices_from_one_log in *.
rewrite <- deghost_spec with (net := net0). auto.
Qed.
Proof using lsi rri.
intros.
pose proof (lift_prop _ leader_sublog_invariant_invariant _ ltac:(eauto)) as Hinv.
unfold leader_sublog_invariant, leader_sublog_nw_invariant in *.
destruct Hinv as [Hhost Hnw].
find_apply_lem_hyp ghost_packet.
eapply_prop_hyp In In; eauto; try find_rewrite_lem deghost_spec; try rewrite deghost_spec; eauto.
Qed.
Proof using ollpti lhllsi lsi aelli rri.
intros.
find_copy_eapply_lem_hyp append_entries_leaderLogs_invariant; eauto.
break_exists. break_and.
subst es.
find_apply_lem_hyp in_app_or. destruct H4.
- find_copy_apply_hyp_hyp.
eapply lifted_leader_sublog_nw; eauto; [subst; auto|auto with datatypes].
- find_eapply_lem_hyp leaders_have_leaderLogs_strong_invariant; auto.
break_exists.  break_and.
pose proof one_leaderLog_per_term_invariant _ ltac:(eauto) h x (currentTerm (snd (nwState net h))) x3 x0.
concludes.
subst_max.
concludes.
break_and. subst.
find_rewrite. eauto using Prefix_In with *.
Qed.
Proof using ollpti lhllsi lsi aelli laei rlmli taifoli naetsi naetli rri.
unfold refined_raft_net_invariant_append_entries', match_index_all_entries_inv.
simpl. intros. break_and.
split.
- unfold match_index_all_entries in *. simpl. intros.
repeat find_higher_order_rewrite.
update_destruct_simplify_hyp.
+ assert (currentTerm (snd (nwState net (pDst p))) <> t).
{ intro.
match goal with
| [ H : pBody _ = AppendEntries _ _ _ _ _ _ |- _ ] =>
eapply lifted_no_AE_to_leader with (net := net) in H; eauto
end.
eapply handleAppendEntries_leader_was_leader; eauto.
}
find_apply_lem_hyp handleAppendEntries_post_leader_nop; auto.
subst. eapply_prop_hyp In In; eauto.
repeat find_rewrite.
update_destruct_simplify_hyp; auto using update_elections_data_appendEntries_preserves_allEntries.
+ eapply_prop_hyp In In; eauto. repeat find_rewrite.
update_destruct_simplify_hyp; auto using update_elections_data_appendEntries_preserves_allEntries.
- unfold match_index_all_entries_nw. simpl.  intros.
find_apply_hyp_hyp. break_or_hyp.
+ unfold match_index_all_entries_nw in *.
repeat find_higher_order_rewrite.
update_destruct_simplify_hyp.
* assert (currentTerm (snd (nwState net (pDst p))) <> t).
{ intro.
match goal with
| [ H : pBody _ = AppendEntries _ _ _ _ _ _ |- _ ] =>
eapply lifted_no_AE_to_leader with (net := net) in H; eauto
end.
eapply handleAppendEntries_leader_was_leader; eauto.
}
match goal with
| [ H : context [handleAppendEntries] |- _ ] =>
apply handleAppendEntries_post_leader_nop in H; auto
end.
subst.
match goal with
| [ H : In _ (_ ++ _), H' : forall _ _ _ _, In _ _ -> _ |- _ ] =>
eapply in_middle_insert in H; eapply H' in H; eauto; try congruence
end.
{ update_destruct_simplify_hyp.
- apply update_elections_data_appendEntries_preserves_allEntries.
repeat find_rewrite. auto.
- auto.
}
* match goal with
| [ H : forall _ _ _ _, In _ _ -> _, H' : pBody _ = AppendEntriesReply _ _ _ |- _ ] =>
eapply H in H'; eauto
end.
{ update_destruct_simplify_hyp.
- apply update_elections_data_appendEntries_preserves_allEntries.
repeat find_rewrite. auto.
- auto.
}
+ simpl in *.
find_copy_apply_lem_hyp handleAppendEntries_message. break_exists.
subst. find_inversion.
repeat find_higher_order_rewrite.
update_destruct_simplify_hyp.
* exfalso. eapply lifted_no_AE_to_self with (net := net); eauto.
* unfold update_elections_data_appendEntries. repeat find_rewrite. simpl.
{ find_copy_apply_lem_hyp handleAppendEntries_success_allEntries.
- break_exists. break_and.
find_copy_apply_lem_hyp handleAppendEntries_success_term.
assert (In x (log (snd (nwState net (pSrc p))))).
{ eapply appendEntries_sublog; eauto. subst. repeat find_rewrite. auto. }
assert (entries_match (log d) (log (snd (nwState net (pSrc p))))).
{ match goal with
| [ H : refined_raft_intermediate_reachable (mkNetwork ?a ?b) |- _ ] =>
let H' := fresh "H" in
pose proof entries_match_invariant _ (pDst p) (pSrc p) H as H';
simpl in H'; repeat find_higher_order_rewrite; rewrite_update;
simpl in H'; auto
end.
}
assert (In e (log d)) as Helogd.
{ match goal with
| [ H : entries_match _ _ |- _ ] =>
specialize (H x x e)
end.
assert (eIndex e <= eIndex x) by lia.
repeat concludes. intuition.
}
match goal with
| [ H : refined_raft_intermediate_reachable (mkNetwork ?a ?b) |- _ ] =>
let H' := fresh "H" in
pose proof log_all_entries_invariant _ H (pDst p) e as H';
simpl in H'; repeat find_higher_order_rewrite; rewrite_update;
simpl in H'; unfold update_elections_data_appendEntries in H';
repeat find_rewrite; simpl in H'
end.
auto.
- find_apply_lem_hyp lifted_terms_and_indices_from_one_log; auto. break_and.
apply maxIndex_gt_0_nonempty. lia.
- intros.
match goal with
| [ H : refined_raft_intermediate_reachable (mkNetwork _ _) |- _ ] => clear H
end.
pose proof entries_match_nw_host_invariant _ ltac:(eauto) _ _ _ _ _ _ _ (pDst p)
e0 e' e'' ltac:(eauto) ltac:(eauto).
repeat find_rewrite. auto.
- apply entries_sorted_invariant. auto.
}
Qed.
Proof using.
unfold handleAppendEntriesReply.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto.
- do_bool. intuition.
- unfold advanceCurrentTerm. break_match; auto.
Qed.

Lemma update_nop_fst :
forall A B f x (v2 : B) y,
fst (update name_eq_dec f x (fst (f x), v2) y) = fst (A := A) (f y).