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.
Proof using.
intros.
unfold candidateEntries in *.
break_exists.  break_and.
exists x.
split; update_destruct; subst; rewrite_update; auto; simpl.
+ rewrite update_elections_data_requestVote_cronies_same. auto.
+ intros.
match goal with
| [ |- context [advanceCurrentTerm ?st ?t] ] =>
pose proof advanceCurrentTerm_same_or_type_follower st t
end.
intuition; try congruence.
repeat find_rewrite. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat break_match; repeat find_inversion;
auto using handleRV_advanceCurrentTerm_preserves_candidateEntries.
- eapply candidateEntries_same; eauto;
intros;
repeat (rewrite update_fun_comm; simpl in * );
update_destruct; subst; rewrite_update;
auto using update_elections_data_requestVote_cronies_same.
- do_bool. intuition. congruence.
- unfold candidateEntries in *. break_exists. break_and. exists x.
simpl.
split.
+ rewrite update_fun_comm with (f := fst). simpl.
rewrite update_fun_comm with (f := cronies). simpl.
rewrite update_elections_data_requestVote_cronies_same.
update_destruct; subst; rewrite_update; auto.
+ rewrite update_fun_comm with (f := snd). simpl.
rewrite update_fun_comm with (f := currentTerm). simpl.
rewrite update_fun_comm with (f := type). simpl.
update_destruct; subst; rewrite_update; auto.
match goal with
| [ |- context [advanceCurrentTerm ?st ?t] ] =>
pose proof advanceCurrentTerm_same_or_type_follower st t
end.
intuition; try congruence.
repeat find_rewrite. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat break_match; repeat find_inversion; eauto.
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; simpl in *;
try find_erewrite_lem handleRequestVote_same_log;
eapply handleRequestVote_preserves_candidateEntries; eauto.
- unfold candidateEntries_nw_invariant in *.
intros. simpl in *.
eapply candidateEntries_ext; eauto.
find_apply_hyp_hyp. intuition.
+ eauto using handleRequestVote_preserves_candidateEntries.
+ subst. simpl in *.
find_apply_lem_hyp handleRequestVote_only_sends_RVR.
subst. break_exists. discriminate.
Qed.
Proof using cci.
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.
match goal with
| |- context [handleRequestVoteReply ?h ?st ?h' ?t ?r] =>
remember (handleRequestVoteReply h st h' t r) as new_state
end.
find_copy_apply_lem_hyp handleRequestVoteReply_spec. break_and.
match goal with
| H : log _ = log _ |- _ => rewrite H in *
end.
find_apply_hyp_hyp.
eapply handleRequestVoteReply_preserves_candidate_entries; eauto.
+ rewrite_update.
eapply handleRequestVoteReply_preserves_candidate_entries; eauto.
- unfold candidateEntries_nw_invariant in *.
intros. simpl in *. eapply candidateEntries_ext; eauto.
repeat find_higher_order_rewrite.
eapply handleRequestVoteReply_preserves_candidate_entries; eauto.
Qed.
Proof using.
unfold doLeader.
intros.
repeat break_match; repeat find_inversion; simpl in *; intuition.
do_in_map.
unfold replicaMessage in *. simpl in *. subst. simpl in *.
find_inversion.
eauto using findGtIndex_in.
Qed.
Proof using.
red. unfold CandidateEntries.
intros.
intuition; simpl in *.
- unfold candidateEntries_host_invariant in *.
intros.
eapply candidateEntries_ext; eauto.
repeat find_higher_order_rewrite.
update_destruct; subst; rewrite_update.
+ simpl in *.
find_erewrite_lem doLeader_same_log.
repeat match goal with
| [ H : nwState ?net ?h = (_, ?d), H' : context [ log ?d ] |- _ ] =>
replace (log d) with (log (snd (nwState net h))) in H' by (repeat find_rewrite; auto)
end.
pose doLeader_preserves_candidateEntries; eauto.
+ pose doLeader_preserves_candidateEntries; eauto.
- unfold candidateEntries_nw_invariant in *.
intros. simpl in *.
eapply candidateEntries_ext; eauto.
find_apply_hyp_hyp.
intuition.
+ pose doLeader_preserves_candidateEntries; eauto.
+ do_in_map. subst. simpl in *.
eapply doLeader_preserves_candidateEntries; eauto.
eapply_prop candidateEntries_host_invariant.
match goal with
| [ H : _ |- _ ] => rewrite H
end.
simpl.
eauto using doLeader_in_entries.
Qed.
Proof using.
unfold doGenericServer.
intros.
repeat break_match; repeat find_inversion;
use_applyEntries_spec; subst; simpl in *;
auto.
Qed.
Proof using.
intros. unfold doGenericServer in *.
repeat break_match; find_inversion; subst; intuition;
use_applyEntries_spec; subst; simpl in *; auto.
Qed.
Proof using.
intros.
eapply candidateEntries_same; eauto;
intros;
repeat (rewrite update_fun_comm; simpl in * );
update_destruct; subst; rewrite_update; auto;
repeat find_rewrite; simpl; auto.
- find_copy_apply_lem_hyp doGenericServer_spec. break_and. auto.
- eauto using doGenericServer_same_type.
Qed.

Lemma candidate_entries_do_generic_server :
refined_raft_net_invariant_do_generic_server CandidateEntries.