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
use core::time::Duration;

use flex_error::{define_error, ErrorMessageTracer};

use ibc_relayer_types::core::ics02_client::error::Error as ClientError;
use ibc_relayer_types::core::ics04_channel::channel::State;
use ibc_relayer_types::core::ics24_host::identifier::{
    ChainId, ChannelId, ClientId, PortChannelId, PortId,
};
use ibc_relayer_types::events::IbcEvent;

use crate::error::Error as RelayerError;
use crate::foreign_client::{ForeignClientError, HasExpiredOrFrozenError};
use crate::supervisor::Error as SupervisorError;

define_error! {
    ChannelError {
        Relayer
            [ RelayerError ]
            |_| { "relayer error" },

        Supervisor
            [ SupervisorError ]
            |_| { "supervisor error" },

        Client
            [ ClientError ]
            |_| { "ICS02 client error" },

        InvalidChannel
            { reason: String }
            | e | {
                format_args!("invalid channel: {0}",
                    e.reason)
            },

        MissingLocalChannelId
            |_| { "failed due to missing local channel id" },

        MissingLocalConnection
            { chain_id: ChainId }
            | e | {
                format_args!("channel constructor failed due to missing connection id on chain id {0}",
                    e.chain_id)
            },

        MissingCounterpartyChannelId
            |_| { "failed due to missing counterparty channel id" },

        MissingCounterpartyConnection
            |_| { "failed due to missing counterparty connection" },

        MissingChannelOnDestination
            |_| { "missing channel on destination chain" },

        ChannelProof
            [ RelayerError ]
            |_| { "failed to build channel proofs" },

        ClientOperation
            {
                client_id: ClientId,
                chain_id: ChainId,
            }
            [ ForeignClientError ]
            | e | {
                format_args!("failed during an operation on client '{0}' hosted by chain '{1}'",
                    e.client_id, e.chain_id)
            },

        FetchSigner
            { chain_id: ChainId }
            [ RelayerError ]
            |e| { format_args!("failed while fetching the signer for destination chain '{}'", e.chain_id) },

        Query
            { chain_id: ChainId }
            [ RelayerError ]
            |e| { format_args!("failed during a query to chain '{0}'", e.chain_id) },

        ChainQuery
            { chain_id: ChainId }
            [ RelayerError ]
            |e| {
                format!("failed during a query to chain id {0}", e.chain_id)
            },

        QueryChannel
            { channel_id: ChannelId }
            [ SupervisorError ]
            |e| { format_args!("failed during a query to channel '{0}'", e.channel_id) },

        Submit
            { chain_id: ChainId }
            [ RelayerError ]
            |e| { format_args!("failed during a transaction submission step to chain '{0}'", e.chain_id) },

        HandshakeFinalize
            |_| { "continue handshake" },

        PartialOpenHandshake
            {
                state: State,
                counterparty_state: State
            }
            | e | {
                format_args!("the channel is partially open ({0}, {1})",
                    e.state, e.counterparty_state)
            },

        IncompleteChannelState
            {
                chain_id: ChainId,
                port_channel_id: PortChannelId,
            }
            | e | {
                format_args!("channel '{0}' on chain '{1}' has no counterparty channel id",
                    e.port_channel_id, e.chain_id)
            },

        ChannelAlreadyExist
            { channel_id: ChannelId }
            |e| { format_args!("channel '{}' already exist in an incompatible state", e.channel_id) },

        MismatchChannelEnds
            {
                chain_id: ChainId,
                port_channel_id: PortChannelId,
                expected_counterrparty_port_channel_id: PortChannelId,
                actual_counterrparty_port_channel_id: PortChannelId,
            }
            | e | {
                format_args!("channel '{0}' on chain '{1}' expected to have counterparty '{2}' but instead has '{3}'",
                    e.port_channel_id, e.chain_id,
                    e.expected_counterrparty_port_channel_id,
                    e.actual_counterrparty_port_channel_id)
            },

        MismatchPort
            {
                destination_chain_id: ChainId,
                destination_port_id: PortId,
                source_chain_id: ChainId,
                counterparty_port_id: PortId,
                counterparty_channel_id: ChannelId,
            }
            | e | {
                format_args!(
                    "channel open try to chain '{}' and destination port '{}' does not match \
                    the source chain '{}' counterparty port '{}' for channel '{}'",
                    e.destination_chain_id, e.destination_port_id,
                    e.source_chain_id,
                    e.counterparty_port_id,
                    e.counterparty_channel_id)
            },

        MissingEvent
            { description: String }
            | e | {
                format_args!("missing event: {}", e.description)
            },

        RetryInternal
            { reason: String }
            | e | {
                format_args!("Encountered internal error during retry: {}",
                    e.reason)
            },

        TxResponse
            { reason: String }
            | e | {
                format_args!("tx response error: {}",
                    e.reason)
            },

        InvalidEvent
            { event: IbcEvent }
            | e | {
                format_args!("channel object cannot be built from event: {}",
                    e.event)
            },

        MaxRetry
            {
                description: String,
                tries: u64,
                total_delay: Duration,
            }
            [ Self ]
            | e | {
                format_args!("error after maximum retry of {} and total delay of {}s: {}",
                    e.tries, e.total_delay.as_secs(), e.description)
            },
    }
}

impl HasExpiredOrFrozenError for ChannelErrorDetail {
    fn is_expired_or_frozen_error(&self) -> bool {
        match self {
            Self::ClientOperation(e) => e.source.is_expired_or_frozen_error(),
            _ => false,
        }
    }
}

impl HasExpiredOrFrozenError for ChannelError {
    fn is_expired_or_frozen_error(&self) -> bool {
        self.detail().is_expired_or_frozen_error()
    }
}