Section SpecLemmas.
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}.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat tuple_inversion; intuition;
simpl in *; intuition;
tuple_inversion; intuition.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros. repeat break_match; repeat tuple_inversion; intuition.
Qed.
Proof using.
intros. unfold update_elections_data_appendEntries.
repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_timeout, handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; simpl in *; intuition; repeat tuple_inversion; intuition.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros. repeat break_match; repeat tuple_inversion; simpl in *; auto; lia.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros. repeat break_match; repeat tuple_inversion; intuition.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat tuple_inversion; do_bool; intuition;
simpl in *; intuition; do_bool; try discriminate; intuition try congruence.
lia.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat tuple_inversion; simpl; auto.
Qed.
Proof using.
unfold update_elections_data_timeout, handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; simpl in *; intuition; repeat tuple_inversion;
intuition (auto; congruence).
Qed.
Proof using.
unfold update_elections_data_timeout.
intros. repeat break_match; simpl in *; intuition; repeat tuple_inversion; intuition.
Qed.
Proof using.
intros. unfold update_elections_data_client_request.
repeat break_match; auto.
Qed.
Proof using.
intros. unfold update_elections_data_client_request.
repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_client_request.
intros. repeat break_match; repeat tuple_inversion; intuition.
Qed.
Proof using.
unfold update_elections_data_timeout.
intros. repeat break_match; simpl in *; intuition; repeat tuple_inversion; intuition.
Qed.
Proof using.
intros. unfold update_elections_data_appendEntries.
repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_appendEntries.
intros. repeat break_match; repeat tuple_inversion; intuition.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat tuple_inversion; intuition;
simpl in *; intuition;
tuple_inversion; intuition.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros. repeat break_match; repeat tuple_inversion; intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_requestVoteReply.
break_match; simpl in *; auto.
Qed.
Proof using.
unfold update_elections_data_client_request in *.
intros. repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
unfold update_elections_data_timeout.
intros.
repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_appendEntries.
intros. repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros.
repeat break_match; repeat find_inversion; intuition.
simpl in *. intuition.
find_inversion. intuition.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros.
repeat break_match; repeat find_inversion; intuition.
simpl in *. intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request.
repeat break_match; repeat find_inversion; auto.
simpl.
find_copy_apply_lem_hyp handleClientRequest_log.
intuition.
- repeat find_rewrite. do_bool. lia.
- right.  break_exists_exists. intuition.
congruence.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request.
unfold handleClientRequest in *.
repeat break_match; repeat tuple_inversion; auto.
- discriminate.
- do_bool. find_rewrite. lia.
- do_bool. find_rewrite. lia.
- simpl in *. right. intuition. exists e. find_inversion. simpl. intuition.
- simpl in *. do_bool. lia.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros.
repeat break_match; auto.
Qed.
Proof using.
intros. unfold handleAppendEntries in *.
repeat break_match; find_inversion; simpl in *; intuition; eauto using advanceCurrentTerm_log.
Qed.
Proof using.
intros.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; simpl in *; auto.
find_apply_lem_hyp handleAppendEntriesReply_entries. subst.
do_in_map. do_in_app. subst. intuition.
- do_in_map. subst. simpl in *. auto.
- left. apply in_map_iff. eexists; eauto.
Qed.
Proof using.
intros.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; simpl in *; auto.
find_apply_lem_hyp handleAppendEntriesReply_entries.
intuition. subst.
do_in_app. intuition.
do_in_map. repeat find_inversion. subst. simpl in *. auto.
Qed.
Proof using.
intros.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; simpl in *; auto.
unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; simpl in *; repeat find_inversion; do_bool; simpl in *; auto;
try (do_in_app; intuition; do_in_map;  repeat find_inversion; subst; simpl in *; auto);
find_eapply_lem_hyp Nat.le_antisymm; eauto.
Qed.
Proof using.
intros. unfold update_elections_data_appendEntries, handleAppendEntries in *.
repeat break_match; repeat find_inversion; auto; simpl in *.
- right. intuition. right. left. do_bool. intuition; eauto using advanceCurrentTerm_term.
find_apply_lem_hyp haveNewEntries_not_empty. congruence.
- rewrite advanceCurrentTerm_log.
right. intuition. left. intuition.
do_bool.
eauto using advanceCurrentTerm_term.
- right. intuition. right. right. do_bool. intuition.
+ eauto using advanceCurrentTerm_term.
+ find_apply_lem_hyp haveNewEntries_not_empty. congruence.
+ find_apply_lem_hyp findAtIndex_elim. intuition; do_bool; eauto.
- rewrite advanceCurrentTerm_log. do_bool. rewrite advanceCurrentTerm_term; auto.
intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; simpl in *; auto.
find_apply_lem_hyp handleAppendEntriesReply_entries. intuition.
find_inversion.
do_in_app. intuition.
do_in_map. find_inversion. find_copy_apply_hyp_hyp. intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request in *.
repeat break_match; subst; simpl in *; auto. intuition. subst.
do_bool. find_apply_lem_hyp handleClientRequest_log.
intuition.
- match goal with
| H : log _ = log (snd _) |- _ => symmetry in H
end. repeat find_rewrite. simpl in *. lia.
- break_exists. intuition. repeat find_rewrite.
find_inversion. intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request in *.
repeat break_match; subst; simpl in *; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request in *.
repeat break_match; subst; simpl in *; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_timeout. repeat break_match; simpl; auto.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; simpl in *; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_client_request in *.
repeat break_match; subst; simpl in *; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_timeout. repeat break_match; simpl; auto.
unfold handleTimeout, tryToBecomeLeader in *.
repeat break_match; find_inversion; simpl in *; auto; congruence.
Qed.
Proof using.
unfold update_elections_data_requestVote.
intros.
repeat break_match; auto.
Qed.
Proof using.
unfold update_elections_data_appendEntries.
intros. break_let. break_match; auto.
break_if; auto.
simpl. intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_requestVote in *.
repeat break_match; simpl in *; intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_timeout.
repeat break_match; simpl in *; auto.
Qed.
Proof using.
unfold update_elections_data_timeout, handleTimeout, tryToBecomeLeader in *.
intros.
repeat break_match; simpl in *; intuition; repeat tuple_inversion; intuition;
simpl in *; repeat find_inversion; intuition; try congruence.
Qed.

Lemma update_elections_data_timeout_votedFor :
forall h cid st out st' m,
handleTimeout h (snd st) = (out, st', m) ->
votedFor st' = Some cid ->
(votedFor (snd st) = Some cid /\
currentTerm st' = currentTerm (snd st) /\
type st' = type (snd st) /\
votesWithLog (update_elections_data_timeout h st) = votesWithLog (fst st)) \/
(cid = h /\
currentTerm st' = S (currentTerm (snd st)) /\
votesWithLog (update_elections_data_timeout h st) =
(currentTerm st', cid, (log st')) :: votesWithLog (fst st)).