Section RaftRefinementProof.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Proof using.
intros.
unfold handleMessage, update_elections_data_net in *.
break_match; repeat break_let; repeat find_inversion;
[eapply_prop refined_raft_net_invariant_request_vote|
eapply_prop refined_raft_net_invariant_request_vote_reply|
eapply_prop refined_raft_net_invariant_append_entries|
eapply_prop refined_raft_net_invariant_append_entries_reply]; eauto;
unfold send_packets in *; simpl in *; intros; subst; auto; find_apply_hyp_hyp; intuition.
Qed.
Proof using.
intros.
unfold handleInput, update_elections_data_input in *.
break_match; repeat break_let; repeat find_inversion;
[eapply_prop refined_raft_net_invariant_timeout|
eapply_prop refined_raft_net_invariant_client_request]; eauto; subst; auto.
Qed.
Proof using.
intros.
induction H10.
- intuition.
-  match goal with [H : step_failure _ _ _ |- _ ] => invcs H end.
+ unfold refined_net_handlers in *. simpl in *.
unfold RaftNetHandler, update_elections_data_net in *.
repeat break_let.
repeat find_inversion.
assert
(refined_raft_intermediate_reachable
{|
nwPackets := (xs ++ ys) ++ send_packets (pDst p) l2;
nwState := update name_eq_dec (nwState net) (pDst p)
(update_elections_data_net (pDst p)
(pSrc p)
(pBody p)
(nwState net (pDst p)), r0) |})
by (eapply RRIR_handleMessage; eauto; in_crush).
assert
(refined_raft_intermediate_reachable
{|
nwPackets := ((xs ++ ys)
++ send_packets (pDst p) l2)
++ send_packets (pDst p) l3 ;
nwState := update name_eq_dec
(nwState
{|
nwPackets := (xs ++ ys) ++ send_packets (pDst p) l2;
nwState := update name_eq_dec
(nwState net)
(pDst p)
(update_elections_data_net
(pDst p) (pSrc p)
(pBody p) (nwState net (pDst p)), r0) |})
(pDst p)
(update_elections_data_net (pDst p)
(pSrc p) (pBody p) (nwState net (pDst p)), r1) |})
by
(eapply RRIR_doLeader; eauto;
[simpl in *; break_if; try congruence; eauto| in_crush]).
eapply_prop refined_raft_net_invariant_do_generic_server. eauto.
eapply_prop refined_raft_net_invariant_do_leader. eauto.
eapply refined_raft_invariant_handle_message with (P := P); eauto using in_app_or.
auto.
simpl. break_if; intuition eauto.
eauto.
simpl. eapply in_app_or. auto.
simpl. break_if; eauto; congruence.
simpl. intros.
break_if; subst;
repeat rewrite update_same by auto;
repeat rewrite update_neq by auto; auto.
simpl. in_crush.
+ unfold refined_input_handlers in *. simpl in *.
unfold RaftInputHandler, update_elections_data_input in *. repeat break_let.
repeat find_inversion.
assert
(refined_raft_intermediate_reachable
{|
nwPackets := nwPackets net ++ send_packets h l2;
nwState := update name_eq_dec (nwState net) h
(update_elections_data_input h
inp
(nwState net h), r0) |})
by (eapply RRIR_handleInput; eauto; in_crush).
assert
(refined_raft_intermediate_reachable
{|
nwPackets := (nwPackets net
++ send_packets h l2)
++ send_packets h l4 ;
nwState := update name_eq_dec
(nwState
{|
nwPackets := nwPackets net ++ send_packets h l2;
nwState := update name_eq_dec
(nwState net)
h
(update_elections_data_input h inp
(nwState net h), r0) |})
h
(update_elections_data_input h inp (nwState net h), r1) |})
by
(eapply RRIR_doLeader; eauto;
[simpl in *; break_if; try congruence; eauto| in_crush]).
eapply_prop refined_raft_net_invariant_do_generic_server. eauto.
eapply_prop refined_raft_net_invariant_do_leader. eauto.
eapply refined_raft_invariant_handle_input with (P := P); eauto using in_app_or.
auto.
simpl. break_if; intuition eauto.
eauto.
simpl. eapply in_app_or.
auto.
simpl. break_if; eauto; congruence.
simpl. intros.
break_if; subst;
repeat rewrite update_same by auto;
repeat rewrite update_neq by auto; auto.
simpl. unfold send_packets.  intros. in_crush.
+ match goal with
| [ H : nwPackets ?net = _ |- _ {| nwPackets := ?ps ; nwState := ?st |} ] =>
assert (forall p, In p (nwPackets {| nwPackets := ps ; nwState := st |}) ->
In p (nwPackets net)) by (intros; simpl in *; find_rewrite; in_crush)
end.
eapply_prop refined_raft_net_invariant_state_same_packet_subset; [|eauto|idtac|];
eauto.
+ match goal with
| [ H : nwPackets ?net = _ |- _ {| nwPackets := ?ps ; nwState := ?st |} ] =>
assert (forall p, In p (nwPackets {| nwPackets := ps ; nwState := st |}) ->
In p (nwPackets net)) by (intros; simpl in *; find_rewrite; in_crush)
end.
eapply_prop refined_raft_net_invariant_state_same_packet_subset; [|eauto|idtac|];
eauto.
+ auto.
+ eapply_prop refined_raft_net_invariant_reboot; eauto;
intros; simpl in *; repeat break_if; intuition; subst; intuition eauto.
destruct (nwState net h); auto.
- eapply refined_raft_invariant_handle_input; eauto.
- eapply refined_raft_invariant_handle_message; eauto.
- eapply_prop refined_raft_net_invariant_do_leader; eauto.
- eapply_prop refined_raft_net_invariant_do_generic_server; eauto.
Qed.
Proof using.
intros.
unfold handleMessage, update_elections_data_net in *.
break_match; repeat break_let; repeat find_inversion;
[eapply_prop refined_raft_net_invariant_request_vote'|
eapply_prop refined_raft_net_invariant_request_vote_reply'|
eapply_prop refined_raft_net_invariant_append_entries'|
eapply_prop refined_raft_net_invariant_append_entries_reply']; eauto;
unfold send_packets in *; simpl in *; intros; subst; auto; find_apply_hyp_hyp; intuition.
Qed.

Lemma refined_raft_invariant_handle_input' P :
forall h inp net st' ps' gd out d l,
refined_raft_net_invariant_timeout' P ->
refined_raft_net_invariant_client_request' P ->
handleInput h inp (snd (nwState net h)) = (out, d, l) ->
update_elections_data_input h inp (nwState net h) = gd ->
P net ->
refined_raft_intermediate_reachable net ->
refined_raft_intermediate_reachable (mkNetwork ps' st') ->
(forall h', st' h' = update name_eq_dec (nwState net) h (gd, d) h') ->
(forall p', In p' ps' -> In p' (nwPackets net) \/
In p' (send_packets h l)) ->
P (mkNetwork ps' st').