Section Serializers.
Variable n : nat.
Instance raft_params : RaftParams VarD.vard_base_params :=
raft_params n.
Definition entry_serialize (e : entry) :=
serialize (Raft.eAt e) +$+
serialize (Raft.eClient e) +$+
serialize (Raft.eId e) +$+
serialize (Raft.eIndex e) +$+
serialize (Raft.eTerm e) +$+
serialize (Raft.eInput e).
Definition entry_deserialize : ByteListReader.t entry :=
At <- deserialize;;
Client <- deserialize;;
Id <- deserialize;;
Index <- deserialize;;
Term <- deserialize;;
Input <- deserialize;;
ByteListReader.ret (Raft.mkEntry At Client Id Index Term Input).
Proof using.
intros.
unfold entry_serialize, entry_deserialize.
cheerios_crush.
destruct a;
reflexivity.
Qed.
Instance entry_Serializer : Serializer entry :=
{| serialize := entry_serialize;
deserialize := entry_deserialize;
serialize_deserialize_id := entry_serialize_deserialize_id |}.
Definition msg_serialize (m : msg) : IOStreamWriter.t :=
match m with
| RequestVote t1 n i t2 =>
serialize x00 +$+
serialize t1 +$+
serialize n +$+
serialize i +$+
serialize t2
| RequestVoteReply t b =>
serialize x01 +$+ serialize t +$+ serialize b
| AppendEntries t1 n i1 t2 l2 i2 =>
serialize x02 +$+
serialize t1 +$+
serialize n +$+
serialize i1 +$+
serialize t2 +$+
serialize l2 +$+
serialize i2
| AppendEntriesReply t l b =>
serialize x03 +$+ serialize t +$+ serialize l +$+ serialize b
end.
Definition RequestVote_deserialize : ByteListReader.t msg :=
t1 <- deserialize;;
n <- deserialize;;
i <- deserialize;;
t2 <- deserialize;;
ByteListReader.ret (RequestVote t1 n i t2).
Definition RequestVoteReply_deserialize : ByteListReader.t msg :=
t <- deserialize;;
b <- deserialize;;
ByteListReader.ret (RequestVoteReply t b).
Definition AppendEntries_deserialize : ByteListReader.t msg :=
t1 <- deserialize;;
n <- deserialize;;
i1 <- deserialize;;
t2 <- deserialize;;
l <- deserialize;;
i2 <- deserialize;;
ByteListReader.ret (AppendEntries t1 n i1 t2 l i2).
Definition AppendEntriesReply_deserialize : ByteListReader.t msg :=
t <- deserialize;;
l <- deserialize;;
b <- deserialize;;
ByteListReader.ret (AppendEntriesReply t l b).
Definition msg_deserialize :=
tag <- deserialize;;
match tag with
| x00 => RequestVote_deserialize
| x01 => RequestVoteReply_deserialize
| x02 => AppendEntries_deserialize
| x03 => AppendEntriesReply_deserialize
| _ => ByteListReader.error
end.
Proof using.
intros.
unfold msg_serialize, msg_deserialize.
destruct a;
cheerios_crush;
simpl;
(unfold RequestVote_deserialize ||
unfold RequestVoteReply_deserialize ||
unfold AppendEntries_deserialize ||
unfold AppendEntriesReply_deserialize);
cheerios_crush.
Qed.
Global Instance msg_Serializer : Serializer msg :=
{| serialize := msg_serialize;
deserialize := msg_deserialize;
serialize_deserialize_id := msg_serialize_deserialize_id |}.
Definition serialize_serverType (t : serverType) :=
match t with
| Follower => serialize x00
| Candidate => serialize x01
| Leader => serialize x02
end.
Definition deserialize_serverType : ByteListReader.t serverType :=
tag <- deserialize;;
match tag with
| x00 => ByteListReader.ret Follower
| x01 => ByteListReader.ret Candidate
| x02 => ByteListReader.ret Leader
| _ => ByteListReader.error
end.

Lemma serverType_serialize_deserialize_id :
serialize_deserialize_id_spec serialize_serverType deserialize_serverType.