Section LeaderLogsLogMatching.
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 {lmi : log_matching_interface}.
Context {lltsi : leaderLogs_term_sanity_interface}.
Context {llsi : leaderLogs_sorted_interface}.
Context {si : sorted_interface}.
Context {llsli : leaderLogs_sublog_interface}.
Context {llci : leaderLogs_contiguous_interface}.
Context {taifoi : terms_and_indices_from_one_interface}.
Definition leaderLogs_entries_match_nw (net : network) : Prop :=
forall h llt ll p t src pli plt es ci,
In (llt, ll) (leaderLogs (fst (nwState net h))) ->
In p (nwPackets net) ->
pBody p = AppendEntries t src pli plt es ci ->
(forall e1 e2,
eIndex e1 = eIndex e2 ->
eTerm e1 = eTerm e2 ->
In e1 es ->
In e2 ll ->
(forall e3,
eIndex e3 <= eIndex e1 ->
In e3 es ->
In e3 ll) /\
(pli <> 0 ->
exists e4,
eIndex e4 = pli /\
eTerm e4 = plt /\
In e4 ll)).
Definition leaderLogs_entries_match (net : network) : Prop :=
leaderLogs_entries_match_host net /\
leaderLogs_entries_match_nw net.
Proof using.
unfold refined_raft_net_invariant_init, leaderLogs_entries_match,
leaderLogs_entries_match_host, leaderLogs_entries_match_nw.
simpl.
intuition.
Qed.
Proof using.
unfold entries_match.
intuition; simpl in *; intuition; subst; subst;
try match goal with
| [ H : In _ _ |- _ ] => apply maxTerm_is_max in H; [| solve[auto]]; lia
| [ H : In _ _ |- _ ] => apply maxIndex_is_max in H; [| solve[auto]]; lia
end.
- match goal with
| [ H : _ |- _ ] => solve [eapply H; eauto]
end.
- right. match goal with
| [ H : _ |- _ ] => solve [eapply H; eauto]
end.
Qed.
Proof using.
unfold entries_match.
intuition; simpl in *; intuition; subst; subst;
try solve [
exfalso; try find_apply_hyp_hyp;
match goal with
| [ H : In _ _ |- _ ] => apply maxIndex_is_max in H; [| solve[auto]]; lia
end].
- match goal with
| [ H : _ |- _ ] => solve [eapply H; eauto]
end.
- right. match goal with
| [ H : _ |- _ ] => solve [eapply H; eauto]
end.
Qed.
Proof using.
red.
simpl.
intuition.
Qed.
Proof using si rri.
intros.
pose proof (lift_prop _ logs_sorted_invariant).
find_insterU. conclude_using eauto.
unfold logs_sorted in *. break_and.
unfold logs_sorted_nw in *.
eapply H3.
- unfold deghost. simpl.
apply in_map_iff. eauto.
- simpl. eauto.
Qed.
Proof using si rri.
intros.
pose proof (lift_prop _ logs_sorted_invariant).
find_insterU. conclude_using eauto.
unfold logs_sorted in *. break_and.
unfold logs_sorted_host in *.
find_insterU.
find_rewrite_lem deghost_spec.
eauto.
Qed.
Proof using.
unfold leaderLogs_entries_match_nw.
intros.
eapply_prop_hyp In nwPackets; [|eauto 10].
match goal with
| [ H : _ |- _ ] =>
solve [eapply H; eauto;
repeat find_higher_order_rewrite;
eauto]
end.
Qed.
Proof using.
unfold leaderLogs_entries_match_host.
intuition.
repeat find_higher_order_rewrite. eauto.
Qed.
Proof using.
unfold handleClientRequest.
intros.
repeat break_match; repeat find_inversion; auto.
Qed.
Proof using llsli si llsi lltsi rri.
unfold refined_raft_net_invariant_client_request, leaderLogs_entries_match.
intros.
split.
- { unfold leaderLogs_entries_match_host.
simpl. intuition. subst. repeat find_higher_order_rewrite.
repeat update_destruct_simplify.
- find_rewrite_lem update_elections_data_client_request_leaderLogs.
destruct (log d) using (handleClientRequest_log_ind ltac:(eauto)).
+ eauto.
+ destruct ll.
* apply entries_match_nil.
* { apply entries_match_cons_gt_maxTerm; eauto.
- eauto using lifted_logs_sorted_host.
- eapply leaderLogs_sorted_invariant; eauto.
- lia.
- find_copy_apply_lem_hyp leaderLogs_currentTerm_invariant; auto.
find_copy_apply_lem_hyp leaderLogs_term_sanity_invariant.
unfold leaderLogs_term_sanity in *.
eapply_prop_hyp In In; simpl; eauto. repeat find_rewrite.
simpl in *. lia.
}
- destruct (log d) using (handleClientRequest_log_ind ltac:(eauto)).
+ eauto.
+ apply entries_match_cons_sublog; eauto.
* eauto using lifted_logs_sorted_host.
* eapply leaderLogs_sorted_invariant; eauto.
* lia.
* intros.
eapply leaderLogs_sublog_invariant; eauto.
simpl in *. congruence.
- find_rewrite_lem update_elections_data_client_request_leaderLogs.
eauto.
- eauto.
}
- eapply leaderLogs_entries_match_nw_packet_set with (net:=net); intuition.
+ find_apply_hyp_hyp. intuition eauto.
erewrite handleClientRequest_no_send with (ms := l) in * by eauto.
simpl in *. intuition.
+ simpl. subst. find_higher_order_rewrite.
rewrite update_fun_comm. simpl.
rewrite update_fun_comm. simpl.
rewrite update_elections_data_client_request_leaderLogs.
now rewrite update_nop_ext' by auto.
Qed.
Proof using.
unfold refined_raft_net_invariant_timeout, leaderLogs_entries_match.
intuition.
- eapply leaderLogs_entries_match_host_state_same; eauto;
simpl; intros; subst; find_higher_order_rewrite;
repeat update_destruct_simplify; rewrite_update; auto;
try rewrite update_elections_data_timeout_leaderLogs;
try erewrite handleTimeout_log_same by eauto; eauto.
- eapply leaderLogs_entries_match_nw_packet_set with (net:=net); intuition.
+ simpl in *. find_apply_hyp_hyp.  intuition.
do_in_map. subst. simpl in *.
exfalso. eapply handleTimeout_not_is_append_entries; eauto 10.
+ simpl. repeat find_higher_order_rewrite.
rewrite update_fun_comm. simpl.
rewrite update_fun_comm. simpl.
rewrite update_elections_data_timeout_leaderLogs.
rewrite update_nop_ext'; auto.
Qed.
Proof using lmi rri.
intros.
pose proof (lift_prop _ log_matching_invariant).
find_insterU. conclude_using eauto.
auto.
Qed.
Proof using lmi rri.
intros.
find_apply_lem_hyp lifted_log_matching.
unfold log_matching, log_matching_hosts in *.
rename net into net0.
intuition; repeat rewrite <- deghost_spec with (net := net0).
- auto.
- match goal with
| [ H : _ |- _ ] => solve [apply H; rewrite deghost_spec; auto]
end.
- match goal with
| [ H : _ |- _ ] => solve [eapply H; rewrite deghost_spec; eauto]
end.
Qed.
Proof using lmi rri.
intros.
find_apply_lem_hyp lifted_log_matching.
unfold log_matching, log_matching_nw in *.
break_and.
match goal with
| [ H : forall _ : packet , _ |- _ ] =>
do 7 insterU H;
conclude H ltac:(unfold deghost; simpl; eapply in_map_iff; eexists; eauto);
conclude H ltac:(simpl; eauto)
end.
rename net into net0.
intuition.
- rewrite <- deghost_spec with (net := net0).
eapply H3 with (e1:=e1)(e2:=e2); eauto.
rewrite deghost_spec.  auto.
- rewrite <- deghost_spec with (net := net0).
eapply H3 with (e1:=e1)(e2:=e2); eauto.
rewrite deghost_spec.  auto.
Qed.
Ltac use_log_matching_nw :=
pose proof (lifted_log_matching_nw _ ltac:(eauto));
match goal with
| [ H : _  |- _ ] =>
eapply H; [|eauto];
repeat find_rewrite; intuition
end.
Proof using.
unfold handleAppendEntries.
intros.
repeat break_match; find_inversion; intro; break_exists; discriminate.
Qed.
Proof using taifoi si llsi lmi rri.
unfold refined_raft_net_invariant_append_entries, leaderLogs_entries_match.
intuition.
- unfold leaderLogs_entries_match_host in *. intros.
{
intros. simpl in *. repeat find_higher_order_rewrite.
find_rewrite_lem update_fun_comm. simpl in *.
find_rewrite_lem update_fun_comm.
find_rewrite_lem update_elections_data_appendEntries_leaderLogs.
find_erewrite_lem update_nop_ext'.
update_destruct_simplify; rewrite_update;
try rewrite update_elections_data_appendEntries_leaderLogs in *; eauto.
destruct (log d) using (handleAppendEntries_log_ind ltac:(eauto)); eauto.
+ subst. eapply @entries_match_scratch with (plt := plt).
* eauto using lifted_logs_sorted_nw.
* apply sorted_uniqueIndices.
eapply leaderLogs_sorted_invariant; eauto.
* eapply_prop leaderLogs_entries_match_nw; eauto.
* use_log_matching_nw.
* use_log_matching_nw.
* match goal with
| [ H : In _ (leaderLogs _) |- _ ] =>
eapply terms_and_indices_from_one_invariant in H; [|solve[auto]]
end.
unfold terms_and_indices_from_one in *. intros.
find_apply_hyp_hyp. intuition.
+ eapply entries_match_append; eauto.
* eauto using lifted_logs_sorted_host.
* eapply leaderLogs_sorted_invariant; eauto.
* eauto using lifted_logs_sorted_nw.
* use_log_matching_nw.
* use_log_matching_nw.
* eapply findAtIndex_intro; eauto using lifted_logs_sorted_host, sorted_uniqueIndices.
}
- (* nw *)
unfold leaderLogs_entries_match_nw in *.
intros. simpl in *. repeat find_higher_order_rewrite.
find_rewrite_lem update_fun_comm. simpl in *.
find_rewrite_lem update_fun_comm.
find_rewrite_lem update_elections_data_appendEntries_leaderLogs.
find_erewrite_lem update_nop_ext'.
find_apply_hyp_hyp. break_or_hyp.
+ intuition; match goal with
| [ H : _ |- _ ] => solve [eapply (H _ _ _ p0); eauto with *]
end.
+ simpl in *.
find_copy_apply_lem_hyp handleAppendEntries_doesn't_send_AE.
exfalso. eauto 10.
Qed.

Lemma leaderLogs_entries_match_append_entries_reply :
refined_raft_net_invariant_append_entries_reply leaderLogs_entries_match.