ibc_client_tendermint/
consensus_state.rs

1//! This module includes trait implementations for the
2//! `ibc_client_tendermint_types::ConsensusState` type. It implements the
3//! `ConsensusStateTrait` for `ConsensusState` by defining a newtype wrapper in
4//! order to circumvent Rust's orphan rule, which disallows foreign traits from
5//! being implemented on foreign types. This module also includes some trait
6//! implementations that serve to pass through traits implemented on the wrapped
7//! `ConsensusState` type.
8
9use ibc_client_tendermint_types::proto::v1::ConsensusState as RawTmConsensusState;
10use ibc_client_tendermint_types::ConsensusState as ConsensusStateType;
11use ibc_core_client::context::consensus_state::ConsensusState as ConsensusStateTrait;
12use ibc_core_client::types::error::ClientError;
13use ibc_core_commitment_types::commitment::CommitmentRoot;
14use ibc_core_host::types::error::DecodingError;
15use ibc_primitives::prelude::*;
16use ibc_primitives::proto::{Any, Protobuf};
17use ibc_primitives::{IntoTimestamp, Timestamp};
18use tendermint::{Hash, Time};
19
20/// Newtype wrapper around the `ConsensusState` type imported from the
21/// `ibc-client-tendermint-types` crate. This wrapper exists so that we can
22/// bypass Rust's orphan rules and implement traits from
23/// `ibc::core::client::context` on the `ConsensusState` type.
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[derive(Clone, Debug, PartialEq, Eq, derive_more::From)]
26pub struct ConsensusState(ConsensusStateType);
27
28impl ConsensusState {
29    pub fn inner(&self) -> &ConsensusStateType {
30        &self.0
31    }
32
33    pub fn into_inner(self) -> ConsensusStateType {
34        self.0
35    }
36
37    pub fn timestamp(&self) -> Time {
38        self.0.timestamp
39    }
40
41    pub fn next_validators_hash(&self) -> Hash {
42        self.0.next_validators_hash
43    }
44}
45
46impl From<ConsensusState> for ConsensusStateType {
47    fn from(value: ConsensusState) -> Self {
48        value.0
49    }
50}
51
52impl Protobuf<RawTmConsensusState> for ConsensusState {}
53
54impl TryFrom<RawTmConsensusState> for ConsensusState {
55    type Error = DecodingError;
56
57    fn try_from(raw: RawTmConsensusState) -> Result<Self, Self::Error> {
58        Ok(Self(ConsensusStateType::try_from(raw)?))
59    }
60}
61
62impl From<ConsensusState> for RawTmConsensusState {
63    fn from(consensus_state: ConsensusState) -> Self {
64        consensus_state.0.into()
65    }
66}
67
68impl Protobuf<Any> for ConsensusState {}
69
70impl TryFrom<Any> for ConsensusState {
71    type Error = DecodingError;
72
73    fn try_from(raw: Any) -> Result<Self, Self::Error> {
74        Ok(Self(ConsensusStateType::try_from(raw)?))
75    }
76}
77
78impl From<ConsensusState> for Any {
79    fn from(client_state: ConsensusState) -> Self {
80        client_state.0.into()
81    }
82}
83
84impl From<tendermint::block::Header> for ConsensusState {
85    fn from(header: tendermint::block::Header) -> Self {
86        Self(ConsensusStateType::from(header))
87    }
88}
89
90impl ConsensusStateTrait for ConsensusState {
91    fn root(&self) -> &CommitmentRoot {
92        &self.0.root
93    }
94
95    fn timestamp(&self) -> Result<Timestamp, ClientError> {
96        self.0.timestamp.into_timestamp().map_err(Into::into)
97    }
98}