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
use crate::chain::Id as ChainId;
use crate::{block, Time};
use crate::{Error, Kind::*};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use tendermint_proto::types::CanonicalVote as RawCanonicalVote;
use tendermint_proto::Protobuf;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(try_from = "RawCanonicalVote", into = "RawCanonicalVote")]
pub struct CanonicalVote {
pub vote_type: super::Type,
pub height: block::Height,
pub round: block::Round,
pub block_id: Option<block::Id>,
pub timestamp: Option<Time>,
pub chain_id: ChainId,
}
impl Protobuf<RawCanonicalVote> for CanonicalVote {}
impl TryFrom<RawCanonicalVote> for CanonicalVote {
type Error = Error;
fn try_from(value: RawCanonicalVote) -> Result<Self, Self::Error> {
if value.timestamp.is_none() {
return Err(NoTimestamp.into());
}
if value.round > i32::MAX as i64 {
return Err(IntegerOverflow.into());
}
let block_id = value.block_id.filter(|i| !i.hash.is_empty());
Ok(CanonicalVote {
vote_type: value.r#type.try_into()?,
height: value.height.try_into()?,
round: (value.round as i32).try_into()?,
block_id: block_id.map(TryInto::try_into).transpose()?,
timestamp: value.timestamp.map(TryInto::try_into).transpose()?,
chain_id: ChainId::try_from(value.chain_id)?,
})
}
}
impl From<CanonicalVote> for RawCanonicalVote {
fn from(value: CanonicalVote) -> Self {
let block_id = value.block_id.filter(|i| i != &block::Id::default());
RawCanonicalVote {
r#type: value.vote_type.into(),
height: value.height.into(),
round: value.round.value().into(),
block_id: block_id.map(Into::into),
timestamp: value.timestamp.map(Into::into),
chain_id: value.chain_id.to_string(),
}
}
}
impl CanonicalVote {
pub fn new(vote: super::Vote, chain_id: ChainId) -> CanonicalVote {
CanonicalVote {
vote_type: vote.vote_type,
height: vote.height,
round: vote.round,
block_id: vote.block_id,
timestamp: vote.timestamp,
chain_id,
}
}
}
#[cfg(test)]
mod tests {
use crate::vote::canonical_vote::CanonicalVote;
use crate::vote::Type;
use std::convert::TryFrom;
use tendermint_proto::google::protobuf::Timestamp;
use tendermint_proto::types::CanonicalBlockId as RawCanonicalBlockId;
use tendermint_proto::types::CanonicalPartSetHeader as RawCanonicalPartSetHeader;
use tendermint_proto::types::CanonicalVote as RawCanonicalVote;
#[test]
fn canonical_vote_domain_checks() {
let proto_cp = RawCanonicalVote {
r#type: 1,
height: 2,
round: 4,
block_id: Some(RawCanonicalBlockId {
hash: vec![],
part_set_header: Some(RawCanonicalPartSetHeader {
total: 1,
hash: vec![1],
}),
}),
timestamp: Some(Timestamp {
seconds: 0,
nanos: 0,
}),
chain_id: "testchain".to_string(),
};
let cp = CanonicalVote::try_from(proto_cp).unwrap();
assert_eq!(cp.vote_type, Type::Prevote);
assert!(cp.block_id.is_none());
assert!(cp.timestamp.is_some());
let mut proto_cp: RawCanonicalVote = cp.into();
proto_cp.timestamp = None;
assert!(CanonicalVote::try_from(proto_cp).is_err());
}
}