Section SortedProof.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Context {tsi : term_sanity_interface}.
Proof using.
unfold raft_net_invariant_init, logs_sorted,
logs_sorted_host, logs_sorted_nw, packets_gt_prevIndex, packets_ge_prevTerm in *.
intuition; simpl in *; intuition.
Qed.
Proof using.
intros. find_apply_lem_hyp handleClientRequest_log. intuition.
Qed.
Proof using.
unfold logs_sorted_nw in *. intros.
simpl in *. find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using.
unfold packets_gt_prevIndex in *. intros.
simpl in *. find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using.
unfold packets_ge_prevTerm in *. intros.
simpl in *. find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using tsi.
unfold logs_sorted_host.
intros.
find_apply_lem_hyp handleClientRequest_log. intuition.
+ repeat find_rewrite. eauto.
+ find_apply_lem_hyp no_entries_past_current_term_invariant.
break_exists; intuition; repeat find_rewrite.
simpl. intuition eauto.
* find_eapply_lem_hyp maxIndex_is_max; eauto.
lia.
* unfold no_entries_past_current_term, no_entries_past_current_term_host in *.
intuition. simpl in *. find_apply_hyp_hyp. lia.
Qed.
Proof using tsi.
unfold raft_net_invariant_client_request. unfold logs_sorted. intuition.
- unfold logs_sorted_host in *. simpl in *. intros.
find_higher_order_rewrite. break_match; eauto.
subst.
eauto using handleClientRequest_logs_sorted.
- find_apply_lem_hyp handleClientRequest_packets. subst. simpl in *.
eauto using logs_sorted_nw_packets_unchanged.
- find_apply_lem_hyp handleClientRequest_packets. subst. simpl in *.
eauto using packets_gt_prevIndex_packets_unchanged.
- find_apply_lem_hyp handleClientRequest_packets. subst. simpl in *.
eauto using packets_ge_prevTerm_packets_unchanged.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
unfold logs_sorted_nw. intros. simpl in *.
find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using.
intros. eapply logs_sorted_nw_only_new_packets_matter; eauto.
unfold logs_sorted_nw. intros. simpl in *. find_apply_hyp_hyp.
exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
intros.
unfold logs_sorted_nw. intros. simpl in *. find_apply_hyp_hyp.
intuition.
- unfold logs_sorted_nw in *. eauto.
- subst. exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
unfold packets_gt_prevIndex. intros. simpl in *.
find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using.
intros. eapply packets_gt_prevIndex_only_new_packets_matter; eauto.
unfold packets_gt_prevIndex. intros. simpl in *. find_apply_hyp_hyp.
exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
intros.
unfold packets_gt_prevIndex. intros. simpl in *. find_apply_hyp_hyp.
intuition.
- unfold packets_gt_prevIndex in *. eauto.
- subst. exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
unfold packets_ge_prevTerm. intros. simpl in *.
find_apply_hyp_hyp. intuition eauto.
Qed.
Proof using.
intros. eapply packets_ge_prevTerm_only_new_packets_matter; eauto.
unfold packets_ge_prevTerm. intros. simpl in *. find_apply_hyp_hyp.
exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
intros.
unfold packets_ge_prevTerm. intros. simpl in *. find_apply_hyp_hyp.
intuition.
- unfold packets_ge_prevTerm in *. eauto.
- subst. exfalso. match goal with H : _ |- _ => apply H end.
repeat eexists; eauto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; simpl in *; eauto;
repeat (do_in_map; subst; simpl in *); intuition; break_exists; congruence.
Qed.
Proof using.
unfold raft_net_invariant_timeout. unfold logs_sorted. intuition.
- unfold logs_sorted_host in *. simpl in *. intros.
find_apply_lem_hyp handleTimeout_log.
find_higher_order_rewrite. break_match; repeat find_rewrite; eauto.
- eapply logs_sorted_nw_no_append_entries; eauto. intros.
eauto using handleTimeout_not_is_append_entries.
- eapply packets_gt_prevIndex_no_append_entries; eauto. intros.
eauto using handleTimeout_not_is_append_entries.
- eapply packets_ge_prevTerm_no_append_entries; eauto. intros.
eauto using handleTimeout_not_is_append_entries.
Qed.
Ltac find_eapply_hyp_goal :=
match goal with
| H : _ |- _ =>
eapply H
end.
Proof using.
induction l; intros; simpl in *; auto.
intuition; do_in_app; intuition; find_apply_hyp_hyp; intuition.
Qed.
Proof using.
induction l; intros; simpl in *; intuition.
- subst_max. intuition.
- subst. find_apply_hyp_hyp. intuition.
- subst. find_apply_hyp_hyp. intuition.
Qed.
Proof using.
intros. unfold logs_sorted in *. intuition.
find_apply_lem_hyp handleAppendEntries_log. intuition.
- find_rewrite; eauto.
- subst. unfold logs_sorted_nw in *. simpl in *.
find_eapply_hyp_goal; eauto.
- find_rewrite. apply sorted_append; eauto using removeAfterIndex_sorted.
+ intros. find_apply_lem_hyp removeAfterIndex_In_le; eauto.
unfold packets_gt_prevIndex in *.
eapply gt_le_trans; [|eauto].
find_eapply_hyp_goal; [in_crush|eauto|eauto].
simpl in *. eauto.
+ intros. find_copy_apply_lem_hyp removeAfterIndex_In_le; eauto.
find_apply_lem_hyp removeAfterIndex_in.
break_exists. intuition. subst.
match goal with
| H : eIndex ?x <= eIndex ?x', _ : In ?x ?ll |- _ =>
apply sorted_index_term with (l := ll) (e := x) (e' := x') in H
end; eauto.
match goal with |- ?a >= ?b => cut (b <= a); [lia|] end.
eapply le_trans; eauto.
unfold packets_ge_prevTerm in *.
find_eapply_hyp_goal; [in_crush|eauto|eauto];
simpl in *; eauto.
Qed.
Proof using.
unfold raft_net_invariant_append_entries. intros. unfold logs_sorted. intuition.
- unfold logs_sorted_host. simpl in *. intros.
repeat find_higher_order_rewrite.
break_match; repeat find_rewrite; eauto;
[|unfold logs_sorted in *; intuition eauto].
subst.
eauto using handleAppendEntries_logs_sorted.
- unfold logs_sorted in *. intuition.
simpl in *.
eapply logs_sorted_nw_not_append_entries; eauto.
+ intros. find_apply_hyp_hyp. intuition eauto.
+ simpl. eauto using handleAppendEntries_not_append_entries.
- unfold logs_sorted in *. intuition.
simpl in *.
eapply packets_gt_prevIndex_not_append_entries; eauto.
+ intros. find_apply_hyp_hyp. intuition eauto.
+ simpl. eauto using handleAppendEntries_not_append_entries.
- unfold logs_sorted in *. intuition.
simpl in *.
eapply packets_ge_prevTerm_not_append_entries; eauto.
+ intros. find_apply_hyp_hyp. intuition eauto.
+ simpl. eauto using handleAppendEntries_not_append_entries.
Qed.
Proof using.
intros. unfold handleAppendEntriesReply, advanceCurrentTerm in *.
repeat break_match; find_inversion; subst; auto.
Qed.

Lemma handleAppendEntriesReply_packets :
forall h st from t es s st' ps,
handleAppendEntriesReply h st from t es s = (st', ps) ->
ps = [].