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
//! Protocol logic specific to ICS4 messages of type `MsgChannelOpenTry`.

use crate::core::ics03_connection::connection::State as ConnectionState;
use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, State};
use crate::core::ics04_channel::context::ChannelReader;
use crate::core::ics04_channel::error::Error;
use crate::core::ics04_channel::handler::{ChannelIdState, ChannelResult};
use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry;
use crate::core::ics04_channel::Version;
use crate::core::ics24_host::identifier::ChannelId;
use crate::handler::{HandlerOutput, HandlerResult};
use crate::prelude::*;

/// Per our convention, this message is processed on chain B.
pub(crate) fn process<Ctx: ChannelReader>(
    ctx_b: &Ctx,
    msg: &MsgChannelOpenTry,
) -> HandlerResult<ChannelResult, Error> {
    let mut output = HandlerOutput::builder();

    // An IBC connection running on the local (host) chain should exist.
    if msg.chan_end_on_b.connection_hops().len() != 1 {
        return Err(Error::invalid_connection_hops_length(
            1,
            msg.chan_end_on_b.connection_hops().len(),
        ));
    }

    let conn_end_on_b = ctx_b.connection_end(&msg.chan_end_on_b.connection_hops()[0])?;
    if !conn_end_on_b.state_matches(&ConnectionState::Open) {
        return Err(Error::connection_not_open(
            msg.chan_end_on_b.connection_hops()[0].clone(),
        ));
    }

    let conn_version = match conn_end_on_b.versions() {
        [version] => version,
        _ => return Err(Error::invalid_version_length_connection()),
    };

    let channel_feature = msg.chan_end_on_b.ordering().to_string();
    if !conn_version.is_supported_feature(channel_feature) {
        return Err(Error::channel_feature_not_suported_by_connection());
    }

    // Verify proofs
    {
        let client_id_on_b = conn_end_on_b.client_id().clone();
        let client_state_of_a_on_b = ctx_b.client_state(&client_id_on_b)?;
        let consensus_state_of_a_on_b =
            ctx_b.client_consensus_state(&client_id_on_b, msg.proof_height_on_a)?;
        let prefix_on_a = conn_end_on_b.counterparty().prefix();
        let port_id_on_a = &msg.chan_end_on_b.counterparty().port_id;
        let chan_id_on_a = msg
            .chan_end_on_b
            .counterparty()
            .channel_id()
            .ok_or_else(Error::invalid_counterparty_channel_id)?;
        let conn_id_on_a = conn_end_on_b
            .counterparty()
            .connection_id()
            .ok_or_else(|| {
                Error::undefined_connection_counterparty(
                    msg.chan_end_on_b.connection_hops()[0].clone(),
                )
            })?;

        // The client must not be frozen.
        if client_state_of_a_on_b.is_frozen() {
            return Err(Error::frozen_client(client_id_on_b));
        }

        let expected_chan_end_on_a = ChannelEnd::new(
            State::Init,
            *msg.chan_end_on_b.ordering(),
            Counterparty::new(msg.port_id_on_b.clone(), None),
            vec![conn_id_on_a.clone()],
            msg.version_on_a.clone(),
        );

        // Verify the proof for the channel state against the expected channel end.
        // A counterparty channel id of None in not possible, and is checked by validate_basic in msg.
        client_state_of_a_on_b
            .verify_channel_state(
                msg.proof_height_on_a,
                prefix_on_a,
                &msg.proof_chan_end_on_a,
                consensus_state_of_a_on_b.root(),
                port_id_on_a,
                chan_id_on_a,
                &expected_chan_end_on_a,
            )
            .map_err(Error::verify_channel_failed)?;
    }

    let chan_end_on_b = ChannelEnd::new(
        State::TryOpen,
        *msg.chan_end_on_b.ordering(),
        msg.chan_end_on_b.counterparty().clone(),
        msg.chan_end_on_b.connection_hops().clone(),
        // Note: This will be rewritten by the module callback
        Version::empty(),
    );

    let chan_id_on_b = ChannelId::new(ctx_b.channel_counter()?);

    output.log(format!(
        "success: channel open try with channel identifier: {}",
        chan_id_on_b
    ));

    let result = ChannelResult {
        port_id: msg.port_id_on_b.clone(),
        channel_id: chan_id_on_b,
        channel_end: chan_end_on_b,
        channel_id_state: ChannelIdState::Generated,
    };

    Ok(output.with_result(result))
}

#[cfg(test)]
mod tests {
    use crate::core::ics04_channel::handler::chan_open_try;
    use crate::downcast;
    use crate::prelude::*;

    use test_log::test;

    use crate::core::ics02_client::error as ics02_error;
    use crate::core::ics03_connection::connection::ConnectionEnd;
    use crate::core::ics03_connection::connection::Counterparty as ConnectionCounterparty;
    use crate::core::ics03_connection::connection::State as ConnectionState;
    use crate::core::ics03_connection::error as ics03_error;
    use crate::core::ics03_connection::msgs::test_util::get_dummy_raw_counterparty;
    use crate::core::ics03_connection::version::get_compatible_versions;
    use crate::core::ics04_channel::channel::{ChannelEnd, State};
    use crate::core::ics04_channel::error;
    use crate::core::ics04_channel::msgs::chan_open_try::test_util::get_dummy_raw_msg_chan_open_try;
    use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry;
    use crate::core::ics04_channel::msgs::ChannelMsg;
    use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId};
    use crate::mock::client_state::client_type as mock_client_type;
    use crate::mock::context::MockContext;
    use crate::timestamp::ZERO_DURATION;
    use crate::Height;

    #[test]
    fn chan_open_try_msg_processing() {
        struct Test {
            name: String,
            ctx: MockContext,
            msg: ChannelMsg,
            want_pass: bool,
            match_error: Box<dyn FnOnce(error::ErrorDetail)>,
        }

        // Some general-purpose variable to parametrize the messages and the context.
        let proof_height = 10;
        let conn_id = ConnectionId::new(2);
        let client_id = ClientId::new(mock_client_type(), 45).unwrap();

        // The context. We'll reuse this same one across all tests.
        let context = MockContext::default();

        // This is the connection underlying the channel we're trying to open.
        let conn_end = ConnectionEnd::new(
            ConnectionState::Open,
            client_id.clone(),
            ConnectionCounterparty::try_from(get_dummy_raw_counterparty()).unwrap(),
            get_compatible_versions(),
            ZERO_DURATION,
        );

        // We're going to test message processing against this message.
        let mut msg =
            MsgChannelOpenTry::try_from(get_dummy_raw_msg_chan_open_try(proof_height)).unwrap();

        let chan_id = ChannelId::new(24);
        let hops = vec![conn_id.clone()];
        msg.chan_end_on_b.connection_hops = hops;

        // A preloaded channel end that resides in the context. This is constructed so as to be
        // consistent with the incoming ChanOpenTry message `msg`.
        let correct_chan_end = ChannelEnd::new(
            State::Init,
            *msg.chan_end_on_b.ordering(),
            msg.chan_end_on_b.counterparty().clone(),
            msg.chan_end_on_b.connection_hops().clone(),
            msg.chan_end_on_b.version().clone(),
        );

        let tests: Vec<Test> = vec![
            Test {
                name: "Processing fails because no connection exists in the context".to_string(),
                ctx: context.clone(),
                msg: ChannelMsg::ChannelOpenTry(msg.clone()),
                want_pass: false,
                match_error: {
                    let connection_id = msg.chan_end_on_b.connection_hops()[0].clone();
                    Box::new(move |e| match e {
                        error::ErrorDetail::Ics03Connection(e) => {
                            assert_eq!(
                                e.source,
                                ics03_error::ErrorDetail::ConnectionNotFound(
                                    ics03_error::ConnectionNotFoundSubdetail { connection_id }
                                )
                            );
                        }
                        _ => {
                            panic!("Expected MissingConnection, instead got {}", e)
                        }
                    })
                },
            },
            Test {
                name: "Processing fails b/c the context has no client state".to_string(),
                ctx: context
                    .clone()
                    .with_connection(conn_id.clone(), conn_end.clone())
                    .with_channel(
                        msg.port_id_on_b.clone(),
                        chan_id.clone(),
                        correct_chan_end.clone(),
                    ),
                msg: ChannelMsg::ChannelOpenTry(msg.clone()),
                want_pass: false,
                match_error: Box::new(|e| match e {
                    error::ErrorDetail::Ics03Connection(e) => {
                        assert_eq!(
                            e.source,
                            ics03_error::ErrorDetail::Ics02Client(
                                ics03_error::Ics02ClientSubdetail {
                                    source: ics02_error::ErrorDetail::ClientNotFound(
                                        ics02_error::ClientNotFoundSubdetail {
                                            client_id: ClientId::new(mock_client_type(), 45)
                                                .unwrap()
                                        }
                                    )
                                }
                            )
                        );
                    }
                    _ => {
                        panic!("Expected MissingClientState, instead got {}", e)
                    }
                }),
            },
            Test {
                name: "Processing is successful".to_string(),
                ctx: context
                    .clone()
                    .with_client(&client_id, Height::new(0, proof_height).unwrap())
                    .with_connection(conn_id.clone(), conn_end.clone())
                    .with_channel(msg.port_id_on_b.clone(), chan_id, correct_chan_end),
                msg: ChannelMsg::ChannelOpenTry(msg.clone()),
                want_pass: true,
                match_error: Box::new(|_| {}),
            },
            Test {
                name: "Processing is successful against an empty context (no preexisting channel)"
                    .to_string(),
                ctx: context
                    .with_client(&client_id, Height::new(0, proof_height).unwrap())
                    .with_connection(conn_id, conn_end),
                msg: ChannelMsg::ChannelOpenTry(msg),
                want_pass: true,
                match_error: Box::new(|_| {}),
            },
        ]
        .into_iter()
        .collect();

        for test in tests {
            let test_msg = downcast!(test.msg => ChannelMsg::ChannelOpenTry).unwrap();
            let res = chan_open_try::process(&test.ctx, &test_msg);
            // Additionally check the events and the output objects in the result.
            match res {
                Ok(proto_output) => {
                    assert!(
                        test.want_pass,
                        "chan_open_ack: test passed but was supposed to fail for test: {}, \nparams {:?} {:?}",
                        test.name,
                        test_msg,
                        test.ctx.clone()
                    );

                    // The object in the output is a channel end, should have TryOpen state.
                    assert_eq!(
                        proto_output.result.channel_end.state().clone(),
                        State::TryOpen
                    );
                }
                Err(e) => {
                    assert!(
                        !test.want_pass,
                        "chan_open_try: did not pass test: {}, \nparams:\n\tmsg={:?}\n\tcontext={:?}\nerror: {:?}",
                        test.name,
                        test_msg,
                        test.ctx.clone(),
                        e,
                    );

                    (test.match_error)(e.0);
                }
            }
        }
    }

    /// Addresses [issue 219](https://github.com/cosmos/ibc-rs/issues/219)
    #[test]
    fn chan_open_try_invalid_counterparty_channel_id() {
        let proof_height = 10;
        let conn_id = ConnectionId::new(2);
        let client_id = ClientId::new(mock_client_type(), 45).unwrap();

        // This is the connection underlying the channel we're trying to open.
        let conn_end = ConnectionEnd::new(
            ConnectionState::Open,
            client_id.clone(),
            ConnectionCounterparty::try_from(get_dummy_raw_counterparty()).unwrap(),
            get_compatible_versions(),
            ZERO_DURATION,
        );

        // We're going to test message processing against this message.
        // Note: we make the counterparty's channel_id `None`.
        let mut msg =
            MsgChannelOpenTry::try_from(get_dummy_raw_msg_chan_open_try(proof_height)).unwrap();
        msg.chan_end_on_b.remote.channel_id = None;

        let chan_id = ChannelId::new(24);
        let hops = vec![conn_id.clone()];
        msg.chan_end_on_b.connection_hops = hops;

        let chan_end = ChannelEnd::new(
            State::Init,
            *msg.chan_end_on_b.ordering(),
            msg.chan_end_on_b.counterparty().clone(),
            msg.chan_end_on_b.connection_hops().clone(),
            msg.chan_end_on_b.version().clone(),
        );
        let context = MockContext::default()
            .with_client(&client_id, Height::new(0, proof_height).unwrap())
            .with_connection(conn_id, conn_end)
            .with_channel(msg.port_id_on_b.clone(), chan_id, chan_end);

        // Makes sure we don't crash
        let _ = chan_open_try::process(&context, &msg);
    }
}