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
use crate::{
block::parts::Header as PartSetHeader,
error::{Error, Kind},
hash::{Algorithm, Hash},
};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::{
fmt::{self, Display},
str::{self, FromStr},
};
use tendermint_proto::types::{
BlockId as RawBlockId, CanonicalBlockId as RawCanonicalBlockId,
PartSetHeader as RawPartSetHeader,
};
use tendermint_proto::Protobuf;
pub const PREFIX_LENGTH: usize = 10;
#[derive(
Serialize, Deserialize, Copy, Clone, Debug, Default, Hash, Eq, PartialEq, PartialOrd, Ord,
)]
#[serde(try_from = "RawBlockId", into = "RawBlockId")]
pub struct Id {
pub hash: Hash,
pub part_set_header: PartSetHeader,
}
impl Protobuf<RawBlockId> for Id {}
impl TryFrom<RawBlockId> for Id {
type Error = Error;
fn try_from(value: RawBlockId) -> Result<Self, Self::Error> {
if value.part_set_header.is_none() {
return Err(Kind::InvalidPartSetHeader
.context("part_set_header is None")
.into());
}
Ok(Self {
hash: value.hash.try_into()?,
part_set_header: value.part_set_header.unwrap().try_into()?,
})
}
}
impl From<Id> for RawBlockId {
fn from(value: Id) -> Self {
if value == Id::default() {
RawBlockId {
hash: vec![],
part_set_header: Some(RawPartSetHeader {
total: 0,
hash: vec![],
}),
}
} else {
RawBlockId {
hash: value.hash.into(),
part_set_header: Some(value.part_set_header.into()),
}
}
}
}
impl TryFrom<RawCanonicalBlockId> for Id {
type Error = Error;
fn try_from(value: RawCanonicalBlockId) -> Result<Self, Self::Error> {
if value.part_set_header.is_none() {
return Err(Kind::InvalidPartSetHeader
.context("part_set_header is None")
.into());
}
Ok(Self {
hash: value.hash.try_into()?,
part_set_header: value.part_set_header.unwrap().try_into()?,
})
}
}
impl From<Id> for RawCanonicalBlockId {
fn from(value: Id) -> Self {
RawCanonicalBlockId {
hash: value.hash.as_bytes().to_vec(),
part_set_header: Some(value.part_set_header.into()),
}
}
}
impl Id {
pub fn prefix(&self) -> String {
let mut result = self.to_string();
result.truncate(PREFIX_LENGTH);
result
}
}
impl Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.hash)
}
}
impl FromStr for Id {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Ok(Self {
hash: Hash::from_hex_upper(Algorithm::Sha256, s)?,
part_set_header: PartSetHeader::default(),
})
}
}
pub trait ParseId {
fn parse_block_id(&self) -> Result<Id, Error>;
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_SHA256_ID: &str =
"26C0A41F3243C6BCD7AD2DFF8A8D83A71D29D307B5326C227F734A1A512FE47D";
#[test]
fn parses_hex_strings() {
let id = Id::from_str(EXAMPLE_SHA256_ID).unwrap();
assert_eq!(
id.hash.as_bytes(),
b"\x26\xC0\xA4\x1F\x32\x43\xC6\xBC\xD7\xAD\x2D\xFF\x8A\x8D\x83\xA7\
\x1D\x29\xD3\x07\xB5\x32\x6C\x22\x7F\x73\x4A\x1A\x51\x2F\xE4\x7D"
.as_ref()
);
}
#[test]
fn serializes_hex_strings() {
let id = Id::from_str(EXAMPLE_SHA256_ID).unwrap();
assert_eq!(&id.to_string(), EXAMPLE_SHA256_ID)
}
}