-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmod.rs
More file actions
322 lines (286 loc) · 9.13 KB
/
mod.rs
File metadata and controls
322 lines (286 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use self::schema::witnet;
use crate::types::IpAddress;
use crate::{chain, types};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use failure::{ensure, format_err, Error};
use protobuf::Message;
use std::convert::TryFrom;
use std::fmt::Debug;
pub mod schema;
/// Used for establishing correspondence between rust struct
/// and protobuf rust struct
pub trait ProtobufConvert: Sized {
/// Type of the protobuf clone of Self
type ProtoStruct;
/// Struct -> ProtoStruct
fn to_pb(&self) -> Self::ProtoStruct;
/// ProtoStruct -> Struct
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error>;
/// Struct -> ProtoStruct -> Bytes
fn to_pb_bytes(&self) -> Result<Vec<u8>, Error>
where
Self::ProtoStruct: Message,
{
// Serialize
self.to_pb().write_to_bytes().map_err(Into::into)
}
/// Bytes -> ProtoStruct -> Struct
fn from_pb_bytes(bytes: &[u8]) -> Result<Self, Error>
where
Self::ProtoStruct: Message,
{
// Deserialize
let mut a = Self::ProtoStruct::new();
a.merge_from_bytes(bytes)?;
Self::from_pb(a)
}
}
impl ProtobufConvert for chain::RADType {
type ProtoStruct = witnet::DataRequestOutput_RADRequest_RADType;
fn to_pb(&self) -> Self::ProtoStruct {
match self {
chain::RADType::Unknown => witnet::DataRequestOutput_RADRequest_RADType::Unknown,
chain::RADType::HttpGet => witnet::DataRequestOutput_RADRequest_RADType::HttpGet,
chain::RADType::Rng => witnet::DataRequestOutput_RADRequest_RADType::Rng,
chain::RADType::HttpPost => witnet::DataRequestOutput_RADRequest_RADType::HttpPost,
chain::RADType::HttpHead => witnet::DataRequestOutput_RADRequest_RADType::HttpHead,
}
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(match pb {
witnet::DataRequestOutput_RADRequest_RADType::Unknown => chain::RADType::Unknown,
witnet::DataRequestOutput_RADRequest_RADType::HttpGet => chain::RADType::HttpGet,
witnet::DataRequestOutput_RADRequest_RADType::Rng => chain::RADType::Rng,
witnet::DataRequestOutput_RADRequest_RADType::HttpPost => chain::RADType::HttpPost,
witnet::DataRequestOutput_RADRequest_RADType::HttpHead => chain::RADType::HttpHead,
})
}
}
impl ProtobufConvert for chain::PublicKey {
type ProtoStruct = witnet::PublicKey;
fn to_pb(&self) -> Self::ProtoStruct {
let mut m = witnet::PublicKey::new();
let mut v = vec![];
v.extend([self.compressed]);
v.extend(self.bytes);
m.set_public_key(v);
m
}
fn from_pb(mut pb: Self::ProtoStruct) -> Result<Self, Error> {
let v = pb.take_public_key();
ensure!(v.len() == 33, "Invalid array length");
let mut bytes = [0; 32];
bytes.copy_from_slice(&v[1..]);
Ok(Self {
compressed: v[0],
bytes,
})
}
}
impl ProtobufConvert for types::Address {
type ProtoStruct = witnet::Address;
fn to_pb(&self) -> Self::ProtoStruct {
let mut address = witnet::Address::new();
let mut bytes = vec![];
match self.ip {
IpAddress::Ipv4 { ip } => {
bytes.write_u32::<BigEndian>(ip).unwrap();
}
IpAddress::Ipv6 { ip0, ip1, ip2, ip3 } => {
bytes.write_u32::<BigEndian>(ip0).unwrap();
bytes.write_u32::<BigEndian>(ip1).unwrap();
bytes.write_u32::<BigEndian>(ip2).unwrap();
bytes.write_u32::<BigEndian>(ip3).unwrap();
}
}
bytes.write_u16::<BigEndian>(self.port).unwrap();
address.set_address(bytes);
address
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
let mut bytes = pb.get_address();
match bytes.len() {
6 => {
// Ipv4
let ip = bytes.read_u32::<BigEndian>()?;
let ip = types::IpAddress::Ipv4 { ip };
let port = bytes.read_u16::<BigEndian>()?;
Ok(types::Address { ip, port })
}
18 => {
// Ipv6
let ip0 = bytes.read_u32::<BigEndian>()?;
let ip1 = bytes.read_u32::<BigEndian>()?;
let ip2 = bytes.read_u32::<BigEndian>()?;
let ip3 = bytes.read_u32::<BigEndian>()?;
let port = bytes.read_u16::<BigEndian>()?;
let ip = types::IpAddress::Ipv6 { ip0, ip1, ip2, ip3 };
Ok(types::Address { ip, port })
}
_ => Err(format_err!("Invalid address size")),
}
}
}
impl ProtobufConvert for String {
type ProtoStruct = Self;
fn to_pb(&self) -> Self::ProtoStruct {
self.clone()
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(pb)
}
}
impl<T> ProtobufConvert for Vec<T>
where
T: ProtobufConvert,
{
type ProtoStruct = Vec<T::ProtoStruct>;
fn to_pb(&self) -> Self::ProtoStruct {
self.iter().map(ProtobufConvert::to_pb).collect()
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
pb.into_iter()
.map(ProtobufConvert::from_pb)
.collect::<Result<Vec<_>, _>>()
}
}
impl ProtobufConvert for Vec<u8> {
type ProtoStruct = Self;
fn to_pb(&self) -> Self::ProtoStruct {
self.clone()
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(pb)
}
}
impl ProtobufConvert for [u8; 20] {
type ProtoStruct = Vec<u8>;
fn to_pb(&self) -> Self::ProtoStruct {
self.to_vec()
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(pb.len() == 20, "Invalid array length");
let mut x = [0; 20];
x.copy_from_slice(&pb);
Ok(x)
}
}
impl ProtobufConvert for [u8; 32] {
type ProtoStruct = Vec<u8>;
fn to_pb(&self) -> Self::ProtoStruct {
self.to_vec()
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(pb.len() == 32, "Invalid array length");
let mut x = [0; 32];
x.copy_from_slice(&pb);
Ok(x)
}
}
impl<T> ProtobufConvert for Option<T>
where
T: ProtobufConvert + Default + Eq + Debug,
{
type ProtoStruct = <T as ProtobufConvert>::ProtoStruct;
fn to_pb(&self) -> Self::ProtoStruct {
match self {
Some(x) => x.to_pb(),
None => T::default().to_pb(),
}
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
let res = T::from_pb(pb)?;
if res == T::default() {
Ok(None)
} else {
Ok(Some(res))
}
}
}
macro_rules! impl_protobuf_convert_scalar {
($name:tt) => {
impl ProtobufConvert for $name {
type ProtoStruct = $name;
fn to_pb(&self) -> Self::ProtoStruct {
*self
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok(pb)
}
}
};
}
impl_protobuf_convert_scalar!(bool);
impl_protobuf_convert_scalar!(u32);
impl_protobuf_convert_scalar!(u64);
impl_protobuf_convert_scalar!(i32);
impl_protobuf_convert_scalar!(i64);
impl_protobuf_convert_scalar!(f32);
impl_protobuf_convert_scalar!(f64);
// Conflicts with Vec<u8>
/*
impl ProtobufConvert for u8 {
type ProtoStruct = u32;
fn to_pb(&self) -> Self::ProtoStruct {
Self::ProtoStruct::from(*self)
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
"Integer out of range"
);
Ok(pb as Self)
}
}
*/
impl ProtobufConvert for i8 {
type ProtoStruct = i32;
fn to_pb(&self) -> Self::ProtoStruct {
Self::ProtoStruct::from(*self)
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
}
}
impl ProtobufConvert for u16 {
type ProtoStruct = u32;
fn to_pb(&self) -> Self::ProtoStruct {
Self::ProtoStruct::from(*self)
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
}
}
impl ProtobufConvert for i16 {
type ProtoStruct = i32;
fn to_pb(&self) -> Self::ProtoStruct {
Self::ProtoStruct::from(*self)
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
}
}
impl ProtobufConvert for (String, String) {
type ProtoStruct = witnet::StringPair;
fn to_pb(&self) -> Self::ProtoStruct {
let mut pb = Self::ProtoStruct::new();
pb.set_left(self.0.clone());
pb.set_right(self.1.clone());
pb
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
Ok((pb.get_left().to_string(), pb.get_right().to_string()))
}
}