Section NextIndexSafety.
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, nextIndex_safety.
intros.
discriminate.
Qed.
Definition nextIndex_preserved st st' :=
(type st' = Leader ->
type st = Leader /\
maxIndex (log st) <= maxIndex (log st') /\
nextIndex st' = nextIndex st).
Proof using.
unfold getNextIndex, nextIndex_preserved in *.
intuition.
repeat find_rewrite.
auto.
unfold assoc_default in *.
specialize (H h').
break_match.
- eauto using le_trans.
- lia.
Qed.
Proof using.
unfold handleClientRequest, nextIndex_preserved.
intros.
repeat break_match; repeat find_inversion; simpl in *; try congruence.
intuition.
Qed.
Proof using.
unfold raft_net_invariant_client_request, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, handleClientRequest_nextIndex_preserved.
- auto.
Qed.
Proof using.
unfold handleTimeout, tryToBecomeLeader, nextIndex_preserved.
intros.
repeat break_match; repeat find_inversion; simpl in *; try congruence.
auto.
Qed.
Proof using.
unfold raft_net_invariant_timeout, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, handleTimeout_nextIndex_preserved.
- auto.
Qed.
Proof using.
unfold handleAppendEntries, nextIndex_preserved, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; simpl in *; try congruence; auto.
Qed.
Proof using.
unfold raft_net_invariant_append_entries, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, handleAppendEntries_nextIndex_preserved.
- auto.
Qed.
Proof using.
unfold handleAppendEntriesReply, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; do_bool; simpl in *; intuition; congruence.
Qed.
Proof using si aersi.
unfold raft_net_invariant_append_entries_reply, nextIndex_safety, getNextIndex.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- erewrite handleAppendEntriesReply_log by eauto.
find_copy_apply_lem_hyp handleAppendEntriesReply_nextIndex; auto.
intuition; repeat find_rewrite.
+ auto.
+ destruct (name_eq_dec h' (pSrc p)).
* subst. rewrite get_set_same_default.
unfold getNextIndex.
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.
+ destruct (name_eq_dec h' (pSrc p)).
* subst. rewrite get_set_same_default.
unfold getNextIndex.
apply NPeano.Nat.le_le_pred.
auto.
* rewrite get_set_diff_default by auto.
auto.
- auto.
Qed.
Proof using.
unfold handleRequestVote, nextIndex_preserved, advanceCurrentTerm.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold raft_net_invariant_request_vote, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, handleRequestVote_nextIndex_preserved.
- auto.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence.
Qed.
Proof using.
unfold raft_net_invariant_request_vote_reply, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- find_copy_apply_lem_hyp handleRequestVoteReply_matchIndex.
unfold getNextIndex in *.
erewrite handleRequestVoteReply_log in * by eauto.
intuition; repeat find_rewrite.
+ auto.
+ unfold assoc_default. simpl.
auto using NPeano.Nat.le_le_pred.
- auto.
Qed.
Proof using.
unfold doLeader, nextIndex_preserved.
intros.
repeat break_match; repeat find_inversion; auto; try congruence.
Qed.
Proof using.
unfold raft_net_invariant_do_leader, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, doLeader_nextIndex_preserved.
- auto.
Qed.
Proof using.
unfold doGenericServer, nextIndex_preserved.
intros.
repeat break_match; repeat find_inversion; simpl in *; auto; try congruence;
use_applyEntries_spec; subst; simpl in *; auto.
Qed.
Proof using.
unfold raft_net_invariant_do_generic_server, nextIndex_safety.
simpl.
intros.
repeat find_higher_order_rewrite.
update_destruct_simplify.
- eauto using nextIndex_safety_preserved, doGenericServer_nextIndex_preserved.
- auto.
Qed.

Lemma nextIndex_safety_state_same_packet_subset :
raft_net_invariant_state_same_packet_subset nextIndex_safety.