Section PrevLogLeaderSublogProof.
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 {vci : votes_correct_interface}.
Context {cci : cronies_correct_interface}.
Context {lsi : leader_sublog_interface}.
Context {plceti : prevLog_candidateEntriesTerm_interface}.
Proof using.
unfold raft_net_invariant_init, prevLog_leader_sublog.
simpl. intuition.
Qed.
Proof using.
intros.
find_apply_lem_hyp handleClientRequest_log.
intuition.
- find_rewrite. auto.
- break_exists. intuition. find_rewrite. intuition.
Qed.
Proof using.
unfold raft_net_invariant_client_request, prevLog_leader_sublog.
intros. simpl in *.
repeat find_higher_order_rewrite.
find_apply_hyp_hyp. break_or_hyp.
- break_if; eauto.
find_copy_apply_lem_hyp handleClientRequest_type. break_and.
repeat find_rewrite.
eapply_prop_hyp In In; eauto.
break_exists_exists. intuition.
eauto using handleClientRequest_log_In.
- find_apply_lem_hyp in_map_iff. break_exists. break_and.
exfalso. eapply handleClientRequest_no_append_entries; eauto.
subst. simpl in *. find_rewrite. eauto 10.
Qed.
Proof using.
unfold raft_net_invariant_timeout, prevLog_leader_sublog.
intros. simpl in *.
repeat find_higher_order_rewrite.
find_apply_hyp_hyp. break_or_hyp.
- break_if; eauto.
find_copy_apply_lem_hyp handleTimeout_type.
intuition; repeat find_rewrite; try discriminate.
eapply_prop_hyp In In; eauto.
break_exists_exists. intuition.
erewrite handleTimeout_log_same by eauto.
eauto.
- find_apply_lem_hyp in_map_iff. break_exists. break_and.
exfalso. subst. simpl in *.
eapply handleTimeout_packets; eauto.
find_rewrite. eauto 10.
Qed.
Proof using.
unfold handleAppendEntries.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto.
Qed.
Proof using.
unfold raft_net_invariant_append_entries, prevLog_leader_sublog.
intros. simpl in *.
repeat find_higher_order_rewrite.
find_apply_hyp_hyp. break_or_hyp.
- find_eapply_lem_hyp app_cons_in_rest; [|solve[eauto]].
find_rewrite. break_if; eauto.
find_copy_apply_lem_hyp handleAppendEntries_type_log.
intuition.
+ congruence.
+ repeat find_rewrite.
eapply_prop_hyp In In; eauto.
- find_apply_lem_hyp handleAppendEntries_not_append_entries.
simpl in *. subst. exfalso. eauto 10.
Qed.
Proof using.
unfold raft_net_invariant_append_entries_reply, prevLog_leader_sublog.
intros. simpl in *.
repeat find_higher_order_rewrite.
find_apply_hyp_hyp. break_or_hyp.
- find_eapply_lem_hyp app_cons_in_rest; [|solve[eauto]].
find_rewrite. break_if; eauto.
find_copy_apply_lem_hyp handleAppendEntriesReply_log.
find_rewrite.
find_copy_apply_lem_hyp handleAppendEntriesReply_type.
intuition; repeat find_rewrite; try discriminate.
eauto.
- find_apply_lem_hyp in_map_iff. break_exists. break_and.
exfalso. subst. simpl in *.
find_apply_lem_hyp handleAppendEntriesReply_packets.
subst. simpl in *. intuition.
Qed.
Proof using.
unfold raft_net_invariant_request_vote, prevLog_leader_sublog.
intros. simpl in *.
repeat find_higher_order_rewrite.
find_apply_hyp_hyp. break_or_hyp.
- find_eapply_lem_hyp app_cons_in_rest; [|solve[eauto]].
find_rewrite. break_if; eauto.
find_copy_apply_lem_hyp handleRequestVote_log.
find_copy_apply_lem_hyp handleRequestVote_type.
intuition; repeat find_rewrite; try discriminate.
eauto.
- find_apply_lem_hyp handleRequestVote_no_append_entries.
simpl in *. subst. exfalso. eauto 10.
Qed.
Definition candidateEntriesTerm_lowered (net : network) t p : Prop :=
In p (nwPackets net) ->
pBody p = RequestVoteReply t true ->
currentTerm (nwState net (pDst p)) = t ->
wonElection (dedup name_eq_dec (pSrc p :: votesReceived (nwState net (pDst p)))) = true ->
type (nwState net (pDst p)) <> Candidate.
Proof using.
unfold deghost.
simpl.
intros.
do_in_map.
eauto.
Qed.
Proof using rri.
unfold candidateEntriesTerm_lowered,
cronies_correct, votes_correct.
intros. break_and.
rewrite deghost_spec.
find_apply_lem_hyp deghost_packet_exists.
find_apply_lem_hyp deghost_packet_exists.
break_exists.  break_and. subst.
eapply_prop_hyp votes_nw pBody. concludes.
eapply_prop_hyp prevLog_candidateEntriesTerm pBody; auto.
concludes.
unfold candidateEntriesTerm in *. break_exists. break_and.
match goal with
| H : wonElection _ = _ |- _ =>
eapply wonElection_one_in_common in H; [|clear H; eauto]
end.
break_exists. break_and.
repeat match goal with
| [ H : _ |- _ ] => rewrite deghost_spec in H
end.
simpl in *.
break_or_hyp.
- apply_prop_hyp cronies_votes In.
assert (pDst x = x1) by (eapply_prop one_vote_per_term; eauto).
subst. auto.
- intro.
assert (pDst x = x1).
{ eapply_prop one_vote_per_term;
eapply_prop cronies_votes.
- eapply_prop votes_received_cronies; eauto.
- repeat find_reverse_rewrite. auto.
}
subst.
concludes. contradiction.
Qed.

Lemma prevLog_candidateEntriesTerm_lowered :
forall net,
raft_intermediate_reachable net ->
forall p p' t leaderId prevLogIndex prevLogTerm entries leaderCommit,
In p (nwPackets net) ->
pBody p = AppendEntries t leaderId prevLogIndex prevLogTerm
entries leaderCommit ->
0 < prevLogTerm ->
candidateEntriesTerm_lowered net prevLogTerm p'.