Section SpecLemmas.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; tuple_inversion; simpl in *; intuition; try discriminate;
try solve [exfalso; do_bool; lia].
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat break_match; repeat find_inversion; intuition idtac.
- simpl in *. discriminate.
- unfold advanceCurrentTerm in *.
break_if; simpl in *; do_bool; intuition.
Qed.
Proof using.
intros.
unfold handleRequestVoteReply, advanceCurrentTerm in *; repeat break_match;
subst; do_bool; intuition.
Qed.
Proof using.
unfold handleAppendEntries, advanceCurrentTerm.
intros.
repeat break_match; repeat tuple_inversion; simpl in *;
do_bool; auto; try lia.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; repeat tuple_inversion; simpl in *; do_bool; congruence.
Qed.
Proof using.
unfold doGenericServer.
intros.
repeat break_match; repeat find_inversion;
use_applyEntries_spec; subst; simpl in *;
auto.
Qed.
Proof using.
intros. unfold doLeader in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; tuple_inversion; do_bool; simpl; auto; lia.
Qed.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; do_bool; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros.
find_inversion. simpl.
auto with arith.
Qed.
Proof using.
unfold handleTimeout.
intros.
break_match; eauto using tryToBecomeLeader_currentTerm.
find_inversion. simpl.
auto with arith.
Qed.
Proof using.
intros. unfold haveNewEntries, not_empty in *.
do_bool. intuition. repeat break_match; congruence.
Qed.
Proof using.
intros.
unfold advanceCurrentTerm. break_if; simpl in *; auto.
Qed.
Proof using.
intros. unfold handleAppendEntries in *.
break_if; [find_inversion; subst; eauto|].
break_if;
[do_bool; break_if; find_inversion; subst;
try find_apply_lem_hyp haveNewEntries_not_empty; intuition; simpl in *;
eauto using advanceCurrentTerm_log|].
break_if.
- break_match; [|find_inversion; subst; eauto].
break_if; [find_inversion; subst; eauto|].
find_inversion; subst; simpl in *.
right. right.
find_apply_lem_hyp findAtIndex_elim. intuition; do_bool; eauto.
find_apply_lem_hyp haveNewEntries_not_empty. congruence.
- repeat break_match; find_inversion; subst; eauto.
simpl in *. eauto using advanceCurrentTerm_log.
Qed.
Proof using.
intros.
find_apply_lem_hyp handleAppendEntries_log.
intuition; subst; try find_rewrite; auto.
break_exists. intuition eauto.
Qed.
Proof using.
intros.
unfold haveNewEntries, not_empty in *.
repeat break_match; do_bool; intuition eauto; try congruence.
do_bool. eauto.
Qed.
Proof using.
intros.
unfold advanceCurrentTerm. break_if; simpl in *; auto.
Qed.
Proof using.
congruence.
Qed.
Proof using.
intros. unfold advanceCurrentTerm in *.
break_if; do_bool; intuition.
Qed.
Proof using.
intros. unfold handleAppendEntries in *.
break_if; [find_inversion; subst; eauto|].
break_if;
[do_bool; break_if; find_inversion; subst;
try find_apply_lem_hyp haveNewEntries_true;
simpl in *; intuition eauto using advanceCurrentTerm_log, advanceCurrentTerm_commitIndex, some_none, advanceCurrentTerm_term|].
simpl in *. intuition eauto using advanceCurrentTerm_log, advanceCurrentTerm_commitIndex.
break_match; [|find_inversion; subst; eauto].
break_if; [find_inversion; subst; eauto|].
break_if; [|find_inversion; subst; eauto using advanceCurrentTerm_log, advanceCurrentTerm_commitIndex].
find_inversion; subst; simpl in *.
right. right.
find_apply_lem_hyp findAtIndex_elim.
intuition; do_bool; find_apply_lem_hyp haveNewEntries_true;
intuition eauto using advanceCurrentTerm_term; congruence.
Qed.
Proof using.
intros.
unfold advanceCurrentTerm in *.
break_if; simpl in *; do_bool; auto.
Qed.
Proof using.
intros. unfold advanceCurrentTerm in *.
break_if; simpl in *; do_bool; lia.
Qed.
Proof using.
intros.
unfold handleRequestVote in *.
repeat break_match; find_inversion; subst; auto;
simpl in *; apply advanceCurrentTerm_currentTerm.
Qed.
Proof using.
intros.
find_copy_apply_lem_hyp handleRequestVote_currentTerm_monotonic.
find_apply_lem_hyp le_lt_or_eq. intuition.
right. find_apply_lem_hyp handleRequestVote_votedFor; intuition.
Qed.
Proof using.
intros. unfold handleRequestVoteReply in *.
repeat break_match; subst; do_bool; intuition.
apply advanceCurrentTerm_currentTerm.
Qed.
Proof using.
intros. unfold handleRequestVote in *.
repeat break_match; tuple_inversion; do_bool;
simpl; auto using advanceCurrentTerm_currentTerm.
Qed.
Proof using.
intros. unfold handleAppendEntries in *.
repeat break_match; find_inversion; simpl in *; auto; right;
intuition eauto using advanceCurrentTerm_currentTerm; right; congruence.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; simpl in *; auto);
do_bool; auto.
Qed.
Proof using.
intros. unfold handleRequestVoteReply, advanceCurrentTerm in *.
subst.
repeat (break_match; try find_inversion; simpl in *; auto);
do_bool; auto.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; intuition.
simpl in *. eauto 10.
Qed.
Proof using.
intros.
find_apply_lem_hyp handleClientRequest_log.
intuition; repeat find_rewrite; auto.
break_exists. intuition. repeat find_rewrite. eauto.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; find_inversion; subst; auto.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader.
intros.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
intros. unfold doGenericServer in *.
repeat break_match; find_inversion;
use_applyEntries_spec; simpl in *;
subst; auto.
Qed.

Lemma handleRequestVoteReply_spec :
forall h st h' t r st',
st' = handleRequestVoteReply h st h' t r ->
log st' = log st /\
(forall v, In v (votesReceived st) -> In v (votesReceived st')) /\
((currentTerm st' = currentTerm st /\ type st' = type st)
\/ type st' <> Candidate) /\
(type st <> Leader /\ type st' = Leader ->
(type st = Candidate /\ wonElection (dedup name_eq_dec
(votesReceived st')) = true)).