Section AppendEntriesLeader.
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 {aecfli : append_entries_came_from_leaders_interface}.
Context {ollpti : one_leaderLog_per_term_interface}.
Context {lltsi : leaderLogs_term_sanity_interface}.
Context {olpti : one_leader_per_term_interface}.
Proof using.
unfold refined_raft_net_invariant_init, appendEntries_leader.
simpl. intuition.
Qed.
Definition type_term_log_monotonic st st' :=
type st' = Leader ->
type st = Leader /\
currentTerm st' = currentTerm st /\
(forall e, In e (log st) -> In e (log st')).
Notation appendEntries_leader_predicate ps st :=
(forall p t lid pli plt es lci e,
In p ps ->
pBody p = AppendEntries t lid pli plt es lci ->
In e es ->
currentTerm st = t ->
type st = Leader ->
In e (log st)).
Proof using.
unfold type_term_log_monotonic.
intuition.
repeat find_rewrite.
eauto.
Qed.
Proof using.
unfold type_term_log_monotonic.
intros.
find_copy_apply_lem_hyp handleClientRequest_type.
find_copy_apply_lem_hyp handleClientRequest_log.
intuition; try congruence.
break_exists.
intuition. subst. repeat find_rewrite. auto with *.
Qed.
Proof using.
unfold refined_raft_net_invariant_client_request, appendEntries_leader.
simpl.
intros.
find_apply_hyp_hyp. intuition.
- repeat find_higher_order_rewrite.
update_destruct_simplify.
+ eapply appendEntries_leader_predicate_TTLM_preserved; eauto using handleClientRequest_TTLM.
eauto.
+ eauto.
- find_apply_lem_hyp handleClientRequest_packets.
subst. simpl in *. intuition.
Qed.
Proof using.
unfold type_term_log_monotonic.
intros.
find_copy_apply_lem_hyp handleTimeout_type.
find_apply_lem_hyp handleTimeout_log_same.
intuition; try congruence.
Qed.
Proof using.
unfold refined_raft_net_invariant_timeout, appendEntries_leader.
simpl.
intros.
find_apply_hyp_hyp.
intuition.
- repeat find_higher_order_rewrite.
update_destruct_simplify.
+ eapply appendEntries_leader_predicate_TTLM_preserved; eauto using handleTimeout_TTLM.
eauto.
+ eauto.
- do_in_map.
find_eapply_lem_hyp handleTimeout_packets; eauto.
subst. exfalso. eauto 10.
Qed.
Proof using.
unfold type_term_log_monotonic, handleAppendEntries.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold refined_raft_net_invariant_append_entries, appendEntries_leader.
simpl.
intros.
find_apply_hyp_hyp.
intuition.
- repeat find_higher_order_rewrite.
update_destruct_simplify.
+ eapply appendEntries_leader_predicate_TTLM_preserved.
2-9:eauto using handleAppendEntries_TTLM.
eauto.
+ eauto.
- find_apply_lem_hyp handleAppendEntries_not_append_entries.
subst. exfalso. eauto 10.
Qed.
Proof using.
unfold type_term_log_monotonic, handleAppendEntriesReply, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold refined_raft_net_invariant_append_entries_reply, appendEntries_leader.
simpl.
intros.
find_apply_hyp_hyp.
intuition.
- repeat find_higher_order_rewrite.
update_destruct_simplify.
+ eapply appendEntries_leader_predicate_TTLM_preserved.
2-9: eauto using handleAppendEntriesReply_TTLM.
eauto.
+ eauto.
- find_apply_lem_hyp handleAppendEntriesReply_packets.
subst. simpl in *. intuition.
Qed.
Proof using.
unfold type_term_log_monotonic, handleRequestVote, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold refined_raft_net_invariant_request_vote, appendEntries_leader.
simpl.
intros.
find_apply_hyp_hyp.
intuition.
- repeat find_higher_order_rewrite.
update_destruct_simplify.
+ eapply appendEntries_leader_predicate_TTLM_preserved.
2-9:eauto using handleRequestVote_TTLM.
eauto.
+ eauto.
- find_apply_lem_hyp handleRequestVote_no_append_entries.
subst. exfalso. eauto 10.
Qed.
(* p0 is AE. by AE_came_from_leaders, there is a ll in pre state at (pSrc p0) for p0's term. claim I am (pSrc p0). I am ascending, so I have ll in post state. pSrc p0 ll still is preserved into post state. finish with one ll per term. thus I had a leaderLog in the pre state. this contradicts leaderLogs_currentTerm_sanity_candidate. *)

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