Section GhostLogLogMatching.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Context {rmri : raft_msg_refinement_interface}.
Context {rlmli : refined_log_matching_lemmas_interface}.
Context {glci : ghost_log_correct_interface}.
Context {lphogli : log_properties_hold_on_ghost_logs_interface}.
Context {tsi : term_sanity_interface}.
Context {aelsi : allEntries_leader_sublog_interface}.
Context {glaei : ghost_log_allEntries_interface}.
Definition ghost_log_entries_match_nw (net : network) : Prop :=
forall p p',
In p (nwPackets net) ->
In p' (nwPackets net) ->
entries_match (fst (pBody p)) (fst (pBody p')).
Definition ghost_log_entries_match (net : network) : Prop :=
ghost_log_entries_match_host net /\
ghost_log_entries_match_nw net.
Definition lifted_entries_contiguous net :=
forall h,
contiguous_range_exact_lo (log (snd (nwState net h))) 0.
Definition lifted_entries_sorted net :=
forall h,
sorted (log (snd (nwState net h))).
Proof using rlmli rmri.
intros.
enough (entries_contiguous (mgv_deghost net)) by
(unfold entries_contiguous, lifted_entries_contiguous, mgv_deghost in *;
simpl in *;
repeat break_match; simpl in *; auto).
apply msg_lift_prop; eauto using entries_contiguous_invariant.
Qed.
Proof using rlmli rmri.
intros.
enough (entries_sorted (mgv_deghost net)) by
(unfold entries_sorted, lifted_entries_sorted, mgv_deghost in *;
simpl in *;
repeat break_match; simpl in *; auto).
apply msg_lift_prop; eauto using entries_sorted_invariant.
Qed.
Definition lifted_entries_contiguous_nw net :=
forall p t n pli plt es ci,
In p (nwPackets net) ->
snd (pBody p) = AppendEntries t n pli plt es ci ->
contiguous_range_exact_lo es pli.
Proof using rlmli rmri.
intros.
assert (entries_contiguous_nw (mgv_deghost net)) by
(apply msg_lift_prop; eauto using entries_contiguous_nw_invariant).
unfold entries_contiguous_nw, lifted_entries_contiguous_nw, mgv_deghost in *;
intros.
simpl in *;
repeat break_match; simpl in *; auto.
match goal with
| H : context [contiguous_range_exact_lo] |- _ =>
specialize (H (@mgv_deghost_packet _ _ ghost_log_params p));
eapply H; simpl in *; eauto
end.
apply in_map_iff. eexists; eauto.
Qed.
Definition lifted_entries_match net :=
forall h h',
entries_match (log (snd (nwState net h))) (log (snd (nwState net h'))).
Proof using rlmli rmri.
intros.
unfold lifted_entries_match; intros.
find_eapply_lem_hyp msg_lift_prop;
[|intros; eapply (entries_match_invariant ltac:(eauto) h h'); eauto].
simpl in *.
repeat break_match; simpl in *; auto.
Qed.
Definition lifted_no_entries_past_current_term_host net :=
forall (h : name) e,
In e (log (snd (nwState net h))) ->
eTerm e <= currentTerm (snd (nwState net h)).
Proof using tsi rmri.
intros.
enough (no_entries_past_current_term_host (deghost (mgv_deghost net))) by
(unfold no_entries_past_current_term_host, lifted_no_entries_past_current_term_host, deghost, mgv_deghost in *;
simpl in *;
repeat break_match; simpl in *; auto).
apply msg_lift_prop_all_the_way; eauto.
intros.
eapply no_entries_past_current_term_invariant; eauto.
Qed.
Proof using lphogli rlmli rmri.
assert (msg_log_property sorted) by
(unfold msg_log_property; intros; eapply lifted_entries_sorted_invariant; eauto).
intros.
find_eapply_lem_hyp log_properties_hold_on_ghost_logs_invariant; eauto.
Qed.
Proof using lphogli rlmli rmri.
assert (msg_log_property (fun x => contiguous_range_exact_lo x 0)) by
(unfold msg_log_property; intros; eapply lifted_entries_contiguous_invariant; eauto).
intros.
find_eapply_lem_hyp log_properties_hold_on_ghost_logs_invariant;
eauto; simpl in *; auto.
Qed.
Definition lifted_allEntries_leader_sublog (net : network) :=
forall leader e h,
type (snd (nwState net leader)) = Leader ->
In e (map snd (allEntries (fst (nwState net h)))) ->
eTerm e = currentTerm (snd (nwState net leader)) ->
In e (log (snd (nwState net leader))).
Proof using aelsi rmri.
intros.
unfold lifted_allEntries_leader_sublog; intros.
find_eapply_lem_hyp msg_lift_prop;
[|intros; eapply allEntries_leader_sublog_invariant; eauto].
simpl in *.
unfold allEntries_leader_sublog in *.
unfold mgv_deghost in *.
simpl in *.
repeat break_match; simpl in *; eauto.
Qed.
Proof using.
red. split;
red; intros; simpl in *; intuition.
Qed.

Lemma handleAppendEntries_ghost_log:
forall (p : packet) (net : network) (d : raft_data)
(m : msg) (t : term) (n : name) (pli : logIndex)
(plt : term) (es : list entry) (ci : logIndex)
(h : Net.name),
msg_refined_raft_intermediate_reachable net ->
entries_match (log (snd (nwState net h))) (fst (pBody p)) ->
handleAppendEntries h (snd (nwState net h)) t n pli plt es ci = (d, m) ->
snd (pBody p) = AppendEntries t n pli plt es ci ->
In p (nwPackets net) -> log d = log (snd (nwState net h)) \/ log d = fst (pBody p).