-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathconsensus_state.rs
98 lines (83 loc) · 3.05 KB
/
consensus_state.rs
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
use ibc::core::client::context::consensus_state::ConsensusState;
use ibc::core::client::types::error::ClientError;
use ibc::core::commitment_types::commitment::CommitmentRoot;
use ibc::core::host::types::error::DecodingError;
use ibc::core::primitives::prelude::*;
use ibc::core::primitives::Timestamp;
use ibc::primitives::proto::{Any, Protobuf};
use crate::testapp::ibc::clients::mock::header::MockHeader;
use crate::testapp::ibc::clients::mock::proto::ConsensusState as RawMockConsensusState;
pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";
/// The mock consensus state type used within ibc-testkit for testing situations
/// when a consensus state is required.
///
/// Note, this type slightly differs from the [`RawMockConsensusState`] type exposed by
/// ibc-proto. It contains a (private) `root` field to easily return a
/// reference to the mock consensus state's dummy [`CommitmentRoot`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MockConsensusState {
pub header: MockHeader,
root: CommitmentRoot,
}
impl MockConsensusState {
pub fn new(header: MockHeader) -> Self {
Self {
header,
root: CommitmentRoot::from(vec![0]),
}
}
pub fn timestamp(&self) -> Timestamp {
self.header.timestamp
}
}
impl Protobuf<RawMockConsensusState> for MockConsensusState {}
impl TryFrom<RawMockConsensusState> for MockConsensusState {
type Error = DecodingError;
fn try_from(raw: RawMockConsensusState) -> Result<Self, Self::Error> {
let raw_header = raw.header.ok_or(DecodingError::missing_raw_data(
"mock consensus state header",
))?;
Ok(Self {
header: raw_header.try_into()?,
root: CommitmentRoot::from(vec![0]),
})
}
}
impl From<MockConsensusState> for RawMockConsensusState {
fn from(value: MockConsensusState) -> Self {
Self {
header: Some(value.header.into()),
}
}
}
impl Protobuf<Any> for MockConsensusState {}
impl TryFrom<Any> for MockConsensusState {
type Error = DecodingError;
fn try_from(raw: Any) -> Result<Self, Self::Error> {
if let MOCK_CONSENSUS_STATE_TYPE_URL = raw.type_url.as_str() {
Protobuf::<RawMockConsensusState>::decode(raw.value.as_ref()).map_err(Into::into)
} else {
Err(DecodingError::MismatchedResourceName {
expected: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
actual: raw.type_url,
})
}
}
}
impl From<MockConsensusState> for Any {
fn from(consensus_state: MockConsensusState) -> Self {
Self {
type_url: MOCK_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawMockConsensusState>::encode_vec(consensus_state),
}
}
}
impl ConsensusState for MockConsensusState {
fn root(&self) -> &CommitmentRoot {
&self.root
}
fn timestamp(&self) -> Result<Timestamp, ClientError> {
Ok(self.header.timestamp)
}
}