Section LogsLeaderLogs.
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 {llsi : leaderLogs_sorted_interface}.
Context {rlmli : refined_log_matching_lemmas_interface}.
Context {llci : leaderLogs_contiguous_interface}.
Context {lllmi : leaderLogs_entries_match_interface}.
Context {lhllsi : leaders_have_leaderLogs_strong_interface}.
Context {nisi : nextIndex_safety_interface}.
Context {si : sorted_interface}.
Context {lpholli : log_properties_hold_on_leader_logs_interface}.
Definition weak_sanity pli ll ll' :=
pli = 0 ->
(exists e, eIndex e = 0 /\ In e ll) \/
ll = ll'.
Definition logs_leaderLogs_nw_weak net :=
forall p t n pli plt es ci e,
In p (nwPackets net) ->
pBody p = AppendEntries t n pli plt es ci ->
In e es ->
exists leader ll es' ll',
In (eTerm e, ll) (leaderLogs (fst (nwState net leader))) /\
Prefix ll' ll /\
removeAfterIndex es (eIndex e) = es' ++ ll' /\
(forall e', In e' es' -> eTerm e' = eTerm e) /\
weak_sanity pli ll ll'.
Proof using.
intros. unfold logs_leaderLogs_nw, logs_leaderLogs_nw_weak, weak_sanity in *.
intros.
eapply_prop_hyp In In; eauto.
break_exists_exists; intuition; subst; try lia.
break_exists; intuition; eauto.
Qed.
Definition logs_leaderLogs_inductive net :=
logs_leaderLogs net /\
logs_leaderLogs_nw net.
Proof using si rri.
intros.
eapply lift_prop; eauto using logs_sorted_invariant.
Qed.
Proof using si rri.
intros.
find_apply_lem_hyp lift_sorted.
unfold logs_sorted, logs_sorted_host in *.
intuition.
unfold deghost in *. simpl in *. break_match; eauto.
Qed.
Proof using nisi rri.
intros.
eapply lift_prop; eauto using nextIndex_safety_invariant.
Qed.

Lemma nextIndex_sanity :
forall net h h',
refined_raft_intermediate_reachable net ->
type (snd (nwState net h)) = Leader ->
pred (getNextIndex (snd (nwState net h)) h') <> 0 ->
exists e,
findAtIndex (log (snd (nwState net h))) (pred (getNextIndex (snd (nwState net h)) h')) = Some e.