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.

Lemma handleAppendEntriesReply_nextIndex :
forall h st st' m t es res h',
handleAppendEntriesReply h st h' t es res = (st', m) ->
type st' = Leader ->
type st = Leader /\
((nextIndex st' = nextIndex st \/
(res = true /\
currentTerm st = t /\
nextIndex st' =
(assoc_set name_eq_dec (nextIndex st) h'
(Nat.max (getNextIndex st h') (S (maxIndex es)))))) \/
(res = false /\
currentTerm st = t /\
nextIndex st' =
(assoc_set name_eq_dec (nextIndex st) h'
(pred (getNextIndex st h'))))).