Section AllEntriesLogMatching.
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 {aelsi : allEntries_leader_sublog_interface}.
Context {lsi : leader_sublog_interface}.
Context {rlmli : refined_log_matching_lemmas_interface}.
Definition allEntries_log_matching_nw net :=
forall (e e' : entry) (h : name) (p : packet)
t leaderId prevLogIndex prevLogTerm entries leaderCommit,
In p (nwPackets net) ->
pBody p = AppendEntries t leaderId prevLogIndex prevLogTerm entries leaderCommit ->
In e entries ->
In e' (map snd (allEntries (fst (nwState net h)))) ->
eTerm e = eTerm e' -> eIndex e = eIndex e' -> e = e'.
Definition allEntries_log_matching_inductive net :=
allEntries_log_matching net /\ allEntries_log_matching_nw net.
Ltac start :=
red; unfold allEntries_log_matching_inductive; simpl; intros.
Ltac start_update :=
start; intuition; [unfold allEntries_log_matching in *|unfold allEntries_log_matching_nw in *];
intros; repeat find_higher_order_rewrite; repeat (update_destruct; rewrite_update); subst; simpl; eauto.
Proof using.
start. split.
- unfold allEntries_log_matching. intros. simpl in *. intuition.
- unfold allEntries_log_matching_nw. intros. simpl in *. intuition.
Qed.
Definition leader_sublog (net : network) :=
forall leader e h,
type (snd (nwState net leader)) = Leader ->
In e (log (snd (nwState net h))) ->
eTerm e = currentTerm (snd (nwState net leader)) ->
In e (log (snd (nwState net leader))).
Proof using lsi rri.
pose proof lift_prop _ leader_sublog_invariant_invariant.
unfold leader_sublog, leader_sublog_invariant, leader_sublog_host_invariant in *.
intuition.
unfold raft_refined_base_params in *.
repeat rewrite <- deghost_spec in *.
repeat match goal with
| [ H : _ |- _ ] => rewrite <- deghost_spec in H
end.
find_apply_hyp_hyp. intuition.
eauto.
Qed.
Proof using lsi rri.
intros.
pose proof (lift_prop _ leader_sublog_invariant_invariant _ ltac:(eauto)) as Hinv.
unfold leader_sublog_invariant, leader_sublog_nw_invariant in *.
destruct Hinv as [Hhost Hnw].
find_apply_lem_hyp ghost_packet.
eapply_prop_hyp In In; eauto; try find_rewrite_lem deghost_spec; try rewrite deghost_spec; eauto.
Qed.
Proof using rlmli.
intros.
intro_refined_invariant entries_sorted_invariant.
find_apply_lem_hyp maxIndex_is_max; auto.
find_rewrite.
lia.
Qed.
Ltac fix_data :=
do 2 (unfold raft_data in *; simpl in *).
Ltac contradict_maxIndex :=
exfalso; find_apply_lem_hyp maxIndex_is_max;
try (fix_data; lia);
eapply entries_sorted_invariant; eauto.
Proof using rlmli lsi aelsi rri.
start_update.
- subst.
find_copy_apply_lem_hyp update_elections_data_client_request_log_allEntries.
intuition; simpl in *; repeat find_rewrite; eauto.
break_exists; intuition; repeat find_rewrite; simpl in *; intuition; subst; simpl in *; eauto.
+ contradict_maxIndex.
+ enough (In e' (log (snd (nwState net h0)))) by
contradict_maxIndex.
eapply allEntries_leader_sublog_invariant; repeat find_rewrite; eauto.
- find_apply_lem_hyp update_elections_data_client_request_log_allEntries.
intuition; simpl in *; repeat find_rewrite; eauto.
break_exists. intuition; repeat find_rewrite; simpl in *; intuition; eauto.
subst.
enough (In e (log (snd (nwState net h')))) by
contradict_maxIndex.
eapply lifted_leader_sublog_invariant; repeat find_rewrite; eauto.
- find_apply_lem_hyp update_elections_data_client_request_log_allEntries.
intuition; simpl in *; repeat find_rewrite; eauto.
break_exists. intuition; repeat find_rewrite; simpl in *; intuition; eauto.
subst.
enough (In e' (log (snd (nwState net h0)))) by
contradict_maxIndex.
eapply allEntries_leader_sublog_invariant; repeat find_rewrite; eauto.
- find_apply_hyp_hyp.
intuition;
[|exfalso; do_in_map; subst; simpl in *;
find_eapply_lem_hyp handleClientRequest_no_append_entries; eauto;
intuition; repeat find_rewrite; find_false; repeat eexists; eauto].
find_apply_lem_hyp update_elections_data_client_request_log_allEntries.
intuition; simpl in *; repeat find_rewrite; eauto.
break_exists. intuition; repeat find_rewrite; simpl in *; intuition; eauto.
subst.
enough (In e (log (snd (nwState net h0)))) by
contradict_maxIndex.
eapply lifted_leader_sublog_nw_invariant; repeat find_rewrite; eauto.
- find_apply_hyp_hyp.
intuition;
[|exfalso; do_in_map; subst; simpl in *;
find_eapply_lem_hyp handleClientRequest_no_append_entries; eauto;
intuition; repeat find_rewrite; find_false; repeat eexists; eauto].
eauto.
Qed.

Lemma allEntries_log_matching_unchanged :
forall net st' h gd d ps',
allEntries_log_matching net ->
(forall h' : Net.name, st' h' = update name_eq_dec (nwState net) h (gd, d) h') ->
log d = log (snd (nwState net h)) ->
allEntries gd = allEntries (fst (nwState net h)) ->
allEntries_log_matching {| nwPackets := ps'; nwState := st' |}.