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
//! Message definitions for all ICS4 domain types: channel open & close handshake datagrams, as well
//! as packets.

use crate::core::ics04_channel::error::Error;
use crate::core::ics04_channel::msgs::acknowledgement::MsgAcknowledgement;
use crate::core::ics04_channel::msgs::chan_close_confirm::MsgChannelCloseConfirm;
use crate::core::ics04_channel::msgs::chan_close_init::MsgChannelCloseInit;
use crate::core::ics04_channel::msgs::chan_open_ack::MsgChannelOpenAck;
use crate::core::ics04_channel::msgs::chan_open_confirm::MsgChannelOpenConfirm;
use crate::core::ics04_channel::msgs::chan_open_init::MsgChannelOpenInit;
use crate::core::ics04_channel::msgs::chan_open_try::MsgChannelOpenTry;
use crate::core::ics04_channel::msgs::recv_packet::MsgRecvPacket;
use crate::core::ics04_channel::msgs::timeout::MsgTimeout;
use crate::core::ics04_channel::msgs::timeout_on_close::MsgTimeoutOnClose;
use crate::core::ics26_routing::context::{Ics26Context, ModuleId};

// Opening handshake messages.
pub mod chan_open_ack;
pub mod chan_open_confirm;
pub mod chan_open_init;
pub mod chan_open_try;

// Closing handshake messages.
pub mod chan_close_confirm;
pub mod chan_close_init;

// Packet specific messages.
pub mod acknowledgement;
pub mod recv_packet;
pub mod timeout;
pub mod timeout_on_close;

/// Enumeration of all possible messages that the ICS4 protocol processes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ChannelMsg {
    ChannelOpenInit(MsgChannelOpenInit),
    ChannelOpenTry(MsgChannelOpenTry),
    ChannelOpenAck(MsgChannelOpenAck),
    ChannelOpenConfirm(MsgChannelOpenConfirm),
    ChannelCloseInit(MsgChannelCloseInit),
    ChannelCloseConfirm(MsgChannelCloseConfirm),
}

impl ChannelMsg {
    pub(super) fn lookup_module(&self, ctx: &impl Ics26Context) -> Result<ModuleId, Error> {
        let module_id = match self {
            ChannelMsg::ChannelOpenInit(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_a)
                .map_err(Error::ics05_port)?,
            ChannelMsg::ChannelOpenTry(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_b)
                .map_err(Error::ics05_port)?,
            ChannelMsg::ChannelOpenAck(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_a)
                .map_err(Error::ics05_port)?,
            ChannelMsg::ChannelOpenConfirm(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_b)
                .map_err(Error::ics05_port)?,
            ChannelMsg::ChannelCloseInit(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_a)
                .map_err(Error::ics05_port)?,
            ChannelMsg::ChannelCloseConfirm(msg) => ctx
                .lookup_module_by_port(&msg.port_id_on_b)
                .map_err(Error::ics05_port)?,
        };
        Ok(module_id)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PacketMsg {
    RecvPacket(MsgRecvPacket),
    AckPacket(MsgAcknowledgement),
    TimeoutPacket(MsgTimeout),
    TimeoutOnClosePacket(MsgTimeoutOnClose),
}