Section MatchIndexSanity.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Context {aersi : append_entries_reply_sublog_interface}.
Context {si : sorted_interface}.
Proof using.
unfold raft_net_invariant_init, match_index_sanity.
simpl.
intuition.
Qed.
Proof using.
unfold raft_net_invariant_client_request, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_copy_apply_lem_hyp handleClientRequest_type.
find_copy_apply_lem_hyp handleClientRequest_matchIndex.
intuition.
+ repeat find_rewrite. auto.
+ repeat find_rewrite.
destruct (name_eq_dec h0 leader).
* subst. rewrite get_set_same_default. auto.
* rewrite get_set_diff_default by auto.
eauto using le_trans.
- auto.
Qed.
Proof using.
unfold matchIndex_preserved.
intros. intuition.
repeat find_rewrite.
auto.
Qed.
Proof using.
unfold raft_net_invariant_timeout, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_apply_lem_hyp handleTimeout_matchIndex_preserved.
eauto using match_index_sanity_preserved.
- auto.
Qed.
Proof using.
unfold raft_net_invariant_append_entries, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_apply_lem_hyp handleAppendEntries_matchIndex_preserved.
eauto using match_index_sanity_preserved.
- auto.
Qed.
Proof using.
unfold handleAppendEntriesReply, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; simpl in *; do_bool; auto; congruence.
Qed.
Proof using si aersi.
unfold raft_net_invariant_append_entries_reply, match_index_sanity.
simpl. intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- erewrite handleAppendEntriesReply_log by eauto.
find_copy_apply_lem_hyp handleAppendEntriesReply_matchIndex; auto.
intuition.
+ repeat find_rewrite. auto.
+ repeat find_rewrite.
destruct (name_eq_dec h (pSrc p)).
* subst. rewrite get_set_same_default.
apply Max.max_case; auto.
{ destruct es; simpl.
* lia.
* pose proof append_entries_reply_sublog_invariant _ ltac:(eauto).
unfold append_entries_reply_sublog in *.
eapply_prop_hyp pBody pBody; simpl; eauto.
apply maxIndex_is_max; auto.
apply logs_sorted_invariant; auto.
}
* rewrite get_set_diff_default by auto.
auto.
- auto.
Qed.
Proof using.
unfold raft_net_invariant_request_vote, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_apply_lem_hyp handleRequestVote_matchIndex_preserved.
eauto using match_index_sanity_preserved.
- auto.
Qed.
Proof using.
unfold handleRequestVoteReply, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; auto; simpl in *; try congruence.
Qed.
Proof using.
unfold raft_net_invariant_request_vote_reply, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- pose proof handleRequestVoteReply_matchIndex (pDst p) (nwState net (pDst p)) (pSrc p) t v.
erewrite handleRequestVoteReply_log by eauto.
intuition.
+ repeat find_rewrite. auto.
+ repeat find_rewrite.
destruct (name_eq_dec h (pDst p)).
* subst. rewrite get_set_same_default. auto.
* rewrite get_set_diff_default by auto.
unfold assoc_default. simpl. lia.
- auto.
Qed.
Proof using.
unfold raft_net_invariant_do_leader, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_apply_lem_hyp doLeader_matchIndex_preserved.
eauto using match_index_sanity_preserved.
- auto.
Qed.
Proof using.
unfold raft_net_invariant_do_generic_server, match_index_sanity.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_apply_lem_hyp doGenericServer_matchIndex_preserved.
eauto using match_index_sanity_preserved.
- auto.
Qed.
Proof using.
unfold raft_net_invariant_state_same_packet_subset, match_index_sanity.
simpl. intros.
repeat find_reverse_higher_order_rewrite.
auto.
Qed.
Proof using.
unfold raft_net_invariant_reboot, match_index_sanity, reboot.
simpl.
intros.
subst.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- unfold assoc_default. simpl. lia.
- auto.
Qed.

Lemma match_index_sanity_invariant :
forall net,
raft_intermediate_reachable net ->
match_index_sanity net.