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.

Lemma invalid_index :
forall net h e,
refined_raft_intermediate_reachable net ->
In e (log (snd (nwState net h))) ->
eIndex e = S (maxIndex (log (snd (nwState net h)))) ->
False.