arc_malachitebft_sync/ser/
borsh.rs1use {
2 crate::{RawDecidedValue, Request, Response, Status, ValueRequest, ValueResponse},
3 borsh::BorshSerialize,
4 malachitebft_core_types::{CommitCertificate, Context},
5 malachitebft_peer::PeerId,
6 std::ops::RangeInclusive,
7};
8
9impl<Ctx: Context> borsh::BorshSerialize for Status<Ctx>
10where
11 Ctx::Height: borsh::BorshSerialize,
12{
13 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
14 self.peer_id.serialize(writer)?;
15 self.tip_height.serialize(writer)?;
16 self.history_min_height.serialize(writer)?;
17 Ok(())
18 }
19}
20
21impl<Ctx: Context> borsh::BorshDeserialize for Status<Ctx>
22where
23 Ctx::Height: borsh::BorshDeserialize,
24{
25 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
26 let peer_id = PeerId::deserialize_reader(reader)?;
27 let tip_height = Ctx::Height::deserialize_reader(reader)?;
28 let history_min_height = Ctx::Height::deserialize_reader(reader)?;
29 Ok(Status {
30 peer_id,
31 tip_height,
32 history_min_height,
33 })
34 }
35}
36
37impl<Ctx: Context> borsh::BorshSerialize for Request<Ctx>
38where
39 Ctx::Height: borsh::BorshSerialize,
40{
41 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
42 match self {
43 Request::ValueRequest(value_request) => value_request.range.serialize(writer),
44 }
45 }
46}
47
48impl<Ctx: Context> borsh::BorshDeserialize for Request<Ctx>
49where
50 Ctx::Height: borsh::BorshDeserialize,
51{
52 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
53 let range = RangeInclusive::<Ctx::Height>::deserialize_reader(reader)?;
54 Ok(Request::ValueRequest(ValueRequest::new(range)))
55 }
56}
57
58impl<Ctx: Context> borsh::BorshSerialize for Response<Ctx>
59where
60 ValueResponse<Ctx>: borsh::BorshSerialize,
61{
62 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
63 match self {
64 Response::ValueResponse(value_response) => value_response.serialize(writer),
65 }
66 }
67}
68
69impl<Ctx: Context> borsh::BorshDeserialize for Response<Ctx>
70where
71 ValueResponse<Ctx>: borsh::BorshDeserialize,
72{
73 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
74 let value = ValueResponse::deserialize_reader(reader)?;
75 Ok(Response::ValueResponse(value))
76 }
77}
78
79impl<Ctx: Context> borsh::BorshSerialize for ValueResponse<Ctx>
80where
81 Ctx::Height: borsh::BorshSerialize,
82 RawDecidedValue<Ctx>: borsh::BorshSerialize,
83{
84 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
85 self.start_height.serialize(writer)?;
86 self.values.serialize(writer)?;
87 Ok(())
88 }
89}
90
91impl<Ctx: Context> borsh::BorshDeserialize for ValueResponse<Ctx>
92where
93 Ctx::Height: borsh::BorshDeserialize,
94 RawDecidedValue<Ctx>: borsh::BorshDeserialize,
95{
96 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
97 let start_height = Ctx::Height::deserialize_reader(reader)?;
98 let values = Vec::<RawDecidedValue<Ctx>>::deserialize_reader(reader)?;
99 Ok(ValueResponse {
100 start_height,
101 values,
102 })
103 }
104}
105
106impl<Ctx: Context> borsh::BorshSerialize for RawDecidedValue<Ctx>
107where
108 CommitCertificate<Ctx>: borsh::BorshSerialize,
109{
110 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
111 BorshSerialize::serialize(&self.value_bytes.to_vec(), writer)?;
112 self.certificate.serialize(writer)?;
113 Ok(())
114 }
115}
116
117impl<Ctx: Context> borsh::BorshDeserialize for RawDecidedValue<Ctx>
118where
119 CommitCertificate<Ctx>: borsh::BorshDeserialize,
120{
121 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
122 let value_bytes = Vec::<u8>::deserialize_reader(reader)?;
123 let certificate = CommitCertificate::deserialize_reader(reader)?;
124 Ok(RawDecidedValue {
125 value_bytes: value_bytes.into(),
126 certificate,
127 })
128 }
129}