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
use crate::merkle::simple_hash_from_byte_vectors;
use crate::serializers;
use crate::{account, block, chain, AppHash, Hash, Time};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use tendermint_proto::version::Consensus as RawConsensusVersion;
use tendermint_proto::DomainType;
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Header {
pub version: Version,
pub chain_id: chain::Id,
pub height: block::Height,
pub time: Time,
#[serde(deserialize_with = "serializers::parse_non_empty_block_id")]
pub last_block_id: Option<block::Id>,
#[serde(deserialize_with = "serializers::parse_non_empty_hash")]
pub last_commit_hash: Option<Hash>,
#[serde(deserialize_with = "serializers::parse_non_empty_hash")]
pub data_hash: Option<Hash>,
pub validators_hash: Hash,
pub next_validators_hash: Hash,
pub consensus_hash: Hash,
pub app_hash: AppHash,
#[serde(deserialize_with = "serializers::parse_non_empty_hash")]
pub last_results_hash: Option<Hash>,
#[serde(deserialize_with = "serializers::parse_non_empty_hash")]
pub evidence_hash: Option<Hash>,
pub proposer_address: account::Id,
}
impl Header {
pub fn hash(&self) -> Hash {
let mut fields_bytes: Vec<Vec<u8>> = Vec::with_capacity(14);
fields_bytes.push(self.version.encode_vec().unwrap());
fields_bytes.push(self.chain_id.encode_vec().unwrap());
fields_bytes.push(self.height.encode_vec().unwrap());
fields_bytes.push(self.time.encode_vec().unwrap());
fields_bytes.push(self.last_block_id.unwrap_or_default().encode_vec().unwrap());
fields_bytes.push(
self.last_commit_hash
.unwrap_or_default()
.encode_vec()
.unwrap(),
);
fields_bytes.push(self.data_hash.unwrap_or_default().encode_vec().unwrap());
fields_bytes.push(self.validators_hash.encode_vec().unwrap());
fields_bytes.push(self.next_validators_hash.encode_vec().unwrap());
fields_bytes.push(self.consensus_hash.encode_vec().unwrap());
fields_bytes.push(self.app_hash.encode_vec().unwrap());
fields_bytes.push(
self.last_results_hash
.unwrap_or_default()
.encode_vec()
.unwrap(),
);
fields_bytes.push(self.evidence_hash.unwrap_or_default().encode_vec().unwrap());
fields_bytes.push(self.proposer_address.encode_vec().unwrap());
Hash::Sha256(simple_hash_from_byte_vectors(fields_bytes))
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct Version {
#[serde(with = "serializers::from_str")]
pub block: u64,
#[serde(with = "serializers::from_str", default)]
pub app: u64,
}
impl DomainType<RawConsensusVersion> for Version {}
impl TryFrom<RawConsensusVersion> for Version {
type Error = anomaly::BoxError;
fn try_from(value: RawConsensusVersion) -> Result<Self, Self::Error> {
Ok(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, Version};
use crate::hash::Algorithm;
use crate::test::test_serialization_roundtrip;
use crate::Hash;
#[test]
fn serialization_roundtrip() {
let json_data = include_str!("../../tests/support/serialization/block/header.json");
test_serialization_roundtrip::<Header>(json_data);
}
#[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());
}
#[test]
fn empty_header_version_app_field() {
let json_data = r#"{"block": "11"}"#;
let version: Version = serde_json::from_str(json_data).unwrap();
assert_eq!(11, version.block);
assert_eq!(0, version.app);
}
}