-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathpacket.rs
303 lines (279 loc) · 9.8 KB
/
packet.rs
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
use serde_derive::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use ibc_proto::ibc::core::channel::v1::Packet as RawPacket;
use crate::ics04_channel::error::Kind;
use crate::ics24_host::identifier::{ChannelId, PortId};
use crate::Height;
/// Enumeration of proof carrying ICS3 message, helper for relayer.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PacketMsgType {
Recv,
Ack,
Timeout,
}
/// The sequence number of a packet enforces ordering among packets from the same source.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct Sequence(u64);
impl From<u64> for Sequence {
fn from(seq: u64) -> Self {
Sequence(seq)
}
}
impl From<Sequence> for u64 {
fn from(s: Sequence) -> u64 {
s.0
}
}
impl std::fmt::Display for Sequence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0)
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct Packet {
pub sequence: Sequence,
pub source_port: PortId,
pub source_channel: ChannelId,
pub destination_port: PortId,
pub destination_channel: ChannelId,
pub data: Vec<u8>,
pub timeout_height: Height,
pub timeout_timestamp: u64,
}
impl std::fmt::Display for Packet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{:?} {:?} {:?}",
self.source_port, self.source_channel, self.sequence
)
}
}
impl Default for Packet {
fn default() -> Self {
Packet {
sequence: Sequence(0),
source_port: Default::default(),
source_channel: Default::default(),
destination_port: Default::default(),
destination_channel: Default::default(),
data: vec![],
timeout_height: Default::default(),
timeout_timestamp: 0,
}
}
}
impl TryFrom<RawPacket> for Packet {
type Error = anomaly::Error<Kind>;
fn try_from(raw_pkt: RawPacket) -> Result<Self, Self::Error> {
Ok(Packet {
sequence: Sequence::from(raw_pkt.sequence),
source_port: raw_pkt
.source_port
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
source_channel: raw_pkt
.source_channel
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
destination_port: raw_pkt
.destination_port
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
destination_channel: raw_pkt
.destination_channel
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
data: raw_pkt.data,
timeout_height: raw_pkt
.timeout_height
.ok_or(Kind::MissingHeight)?
.try_into()
.map_err(|e| Kind::InvalidTimeoutHeight.context(e))?,
timeout_timestamp: raw_pkt.timeout_timestamp,
})
}
}
impl From<Packet> for RawPacket {
fn from(packet: Packet) -> Self {
RawPacket {
sequence: packet.sequence.0,
source_port: packet.source_port.to_string(),
source_channel: packet.source_channel.to_string(),
destination_port: packet.destination_port.to_string(),
destination_channel: packet.destination_channel.to_string(),
data: packet.data,
timeout_height: Some(packet.timeout_height.into()),
timeout_timestamp: packet.timeout_timestamp,
}
}
}
#[cfg(test)]
pub mod test_utils {
use ibc_proto::ibc::core::channel::v1::Packet as RawPacket;
use ibc_proto::ibc::core::client::v1::Height as RawHeight;
/// Returns a dummy `RawPacket`, for testing only!
pub fn get_dummy_raw_packet(timeout_height: u64) -> RawPacket {
RawPacket {
sequence: 0,
source_port: "sourceportid".to_string(),
source_channel: "srchannelid".to_string(),
destination_port: "destinationport".to_string(),
destination_channel: "dstchannelid".to_string(),
data: vec![],
timeout_height: Some(RawHeight {
revision_number: 1,
revision_height: timeout_height,
}),
timeout_timestamp: 0,
}
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use ibc_proto::ibc::core::channel::v1::Packet as RawPacket;
use crate::ics04_channel::packet::test_utils::get_dummy_raw_packet;
use crate::ics04_channel::packet::Packet;
#[test]
fn packet_try_from_raw() {
struct Test {
name: String,
raw: RawPacket,
want_pass: bool,
}
let proof_height = 10;
let default_raw_msg = get_dummy_raw_packet(proof_height);
let tests: Vec<Test> = vec![
Test {
name: "Good parameters".to_string(),
raw: default_raw_msg.clone(),
want_pass: true
},
Test {
name: "Src port validation: correct".to_string(),
raw: RawPacket {
source_port: "srcportp34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad src port, name too short".to_string(),
raw: RawPacket {
source_port: "p".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad src port, name too long".to_string(),
raw: RawPacket {
source_port: "abcdefghijasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadgasgasdfasdfasdfasdfaklmnopqrstu".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Dst port validation: correct".to_string(),
raw: RawPacket {
destination_port: "destportsrcp34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad dst port, name too short".to_string(),
raw: RawPacket {
destination_port: "p".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad dst port, name too long".to_string(),
raw: RawPacket {
destination_port: "abcdefghijasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadgasgasdfasdfasdfasdfaklmnopqrstu".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Src channel validation: correct".to_string(),
raw: RawPacket {
source_channel: "srcchannelp34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad src channel, name too short".to_string(),
raw: RawPacket {
source_channel: "p".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad src channel, name too long".to_string(),
raw: RawPacket {
source_channel: "abcdefghijasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadgasgasdfasdfasdfasdfaklmnopqrstu".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Dst channel validation: correct".to_string(),
raw: RawPacket {
destination_channel: "dstchannelp34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad dst channel, name too short".to_string(),
raw: RawPacket {
destination_channel: "p".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad dst channel, name too long".to_string(),
raw: RawPacket {
destination_channel: "abcdefghijasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadgasgasdfasdfasdfasdfaklmnopqrstu".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Missing timeout height".to_string(),
raw: RawPacket {
timeout_height: None,
..default_raw_msg
},
want_pass: false,
},
];
for test in tests {
let res_msg = Packet::try_from(test.raw.clone());
assert_eq!(
test.want_pass,
res_msg.is_ok(),
"Packet::try_from failed for test {}, \nraw packet {:?} with error {:?}",
test.name,
test.raw,
res_msg.err(),
);
}
}
#[test]
fn to_and_from() {
let raw = get_dummy_raw_packet(15);
let msg = Packet::try_from(raw.clone()).unwrap();
let raw_back = RawPacket::from(msg.clone());
let msg_back = Packet::try_from(raw_back.clone()).unwrap();
assert_eq!(raw, raw_back);
assert_eq!(msg, msg_back);
}
}