-
Notifications
You must be signed in to change notification settings - Fork 92
/
consensus_state.rs
185 lines (156 loc) · 5.77 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
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
//! Defines Tendermint's `ConsensusState` type
use crate::prelude::*;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::tendermint::v1::ConsensusState as RawConsensusState;
use tendermint::{hash::Algorithm, time::Time, Hash};
use tendermint_proto::google::protobuf as tpb;
use tendermint_proto::Error as TmProtoError;
use tendermint_proto::Protobuf;
use crate::clients::ics07_tendermint::error::Error;
use crate::clients::ics07_tendermint::header::Header;
use crate::core::ics02_client::consensus_state::ConsensusState as ConsensusStateTrait;
use crate::core::ics02_client::error::ClientError;
use crate::core::ics23_commitment::commitment::CommitmentRoot;
use crate::core::timestamp::Timestamp;
pub const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =
"/ibc.lightclients.tendermint.v1.ConsensusState";
/// Defines the Tendermint light client's consensus state
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConsensusState {
pub timestamp: Time,
pub root: CommitmentRoot,
pub next_validators_hash: Hash,
}
impl ConsensusState {
pub fn new(root: CommitmentRoot, timestamp: Time, next_validators_hash: Hash) -> Self {
Self {
timestamp,
root,
next_validators_hash,
}
}
}
impl Protobuf<RawConsensusState> for ConsensusState {}
impl TryFrom<RawConsensusState> for ConsensusState {
type Error = Error;
fn try_from(raw: RawConsensusState) -> Result<Self, Self::Error> {
let proto_root = raw
.root
.ok_or(Error::InvalidRawClientState {
reason: "missing commitment root".into(),
})?
.hash;
let ibc_proto::google::protobuf::Timestamp { seconds, nanos } =
raw.timestamp.ok_or(Error::InvalidRawClientState {
reason: "missing timestamp".into(),
})?;
// FIXME: shunts like this are necessary due to
// https://github.com/informalsystems/tendermint-rs/issues/1053
let proto_timestamp = tpb::Timestamp { seconds, nanos };
let timestamp = proto_timestamp
.try_into()
.map_err(|e| Error::InvalidRawClientState {
reason: format!("invalid timestamp: {e}"),
})?;
let next_validators_hash = Hash::from_bytes(Algorithm::Sha256, &raw.next_validators_hash)
.map_err(|e| Error::InvalidRawClientState {
reason: e.to_string(),
})?;
Ok(Self {
root: proto_root.into(),
timestamp,
next_validators_hash,
})
}
}
impl From<ConsensusState> for RawConsensusState {
fn from(value: ConsensusState) -> Self {
// FIXME: shunts like this are necessary due to
// https://github.com/informalsystems/tendermint-rs/issues/1053
let tpb::Timestamp { seconds, nanos } = value.timestamp.into();
let timestamp = ibc_proto::google::protobuf::Timestamp { seconds, nanos };
RawConsensusState {
timestamp: Some(timestamp),
root: Some(ibc_proto::ibc::core::commitment::v1::MerkleRoot {
hash: value.root.into_vec(),
}),
next_validators_hash: value.next_validators_hash.as_bytes().to_vec(),
}
}
}
impl Protobuf<Any> for ConsensusState {}
impl TryFrom<Any> for ConsensusState {
type Error = ClientError;
fn try_from(raw: Any) -> Result<Self, Self::Error> {
use bytes::Buf;
use core::ops::Deref;
use prost::Message;
fn decode_consensus_state<B: Buf>(buf: B) -> Result<ConsensusState, Error> {
RawConsensusState::decode(buf)
.map_err(Error::Decode)?
.try_into()
}
match raw.type_url.as_str() {
TENDERMINT_CONSENSUS_STATE_TYPE_URL => {
decode_consensus_state(raw.value.deref()).map_err(Into::into)
}
_ => Err(ClientError::UnknownConsensusStateType {
consensus_state_type: raw.type_url,
}),
}
}
}
impl From<ConsensusState> for Any {
fn from(consensus_state: ConsensusState) -> Self {
Any {
type_url: TENDERMINT_CONSENSUS_STATE_TYPE_URL.to_string(),
value: Protobuf::<RawConsensusState>::encode_vec(&consensus_state)
.expect("Out of memory"),
}
}
}
impl From<tendermint::block::Header> for ConsensusState {
fn from(header: tendermint::block::Header) -> Self {
Self {
root: CommitmentRoot::from_bytes(header.app_hash.as_ref()),
timestamp: header.time,
next_validators_hash: header.next_validators_hash,
}
}
}
impl From<Header> for ConsensusState {
fn from(header: Header) -> Self {
Self::from(header.signed_header.header)
}
}
impl ConsensusStateTrait for ConsensusState {
fn root(&self) -> &CommitmentRoot {
&self.root
}
fn timestamp(&self) -> Timestamp {
self.timestamp.into()
}
fn encode_vec(&self) -> Result<Vec<u8>, TmProtoError> {
<Self as Protobuf<Any>>::encode_vec(self)
}
}
#[cfg(test)]
#[cfg(feature = "serde")]
mod tests {
use tendermint_rpc::endpoint::abci_query::AbciQuery;
use test_log::test;
use crate::test::test_serialization_roundtrip;
#[test]
fn serialization_roundtrip_no_proof() {
let json_data =
include_str!("../../../tests/support/query/serialization/consensus_state.json");
test_serialization_roundtrip::<AbciQuery>(json_data);
}
#[test]
fn serialization_roundtrip_with_proof() {
let json_data =
include_str!("../../../tests/support/query/serialization/consensus_state_proof.json");
test_serialization_roundtrip::<AbciQuery>(json_data);
}
}