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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::fmt::Display;

use flex_error::define_error;
use serde::{Deserialize, Serialize};

use ibc_relayer_types::applications::ics29_fee::events::IncentivizedPacket;
use ibc_relayer_types::core::{
    ics02_client::events::UpdateClient,
    ics03_connection::events::Attributes as ConnectionAttributes,
    ics04_channel::events::{
        Attributes, CloseInit, SendPacket, TimeoutPacket, WriteAcknowledgement,
    },
    ics24_host::identifier::{ChainId, ChannelId, ClientId, ConnectionId, PortId},
};

use crate::chain::{
    counterparty::{
        channel_connection_client, channel_connection_client_no_checks,
        counterparty_chain_from_channel, counterparty_chain_from_connection,
    },
    handle::ChainHandle,
    requests::{IncludeProof, QueryClientStateRequest, QueryHeight},
};
use crate::error::Error as RelayerError;
use crate::supervisor::Error as SupervisorError;

/// Client
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Client {
    /// Destination chain identifier.
    /// This is the chain hosting the client.
    pub dst_chain_id: ChainId,

    /// Client identifier (allocated on the destination chain `dst_chain_id`).
    pub dst_client_id: ClientId,

    /// Source chain identifier.
    /// This is the chain whose headers the client worker is verifying.
    pub src_chain_id: ChainId,
}

impl Client {
    pub fn short_name(&self) -> String {
        format!(
            "client::{}->{}:{}",
            self.src_chain_id, self.dst_chain_id, self.dst_client_id
        )
    }
}

/// Connection
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Connection {
    /// Destination chain identifier.
    pub dst_chain_id: ChainId,

    /// Source chain identifier.
    pub src_chain_id: ChainId,

    /// Source connection identifier.
    pub src_connection_id: ConnectionId,
}

impl Connection {
    pub fn short_name(&self) -> String {
        format!(
            "connection::{}:{}->{}",
            self.src_connection_id, self.src_chain_id, self.dst_chain_id,
        )
    }
}

/// Channel
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Channel {
    /// Destination chain identifier.
    pub dst_chain_id: ChainId,

    /// Source chain identifier.
    pub src_chain_id: ChainId,

    /// Source channel identifier.
    pub src_channel_id: ChannelId,

    /// Source port identifier.
    pub src_port_id: PortId,
}

impl Channel {
    pub fn short_name(&self) -> String {
        format!(
            "channel::{}/{}:{}->{}",
            self.src_channel_id, self.src_port_id, self.src_chain_id, self.dst_chain_id,
        )
    }
}

/// A packet worker between a source and destination chain, and a specific channel and port.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Packet {
    /// Destination chain identifier.
    pub dst_chain_id: ChainId,

    /// Source chain identifier.
    pub src_chain_id: ChainId,

    /// Source channel identifier.
    pub src_channel_id: ChannelId,

    /// Source port identifier.
    pub src_port_id: PortId,
}

impl Packet {
    pub fn short_name(&self) -> String {
        format!(
            "packet::{}/{}:{}->{}",
            self.src_channel_id, self.src_port_id, self.src_chain_id, self.dst_chain_id,
        )
    }
}

/// A wallet worker which monitors the balance of the wallet in use by Hermes
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Wallet {
    /// Chain identifier
    pub chain_id: ChainId,
}

impl Wallet {
    pub fn short_name(&self) -> String {
        format!("wallet::{}", self.chain_id)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct CrossChainQuery {
    pub src_chain_id: ChainId,
    pub dst_chain_id: ChainId,
    pub query_id: String,
    pub connection_id: ConnectionId,
}

impl CrossChainQuery {
    pub fn short_name(&self) -> String {
        format!("cross_chain_query::{}/{}", self.dst_chain_id, self.query_id)
    }
}

/// An object determines the amount of parallelism that can
/// be exercised when processing [`IbcEvent`](ibc_relayer_types::events::IbcEvent)
/// between two chains. For each [`Object`], a corresponding
/// [`WorkerHandle`](crate::worker::WorkerHandle) is spawned and all [`IbcEvent`](ibc_relayer_types::events::IbcEvent)s mapped
/// to an [`Object`] are sent to the associated [`WorkerHandle`](crate::worker::WorkerHandle)
/// for processing.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Object {
    /// See [`Client`].
    Client(Client),
    /// See [`Connection`].
    Connection(Connection),
    /// See [`Channel`].
    Channel(Channel),
    /// See [`Packet`].
    Packet(Packet),
    /// See [`Wallet`]
    Wallet(Wallet),
    /// See [`CrossChainQuery`]
    CrossChainQuery(CrossChainQuery),
}

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

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

        RefreshNotRequired
            {
                client_id: ClientId,
                chain_id: ChainId,
            }
            | e | {
                format!("client '{}' on chain {} does not require refresh",
                    e.client_id, e.chain_id)
            },

        MissingChannelId
            { event: Attributes }
            | e | {
                format!("channel_id missing in channel open event '{:?}'",
                    e.event)
            },

        MissingConnectionId
            { event: ConnectionAttributes }
            | e | {
                format!("connection_id missing from connection handshake event '{:?}'",
                    e.event)
            },
    }
}

impl Object {
    /// Returns `true` if this [`Object`] is for a [`WorkerHandle`](crate::worker::WorkerHandle)
    /// which is interested in new block events originating from the chain with
    /// the given [`ChainId`]. Returns `false` otherwise.
    pub fn notify_new_block(&self, src_chain_id: &ChainId) -> bool {
        match self {
            Object::Client(_) => false,
            Object::Connection(c) => &c.src_chain_id == src_chain_id,
            Object::Channel(c) => &c.src_chain_id == src_chain_id,
            Object::Packet(p) => &p.src_chain_id == src_chain_id,
            Object::Wallet(_) => false,
            Object::CrossChainQuery(c) => &c.src_chain_id == src_chain_id,
        }
    }

    /// Returns whether or not this object pertains to the given chain.
    pub fn for_chain(&self, chain_id: &ChainId) -> bool {
        match self {
            Object::Client(c) => &c.src_chain_id == chain_id || &c.dst_chain_id == chain_id,
            Object::Connection(c) => &c.src_chain_id == chain_id || &c.dst_chain_id == chain_id,
            Object::Channel(c) => &c.src_chain_id == chain_id || &c.dst_chain_id == chain_id,
            Object::Packet(p) => &p.src_chain_id == chain_id || &p.dst_chain_id == chain_id,
            Object::Wallet(w) => &w.chain_id == chain_id,
            Object::CrossChainQuery(c) => {
                &c.src_chain_id == chain_id || &c.dst_chain_id == chain_id
            }
        }
    }

    /// Return the type of object
    pub fn object_type(&self) -> ObjectType {
        match self {
            Object::Client(_) => ObjectType::Client,
            Object::Channel(_) => ObjectType::Channel,
            Object::Connection(_) => ObjectType::Connection,
            Object::Packet(_) => ObjectType::Packet,
            Object::Wallet(_) => ObjectType::Wallet,
            Object::CrossChainQuery(_) => ObjectType::CrossChainQuery,
        }
    }
}

/// The type of [`Object`].
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ObjectType {
    Client,
    Channel,
    Connection,
    Packet,
    Wallet,
    CrossChainQuery,
}

impl Display for ObjectType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ObjectType::Client => write!(f, "client"),
            ObjectType::Channel => write!(f, "channel"),
            ObjectType::Connection => write!(f, "connection"),
            ObjectType::Packet => write!(f, "packet"),
            ObjectType::Wallet => write!(f, "wallet"),
            ObjectType::CrossChainQuery => write!(f, "cross_chain_query"),
        }
    }
}

impl From<Client> for Object {
    fn from(c: Client) -> Self {
        Self::Client(c)
    }
}

impl From<Connection> for Object {
    fn from(c: Connection) -> Self {
        Self::Connection(c)
    }
}

impl From<Channel> for Object {
    fn from(c: Channel) -> Self {
        Self::Channel(c)
    }
}

impl From<Packet> for Object {
    fn from(p: Packet) -> Self {
        Self::Packet(p)
    }
}

impl From<Wallet> for Object {
    fn from(w: Wallet) -> Self {
        Self::Wallet(w)
    }
}

impl From<CrossChainQuery> for Object {
    fn from(c: CrossChainQuery) -> Self {
        Self::CrossChainQuery(c)
    }
}

impl Object {
    pub fn src_chain_id(&self) -> &ChainId {
        match self {
            Self::Client(ref client) => &client.src_chain_id,
            Self::Connection(ref connection) => &connection.src_chain_id,
            Self::Channel(ref channel) => &channel.src_chain_id,
            Self::Packet(ref path) => &path.src_chain_id,
            Self::Wallet(ref wallet) => &wallet.chain_id,
            Self::CrossChainQuery(ref query) => &query.src_chain_id,
        }
    }

    pub fn dst_chain_id(&self) -> &ChainId {
        match self {
            Self::Client(ref client) => &client.dst_chain_id,
            Self::Connection(ref connection) => &connection.dst_chain_id,
            Self::Channel(ref channel) => &channel.dst_chain_id,
            Self::Packet(ref path) => &path.dst_chain_id,
            Self::Wallet(ref wallet) => &wallet.chain_id,
            Self::CrossChainQuery(ref query) => &query.dst_chain_id,
        }
    }

    pub fn short_name(&self) -> String {
        match self {
            Self::Client(ref client) => client.short_name(),
            Self::Connection(ref connection) => connection.short_name(),
            Self::Channel(ref channel) => channel.short_name(),
            Self::Packet(ref path) => path.short_name(),
            Self::Wallet(ref wallet) => wallet.short_name(),
            Self::CrossChainQuery(ref query) => query.short_name(),
        }
    }

    /// Build the object associated with the given [`UpdateClient`] event.
    pub fn for_update_client(
        e: &UpdateClient,
        dst_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let (client_state, _) = dst_chain
            .query_client_state(
                QueryClientStateRequest {
                    client_id: e.client_id().clone(),
                    height: QueryHeight::Latest,
                },
                IncludeProof::No,
            )
            .map_err(ObjectError::relayer)?;

        if client_state.refresh_period().is_none() {
            return Err(ObjectError::refresh_not_required(
                e.client_id().clone(),
                dst_chain.id(),
            ));
        }
        let src_chain_id = client_state.chain_id();

        Ok(Client {
            dst_client_id: e.client_id().clone(),
            dst_chain_id: dst_chain.id(),
            src_chain_id,
        }
        .into())
    }

    /// Build the client object associated with the given channel event attributes.
    pub fn client_from_chan_open_events(
        e: &Attributes,           // The attributes of the emitted event
        chain: &impl ChainHandle, // The chain which emitted the event
    ) -> Result<Self, ObjectError> {
        let channel_id = e
            .channel_id()
            .ok_or_else(|| ObjectError::missing_channel_id(e.clone()))?;

        let client = channel_connection_client(chain, e.port_id(), channel_id)
            .map_err(ObjectError::supervisor)?
            .client;

        if client.client_state.refresh_period().is_none() {
            return Err(ObjectError::refresh_not_required(
                client.client_id,
                chain.id(),
            ));
        }

        Ok(Client {
            dst_client_id: client.client_id.clone(),
            dst_chain_id: chain.id(), // The object's destination is the chain hosting the client
            src_chain_id: client.client_state.chain_id(),
        }
        .into())
    }

    /// Build the Connection object associated with the given
    /// [`Open`](ibc_relayer_types::core::ics03_connection::connection::State::Open)
    /// connection event.
    pub fn connection_from_conn_open_events(
        e: &ConnectionAttributes,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let connection_id = e
            .connection_id
            .as_ref()
            .ok_or_else(|| ObjectError::missing_connection_id(e.clone()))?;

        let dst_chain_id = counterparty_chain_from_connection(src_chain, connection_id)
            .map_err(ObjectError::supervisor)?;

        Ok(Connection {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_connection_id: connection_id.clone(),
        }
        .into())
    }

    /// Build the Channel object associated with the given
    /// [`Open`](ibc_relayer_types::core::ics04_channel::channel::State::Open) channel event.
    pub fn channel_from_chan_open_events(
        attributes: &Attributes,
        src_chain: &impl ChainHandle,
        allow_non_open_connection: bool,
    ) -> Result<Self, ObjectError> {
        let channel_id = attributes
            .channel_id()
            .ok_or_else(|| ObjectError::missing_channel_id(attributes.clone()))?;

        let port_id = attributes.port_id();

        let dst_chain_id = if allow_non_open_connection {
            // Get the destination chain allowing for the possibility that the connection is not yet open.
            // This is to support the optimistic channel handshake by allowing the channel worker to get
            // the channel events while the connection is being established.
            // The channel worker will eventually finish the channel handshake via the retry mechanism.
            channel_connection_client_no_checks(src_chain, port_id, channel_id)
                .map(|c| c.client.client_state.chain_id())
                .map_err(ObjectError::supervisor)?
        } else {
            channel_connection_client(src_chain, port_id, channel_id)
                .map(|c| c.client.client_state.chain_id())
                .map_err(ObjectError::supervisor)?
        };

        Ok(Channel {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: channel_id.clone(),
            src_port_id: port_id.clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`SendPacket`] event.
    pub fn for_send_packet(
        e: &SendPacket,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_channel(
            src_chain,
            &e.packet.source_channel,
            &e.packet.source_port,
        )
        .map_err(ObjectError::supervisor)?;

        Ok(Packet {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: e.packet.source_channel.clone(),
            src_port_id: e.packet.source_port.clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`WriteAcknowledgement`] event.
    pub fn for_write_ack(
        e: &WriteAcknowledgement,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_channel(
            src_chain,
            &e.packet.destination_channel,
            &e.packet.destination_port,
        )
        .map_err(ObjectError::supervisor)?;

        Ok(Packet {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: e.packet.destination_channel.clone(),
            src_port_id: e.packet.destination_port.clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`TimeoutPacket`] event.
    pub fn for_timeout_packet(
        e: &TimeoutPacket,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_channel(
            src_chain,
            &e.packet.source_channel,
            &e.packet.source_port,
        )
        .map_err(ObjectError::supervisor)?;

        Ok(Packet {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: e.src_channel_id().clone(),
            src_port_id: e.src_port_id().clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`CloseInit`] event.
    pub fn for_close_init_channel(
        e: &CloseInit,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_channel(src_chain, e.channel_id(), e.port_id())
            .map_err(ObjectError::supervisor)?;

        Ok(Packet {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: e.channel_id().clone(),
            src_port_id: e.port_id().clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`CrossChainQuery`] event.
    pub fn for_cross_chain_query_packet(
        p: &ibc_relayer_types::applications::ics31_icq::events::CrossChainQueryPacket,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_connection(&src_chain.clone(), &p.connection_id)
            .map_err(ObjectError::supervisor)?;
        Ok(CrossChainQuery {
            src_chain_id: src_chain.clone().id(),
            dst_chain_id,
            query_id: p.query_id.to_string(),
            connection_id: p.connection_id.clone(),
        }
        .into())
    }

    /// Build the object associated with the given [`IncentivizedPacket`] event.
    pub fn for_incentivized_packet(
        e: &IncentivizedPacket,
        src_chain: &impl ChainHandle,
    ) -> Result<Self, ObjectError> {
        let dst_chain_id = counterparty_chain_from_channel(src_chain, &e.channel_id, &e.port_id)
            .map_err(ObjectError::supervisor)?;

        Ok(Packet {
            dst_chain_id,
            src_chain_id: src_chain.id(),
            src_channel_id: e.channel_id.clone(),
            src_port_id: e.port_id.clone(),
        }
        .into())
    }
}