Definition log_leaderLogs_prefix_within_term net :=
    forall h t ll leader,
      In (t, ll) (leaderLogs (fst (nwState net leader))) ->
      prefix_within_term (log (snd (nwState net h))) ll.Definition allEntries_log_prefix_within_term net :=
    forall h h',
      prefix_within_term (map snd (allEntries (fst (nwState net h)))) (log (snd (nwState net h'))).Definition allEntries_append_entries_prefix_within_term_nw net :=
    forall p t n pli plt es ci h e e',
      In p (nwPackets net) ->
      pBody p = AppendEntries t n pli plt es ci ->
      eTerm e = eTerm e' ->
      eIndex e <= eIndex e' ->
      In e (map snd (allEntries (fst (nwState net h)))) ->
      In e' es ->
      (In e es \/ (eIndex e = pli /\ eTerm e = plt) \/ (eIndex e < pli /\ eTerm e <= plt)).Definition append_entries_leaderLogs_prefix_within_term net :=
    forall p t n pli plt es ci h t' ll,
      In p (nwPackets net) ->
      pBody p = AppendEntries t n pli plt es ci ->
      In (t', ll) (leaderLogs (fst (nwState net h))) ->
      prefix_within_term es ll.Definition append_entries_log_prefix_within_term net :=
    forall p t n pli plt es ci h,
      In p (nwPackets net) ->
      pBody p = AppendEntries t n pli plt es ci ->
      prefix_within_term es (log (snd (nwState net h))).Definition prefix_within_term_inductive net :=
    allEntries_leaderLogs_prefix_within_term net /\
    log_leaderLogs_prefix_within_term net /\
    allEntries_log_prefix_within_term net /\
    allEntries_append_entries_prefix_within_term_nw net /\
    append_entries_leaderLogs_prefix_within_term net /\
    append_entries_log_prefix_within_term net.

(* Specifications: *)
Lemma findGtIndex_prefix_within_term : forall l1 l2 i, prefix_within_term l1 l2 -> prefix_within_term (findGtIndex l1 i) l2.
Lemma update_elections_data_client_request_leaderLogs : forall h st client id c, leaderLogs (update_elections_data_client_request h st client id c) = leaderLogs (fst st).
Lemma update_elections_data_timeout_leaderLogs : forall h st, leaderLogs (update_elections_data_timeout h st) = leaderLogs (fst st).
Lemma update_elections_data_appendEntries_leaderLogs : forall h st t h' pli plt es ci, leaderLogs (update_elections_data_appendEntries h st t h' pli plt es ci) = leaderLogs (fst st).
Lemma update_elections_data_requestVote_leaderLogs : forall h h' t lli llt st, leaderLogs (update_elections_data_requestVote h h' t h' lli llt st) = leaderLogs (fst st).
Lemma update_elections_data_requestVoteReply_leaderLogs : forall h h' t st t' ll' r, In (t', ll') (leaderLogs (fst st)) -> In (t', ll') (leaderLogs (update_elections_data_requestVoteReply h h' t r st)).
Lemma handleAppendEntriesReply_entries : forall h st t h' pli plt es ci st' t' es', handleAppendEntries h st t h' pli plt es ci = (st', AppendEntriesReply t' es' true) -> es' = es.
Lemma update_elections_data_appendEntries_allEntries : forall h st t h' pli plt es ci e, In e (map snd (allEntries (update_elections_data_appendEntries h st t h' pli plt es ci))) -> In e (map snd (allEntries (fst st))) \/ In e es.
Lemma update_elections_data_clientRequest_allEntries_new : forall h st client id c e, In e (map snd (allEntries (update_elections_data_client_request h st client id c))) -> In e (map snd (allEntries (fst st))) \/ (eIndex e = S (maxIndex (log (snd st))) /\ eTerm e = currentTerm (snd st) /\ type (snd st) = Leader).
Lemma update_elections_data_clientRequest_allEntries_old : forall h st client id c e, In e (map snd (allEntries (fst st))) -> In e (map snd (allEntries (update_elections_data_client_request h st client id c))).
Lemma prefix_within_term_union : forall l1 l1' l1'' l2, prefix_within_term l1' l2 -> prefix_within_term l1'' l2 -> (forall e, In e l1 -> In e l1' \/ In e l1'') -> prefix_within_term l1 l2.
Lemma removeAfterIndex_maxTerm_in : forall e l, sorted l -> In e l -> maxTerm (removeAfterIndex l (eIndex e)) = eTerm e.
Lemma prefix_within_term_inductive_append_entries : refined_raft_net_invariant_append_entries prefix_within_term_inductive.
Lemma handleClientRequest_log' : forall h st client id c out st' ms e, handleClientRequest h st client id c = (out, st', ms) -> In e (log st') -> In e (log st) \/ eIndex e = S (maxIndex (log st)) /\ eTerm e = currentTerm st /\ type st = Leader.
Lemma prefix_within_term_inductive_client_request : refined_raft_net_invariant_client_request prefix_within_term_inductive.
Lemma doLeader_spec : forall st h os st' ms m t n pli plt es ci, doLeader st h = (os, st', ms) -> In m ms -> snd m = AppendEntries t n pli plt es ci -> t = currentTerm st /\ log st' = log st /\ type st = Leader /\ ((pli = 0 /\ plt = 0 /\ es = findGtIndex (log st) 0) \/ ((exists e, findAtIndex (log st) pli = Some e /\ eTerm e = plt) /\ es = findGtIndex (log st) pli) \/ exists h', pred (getNextIndex st h') <> 0 /\ findAtIndex (log st) (pred (getNextIndex st h')) = None).
Lemma doLeader_in_entries_in_log : forall st h os st' ms m t n pli plt es ci e, doLeader st h = (os, st', ms) -> In m ms -> snd m = AppendEntries t n pli plt es ci -> In e es -> In e (log st).
Lemma lift_nextIndex_safety : forall net, refined_raft_intermediate_reachable net -> nextIndex_safety (deghost net).
Lemma nextIndex_sanity : forall net h h', refined_raft_intermediate_reachable net -> type (snd (nwState net h)) = Leader -> pred (getNextIndex (snd (nwState net h)) h') <> 0 -> exists e, findAtIndex (log (snd (nwState net h))) (pred (getNextIndex (snd (nwState net h)) h')) = Some e.
Lemma prefix_within_term_subset : forall l1 l1' l2, prefix_within_term l1' l2 -> (forall e, In e l1 -> In e l1') -> prefix_within_term l1 l2.
Lemma prefix_within_term_inductive_do_leader : refined_raft_net_invariant_do_leader prefix_within_term_inductive.
Lemma update_elections_data_requestVoteReply_leaderLogs' : forall h h' t st t' ll' r, In (t', ll') (leaderLogs (update_elections_data_requestVoteReply h h' t r st)) -> In (t', ll') (leaderLogs (fst st)) \/ ll' = log (snd st).
Lemma update_elections_data_requestVoteReply_allEntries : forall h h' t st r, allEntries (update_elections_data_requestVoteReply h h' t r st) = allEntries (fst st).
Lemma prefix_within_term_inductive_request_vote_reply : refined_raft_net_invariant_request_vote_reply prefix_within_term_inductive.
Lemma prefix_within_term_inductive_append_entries_reply : refined_raft_net_invariant_append_entries_reply prefix_within_term_inductive.
Lemma update_elections_data_timeout_allEntries : forall h st, allEntries (update_elections_data_timeout h st) = allEntries (fst st).
Lemma prefix_within_term_inductive_timeout : refined_raft_net_invariant_timeout prefix_within_term_inductive.
Lemma update_elections_data_requestVote_allEntries : forall h h' t lli llt st, allEntries (update_elections_data_requestVote h h' t h' lli llt st) = allEntries (fst st).
Lemma prefix_within_term_inductive_request_vote : refined_raft_net_invariant_request_vote prefix_within_term_inductive.
Lemma prefix_within_term_inductive_do_generic_server : refined_raft_net_invariant_do_generic_server prefix_within_term_inductive.
Lemma prefix_within_term_inductive_init : refined_raft_net_invariant_init prefix_within_term_inductive.
Lemma prefix_within_term_inductive_state_same_packet_subset : refined_raft_net_invariant_state_same_packet_subset prefix_within_term_inductive.
Lemma prefix_within_term_inductive_reboot : refined_raft_net_invariant_reboot prefix_within_term_inductive.
Theorem prefix_within_term_inductive_invariant : forall net, refined_raft_intermediate_reachable net -> prefix_within_term_inductive net.