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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use serde::{Deserialize, Serialize};
use tendermint_proto::v0_37::{
types::{BlockId as RawBlockId, Header as RawHeader},
version::Consensus as RawConsensusVersion,
};
use tendermint_proto::Protobuf;
use crate::{
account, block, chain,
crypto::Sha256,
merkle::{self, MerkleHash},
prelude::*,
AppHash, Hash, Time,
};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "RawHeader", into = "RawHeader")]
pub struct Header {
pub version: Version,
pub chain_id: chain::Id,
pub height: block::Height,
pub time: Time,
pub last_block_id: Option<block::Id>,
pub last_commit_hash: Option<Hash>,
pub data_hash: Option<Hash>,
pub validators_hash: Hash,
pub next_validators_hash: Hash,
pub consensus_hash: Hash,
pub app_hash: AppHash,
pub last_results_hash: Option<Hash>,
pub evidence_hash: Option<Hash>,
pub proposer_address: account::Id,
}
impl Header {
#[cfg(feature = "rust-crypto")]
pub fn hash(&self) -> Hash {
self.hash_with::<crate::crypto::default::Sha256>()
}
pub fn hash_with<H>(&self) -> Hash
where
H: MerkleHash + Sha256 + Default,
{
let fields_bytes = vec![
Protobuf::<RawConsensusVersion>::encode_vec(&self.version).unwrap(),
self.chain_id.encode_vec().unwrap(),
self.height.encode_vec().unwrap(),
self.time.encode_vec().unwrap(),
Protobuf::<RawBlockId>::encode_vec(&self.last_block_id.unwrap_or_default()).unwrap(),
self.last_commit_hash
.unwrap_or_default()
.encode_vec()
.unwrap(),
self.data_hash.unwrap_or_default().encode_vec().unwrap(),
self.validators_hash.encode_vec().unwrap(),
self.next_validators_hash.encode_vec().unwrap(),
self.consensus_hash.encode_vec().unwrap(),
self.app_hash.encode_vec().unwrap(),
self.last_results_hash
.unwrap_or_default()
.encode_vec()
.unwrap(),
self.evidence_hash.unwrap_or_default().encode_vec().unwrap(),
self.proposer_address.encode_vec().unwrap(),
];
Hash::Sha256(merkle::simple_hash_from_byte_vectors::<H>(&fields_bytes))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Version {
pub block: u64,
pub app: u64,
}
tendermint_pb_modules! {
use super::{Header, Version};
use crate::{block, Error};
use pb::{
types::Header as RawHeader,
version::Consensus as RawConsensusVersion,
};
impl Protobuf<RawHeader> for Header {}
impl TryFrom<RawHeader> for Header {
type Error = Error;
fn try_from(value: RawHeader) -> Result<Self, Self::Error> {
let last_block_id = value
.last_block_id
.map(TryInto::try_into)
.transpose()?
.filter(|l| l != &block::Id::default());
let last_commit_hash = if value.last_commit_hash.is_empty() {
None
} else {
Some(value.last_commit_hash.try_into()?)
};
let last_results_hash = if value.last_results_hash.is_empty() {
None
} else {
Some(value.last_results_hash.try_into()?)
};
let height: block::Height = value.height.try_into()?;
if last_block_id.is_some() && height.value() == 1 {
return Err(Error::invalid_first_header());
}
Ok(Header {
version: value.version.ok_or_else(Error::missing_version)?.into(),
chain_id: value.chain_id.try_into()?,
height,
time: value
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
last_block_id,
last_commit_hash,
data_hash: if value.data_hash.is_empty() {
None
} else {
Some(value.data_hash.try_into()?)
},
validators_hash: value.validators_hash.try_into()?,
next_validators_hash: value.next_validators_hash.try_into()?,
consensus_hash: value.consensus_hash.try_into()?,
app_hash: value.app_hash.try_into()?,
last_results_hash,
evidence_hash: if value.evidence_hash.is_empty() {
None
} else {
Some(value.evidence_hash.try_into()?)
}, proposer_address: value.proposer_address.try_into()?,
})
}
}
impl From<Header> for RawHeader {
fn from(value: Header) -> Self {
RawHeader {
version: Some(value.version.into()),
chain_id: value.chain_id.into(),
height: value.height.into(),
time: Some(value.time.into()),
last_block_id: value.last_block_id.map(Into::into),
last_commit_hash: value.last_commit_hash.unwrap_or_default().into(),
data_hash: value.data_hash.unwrap_or_default().into(),
validators_hash: value.validators_hash.into(),
next_validators_hash: value.next_validators_hash.into(),
consensus_hash: value.consensus_hash.into(),
app_hash: value.app_hash.into(),
last_results_hash: value.last_results_hash.unwrap_or_default().into(),
evidence_hash: value.evidence_hash.unwrap_or_default().into(),
proposer_address: value.proposer_address.into(),
}
}
}
impl Protobuf<RawConsensusVersion> for Version {}
impl From<RawConsensusVersion> for Version {
fn from(value: RawConsensusVersion) -> Self {
Version {
block: value.block,
app: value.app,
}
}
}
impl From<Version> for RawConsensusVersion {
fn from(value: Version) -> Self {
RawConsensusVersion {
block: value.block,
app: value.app,
}
}
}
}
#[cfg(test)]
mod tests {
use super::Header;
use crate::test::test_serialization_roundtrip;
#[test]
fn serialization_roundtrip() {
let json_data = include_str!("../../tests/support/serialization/block/header.json");
test_serialization_roundtrip::<Header>(json_data);
}
#[cfg(feature = "rust-crypto")]
mod crypto {
use super::*;
use crate::{hash::Algorithm, Hash};
#[test]
fn header_hashing() {
let expected_hash = Hash::from_hex_upper(
Algorithm::Sha256,
"F30A71F2409FB15AACAEDB6CC122DFA2525BEE9CAE521721B06BFDCA291B8D56",
)
.unwrap();
let header: Header = serde_json::from_str(include_str!(
"../../tests/support/serialization/block/header_with_known_hash.json"
))
.unwrap();
assert_eq!(expected_hash, header.hash());
}
}
}