Section AllEntriesVotesWithLog.
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 {aeli : allEntries_log_interface}.
Context {vwltsi : votesWithLog_term_sanity_interface}.
Context {vvwlci : votes_votesWithLog_correspond_interface}.
Context {vci : votes_correct_interface}.
Proof using.
intros. unfold update_elections_data_appendEntries in *.
repeat break_match; auto. simpl in *.
do_in_app. intuition. do_in_map. find_inversion.
right.
unfold handleAppendEntries in *.
repeat break_match; find_inversion; simpl in *; do_bool; auto.
Qed.
Proof using vwltsi.
red. unfold allEntries_votesWithLog. intros. simpl in *.
repeat find_higher_order_rewrite.
destruct_update; simpl in *.
- find_eapply_lem_hyp votesWithLog_update_elections_data_append_entries; eauto.
find_copy_apply_lem_hyp votesWithLog_term_sanity_invariant; eauto.
find_eapply_lem_hyp update_elections_data_appendEntries_allEntries'; eauto.
intuition; do 2 (unfold raft_data, ghost_data in *; simpl in *); try lia.
eapply_prop_hyp votesWithLog votesWithLog; eauto. intuition.
right. break_exists_exists. intuition.
find_higher_order_rewrite. destruct_update; simpl in *; auto.
rewrite update_elections_data_appendEntries_leaderLogs. auto.
- eapply_prop_hyp votesWithLog votesWithLog; eauto. intuition.
right. break_exists_exists. intuition.
find_higher_order_rewrite. destruct_update; simpl in *; auto.
rewrite update_elections_data_appendEntries_leaderLogs. auto.
Qed.
Proof using.
red. unfold allEntries_votesWithLog. intros. simpl in *.
repeat find_higher_order_rewrite.
destruct_update; simpl in *;
eapply_prop_hyp votesWithLog votesWithLog; eauto; intuition;
right; break_exists_exists; intuition;
find_higher_order_rewrite; destruct_update; simpl in *; auto.
Qed.
Definition currentTerm_votedFor_votesWithLog net :=
forall h t n,
(currentTerm (snd (nwState net h)) = t /\
votedFor (snd (nwState net h)) = Some n) ->
exists l,
In (t, n, l) (votesWithLog (fst (nwState net h))).
Proof using vci vvwlci.
unfold currentTerm_votedFor_votesWithLog. intros.
eapply votes_votesWithLog_correspond_invariant; eauto.
break_and.
eapply votes_correct_invariant; eauto.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; simpl in *; auto);
do_bool; auto; congruence.
Qed.
Proof using.
intros.
unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; find_inversion; simpl in *; do_bool; auto.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat tuple_inversion; intuition;
simpl in *; intuition;
tuple_inversion; intuition; repeat (do_bool; intuition);
try congruence;
unfold raft_data, ghost_data in *; simpl in *;
repeat find_rewrite; repeat find_inversion;
find_copy_apply_lem_hyp handleRequestVote_currentTerm_leaderId;
intuition;
find_apply_lem_hyp handleRequestVote_currentTerm_leaderId'; repeat find_rewrite; try congruence; intuition.
Qed.
Proof using aeli.
red. unfold allEntries_votesWithLog. intros. simpl in *.
repeat find_higher_order_rewrite.
destruct_update; simpl in *.
- find_rewrite_lem update_elections_data_requestVote_allEntries.
find_copy_apply_lem_hyp handleRequestVote_currentTerm.
find_copy_eapply_lem_hyp votesWithLog_update_elections_data_request_vote; eauto.
intuition.
+ eapply_prop_hyp votesWithLog votesWithLog; eauto; intuition;
right; break_exists_exists; intuition;
find_higher_order_rewrite; destruct_update; simpl in *; auto.
rewrite leaderLogs_update_elections_data_requestVote. auto.
+ subst.
find_apply_lem_hyp handleRequestVote_log. repeat find_rewrite.
find_copy_eapply_lem_hyp allEntries_log_invariant; eauto.
intuition.
right. break_exists_exists. repeat find_higher_order_rewrite.
simpl in *.
destruct_update; simpl in *; intuition;
try rewrite leaderLogs_update_elections_data_requestVote; eauto.
+  subst.
find_apply_lem_hyp handleRequestVote_log. repeat find_rewrite.
find_copy_eapply_lem_hyp allEntries_log_invariant; eauto.
intuition.
right. break_exists_exists. repeat find_higher_order_rewrite.
simpl in *.
destruct_update; simpl in *; intuition;
try rewrite leaderLogs_update_elections_data_requestVote; eauto.
- eapply_prop_hyp votesWithLog votesWithLog; eauto; intuition;
right; break_exists_exists; intuition;
find_higher_order_rewrite; destruct_update; simpl in *; auto.
rewrite leaderLogs_update_elections_data_requestVote. auto.
Qed.
Proof using.
red. unfold allEntries_votesWithLog. intros. simpl in *.
repeat find_higher_order_rewrite.
destruct_update; simpl in *.
- find_rewrite_lem update_elections_data_requestVoteReply_allEntries.
find_eapply_lem_hyp votesWithLog_update_elections_data_request_vote_reply; eauto.
eapply_prop_hyp votesWithLog votesWithLog; eauto; intuition;
right; break_exists_exists; intuition;
find_higher_order_rewrite; destruct_update; simpl in *; auto.
eauto using update_elections_data_requestVoteReply_old.
- eapply_prop_hyp votesWithLog votesWithLog; eauto; intuition;
right; break_exists_exists; intuition;
find_higher_order_rewrite; destruct_update; simpl in *; auto.
eauto using update_elections_data_requestVoteReply_old.
Qed.

Lemma update_elections_data_client_request_allEntries_in_or_term :
forall h st client id c out st' ms t e,
handleClientRequest h (snd st) client id c = (out, st', ms) ->
In (t, e) (allEntries (update_elections_data_client_request h st client id c)) ->
In (t, e) (allEntries (fst st)) \/
t = currentTerm (snd st).