ibc_relayer_types/
proofs.rs

1use flex_error::{define_error, TraceError};
2use prost::EncodeError;
3use serde::Serialize;
4
5use crate::core::ics23_commitment::commitment::CommitmentProofBytes;
6use crate::Height;
7
8define_error! {
9    #[derive(Debug, PartialEq, Eq)]
10    ProofError {
11        ZeroHeight
12            | _ | { format_args!("proof height cannot be zero") },
13
14        EmptyProof
15            | _ | { format_args!("proof cannot be empty") },
16
17        Encode
18            [ TraceError<EncodeError> ]
19            | _ | { "protobuf encode error" },
20    }
21}
22
23/// Structure comprising proofs in a message. Proofs are typically present in messages for
24/// handshake protocols, e.g., ICS3 connection (open) handshake or ICS4 channel (open and close)
25/// handshake, as well as for ICS4 packets, timeouts, and acknowledgements.
26#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
27pub struct Proofs {
28    object_proof: CommitmentProofBytes,
29    client_proof: Option<CommitmentProofBytes>,
30    consensus_proof: Option<ConsensusProof>,
31    host_consensus_state_proof: Option<CommitmentProofBytes>,
32    /// Currently used for proof_close for MsgTimeoutOnCLose where object_proof is proof_unreceived
33    other_proof: Option<CommitmentProofBytes>,
34    /// Height for the commitment root for proving the proofs above.
35    /// When creating these proofs, the chain is queried at `height-1`.
36    height: Height,
37}
38
39impl Proofs {
40    pub fn new(
41        object_proof: CommitmentProofBytes,
42        client_proof: Option<CommitmentProofBytes>,
43        consensus_proof: Option<ConsensusProof>,
44        host_consensus_state_proof: Option<CommitmentProofBytes>,
45        other_proof: Option<CommitmentProofBytes>,
46        height: Height,
47    ) -> Result<Self, ProofError> {
48        Ok(Self {
49            object_proof,
50            client_proof,
51            consensus_proof,
52            host_consensus_state_proof,
53            other_proof,
54            height,
55        })
56    }
57
58    /// Getter for the consensus_proof field of this proof. Intuitively, this is a proof that a
59    /// client on the source chain stores a consensus state for the destination chain.
60    pub fn consensus_proof(&self) -> Option<&ConsensusProof> {
61        self.consensus_proof.as_ref()
62    }
63
64    /// Getter for the host_consensus_state_proof field of this proof.
65    /// This is an optional proof data for host state machines that are unable to introspect their own consensus state.
66    pub fn host_consensus_state_proof(&self) -> Option<&CommitmentProofBytes> {
67        self.host_consensus_state_proof.as_ref()
68    }
69
70    /// Getter for the height field of this proof (i.e., the consensus height where this proof was
71    /// created).
72    pub fn height(&self) -> Height {
73        self.height
74    }
75
76    /// Getter for the object-specific proof (e.g., proof for connection state or channel state).
77    pub fn object_proof(&self) -> &CommitmentProofBytes {
78        &self.object_proof
79    }
80
81    /// Getter for the client_proof.
82    pub fn client_proof(&self) -> Option<&CommitmentProofBytes> {
83        self.client_proof.as_ref()
84    }
85
86    /// Getter for the other_proof.
87    pub fn other_proof(&self) -> Option<&CommitmentProofBytes> {
88        self.other_proof.as_ref()
89    }
90}
91
92#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
93pub struct ConsensusProof {
94    proof: CommitmentProofBytes,
95    height: Height,
96}
97
98impl ConsensusProof {
99    pub fn new(
100        consensus_proof: CommitmentProofBytes,
101        consensus_height: Height,
102    ) -> Result<Self, ProofError> {
103        Ok(Self {
104            proof: consensus_proof,
105            height: consensus_height,
106        })
107    }
108
109    /// Getter for the height field of this consensus proof.
110    pub fn height(&self) -> Height {
111        self.height
112    }
113
114    /// Getter for the proof (CommitmentProof) field of this consensus proof.
115    pub fn proof(&self) -> &CommitmentProofBytes {
116        &self.proof
117    }
118}