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.

Theorem handleAppendEntries_log :
forall h st t n pli plt es ci st' ps,
handleAppendEntries h st t n pli plt es ci = (st', ps) ->
log st' = log st \/
(currentTerm st <= t /\ es <> [] /\ pli = 0 /\ log st' = es) \/
(currentTerm st <= t /\ es <> [] /\ pli <> 0 /\ exists e,
In e (log st) /\
eIndex e = pli /\
eTerm e = plt) /\
log st' = es ++ (removeAfterIndex (log st) pli).