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.
Proof using.
intros.
unfold handleRequestVoteReply, advanceCurrentTerm in *.
repeat break_match; try find_inversion; subst; simpl in *; intuition;
do_bool; intuition; try right; congruence.
Qed.
Proof using.
intros.
unfold handleRequestVoteReply, advanceCurrentTerm in *.
repeat break_match; try find_inversion; subst; simpl in *; intuition;
do_bool; intuition; try right; congruence.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; simpl in *; eauto;
repeat (do_in_map; subst; simpl in *); intuition; break_exists; congruence.
Qed.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; do_bool; auto.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; tuple_inversion; do_bool; intuition.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; tuple_inversion; do_bool; auto.
Qed.
Proof using.
intros. unfold handleRequestVoteReply, advanceCurrentTerm in *.
repeat break_match; subst; do_bool; intuition.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros.
find_apply_lem_hyp handleClientRequest_type.
intuition.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
repeat break_match; find_inversion; simpl; intuition.
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.
unfold doLeader. intros.
repeat break_match; find_inversion; simpl in *; auto.
Qed.
Proof using.
unfold handleAppendEntriesReply, advanceCurrentTerm.
intros.
repeat break_match; find_inversion; simpl in *; auto.
Qed.
Proof using.
intros.
eapply handleRequestVoteReply_spec; eauto.
Qed.
Proof using.
intros.
erewrite handleRequestVoteReply_log; eauto.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; find_inversion; subst; auto.
Qed.
Proof using.
intros. unfold doGenericServer in *.
repeat break_match; find_inversion; simpl in *;
subst; auto.
Qed.
Proof using.
intros. unfold handleAppendEntries in *.
repeat break_match; find_inversion;
intuition; break_exists; congruence.
Qed.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; simpl in *; auto.
Qed.
Proof using.
intros.
unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; simpl in *; auto).
Qed.
Proof using.
unfold advanceCurrentTerm. intros.
break_if; auto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros. find_inversion. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat (break_match; try discriminate; repeat (find_inversion; simpl in *));
auto using advanceCurrentTerm_clientCache.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros. repeat break_match; simpl; auto using advanceCurrentTerm_clientCache.
Qed.
Proof using.
intros. unfold doLeader in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; simpl in *; auto.
Qed.
Proof using.
intros.
unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; simpl in *; auto).
Qed.
Proof using.
unfold advanceCurrentTerm. intros.
break_if; auto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros. find_inversion. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat (break_match; try discriminate; repeat (find_inversion; simpl in *));
auto using advanceCurrentTerm_stateMachine.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros. repeat break_match; simpl; auto using advanceCurrentTerm_stateMachine.
Qed.
Proof using.
intros. unfold doLeader in *.
repeat break_match; find_inversion; auto.
Qed.
(* matchIndex *)
Definition matchIndex_preserved st st' :=
type st' = Leader ->
(type st = Leader /\ matchIndex st' = matchIndex st /\ log st' = log st).
Arguments matchIndex_preserved / _ _.
Definition matchIndex_preserved_except_at_host h st st' :=
type st' = Leader ->
(type st = Leader /\
forall h',
h <> h' ->
(assoc_default name_eq_dec (matchIndex st') h' 0) = (assoc_default name_eq_dec (matchIndex st) h') 0).
Arguments matchIndex_preserved_except_at_host / _ _ _.
Proof using.
intros. unfold handleAppendEntries, advanceCurrentTerm in *.
repeat break_match; find_inversion; simpl in *; auto; intros; congruence.
Qed.
Proof using.
intros.
unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; simpl in *; auto);
[|intros; congruence].
intros. intuition.
unfold assoc_default.
repeat break_match; rewrite get_set_diff in *; repeat find_rewrite; congruence.
Qed.
Proof using.
unfold advanceCurrentTerm. intros.
break_if; simpl in *; auto; congruence.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; simpl in *; find_inversion; subst; simpl in *; auto; congruence.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; simpl in *; auto.
unfold assoc_default. break_match;
rewrite get_set_same in *; try congruence; find_inversion; auto.
Qed.
Proof using.
unfold handleClientRequest.
intros.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros. find_inversion. simpl; intros; auto; congruence.
Qed.
Proof using.
unfold handleRequestVote, advanceCurrentTerm.
intros.
repeat (break_match; try discriminate; repeat (find_inversion; simpl in *)); auto.
Qed.
Proof using.
intros. unfold doGenericServer in *.
repeat break_match; find_inversion;
use_applyEntries_spec; simpl in *;
subst; auto.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros.
repeat break_match; simpl; auto using advanceCurrentTerm_matchIndex_preserved;
simpl in *; try congruence.
unfold assoc_default. simpl.
repeat break_match; simpl in *; try congruence; find_inversion; auto.
Qed.
Proof using.
intros. unfold doLeader in *. simpl; intros.
repeat break_match; find_inversion; auto; congruence.
Qed.
Proof using.
intros. unfold doLeader in *.
repeat break_match; find_inversion; auto.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat break_match; find_inversion; subst; auto;
intuition; break_exists; congruence.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
repeat break_match; find_inversion; subst; auto;
intuition; break_exists; congruence.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
repeat break_match; find_inversion; subst; auto;
intuition; break_exists;
do_in_map; subst; simpl in *; congruence.
Qed.

Lemma doLeader_messages :
forall st h os st' ms m t n pli plt es ci,
doLeader st h = (os, st', ms) ->
In m ms ->
snd m = AppendEntries t n pli plt es ci ->
t = currentTerm st /\
log st' = log st /\
type st = Leader /\
((plt = 0) \/
((exists e, findAtIndex (log st) pli = Some e /\
eTerm e = plt))).