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.
Proof using.
unfold allEntries_log_matching. intros.
find_higher_order_rewrite.
do 2 (update_destruct; rewrite_update); simpl in *;
repeat find_rewrite; eauto.
Qed.
Ltac unchanged :=
red; intros; eapply allEntries_log_matching_unchanged; subst; eauto.
Proof using.
start_update; simpl in *; try find_erewrite_lem handleTimeout_log_same; eauto;
try find_erewrite_lem update_elections_data_timeout_allEntries; eauto;
find_apply_hyp_hyp; intuition; eauto;
exfalso; do_in_map; subst; simpl in *;
find_eapply_lem_hyp handleTimeout_not_is_append_entries; eauto;
intuition; repeat find_rewrite; find_false; repeat eexists; eauto.
Qed.
Proof using rlmli.
intros.
unfold haveNewEntries in *. do_bool. intuition;
[unfold not_empty in *; break_match; subst; simpl in *; intuition; congruence|].
break_match; try congruence.
do_bool. find_apply_lem_hyp findAtIndex_elim. intuition.
assert (es <> nil) by (destruct es; subst; simpl in *; intuition; congruence).
find_eapply_lem_hyp maxIndex_non_empty.
break_exists. intuition.
find_copy_eapply_lem_hyp entries_sorted_nw_invariant; eauto.
match goal with
| H : In e es |- _ => copy_eapply maxIndex_is_max H; eauto
end.
repeat find_rewrite.
find_eapply_lem_hyp entries_match_nw_host_invariant; eauto.
Qed.

Lemma packets_entries_eq:
forall net p p0 entries es e e'
(d : raft_data) (n : name) (pli : logIndex) (plt : term)
(ci : logIndex)
(t0 : term) (leaderId : name) (prevLogIndex : logIndex)
(prevLogTerm : term) (leaderCommit : logIndex),
refined_raft_intermediate_reachable net ->
In p (nwPackets net) ->
In p0 (nwPackets net) ->
pBody p0 =
AppendEntries t0 leaderId prevLogIndex prevLogTerm entries leaderCommit ->
pBody p = AppendEntries (currentTerm d) n pli plt es ci ->
eTerm e = eTerm e' ->
eIndex e = eIndex e' -> In e entries -> In e' es -> e = e'.