Section OneLeaderLogPerTerm.
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 {llvwli : leaderLogs_votesWithLog_interface}.
Context {vci : votes_correct_interface}.
Context {cci : cronies_correct_interface}.
Context {vvci : votes_votesWithLog_correspond_interface}.
Context {lltsi : leaderLogs_term_sanity_interface}.
Ltac start :=
red; unfold one_leaderLog_per_term; simpl; intros.
Ltac start_update :=
start;
repeat find_higher_order_rewrite;
repeat (update_destruct; subst; rewrite_update);
[| | |eauto].
Proof using.
start. contradiction.
Qed.
Proof using.
unfold one_leaderLog_per_term. intros.
repeat find_higher_order_rewrite;
repeat (update_destruct; subst; rewrite_update);
simpl in *; repeat find_rewrite; eauto.
Qed.
Ltac start_unchanged :=
red; intros; eapply one_leaderLog_per_term_unchanged; eauto; subst.
(* solve invariant by lemma which shows leader logs do not change *)
Ltac unchanged lem :=
start_unchanged; apply lem.
Proof using.
unchanged update_elections_data_client_request_leaderLogs.
Qed.
Proof using.
unchanged update_elections_data_timeout_leaderLogs.
Qed.
Proof using.
unchanged update_elections_data_appendEntries_leaderLogs.
Qed.
Proof using.
start_unchanged. auto.
Qed.
Proof using.
unchanged leaderLogs_update_elections_data_requestVote.
Qed.
Proof using.
unfold update_elections_data_requestVoteReply.
intros.
repeat break_match; auto.
simpl in *. intuition.
find_inversion. right.
unfold handleRequestVoteReply in *.
repeat break_match; simpl in *; intuition; try congruence;
break_if; try congruence; do_bool; eauto using le_antisym.
Qed.
Proof using.
unfold wonElection. intros. find_apply_lem_hyp leb_true_le. lia.
Qed.
Proof using one_node_params.
intros. eapply pigeon with (l := nodes).
- apply name_eq_dec.
- intros. apply (@all_names_nodes _ multi_params).
- intros. apply (@all_names_nodes _ multi_params).
- apply (@no_dup_nodes _ multi_params).
- assumption.
- assumption.
- apply div2_correct; assumption.
Qed.

(* two different hosts cannot both have the same term in their leader logs *)
Lemma contradiction_case :
forall (net : network ) t ll ll' (h h' : name) (p : packet (params := refined_multi_params (multi_params := multi_params))) t0 v xs ys,
refined_raft_intermediate_reachable net ->
pBody p = RequestVoteReply (raft_params := raft_params) t0 v ->
nwPackets net = xs ++ p :: ys ->
In (t, ll) (leaderLogs (fst (nwState net h))) ->
In (t, ll') (leaderLogs (update_elections_data_requestVoteReply (pDst p) (pSrc p) t0 v (nwState net (pDst p)))) ->
pDst p = h' ->
pDst p <> h ->
False.