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
use crate::core::ics04_channel::channel::Counterparty;
use crate::core::ics04_channel::channel::State;
use crate::core::ics04_channel::commitment::PacketCommitment;
use crate::core::ics04_channel::events::SendPacket;
use crate::core::ics04_channel::packet::Sequence;
use crate::core::ics04_channel::{context::SendPacketReader, error::PacketError, packet::Packet};
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::core::ics24_host::path::ChannelEndPath;
use crate::core::ics24_host::path::ClientConsensusStatePath;
use crate::core::ics24_host::path::SeqSendPath;
use crate::core::ContextError;
use crate::events::IbcEvent;
use crate::handler::{HandlerOutput, HandlerResult};
use crate::prelude::*;
use crate::timestamp::Expiry;

#[derive(Clone, Debug)]
pub struct SendPacketResult {
    pub port_id: PortId,
    pub channel_id: ChannelId,
    pub seq: Sequence,
    pub seq_number: Sequence,
    pub commitment: PacketCommitment,
}

/// Per our convention, this message is processed on chain A.
pub fn send_packet(
    ctx_a: &impl SendPacketReader,
    packet: Packet,
) -> HandlerResult<SendPacketResult, ContextError> {
    let mut output = HandlerOutput::builder();

    let chan_end_path_on_a = ChannelEndPath::new(&packet.port_on_a, &packet.chan_on_a);
    let chan_end_on_a = ctx_a.channel_end(&chan_end_path_on_a)?;

    if chan_end_on_a.state_matches(&State::Closed) {
        return Err(PacketError::ChannelClosed {
            channel_id: packet.chan_on_a,
        }
        .into());
    }

    let counterparty = Counterparty::new(packet.port_on_b.clone(), Some(packet.chan_on_b.clone()));

    if !chan_end_on_a.counterparty_matches(&counterparty) {
        return Err(PacketError::InvalidPacketCounterparty {
            port_id: packet.port_on_b.clone(),
            channel_id: packet.chan_on_b,
        }
        .into());
    }
    let conn_id_on_a = &chan_end_on_a.connection_hops()[0];
    let conn_end_on_a = ctx_a.connection_end(conn_id_on_a)?;

    let client_id_on_a = conn_end_on_a.client_id();

    let client_state_of_b_on_a = ctx_a.client_state(client_id_on_a)?;

    // prevent accidental sends with clients that cannot be updated
    if client_state_of_b_on_a.is_frozen() {
        return Err(PacketError::FrozenClient {
            client_id: conn_end_on_a.client_id().clone(),
        }
        .into());
    }

    let latest_height_on_a = client_state_of_b_on_a.latest_height();

    if packet.timeout_height_on_b.has_expired(latest_height_on_a) {
        return Err(PacketError::LowPacketHeight {
            chain_height: latest_height_on_a,
            timeout_height: packet.timeout_height_on_b,
        }
        .into());
    }

    let client_cons_state_path_on_a =
        ClientConsensusStatePath::new(client_id_on_a, &latest_height_on_a);
    let consensus_state_of_b_on_a = ctx_a.client_consensus_state(&client_cons_state_path_on_a)?;
    let latest_timestamp = consensus_state_of_b_on_a.timestamp();
    let packet_timestamp = packet.timeout_timestamp_on_b;
    if let Expiry::Expired = latest_timestamp.check_expiry(&packet_timestamp) {
        return Err(PacketError::LowPacketTimestamp.into());
    }

    let seq_send_path_on_a = SeqSendPath::new(&packet.port_on_a, &packet.chan_on_a);
    let next_seq_send_on_a = ctx_a.get_next_sequence_send(&seq_send_path_on_a)?;

    if packet.sequence != next_seq_send_on_a {
        return Err(PacketError::InvalidPacketSequence {
            given_sequence: packet.sequence,
            next_sequence: next_seq_send_on_a,
        }
        .into());
    }

    output.log("success: packet send ");

    let result = SendPacketResult {
        port_id: packet.port_on_a.clone(),
        channel_id: packet.chan_on_a.clone(),
        seq: packet.sequence,
        seq_number: next_seq_send_on_a.increment(),
        commitment: ctx_a.packet_commitment(
            &packet.data,
            &packet.timeout_height_on_b,
            &packet.timeout_timestamp_on_b,
        ),
    };

    output.emit(IbcEvent::SendPacket(SendPacket::new(
        packet,
        chan_end_on_a.ordering,
        conn_id_on_a.clone(),
    )));

    Ok(output.with_result(result))
}

#[cfg(test)]
mod tests {
    use core::ops::Add;
    use core::time::Duration;

    use test_log::test;

    use crate::core::ics02_client::height::Height;
    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::version::get_compatible_versions;
    use crate::core::ics04_channel::channel::{ChannelEnd, Counterparty, Order, State};
    use crate::core::ics04_channel::handler::send_packet::send_packet;
    use crate::core::ics04_channel::packet::test_utils::get_dummy_raw_packet;
    use crate::core::ics04_channel::packet::Packet;
    use crate::core::ics04_channel::Version;
    use crate::core::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
    use crate::events::IbcEvent;
    use crate::mock::context::MockContext;
    use crate::prelude::*;
    use crate::timestamp::Timestamp;
    use crate::timestamp::ZERO_DURATION;

    #[test]
    fn send_packet_processing() {
        struct Test {
            name: String,
            ctx: MockContext,
            packet: Packet,
            want_pass: bool,
        }

        let context = MockContext::default();

        let chan_end_on_a = ChannelEnd::new(
            State::TryOpen,
            Order::default(),
            Counterparty::new(PortId::default(), Some(ChannelId::default())),
            vec![ConnectionId::default()],
            Version::new("ics20-1".to_string()),
        );

        let conn_end_on_a = ConnectionEnd::new(
            ConnectionState::Open,
            ClientId::default(),
            ConnectionCounterparty::new(
                ClientId::default(),
                Some(ConnectionId::default()),
                Default::default(),
            ),
            get_compatible_versions(),
            ZERO_DURATION,
        );

        let timestamp_future = Timestamp::now().add(Duration::from_secs(10)).unwrap();
        let timestamp_ns_past = 1;

        let timeout_height_future = 10;

        let mut packet: Packet =
            get_dummy_raw_packet(timeout_height_future, timestamp_future.nanoseconds())
                .try_into()
                .unwrap();
        packet.sequence = 1.into();
        packet.data = vec![0];

        let mut packet_with_timestamp_old: Packet =
            get_dummy_raw_packet(timeout_height_future, timestamp_ns_past)
                .try_into()
                .unwrap();
        packet_with_timestamp_old.sequence = 1.into();
        packet_with_timestamp_old.data = vec![0];

        let client_raw_height = 5;
        let packet_timeout_equal_client_height: Packet =
            get_dummy_raw_packet(client_raw_height, timestamp_future.nanoseconds())
                .try_into()
                .unwrap();
        let packet_timeout_one_before_client_height: Packet =
            get_dummy_raw_packet(client_raw_height - 1, timestamp_future.nanoseconds())
                .try_into()
                .unwrap();

        let client_height = Height::new(0, client_raw_height).unwrap();

        let tests: Vec<Test> = vec![
            Test {
                name: "Processing fails because no channel exists in the context".to_string(),
                ctx: context.clone(),
                packet: packet.clone(),
                want_pass: false,
            },
            Test {
                name: "Good parameters".to_string(),
                ctx: context
                    .clone()
                    .with_client(&ClientId::default(), client_height)
                    .with_connection(ConnectionId::default(), conn_end_on_a.clone())
                    .with_channel(
                        PortId::default(),
                        ChannelId::default(),
                        chan_end_on_a.clone(),
                    )
                    .with_send_sequence(PortId::default(), ChannelId::default(), 1.into()),
                packet,
                want_pass: true,
            },
            Test {
                name: "Packet timeout height same as destination chain height".to_string(),
                ctx: context
                    .clone()
                    .with_client(&ClientId::default(), client_height)
                    .with_connection(ConnectionId::default(), conn_end_on_a.clone())
                    .with_channel(
                        PortId::default(),
                        ChannelId::default(),
                        chan_end_on_a.clone(),
                    )
                    .with_send_sequence(PortId::default(), ChannelId::default(), 1.into()),
                packet: packet_timeout_equal_client_height,
                want_pass: true,
            },
            Test {
                name: "Packet timeout height one more than destination chain height".to_string(),
                ctx: context
                    .clone()
                    .with_client(&ClientId::default(), client_height)
                    .with_connection(ConnectionId::default(), conn_end_on_a.clone())
                    .with_channel(
                        PortId::default(),
                        ChannelId::default(),
                        chan_end_on_a.clone(),
                    )
                    .with_send_sequence(PortId::default(), ChannelId::default(), 1.into()),
                packet: packet_timeout_one_before_client_height,
                want_pass: false,
            },
            Test {
                name: "Packet timeout due to timestamp".to_string(),
                ctx: context
                    .with_client(&ClientId::default(), client_height)
                    .with_connection(ConnectionId::default(), conn_end_on_a)
                    .with_channel(PortId::default(), ChannelId::default(), chan_end_on_a)
                    .with_send_sequence(PortId::default(), ChannelId::default(), 1.into()),
                packet: packet_with_timestamp_old,
                want_pass: false,
            },
        ]
        .into_iter()
        .collect();

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

                    assert!(!proto_output.events.is_empty()); // Some events must exist.

                    // TODO: The object in the output is a PacketResult what can we check on it?
                    for e in proto_output.events.iter() {
                        assert!(matches!(e, &IbcEvent::SendPacket(_)));
                    }
                }
                Err(e) => {
                    assert!(
                        !test.want_pass,
                        "send_packet: did not pass test: {}, \nparams {:?} {:?} error: {:?}",
                        test.name,
                        test.packet.clone(),
                        test.ctx.clone(),
                        e,
                    );
                }
            }
        }
    }
}