-
Notifications
You must be signed in to change notification settings - Fork 329
/
conn_open_try.rs
398 lines (358 loc) · 14.3 KB
/
conn_open_try.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use std::{
convert::{TryFrom, TryInto},
str::FromStr,
time::Duration,
};
use tendermint_proto::Protobuf;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry;
use crate::ics02_client::client_state::AnyClientState;
use crate::ics03_connection::connection::Counterparty;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::version::Version;
use crate::ics23_commitment::commitment::CommitmentProofBytes;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::proofs::{ConsensusProof, Proofs};
use crate::signer::Signer;
use crate::tx_msg::Msg;
use crate::Height;
pub const TYPE_URL: &str = "/ibc.core.connection.v1.MsgConnectionOpenTry";
///
/// Message definition `MsgConnectionOpenTry` (i.e., `ConnOpenTry` datagram).
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MsgConnectionOpenTry {
pub previous_connection_id: Option<ConnectionId>,
pub client_id: ClientId,
pub client_state: Option<AnyClientState>,
pub counterparty: Counterparty,
pub counterparty_versions: Vec<Version>,
pub proofs: Proofs,
pub delay_period: Duration,
pub signer: Signer,
}
impl MsgConnectionOpenTry {
/// Getter for accessing the previous connection identifier of this message.
pub fn previous_connection_id(&self) -> &Option<ConnectionId> {
&self.previous_connection_id
}
/// Getter for accessing the client identifier from this message.
pub fn client_id(&self) -> &ClientId {
&self.client_id
}
/// Getter for accessing the client state.
pub fn client_state(&self) -> Option<AnyClientState> {
self.client_state.clone()
}
/// Getter for accesing the whole counterparty of this message. Returns a `clone()`.
pub fn counterparty(&self) -> Counterparty {
self.counterparty.clone()
}
/// Getter for accessing the versions from this message. Returns a `clone()`.
pub fn counterparty_versions(&self) -> Vec<Version> {
self.counterparty_versions.clone()
}
/// Getter for accessing the proofs in this message.
pub fn proofs(&self) -> &Proofs {
&self.proofs
}
/// Getter for accessing the `consensus_height` field from this message. Returns the special
/// value `0` if this field is not set.
pub fn consensus_height(&self) -> Height {
match self.proofs.consensus_proof() {
None => Height::zero(),
Some(p) => p.height(),
}
}
}
impl Msg for MsgConnectionOpenTry {
type ValidationError = Error;
type Raw = RawMsgConnectionOpenTry;
fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
}
fn type_url(&self) -> String {
TYPE_URL.to_string()
}
}
impl Protobuf<RawMsgConnectionOpenTry> for MsgConnectionOpenTry {}
impl TryFrom<RawMsgConnectionOpenTry> for MsgConnectionOpenTry {
type Error = Error;
fn try_from(msg: RawMsgConnectionOpenTry) -> Result<Self, Self::Error> {
let previous_connection_id = Some(msg.previous_connection_id)
.filter(|x| !x.is_empty())
.map(|v| FromStr::from_str(v.as_str()))
.transpose()
.map_err(|e| Kind::IdentifierError.context(e))?;
let consensus_height = msg
.consensus_height
.ok_or(Kind::MissingConsensusHeight)?
.try_into() // Cast from the raw height type into the domain type.
.map_err(|e| Kind::InvalidProof.context(e))?;
let consensus_proof_obj = ConsensusProof::new(msg.proof_consensus.into(), consensus_height)
.map_err(|e| Kind::InvalidProof.context(e))?;
let proof_height = msg
.proof_height
.ok_or(Kind::MissingProofHeight)?
.try_into()
.map_err(|e| Kind::InvalidProof.context(e))?;
let client_proof = Some(msg.proof_client)
.filter(|x| !x.is_empty())
.map(CommitmentProofBytes::from);
let counterparty_versions = msg
.counterparty_versions
.into_iter()
.map(Version::try_from)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| Kind::InvalidVersion.context(e))?;
if counterparty_versions.is_empty() {
return Err(Kind::EmptyVersions
.context("empty counterparty versions in try message".to_string())
.into());
}
Ok(Self {
previous_connection_id,
client_id: msg
.client_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
client_state: msg
.client_state
.map(AnyClientState::try_from)
.transpose()
.map_err(|e| Kind::InvalidProof.context(e))?,
counterparty: msg
.counterparty
.ok_or(Kind::MissingCounterparty)?
.try_into()?,
counterparty_versions,
proofs: Proofs::new(
msg.proof_init.into(),
client_proof,
Some(consensus_proof_obj),
None,
proof_height,
)
.map_err(|e| Kind::InvalidProof.context(e))?,
delay_period: Duration::from_secs(msg.delay_period),
signer: msg.signer.into(),
})
}
}
impl From<MsgConnectionOpenTry> for RawMsgConnectionOpenTry {
fn from(ics_msg: MsgConnectionOpenTry) -> Self {
RawMsgConnectionOpenTry {
client_id: ics_msg.client_id.as_str().to_string(),
previous_connection_id: ics_msg
.previous_connection_id
.map_or_else(|| "".to_string(), |v| v.as_str().to_string()),
client_state: ics_msg
.client_state
.map_or_else(|| None, |v| Some(v.into())),
counterparty: Some(ics_msg.counterparty.into()),
delay_period: ics_msg.delay_period.as_secs(),
counterparty_versions: ics_msg
.counterparty_versions
.iter()
.map(|v| v.clone().into())
.collect(),
proof_height: Some(ics_msg.proofs.height().into()),
proof_init: ics_msg.proofs.object_proof().clone().into(),
proof_client: ics_msg
.proofs
.client_proof()
.clone()
.map_or_else(Vec::new, |v| v.into()),
proof_consensus: ics_msg
.proofs
.consensus_proof()
.map_or_else(Vec::new, |v| v.proof().clone().into()),
consensus_height: ics_msg
.proofs
.consensus_proof()
.map_or_else(|| None, |h| Some(h.height().into())),
signer: ics_msg.signer.to_string(),
}
}
}
#[cfg(test)]
pub mod test_util {
use ibc_proto::ibc::core::client::v1::Height;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry;
use crate::ics03_connection::msgs::conn_open_try::MsgConnectionOpenTry;
use crate::ics03_connection::msgs::test_util::get_dummy_raw_counterparty;
use crate::ics03_connection::version::get_compatible_versions;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::test_utils::{get_dummy_bech32_account, get_dummy_proof};
/// Testing-specific helper methods.
impl MsgConnectionOpenTry {
/// Moves the given message into another one, and updates the `previous_connection_id` field.
pub fn with_previous_connection_id(
self,
previous_connection_id: Option<ConnectionId>,
) -> MsgConnectionOpenTry {
MsgConnectionOpenTry {
previous_connection_id,
..self
}
}
/// Setter for `client_id`.
pub fn with_client_id(self, client_id: ClientId) -> MsgConnectionOpenTry {
MsgConnectionOpenTry { client_id, ..self }
}
}
/// Returns a dummy `RawMsgConnectionOpenTry` with parametrized heights. The parameter
/// `proof_height` represents the height, on the source chain, at which this chain produced the
/// proof. Parameter `consensus_height` represents the height of destination chain which a
/// client on the source chain stores.
pub fn get_dummy_raw_msg_conn_open_try(
proof_height: u64,
consensus_height: u64,
) -> RawMsgConnectionOpenTry {
RawMsgConnectionOpenTry {
client_id: ClientId::default().to_string(),
previous_connection_id: ConnectionId::default().to_string(),
client_state: None,
counterparty: Some(get_dummy_raw_counterparty()),
delay_period: 0,
counterparty_versions: get_compatible_versions()
.iter()
.map(|v| v.clone().into())
.collect(),
proof_init: get_dummy_proof(),
proof_height: Some(Height {
revision_number: 0,
revision_height: proof_height,
}),
proof_consensus: get_dummy_proof(),
consensus_height: Some(Height {
revision_number: 0,
revision_height: consensus_height,
}),
proof_client: vec![],
signer: get_dummy_bech32_account(),
}
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use ibc_proto::ibc::core::client::v1::Height;
use ibc_proto::ibc::core::connection::v1::Counterparty as RawCounterparty;
use ibc_proto::ibc::core::connection::v1::MsgConnectionOpenTry as RawMsgConnectionOpenTry;
use crate::ics03_connection::msgs::conn_open_try::test_util::get_dummy_raw_msg_conn_open_try;
use crate::ics03_connection::msgs::conn_open_try::MsgConnectionOpenTry;
use crate::ics03_connection::msgs::test_util::get_dummy_raw_counterparty;
#[test]
fn parse_connection_open_try_msg() {
#[derive(Clone, Debug, PartialEq)]
struct Test {
name: String,
raw: RawMsgConnectionOpenTry,
want_pass: bool,
}
let default_try_msg = get_dummy_raw_msg_conn_open_try(10, 34);
let tests: Vec<Test> =
vec![
Test {
name: "Good parameters".to_string(),
raw: default_try_msg.clone(),
want_pass: true,
},
Test {
name: "Bad client id, name too short".to_string(),
raw: RawMsgConnectionOpenTry {
client_id: "client".to_string(),
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad destination connection id, name too long".to_string(),
raw: RawMsgConnectionOpenTry {
counterparty: Some(RawCounterparty {
connection_id:
"abcdasdfasdfsdfasfdwefwfsdfsfsfasfwewvxcvdvwgadvaadsefghijklmnopqrstu"
.to_string(),
..get_dummy_raw_counterparty()
}),
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Correct destination client id with lower/upper case and special chars"
.to_string(),
raw: RawMsgConnectionOpenTry {
counterparty: Some(RawCounterparty {
client_id: "ClientId_".to_string(),
..get_dummy_raw_counterparty()
}),
..default_try_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad counterparty versions, empty versions vec".to_string(),
raw: RawMsgConnectionOpenTry {
counterparty_versions: vec![],
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad counterparty versions, empty version string".to_string(),
raw: RawMsgConnectionOpenTry {
counterparty_versions: vec![],
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad proof height, height is 0".to_string(),
raw: RawMsgConnectionOpenTry {
proof_height: Some(Height { revision_number: 1, revision_height: 0 }),
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad consensus height, height is 0".to_string(),
raw: RawMsgConnectionOpenTry {
proof_height: Some(Height { revision_number: 1, revision_height: 0 }),
..default_try_msg.clone()
},
want_pass: false,
},
Test {
name: "Empty proof".to_string(),
raw: RawMsgConnectionOpenTry {
proof_init: b"".to_vec(),
..default_try_msg
},
want_pass: false,
}
]
.into_iter()
.collect();
for test in tests {
let msg = MsgConnectionOpenTry::try_from(test.raw.clone());
assert_eq!(
test.want_pass,
msg.is_ok(),
"MsgConnOpenTry::new failed for test {}, \nmsg {:?} with error {:?}",
test.name,
test.raw,
msg.err(),
);
}
}
#[test]
fn to_and_from() {
let raw = get_dummy_raw_msg_conn_open_try(10, 34);
let msg = MsgConnectionOpenTry::try_from(raw.clone()).unwrap();
let raw_back = RawMsgConnectionOpenTry::from(msg.clone());
let msg_back = MsgConnectionOpenTry::try_from(raw_back.clone()).unwrap();
assert_eq!(raw, raw_back);
assert_eq!(msg, msg_back);
}
}