Section CandidateEntriesProof.
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 {cti : cronies_term_interface}.
Context {tsi : term_sanity_interface}.
Context {vci : votes_correct_interface}.
Context {cci : cronies_correct_interface}.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; intuition.
simpl in *. intuition. subst. auto.
Qed.
Proof using cci.
unfold refined_raft_net_invariant_client_request, CandidateEntries.
intros. subst.
intuition.
- unfold candidateEntries_host_invariant in *.
intros; simpl in *.
eapply candidateEntries_ext; try eassumption.
repeat find_higher_order_rewrite.
unfold update_elections_data_client_request in *. repeat break_let. simpl in *.
destruct (name_eq_dec h0 h); subst.
+ rewrite_update.
unfold update_elections_data_client_request in *. repeat break_let. simpl in *.
simpl in *. find_inversion.
find_apply_lem_hyp handleClientRequest_spec; intuition eauto.
find_apply_hyp_hyp.
intuition.
* rewrite_update.
eapply candidateEntries_same; eauto; intuition;
destruct (name_eq_dec h0 h); subst; rewrite_update; auto.
repeat break_match; simpl; auto.
* find_apply_lem_hyp cronies_correct_invariant.
unfold candidateEntries. exists h.
intuition; rewrite_update; simpl in *; try congruence.
repeat find_rewrite. simpl in *.
repeat break_match; simpl; pose won_election_cronies; eauto.
+ rewrite_update.
find_apply_lem_hyp handleClientRequest_spec.
eapply candidateEntries_same; eauto; intuition;
destruct (name_eq_dec h1 h); subst; rewrite_update; auto;
tuple_inversion; repeat find_rewrite;
repeat break_match; simpl; auto.
- unfold candidateEntries_nw_invariant in *.
intros. simpl in *.
eapply candidateEntries_ext; try eassumption.
find_apply_lem_hyp handleClientRequest_spec.
intuition.
subst. simpl in *.
eapply_prop_hyp candidateEntries AppendEntries; eauto.
+ eapply candidateEntries_same; eauto; intuition;
destruct (name_eq_dec h h0); subst; rewrite_update; auto.
unfold update_elections_data_client_request; repeat break_match; simpl; auto.
+ find_apply_hyp_hyp. intuition.
Qed.
Proof using.
unfold update_elections_data_timeout.
intros.
repeat break_match; subst; simpl in *; auto.
unfold handleTimeout, tryToBecomeLeader in *.
repeat break_match; try congruence; repeat find_inversion; simpl in *;
unfold raft_data in *;
congruence.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; repeat find_inversion; simpl in *; intuition;
do_in_map; subst; simpl; eauto.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros. simpl in *.
repeat break_match; try congruence; repeat find_inversion; auto.
Qed.
Proof using.
unfold update_elections_data_timeout.
intros.
repeat break_match; repeat find_inversion; simpl; auto.
break_match; auto.
Qed.
Proof using cti.
intros.
destruct (serverType_eq_dec (type (snd (A:=electionsData) (B:=raft_data) (nwState net h))) Leader).
+ (* Leader case *)
unfold handleTimeout, tryToBecomeLeader in *. simpl in *.
find_rewrite. find_inversion.
eapply candidateEntries_same; eauto;
intros;
repeat (rewrite update_fun_comm; simpl in * );
update_destruct; subst; rewrite_update;
auto using update_elections_data_timeout_leader_cronies_same.
+ (* non-Leader case *)
unfold candidateEntries in *.
break_exists. break_and.
exists x.
rewrite update_fun_comm; simpl.
rewrite update_fun_comm; simpl.
rewrite update_fun_comm; simpl.
rewrite update_fun_comm; simpl.
rewrite update_fun_comm with (f := type); simpl.
update_destruct; subst; rewrite_update; auto.
split.
* match goal with
| [ H : handleTimeout _ _ = _ |- _ ] =>
pose proof H;
apply update_elections_data_timeout_cronies with (t := eTerm e) in H
end.
intuition; find_rewrite; auto.
find_apply_lem_hyp wonElection_exists_voter.
break_exists.
find_apply_lem_hyp in_dedup_was_in.
find_copy_apply_lem_hyp cronies_term_invariant; auto.
find_copy_apply_lem_hyp handleTimeout_not_leader_inc_term; auto.
simpl in *.
lia.
* intros.
find_apply_lem_hyp wonElection_exists_voter.
break_exists.
find_apply_lem_hyp in_dedup_was_in.
find_copy_apply_lem_hyp cronies_term_invariant; auto.
find_copy_apply_lem_hyp handleTimeout_not_leader_inc_term; auto.
simpl in *.
lia.
Qed.
Proof using cti.
unfold refined_raft_net_invariant_timeout, CandidateEntries.
intros. subst.
intuition; simpl in *.
- unfold candidateEntries_host_invariant in *.
intros.
eapply candidateEntries_ext; try eassumption.
repeat find_higher_order_rewrite.
find_rewrite_lem update_fun_comm. simpl in *.
find_rewrite_lem update_fun_comm. simpl in *.
find_erewrite_lem handleTimeout_log_same.
find_rewrite_lem_by update_nop_ext' auto.
find_apply_hyp_hyp.
eauto using handleTimeout_preserves_candidateEntries.
- unfold candidateEntries_nw_invariant in *.
intros.
simpl in *.
eapply candidateEntries_ext; eauto.
find_apply_hyp_hyp.
break_or_hyp.
+ eapply_prop_hyp pBody pBody; eauto.
eauto using handleTimeout_preserves_candidateEntries.
+ do_in_map. subst. simpl in *.
eapply handleTimeout_only_sends_RequestVotes in H8; eauto.
break_exists. congruence.
Qed.
Proof using.
unfold update_elections_data_appendEntries.
intros.
repeat break_match; auto.
Qed.
Proof using.
intros.
unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; try find_inversion; subst; simpl in *; intuition;
do_bool; intuition; try solve [break_exists; congruence];
in_crush; eauto using removeAfterIndex_in.
Qed.
Proof using.
unfold handleAppendEntries in *.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold candidateEntries.
intros. break_exists. break_and.
exists x.
split.
- rewrite update_fun_comm. simpl.
rewrite update_fun_comm. simpl.
rewrite update_elections_data_appendEntries_cronies_same.
destruct (name_eq_dec x h); subst; rewrite_update; auto.
- intros.
rewrite update_fun_comm. simpl.
find_rewrite_lem update_fun_comm. simpl in *.
destruct (name_eq_dec x h); subst; rewrite_update; auto.
find_apply_lem_hyp handleAppendEntries_term_same_or_type_follower.
intro; intuition;
repeat find_rewrite; auto; discriminate.
Qed.
Proof using.
eauto 20.
Qed.
Proof using.
red. unfold CandidateEntries.
intros. subst.
intuition; simpl in *.
- unfold candidateEntries_host_invariant in *.
intros.
eapply candidateEntries_ext; eauto.
repeat find_higher_order_rewrite.
find_rewrite_lem update_fun_comm. simpl in *.
update_destruct; subst; rewrite_update;
eapply handleAppendEntries_preserves_candidate_entries; eauto.
find_copy_apply_lem_hyp handleAppendEntries_spec. break_and.
find_apply_hyp_hyp. intuition eauto.
- unfold candidateEntries_nw_invariant in *.
intros. simpl in *.
eapply candidateEntries_ext; eauto.
find_apply_hyp_hyp.
intuition.
+ eapply handleAppendEntries_preserves_candidate_entries; eauto.
+ subst. simpl in *. find_apply_lem_hyp handleAppendEntries_spec.
break_and.
subst.
exfalso.
eauto using is_append_entries_intro.
Qed.
Proof using.
intros.
unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; try find_inversion; subst; simpl in *; intuition;
do_bool; intuition.
Qed.
Proof using.
unfold candidateEntries.
intros. break_exists. break_and.
exists x.
split.
- rewrite update_fun_comm. simpl.
rewrite update_fun_comm. simpl.
update_destruct; subst; rewrite_update; auto.
- intros. update_destruct; subst; rewrite_update; auto.
simpl in *.
find_apply_lem_hyp handleAppendEntriesReply_spec.
intuition; repeat find_rewrite; intuition.
congruence.
Qed.
Ltac prove_in :=
match goal with
| [ _ : nwPackets ?net = _,
_ : In (?p : packet) _ |- _] =>
assert (In p (nwPackets net)) by (repeat find_rewrite; do_in_app; intuition)
| [ _ : nwPackets ?net = _,
_ : pBody ?p = _ |- _] =>
assert (In p (nwPackets net)) by (repeat find_rewrite; intuition)
end.
Proof using.
red. unfold CandidateEntries. intros. intuition.
- unfold candidateEntries_host_invariant in *.
intros. simpl in *. eapply candidateEntries_ext; eauto.
repeat find_higher_order_rewrite.
find_rewrite_lem update_fun_comm. simpl in *.
update_destruct.
+ subst. rewrite_update.
unfold candidateEntries in *.
find_apply_lem_hyp handleAppendEntriesReply_spec. break_and.
repeat find_rewrite.
find_apply_hyp_hyp.
break_exists; exists x; eauto.
update_destruct; intuition; subst; rewrite_update; simpl in *; auto;
repeat find_rewrite; intuition; congruence.
+ rewrite_update. find_apply_hyp_hyp.
eauto using handleAppendEntriesReply_preserves_candidate_entries.
- unfold candidateEntries_nw_invariant in *. intros. simpl in *.
find_apply_hyp_hyp. intuition.
+ (* packet already in nw *)
prove_in.
eapply candidateEntries_ext; eauto.
find_eapply_lem_hyp handleAppendEntriesReply_preserves_candidate_entries; eauto.
subst. auto.
+ exfalso.
do_in_map.
find_eapply_lem_hyp handleAppendEntriesReply_spec; eauto.
subst. simpl in *. find_rewrite.
match goal with
| H : ~ is_append_entries _ |- _ => apply H
end.
repeat eexists; eauto.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; auto.
Qed.
Proof using.
unfold advanceCurrentTerm.
intros. repeat break_match; auto.
Qed.

Lemma handleRV_advanceCurrentTerm_preserves_candidateEntries :
forall net h h' t lli llt e,
candidateEntries e (nwState net) ->
candidateEntries e
(update name_eq_dec (nwState net) h
(update_elections_data_requestVote h h' t h' lli llt (nwState net h),
advanceCurrentTerm (snd (nwState net h)) t)).