Definition match_index_all_entries_nw (net : network) : Prop :=
    forall p t es e,
      In p (nwPackets net) ->
      pBody p = AppendEntriesReply t es true ->
      currentTerm (snd (nwState net (pDst p))) = t ->
      In e (log (snd (nwState net (pDst p)))) ->
      eTerm e = t ->
      eIndex e <= maxIndex es ->
      type (snd (nwState net (pDst p))) = Leader ->
      In (t, e) (allEntries (fst (nwState net (pSrc p)))).Definition match_index_all_entries_inv (net : network) : Prop :=
    match_index_all_entries net /\ match_index_all_entries_nw net.

(* Specifications: *)
Lemma match_index_all_entries_init : refined_raft_net_invariant_init match_index_all_entries_inv.
Theorem handleClientRequest_matchIndex_log : forall h st client id c out st' ps, handleClientRequest h st client id c = (out, st', ps) -> ps = nil /\ (log st' = log st /\ matchIndex st' = matchIndex st \/ exists e, log st' = e :: log st /\ eIndex e = S (maxIndex (log st)) /\ eTerm e = currentTerm st /\ eClient e = client /\ eInput e = c /\ eId e = id /\ type st = Leader /\ matchIndex st' = assoc_set name_eq_dec (matchIndex st) h (S (maxIndex (log st)))).
Lemma lifted_match_index_leader : forall net leader, refined_raft_intermediate_reachable net -> type (snd (nwState net leader)) = Leader -> assoc_default name_eq_dec (matchIndex (snd (nwState net leader))) leader 0 = maxIndex (log (snd (nwState net leader))).
Lemma lifted_match_index_sanity : forall net leader h, refined_raft_intermediate_reachable net -> type (snd (nwState net leader)) = Leader -> assoc_default name_eq_dec (matchIndex (snd (nwState net leader))) h 0 <= maxIndex (log (snd (nwState net leader))).
Lemma lifted_append_entries_reply_sublog : forall net p t es h e, refined_raft_intermediate_reachable net -> In p (nwPackets net) -> pBody p = AppendEntriesReply t es true -> currentTerm (snd (nwState net h)) = t -> type (snd (nwState net h)) = Leader -> In e es -> In e (log (snd (nwState net h))).
Lemma match_index_all_entries_client_request : refined_raft_net_invariant_client_request match_index_all_entries_inv.
Lemma handleTimeout_matchIndex : forall h st out st' l, handleTimeout h st = (out, st', l) -> matchIndex st' = matchIndex st.
Lemma allEntries_update_timeout : forall x h h' net d, In x (allEntries (fst (nwState net h))) -> In x (allEntries (fst (update name_eq_dec (nwState net) h' (update_elections_data_timeout h' (nwState net h'), d) h))).
Lemma handleTimeout_sends_RV : forall h st out st' l m, handleTimeout h st = (out, st', l) -> In m l -> exists node t h mi mt, m = (node, RequestVote t h mi mt).
Lemma match_index_all_entries_timeout : refined_raft_net_invariant_timeout match_index_all_entries_inv.
Lemma handleAppendEntries_post_leader_nop : forall h st t n pli plt es ci st' m, currentTerm st <> t -> handleAppendEntries h st t n pli plt es ci = (st', m) -> type st' = Leader -> st' = st.
Lemma handleAppendEntries_leader_was_leader : forall h st t n pli plt es ci st' m, handleAppendEntries h st t n pli plt es ci = (st', m) -> type st' = Leader -> type st = Leader.
Lemma lifted_no_AE_to_leader : forall net p t n pli plt es ci, refined_raft_intermediate_reachable net -> In p (nwPackets net) -> pBody p = AppendEntries t n pli plt es ci -> type (snd (nwState net (pDst p))) = Leader -> currentTerm (snd (nwState net (pDst p))) = t -> False.
Lemma lifted_no_AE_to_self : forall net p t n pli plt es ci, refined_raft_intermediate_reachable net -> In p (nwPackets net) -> pBody p = AppendEntries t n pli plt es ci -> pDst p = pSrc p -> False.
Lemma handleAppendEntries_message : forall h st t n pli plt es ci st' m, handleAppendEntries h st t n pli plt es ci = (st', m) -> exists res, m = AppendEntriesReply (currentTerm st') es res.
Lemma not_empty_true_elim : forall A (l : list A), not_empty l = true -> l <> nil.
Lemma not_empty_false_elim : forall A (l : list A), not_empty l = false -> l = nil.
Lemma handleAppendEntries_success_allEntries : forall h st t n pli plt es ci st' t', handleAppendEntries h st t n pli plt es ci = (st', AppendEntriesReply t' es true) -> es <> nil -> (forall e e' e'', In e es -> In e' (log st) -> eIndex e = eIndex e' -> eTerm e = eTerm e' -> In e'' es -> eIndex e'' <= eIndex e -> In e'' (log st)) -> sorted (log st) -> exists e, In e (log st') /\ In e es /\ eIndex e = maxIndex es /\ eTerm e = maxTerm es.
Lemma handleAppendEntries_success_term : forall h st t n pli plt es ci st' t', handleAppendEntries h st t n pli plt es ci = (st', AppendEntriesReply t' es true) -> currentTerm st' = t.
Lemma lifted_terms_and_indices_from_one_log : forall net h, refined_raft_intermediate_reachable net -> terms_and_indices_from_one (log (snd (nwState net h))).
Lemma lifted_leader_sublog_nw : forall net p t n pli plt es ci h e, refined_raft_intermediate_reachable net -> In p (nwPackets net) -> pBody p = AppendEntries t n pli plt es ci -> type (snd (nwState net h)) = Leader -> eTerm e = currentTerm (snd (nwState net h)) -> In e es -> In e (log (snd (nwState net h))).
Lemma appendEntries_sublog : forall net p t n pli plt es ci h e, refined_raft_intermediate_reachable net -> In p (nwPackets net) -> pBody p = AppendEntries t n pli plt es ci -> currentTerm (snd (nwState net h)) = t -> type (snd (nwState net h)) = Leader -> In e es -> In e (log (snd (nwState net h))).
Lemma match_index_all_entries_append_entries : refined_raft_net_invariant_append_entries' match_index_all_entries_inv.
Lemma handleAppendEntriesReply_spec : forall n st src t es b st' l, handleAppendEntriesReply n st src t es b = (st', l) -> (type st' = type st /\ matchIndex st' = matchIndex st /\ currentTerm st' = currentTerm st) \/ (currentTerm st' = t /\ type st' = Follower) \/ (b = true /\ t = currentTerm st' /\ type st' = type st /\ matchIndex st' = assoc_set name_eq_dec (matchIndex st) src (PeanoNat.Nat.max (assoc_default name_eq_dec (matchIndex st) src 0) (maxIndex es)) /\ currentTerm st' = currentTerm st).
Lemma update_nop_fst : forall A B f x (v2 : B) y, fst (update name_eq_dec f x (fst (f x), v2) y) = fst (A := A) (f y).
Lemma match_index_all_entries_append_entries_reply : refined_raft_net_invariant_append_entries_reply match_index_all_entries_inv.
Lemma handleRequestVote_sends_RVR : forall st h h' t lli llt st' m, handleRequestVote h st t h' lli llt = (st', m) -> exists t b, m = RequestVoteReply t b.
Lemma match_index_all_entries_request_vote : refined_raft_net_invariant_request_vote match_index_all_entries_inv.
Lemma handleRequestVoteReply_spec : forall h st h' t r st', handleRequestVoteReply h st h' t r = st' -> type st' = Follower \/ st' = st \/ (log st' = log st /\ currentTerm st' = currentTerm st /\ matchIndex st' = assoc_set name_eq_dec nil h (maxIndex (log st))).
Lemma handleRequestVoteReply_spec' : forall h st h' t r st', handleRequestVoteReply h st h' t r = st' -> type st' = Follower \/ st' = st \/ type st' = Candidate \/ (type st' = Leader /\ type st = Candidate /\ log st' = log st /\ r = true /\ t = currentTerm st /\ wonElection (dedup name_eq_dec (h' :: votesReceived st)) = true /\ currentTerm st' = currentTerm st).
Lemma match_index_all_entries_request_vote_reply : refined_raft_net_invariant_request_vote_reply match_index_all_entries_inv.
Lemma doLeader_sends_AE : forall st h os st' ms m, doLeader st h = (os, st', ms) -> In m ms -> is_append_entries (snd m).
Lemma match_index_all_entries_do_leader : refined_raft_net_invariant_do_leader match_index_all_entries_inv.
Lemma match_index_all_entries_do_generic_server : refined_raft_net_invariant_do_generic_server match_index_all_entries_inv.
Lemma match_index_all_entries_state_same_packet_subset : refined_raft_net_invariant_state_same_packet_subset match_index_all_entries_inv.
Lemma match_index_all_entries_reboot : refined_raft_net_invariant_reboot match_index_all_entries_inv.
Lemma match_index_all_entries_invariant : forall net, refined_raft_intermediate_reachable net -> match_index_all_entries_inv net.