Section LeaderLogsTermSanity.
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 {ctgli : candidate_term_gt_log_interface}.
Proof using ctgli rri.
intros. eapply lift_prop; eauto using candidate_term_gt_log_invariant.
Qed.
Ltac term_sanity_start :=
red; unfold leaderLogs_term_sanity; simpl; intros.
Ltac term_sanity_start_update :=
term_sanity_start;
repeat find_higher_order_rewrite; update_destruct; rewrite_update; [|eauto].
Proof using.
unfold leaderLogs_term_sanity. intros. find_higher_order_rewrite.
update_destruct; subst; rewrite_update; simpl in *; [find_rewrite|]; eauto.
Qed.
Ltac term_sanity_unchanged' lem :=
red; intros; eapply leaderLogs_term_sanity_unchanged; subst;
pose lem; eauto.
Tactic Notation "term_sanity_unchanged" constr(lem) := term_sanity_unchanged' lem.
Tactic Notation "term_sanity_unchanged" := term_sanity_unchanged' eq_refl (* bogus *).
Proof using ctgli rri.
term_sanity_start_update.
find_copy_apply_lem_hyp handleRequestVoteReply_type.
find_copy_apply_lem_hyp handleRequestVoteReply_log.
find_eapply_lem_hyp leaderLogs_update_elections_data_RVR; auto.
unfold ghost_data, raft_data in *. simpl in *. subst.
intuition; eauto; repeat find_rewrite; try discriminate.
find_apply_lem_hyp candidate_term_gt_log_lifted.
unfold candidate_term_gt_log in *. simpl in *; repeat break_match; simpl in *.
unfold gt in *. subst. repeat find_rewrite. eapply H3; auto.
Qed.
Ltac term_sanity_break_update :=
term_sanity_start_update;
match goal with
h : _ |- _ => eapply h
end; [| eauto];
match goal with
h : _ |- _ => solve [rewrite h; auto]
end.
Proof using ctgli rri.
intros.
apply refined_raft_net_invariant; auto.
- term_sanity_start; contradiction.
- term_sanity_unchanged update_elections_data_client_request_leaderLogs.
- term_sanity_unchanged update_elections_data_timeout_leaderLogs.
- term_sanity_unchanged update_elections_data_appendEntries_leaderLogs.
- term_sanity_unchanged.
- term_sanity_unchanged leaderLogs_update_elections_data_requestVote.
- apply leaderLogs_term_sanity_request_vote_reply.
- term_sanity_break_update.
- term_sanity_break_update.
- term_sanity_start; find_reverse_higher_order_rewrite; eauto.
- term_sanity_break_update.
Qed.
Ltac currentTerm_sanity_start :=
red; unfold leaderLogs_currentTerm_sanity; simpl; intros.
Ltac currentTerm_sanity_start_update :=
currentTerm_sanity_start;
repeat find_higher_order_rewrite; update_destruct; rewrite_update; [|eauto].

Lemma leaderLogs_currentTerm_sanity_unchanged :
forall net st' h gd d ps',
leaderLogs_currentTerm_sanity net ->
(forall h' : Net.name, st' h' = update name_eq_dec (nwState net) h (gd, d) h') ->
leaderLogs gd = leaderLogs (fst (nwState net h)) ->
currentTerm d >= currentTerm (snd (nwState net h)) ->
leaderLogs_currentTerm_sanity {| nwPackets := ps'; nwState := st' |}.