-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathchan_open_init.rs
186 lines (157 loc) · 5.79 KB
/
chan_open_init.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
use crate::address::{account_to_string, string_to_account};
use crate::ics04_channel::channel::ChannelEnd;
use crate::ics04_channel::error::{Error, Kind};
use crate::ics24_host::identifier::PortId;
use crate::tx_msg::Msg;
use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit;
use tendermint::account::Id as AccountId;
use tendermint_proto::Protobuf;
use std::convert::{TryFrom, TryInto};
pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelOpenInit";
///
/// Message definition for the first step in the channel open handshake (`ChanOpenInit` datagram).
///
#[derive(Clone, Debug, PartialEq)]
pub struct MsgChannelOpenInit {
pub port_id: PortId,
pub channel: ChannelEnd,
pub signer: AccountId,
}
impl MsgChannelOpenInit {
/// Getter: borrow the `port_id` from this message.
pub fn port_id(&self) -> &PortId {
&self.port_id
}
/// Getter: borrow the `channelEnd` from this message.
pub fn channel(&self) -> &ChannelEnd {
&self.channel
}
// /// Setter for `connection_id`. Amenable to chaining, since it consumes the input message.
// pub fn with_connection_id(self, connection_hops: Vec<ConnectionId>) -> Self {
// MsgChannelOpenInit { channel.connection_hops, ..self }
// }
}
impl Msg for MsgChannelOpenInit {
type ValidationError = Error;
fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
}
fn validate_basic(&self) -> Result<(), Self::ValidationError> {
self.channel.validate_basic()
}
fn type_url(&self) -> String {
TYPE_URL.to_string()
}
fn get_signers(&self) -> Vec<AccountId> {
vec![self.signer]
}
}
impl Protobuf<RawMsgChannelOpenInit> for MsgChannelOpenInit {}
impl TryFrom<RawMsgChannelOpenInit> for MsgChannelOpenInit {
type Error = anomaly::Error<Kind>;
fn try_from(raw_msg: RawMsgChannelOpenInit) -> Result<Self, Self::Error> {
let signer =
string_to_account(raw_msg.signer).map_err(|e| Kind::InvalidSigner.context(e))?;
Ok(MsgChannelOpenInit {
port_id: raw_msg
.port_id
.parse()
.map_err(|e| Kind::IdentifierError.context(e))?,
channel: raw_msg.channel.ok_or(Kind::MissingChannel)?.try_into()?,
signer,
})
}
}
impl From<MsgChannelOpenInit> for RawMsgChannelOpenInit {
fn from(domain_msg: MsgChannelOpenInit) -> Self {
RawMsgChannelOpenInit {
port_id: domain_msg.port_id.to_string(),
channel: Some(domain_msg.channel.into()),
signer: account_to_string(domain_msg.signer).unwrap(),
}
}
}
#[cfg(test)]
pub mod test_util {
use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit;
use crate::ics04_channel::channel::test_util::get_dummy_raw_channel_end;
use crate::ics04_channel::channel::test_util::get_dummy_raw_channel_end_with_missing_connection;
use crate::test_utils::get_dummy_bech32_account;
//use crate::ics04_channel::channel::State;
/// Returns a dummy `RawMsgChannelOpenInit`, for testing only!
pub fn get_dummy_raw_msg_chan_open_init() -> RawMsgChannelOpenInit {
RawMsgChannelOpenInit {
port_id: "port".to_string(),
channel: Some(get_dummy_raw_channel_end()),
signer: get_dummy_bech32_account(),
}
}
pub fn get_dummy_raw_msg_chan_open_init_with_missing_connection() -> RawMsgChannelOpenInit {
RawMsgChannelOpenInit {
port_id: "port".to_string(),
channel: Some(get_dummy_raw_channel_end_with_missing_connection()),
signer: get_dummy_bech32_account(),
}
}
}
#[cfg(test)]
mod tests {
use crate::ics04_channel::msgs::chan_open_init::test_util::get_dummy_raw_msg_chan_open_init;
use crate::ics04_channel::msgs::chan_open_init::MsgChannelOpenInit;
use ibc_proto::ibc::core::channel::v1::MsgChannelOpenInit as RawMsgChannelOpenInit;
use std::convert::TryFrom;
#[test]
fn channel_open_init_from_raw() {
struct Test {
name: String,
raw: RawMsgChannelOpenInit,
want_pass: bool,
}
let default_raw_msg = get_dummy_raw_msg_chan_open_init();
let tests: Vec<Test> = vec![
Test {
name: "Good parameters".to_string(),
raw: default_raw_msg.clone(),
want_pass: true,
},
Test {
name: "Incorrect port identifier, slash (separator) prohibited".to_string(),
raw: RawMsgChannelOpenInit {
port_id: "p34/".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Missing channel".to_string(),
raw: RawMsgChannelOpenInit {
channel: None,
..default_raw_msg
},
want_pass: false,
},
]
.into_iter()
.collect();
for test in tests {
let res_msg = MsgChannelOpenInit::try_from(test.raw.clone());
assert_eq!(
test.want_pass,
res_msg.is_ok(),
"MsgChanOpenInit::try_from failed for test {}, \nraw msg {:?} with error {:?}",
test.name,
test.raw,
res_msg.err(),
);
}
}
#[test]
fn to_and_from() {
let raw = get_dummy_raw_msg_chan_open_init();
let msg = MsgChannelOpenInit::try_from(raw.clone()).unwrap();
let raw_back = RawMsgChannelOpenInit::from(msg.clone());
let msg_back = MsgChannelOpenInit::try_from(raw_back.clone()).unwrap();
assert_eq!(raw, raw_back);
assert_eq!(msg, msg_back);
}
}