ibc_core_channel/
context.rs

1//! ICS4 (channel) context.
2
3use ibc_core_channel_types::channel::ChannelEnd;
4use ibc_core_channel_types::commitment::PacketCommitment;
5use ibc_core_client::context::prelude::*;
6use ibc_core_connection::types::ConnectionEnd;
7use ibc_core_handler_types::events::IbcEvent;
8use ibc_core_host::types::error::HostError;
9use ibc_core_host::types::identifiers::{ConnectionId, Sequence};
10use ibc_core_host::types::path::{ChannelEndPath, CommitmentPath, SeqSendPath};
11use ibc_core_host::{ExecutionContext, ValidationContext};
12use ibc_primitives::prelude::*;
13
14/// Methods required in send packet validation, to be implemented by the host
15pub trait SendPacketValidationContext {
16    type V: ClientValidationContext;
17
18    /// Retrieve the context that implements all clients' `ValidationContext`.
19    fn get_client_validation_context(&self) -> &Self::V;
20
21    /// Returns the ChannelEnd for the given `port_id` and `chan_id`.
22    fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result<ChannelEnd, HostError>;
23
24    /// Returns the ConnectionState for the given identifier `connection_id`.
25    fn connection_end(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, HostError>;
26
27    fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result<Sequence, HostError>;
28}
29
30impl<T> SendPacketValidationContext for T
31where
32    T: ValidationContext,
33{
34    type V = T::V;
35
36    fn get_client_validation_context(&self) -> &Self::V {
37        self.get_client_validation_context()
38    }
39
40    fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result<ChannelEnd, HostError> {
41        self.channel_end(channel_end_path)
42    }
43
44    fn connection_end(&self, connection_id: &ConnectionId) -> Result<ConnectionEnd, HostError> {
45        self.connection_end(connection_id)
46    }
47
48    fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result<Sequence, HostError> {
49        self.get_next_sequence_send(seq_send_path)
50    }
51}
52
53/// Methods required in send packet execution, to be implemented by the host
54pub trait SendPacketExecutionContext: SendPacketValidationContext {
55    fn store_next_sequence_send(
56        &mut self,
57        seq_send_path: &SeqSendPath,
58        seq: Sequence,
59    ) -> Result<(), HostError>;
60
61    fn store_packet_commitment(
62        &mut self,
63        commitment_path: &CommitmentPath,
64        commitment: PacketCommitment,
65    ) -> Result<(), HostError>;
66
67    /// Ibc events
68    fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), HostError>;
69
70    /// Logging facility
71    fn log_message(&mut self, message: String) -> Result<(), HostError>;
72}
73
74impl<T> SendPacketExecutionContext for T
75where
76    T: ExecutionContext,
77{
78    fn store_next_sequence_send(
79        &mut self,
80        seq_send_path: &SeqSendPath,
81        seq: Sequence,
82    ) -> Result<(), HostError> {
83        self.store_next_sequence_send(seq_send_path, seq)
84    }
85
86    fn store_packet_commitment(
87        &mut self,
88        commitment_path: &CommitmentPath,
89        commitment: PacketCommitment,
90    ) -> Result<(), HostError> {
91        self.store_packet_commitment(commitment_path, commitment)
92    }
93
94    fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), HostError> {
95        self.emit_ibc_event(event)
96    }
97
98    fn log_message(&mut self, message: String) -> Result<(), HostError> {
99        self.log_message(message)
100    }
101}