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.
Proof using lphogli glci rlmli rmri.
intros.
find_copy_eapply_lem_hyp ghost_log_correct_invariant; eauto;
repeat conclude_using eauto.
find_apply_lem_hyp handleAppendEntries_log.
intuition; subst; auto.
- repeat find_rewrite.
rewrite sorted_findGtIndex_0; auto;
[|eapply ghost_log_sorted; eauto].
intros.
eapply ghost_log_contiguous; eauto.
- right.
repeat find_rewrite.
break_exists. intuition.
subst.
replace (eIndex x0) with (eIndex x).
eapply thing; eauto; repeat find_rewrite; auto using findGtIndex_Prefix;
try solve [eapply ghost_log_sorted; eauto];
try solve [eapply ghost_log_contiguous; eauto];
try solve [eapply lifted_entries_sorted_invariant; eauto];
try solve [eapply lifted_entries_contiguous_nw_invariant; eauto].
Qed.
Hint Resolve entries_match_refl : core.
Hint Resolve entries_match_sym : core.
Proof using lphogli glci rlmli rmri.
red.
split; red; intros; simpl in *; intuition;
unfold ghost_log_entries_match in *; break_and.
- repeat find_higher_order_rewrite; destruct_update; simpl in *; eauto.
+ match goal with
| [ H : msg_refined_raft_intermediate_reachable (mkNetwork _ _) |- _ ] => clear H
end.
find_apply_hyp_hyp. intuition.
* find_eapply_lem_hyp handleAppendEntries_ghost_log; eauto.
intuition; repeat find_rewrite; eauto.
* subst. simpl in *.
find_eapply_lem_hyp handleAppendEntries_ghost_log; eauto.
+ find_apply_hyp_hyp. intuition; eauto.
subst. simpl in *. unfold write_ghost_log.
simpl.
replace d with (snd (nwState {| nwPackets := ps'; nwState := st' |} (pDst p))) by
(simpl; find_higher_order_rewrite; rewrite_update; reflexivity).
replace (nwState net h) with (nwState {| nwPackets := ps'; nwState := st' |} h)by
(simpl; find_higher_order_rewrite; rewrite_update; reflexivity).
apply lifted_entries_match_invariant; auto.
- find_apply_hyp_hyp.
find_apply_hyp_hyp.
intuition.
+ eauto.
+ subst. simpl in *.
unfold write_ghost_log.
simpl.
match goal with
| [ H : context [handleAppendEntries] |- _ ] =>
eapply handleAppendEntries_ghost_log in H; eauto
end.
intuition.
* find_rewrite. eauto.
* find_rewrite. eauto.
+ subst. simpl in *.
unfold write_ghost_log.
simpl.
match goal with
| [ H : context [handleAppendEntries] |- _ ] =>
eapply handleAppendEntries_ghost_log in H; eauto
end.
intuition.
* find_rewrite. eauto.
* find_rewrite. eauto.
+ subst. simpl in *. apply entries_match_refl.
Qed.
Ltac packet_simpl :=
first [do_in_map; subst; simpl in *;
unfold add_ghost_msg in *;
do_in_map; subst; simpl in *|subst; simpl in *].
Arguments write_ghost_log / _ _ _ _ _.
Proof using rlmli rmri.
red.
split; red; intros; simpl in *; intuition;
unfold ghost_log_entries_match in *; break_and.
- repeat find_higher_order_rewrite; destruct_update; simpl in *; eauto.
+ find_apply_hyp_hyp. intuition.
* erewrite handleAppendEntriesReply_log; eauto.
* packet_simpl. apply entries_match_refl.
+ find_apply_hyp_hyp. intuition.
* eauto.
* packet_simpl.
erewrite @handleAppendEntriesReply_log with (st' := d) by eauto.
eapply lifted_entries_match_invariant; eauto.
- find_apply_hyp_hyp.
find_apply_hyp_hyp.
intuition.
+ eauto.
+ subst. simpl in *.
do_in_map. subst. simpl in *.
unfold add_ghost_msg in *. do_in_map. subst. simpl in *.
erewrite handleAppendEntriesReply_same_log by eauto.
eauto.
+ subst. simpl in *.
do_in_map. subst. simpl in *.
unfold add_ghost_msg in *. do_in_map. subst. simpl in *.
erewrite handleAppendEntriesReply_same_log by eauto.
eauto.
+ subst. simpl in *.
repeat do_in_map. subst. simpl in *.
unfold add_ghost_msg in *. repeat do_in_map. subst. simpl in *.
auto.
Qed.
Proof using rlmli rmri.
red.
split; red; intros; simpl in *; intuition;
unfold ghost_log_entries_match in *; break_and.
- repeat find_higher_order_rewrite; destruct_update; simpl in *; eauto.
+ find_apply_hyp_hyp. intuition.
* erewrite handleRequestVote_log; eauto.
* packet_simpl. auto.
+ find_apply_hyp_hyp. intuition.
* eauto.
* packet_simpl.
erewrite @handleRequestVote_log with (st' := d) by eauto.
eapply lifted_entries_match_invariant; eauto.
- find_apply_hyp_hyp.
find_apply_hyp_hyp.
intuition.
+ eauto.
+ subst. simpl in *. erewrite handleRequestVote_log; eauto.
+ subst. simpl in *. erewrite handleRequestVote_log; eauto.
+ subst. simpl in *. erewrite handleRequestVote_log; eauto.
Qed.
Proof using.
red.
split; red; intros; simpl in *; intuition;
unfold ghost_log_entries_match in *; break_and.
- repeat find_higher_order_rewrite; destruct_update; simpl in *; eauto.
erewrite handleRequestVoteReply_log; eauto.
- repeat find_apply_hyp_hyp; intuition; eauto;
repeat packet_simpl; eauto.
Qed.

Lemma sorted_entries_match_cons :
forall l l' e,
sorted (e :: l) ->
entries_match l l' ->
(~ exists e', eIndex e' = eIndex e /\ eTerm e' = eTerm e /\ In e'  l') ->
entries_match (e :: l) l'.