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
use crate::prelude::*;
use tendermint_proto::Protobuf;
use ibc_proto::ibc::core::channel::v1::MsgChannelCloseInit as RawMsgChannelCloseInit;
use crate::core::ics04_channel::error::Error;
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::signer::Signer;
use crate::tx_msg::Msg;
pub const TYPE_URL: &str = "/ibc.core.channel.v1.MsgChannelCloseInit";
#[derive(Clone, Debug, PartialEq)]
pub struct MsgChannelCloseInit {
pub port_id: PortId,
pub channel_id: ChannelId,
pub signer: Signer,
}
impl MsgChannelCloseInit {
pub fn new(port_id: PortId, channel_id: ChannelId, signer: Signer) -> Self {
Self {
port_id,
channel_id,
signer,
}
}
pub fn port_id(&self) -> &PortId {
&self.port_id
}
pub fn channel_id(&self) -> &ChannelId {
&self.channel_id
}
}
impl Msg for MsgChannelCloseInit {
type ValidationError = Error;
type Raw = RawMsgChannelCloseInit;
fn route(&self) -> String {
crate::keys::ROUTER_KEY.to_string()
}
fn type_url(&self) -> String {
TYPE_URL.to_string()
}
}
impl Protobuf<RawMsgChannelCloseInit> for MsgChannelCloseInit {}
impl TryFrom<RawMsgChannelCloseInit> for MsgChannelCloseInit {
type Error = Error;
fn try_from(raw_msg: RawMsgChannelCloseInit) -> Result<Self, Self::Error> {
Ok(MsgChannelCloseInit {
port_id: raw_msg.port_id.parse().map_err(Error::identifier)?,
channel_id: raw_msg.channel_id.parse().map_err(Error::identifier)?,
signer: raw_msg.signer.into(),
})
}
}
impl From<MsgChannelCloseInit> for RawMsgChannelCloseInit {
fn from(domain_msg: MsgChannelCloseInit) -> Self {
RawMsgChannelCloseInit {
port_id: domain_msg.port_id.to_string(),
channel_id: domain_msg.channel_id.to_string(),
signer: domain_msg.signer.to_string(),
}
}
}
#[cfg(test)]
pub mod test_util {
use crate::prelude::*;
use ibc_proto::ibc::core::channel::v1::MsgChannelCloseInit as RawMsgChannelCloseInit;
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::test_utils::get_dummy_bech32_account;
pub fn get_dummy_raw_msg_chan_close_init() -> RawMsgChannelCloseInit {
RawMsgChannelCloseInit {
port_id: PortId::default().to_string(),
channel_id: ChannelId::default().to_string(),
signer: get_dummy_bech32_account(),
}
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use test_log::test;
use ibc_proto::ibc::core::channel::v1::MsgChannelCloseInit as RawMsgChannelCloseInit;
use crate::core::ics04_channel::msgs::chan_close_init::test_util::get_dummy_raw_msg_chan_close_init;
use crate::core::ics04_channel::msgs::chan_close_init::MsgChannelCloseInit;
#[test]
fn parse_channel_close_init_msg() {
struct Test {
name: String,
raw: RawMsgChannelCloseInit,
want_pass: bool,
}
let default_raw_msg = get_dummy_raw_msg_chan_close_init();
let tests: Vec<Test> = vec![
Test {
name: "Good parameters".to_string(),
raw: default_raw_msg.clone(),
want_pass: true,
},
Test {
name: "Correct port".to_string(),
raw: RawMsgChannelCloseInit {
port_id: "p34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad port, name too short".to_string(),
raw: RawMsgChannelCloseInit {
port_id: "p".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad port, name too long".to_string(),
raw: RawMsgChannelCloseInit {
port_id: "abcdefsdfasdfasdfasdfasdfasdfadsfasdgafsgadfasdfasdfasdfsdfasdfaghijklmnopqrstuabcdefsdfasdfasdfasdfasdfasdfadsfasdgafsgadfasdfasdfasdfsdfasdfaghijklmnopqrstu".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Correct channel identifier".to_string(),
raw: RawMsgChannelCloseInit {
channel_id: "channelid34".to_string(),
..default_raw_msg.clone()
},
want_pass: true,
},
Test {
name: "Bad channel, name too short".to_string(),
raw: RawMsgChannelCloseInit {
channel_id: "chshort".to_string(),
..default_raw_msg.clone()
},
want_pass: false,
},
Test {
name: "Bad channel, name too long".to_string(),
raw: RawMsgChannelCloseInit {
channel_id: "abcdeasdfasdfasdfasdfasdfasdfasdfasdfdgasdfasdfasdfghijklmnopqrstu".to_string(),
..default_raw_msg
},
want_pass: false,
},
]
.into_iter()
.collect();
for test in tests {
let msg = MsgChannelCloseInit::try_from(test.raw.clone());
assert_eq!(
test.want_pass,
msg.is_ok(),
"MsgChanCloseInit::try_from failed for test {}, \nmsg {:?} with error {:?}",
test.name,
test.raw,
msg.err(),
);
}
}
#[test]
fn to_and_from() {
let raw = get_dummy_raw_msg_chan_close_init();
let msg = MsgChannelCloseInit::try_from(raw.clone()).unwrap();
let raw_back = RawMsgChannelCloseInit::from(msg.clone());
let msg_back = MsgChannelCloseInit::try_from(raw_back.clone()).unwrap();
assert_eq!(raw, raw_back);
assert_eq!(msg, msg_back);
}
}