Section CroniesCorrectProof.
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 {cvfsi : candidates_vote_for_selves_interface}.
Proof using cvfsi rri.
intros.
eapply lift_prop; [exact candidates_vote_for_selves_invariant|eauto].
Qed.
Proof using.
intros.
unfold handleClientRequest in *; repeat break_match;
repeat find_inversion; subst; simpl in *; find_apply_hyp_hyp; intuition.
Qed.
Proof using.
intros.
unfold handleTimeout, tryToBecomeLeader in *; repeat break_match;
repeat find_inversion; subst; simpl in *; find_apply_hyp_hyp;
in_crush; congruence.
Qed.
Proof using.
intros.
unfold handleAppendEntries, advanceCurrentTerm in *; repeat break_match;
repeat find_inversion; subst; simpl in *; find_apply_hyp_hyp;
in_crush; congruence.
Qed.
Proof using.
intros.
unfold handleAppendEntriesReply, advanceCurrentTerm in *; repeat break_match;
repeat find_inversion; subst; simpl in *; find_apply_hyp_hyp;
in_crush; congruence.
Qed.
Proof using.
unfold refined_raft_net_invariant_client_request. intros.
unfold cronies_correct in *; intuition.
- unfold votes_received_cronies, handleClientRequest in *. intros.
simpl in *. repeat find_higher_order_rewrite.
unfold update_elections_data_client_request in *.
repeat break_match; find_inversion; subst; simpl in *; eauto; try congruence.
- unfold cronies_votes in *. intros.
simpl in *. repeat find_higher_order_rewrite.
unfold update_elections_data_client_request in *.
repeat break_match; subst; simpl in *; eauto.
- unfold votes_nw in *. intros. simpl in *.
assert (In p (nwPackets net)) by eauto using handleClientRequest_rvr.
repeat find_apply_hyp_hyp.
repeat find_higher_order_rewrite.
unfold update_elections_data_client_request in *.
repeat break_match; subst; repeat find_rewrite; simpl in *; intuition.
- unfold votes_received_leaders in *. intros. simpl in *. repeat find_higher_order_rewrite.
unfold handleClientRequest in *.
unfold update_elections_data_client_request in *.
repeat break_match; find_inversion; subst; simpl in *; eauto.
Qed.
Proof using.
unfold refined_raft_net_invariant_timeout. intros.
unfold cronies_correct, update_elections_data_timeout in *; intuition.
- unfold votes_received_cronies, handleTimeout, tryToBecomeLeader in *.
intros. simpl in *.
repeat break_match; repeat find_inversion; subst; simpl in *; eauto;
repeat find_higher_order_rewrite; repeat (break_if; simpl in *); auto; congruence.
- unfold cronies_votes, handleTimeout, tryToBecomeLeader in *. intros.
simpl in *.
repeat break_match; repeat find_inversion; subst; simpl in *; eauto;
repeat find_higher_order_rewrite; repeat (break_if; simpl in *); auto;
repeat find_inversion; subst; intuition;
congruence.
- unfold votes_nw in *. intros. simpl in *.
assert (In p (nwPackets net)) by eauto using handleTimeout_rvr.
repeat find_apply_hyp_hyp.
repeat find_higher_order_rewrite.
repeat break_match; subst; repeat find_rewrite; simpl in *; intuition.
- unfold votes_received_leaders in *. intros. simpl in *. repeat find_higher_order_rewrite.
unfold handleTimeout, tryToBecomeLeader in *. simpl in *.
repeat (break_match; subst; simpl in *; repeat find_inversion; try discriminate);
intuition eauto.
Qed.
Proof using.
unfold refined_raft_net_invariant_append_entries. intros.
unfold cronies_correct in *; intuition.
- unfold votes_received_cronies, handleAppendEntries in *.
intros. simpl in *.
repeat find_higher_order_rewrite.
unfold update_elections_data_appendEntries.
repeat break_match; repeat find_inversion; subst; simpl in *; eauto;
repeat find_higher_order_rewrite; repeat (break_if; simpl in *); intuition; try congruence.
- unfold cronies_votes in *. intros.
simpl in *. repeat find_higher_order_rewrite.
unfold update_elections_data_appendEntries in *.
repeat break_match; repeat find_inversion; subst; simpl in *; eauto.
- unfold votes_nw in *. intros. simpl in *.
assert (In p0 (xs ++ ys)) by eauto using handleAppendEntries_rvr.
assert (In p0 (nwPackets net)) by (repeat find_rewrite; in_crush).
repeat find_apply_hyp_hyp.
repeat find_higher_order_rewrite.
unfold update_elections_data_appendEntries in *.
repeat break_match; subst; repeat find_rewrite; simpl in *; intuition.
- unfold votes_received_leaders in *. intros. simpl in *.
repeat find_higher_order_rewrite. repeat break_if; eauto.
subst. simpl in *. unfold handleAppendEntries in *.
repeat break_match; find_inversion; subst; simpl in *;
try discriminate; eauto.
Qed.
Proof using.
unfold refined_raft_net_invariant_append_entries_reply. intros.
unfold cronies_correct in *; intuition.
- unfold votes_received_cronies, handleAppendEntriesReply, advanceCurrentTerm in *.
intros. simpl in *.
repeat break_match; repeat find_inversion; subst; simpl in *; eauto;
repeat find_higher_order_rewrite; repeat (break_if; simpl in *); intuition; try congruence.
- unfold cronies_votes in *. intros.
simpl in *. repeat find_higher_order_rewrite.
repeat break_match; subst; simpl in *; eauto.
- unfold votes_nw in *. intros. simpl in *.
assert (In p0 (xs ++ ys)) by eauto using handleAppendEntriesReply_rvr.
assert (In p0 (nwPackets net)) by (repeat find_rewrite; in_crush).
repeat find_apply_hyp_hyp.
repeat find_higher_order_rewrite.
repeat break_match; subst; repeat find_rewrite; simpl in *; intuition.
- unfold votes_received_leaders in *. intros. simpl in *.
repeat find_higher_order_rewrite. repeat break_if; eauto.
subst. simpl in *. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; find_inversion; subst; simpl in *;
try discriminate; eauto.
Qed.
Proof using.
intros. unfold handleRequestVote, advanceCurrentTerm in *.
repeat (break_match; try find_inversion; try discriminate);
simpl in *; subst; intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_requestVote in *.
repeat break_match; simpl in *; auto.
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; find_inversion; auto.
Qed.
Proof using.
intros.
unfold update_elections_data_requestVote.
repeat break_match; simpl in *; intuition.
Qed.
Proof using.
intros.
unfold update_elections_data_requestVote in *.
repeat break_match; simpl in *;
repeat (repeat find_inversion; repeat find_rewrite);
repeat (do_bool; break_and); try congruence; intuition.
Qed.
Proof using vci.
unfold refined_raft_net_invariant_request_vote. intros.
unfold cronies_correct in *; intuition.
- unfold votes_received_cronies in *.
intros. simpl in *. subst.
repeat find_higher_order_rewrite. break_if; simpl in *; subst; eauto.
rewrite update_elections_data_requestVote_cronies.
find_copy_apply_lem_hyp handleRequestVote_votesReceived. repeat find_rewrite.
find_apply_lem_hyp handleRequestVote_currentTerm_same_or_follower.
match goal with
| H : currentTerm _ = currentTerm _ /\ _ \/ _ |- _ =>
destruct H
end; try break_and; repeat find_rewrite; eauto. intuition; congruence.
- unfold cronies_votes in *. intros.
simpl in *.
repeat find_higher_order_rewrite.
repeat break_if; subst; simpl in *; eauto;
try rewrite update_elections_data_requestVote_cronies in *;
eauto using update_elections_data_requestVote_votes_preserved.
- unfold votes_nw in *. intros.
simpl in *.
subst. repeat find_higher_order_rewrite.
find_apply_hyp_hyp. intuition.
+ assert (In p0 (nwPackets net)) by (repeat find_rewrite; in_crush).
simpl.
break_if; simpl in *; eauto.
eapply update_elections_data_requestVote_votes_preserved.
repeat find_reverse_rewrite. eauto.
+ unfold raft_data in *. repeat find_rewrite. repeat find_inversion.
repeat (subst; simpl in *).
find_copy_apply_lem_hyp handleRequestVote_true_votedFor. intuition.
repeat find_rewrite. simpl in *.
break_if; simpl in *; try congruence.
subst.
find_apply_lem_hyp update_elections_data_requestVote_votedFor; intuition.
repeat find_rewrite.
apply update_elections_data_requestVote_votes_preserved.
eapply votes_correct_invariant; eauto.
- unfold votes_received_leaders in *. intros. simpl in *.
repeat find_higher_order_rewrite. repeat break_if; eauto.
subst. simpl in *.
find_copy_apply_lem_hyp handleRequestVote_votesReceived.
find_apply_lem_hyp handleRequestVote_currentTerm_same_or_follower.
intuition; repeat find_rewrite; try congruence; eauto.
Qed.
Proof using.
intros.
unfold handleRequestVoteReply, advanceCurrentTerm in *.
repeat break_match; subst; simpl in *; auto; congruence.
Qed.
Proof using.
intros.
unfold handleRequestVoteReply, advanceCurrentTerm in *.
repeat break_match; simpl in *; subst; simpl in *; do_bool; intuition.
Qed.

Lemma handleRequestVoteReply_leader :
forall h st src t v st',
handleRequestVoteReply h st src t v = st' ->
type st' = Leader ->
(st' = st \/
(type st = Candidate /\
wonElection (dedup name_eq_dec (votesReceived st')) = true /\
currentTerm st' = currentTerm st)).