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
use crate::{account, Signature, Time};
use crate::{Error, Kind};
use num_traits::ToPrimitive;
use std::convert::{TryFrom, TryInto};
use tendermint_proto::types::BlockIdFlag;
use tendermint_proto::types::CommitSig as RawCommitSig;
#[derive(Clone, Debug, PartialEq)]
pub enum CommitSig {
BlockIdFlagAbsent,
BlockIdFlagCommit {
validator_address: account::Id,
timestamp: Time,
signature: Signature,
},
BlockIdFlagNil {
validator_address: account::Id,
timestamp: Time,
signature: Signature,
},
}
impl CommitSig {
pub fn validator_address(&self) -> Option<account::Id> {
match self {
Self::BlockIdFlagCommit {
validator_address, ..
} => Some(*validator_address),
Self::BlockIdFlagNil {
validator_address, ..
} => Some(*validator_address),
_ => None,
}
}
pub fn is_absent(&self) -> bool {
self == &Self::BlockIdFlagAbsent
}
pub fn is_commit(&self) -> bool {
matches!(self, Self::BlockIdFlagCommit { .. })
}
pub fn is_nil(&self) -> bool {
matches!(self, Self::BlockIdFlagNil { .. })
}
}
impl TryFrom<RawCommitSig> for CommitSig {
type Error = Error;
fn try_from(value: RawCommitSig) -> Result<Self, Self::Error> {
if value.block_id_flag == BlockIdFlag::Absent.to_i32().unwrap() {
if value.timestamp.is_some() {
let timestamp = value.timestamp.unwrap();
if timestamp.nanos != 0 || timestamp.seconds != -62135596800 {
return Err(Kind::InvalidTimestamp
.context("absent commitsig has non-zero timestamp")
.into());
}
}
if !value.signature.is_empty() {
return Err(Kind::InvalidSignature.into());
}
return Ok(CommitSig::BlockIdFlagAbsent);
}
if value.block_id_flag == BlockIdFlag::Commit.to_i32().unwrap() {
if value.signature.is_empty() {
return Err(Kind::InvalidSignature
.context("regular commitsig has no signature")
.into());
}
if value.validator_address.is_empty() {
return Err(Kind::InvalidValidatorAddress.into());
}
return Ok(CommitSig::BlockIdFlagCommit {
validator_address: value.validator_address.try_into()?,
timestamp: value.timestamp.ok_or(Kind::NoTimestamp)?.try_into()?,
signature: value.signature.try_into()?,
});
}
if value.block_id_flag == BlockIdFlag::Nil.to_i32().unwrap() {
if value.signature.is_empty() {
return Err(Kind::InvalidSignature
.context("nil commitsig has no signature")
.into());
}
if value.validator_address.is_empty() {
return Err(Kind::InvalidValidatorAddress.into());
}
return Ok(CommitSig::BlockIdFlagNil {
validator_address: value.validator_address.try_into()?,
timestamp: value.timestamp.ok_or(Kind::NoTimestamp)?.try_into()?,
signature: value.signature.try_into()?,
});
}
Err(Kind::BlockIdFlag.into())
}
}
impl From<CommitSig> for RawCommitSig {
fn from(commit: CommitSig) -> RawCommitSig {
match commit {
CommitSig::BlockIdFlagAbsent => RawCommitSig {
block_id_flag: BlockIdFlag::Absent.to_i32().unwrap(),
validator_address: Vec::new(),
timestamp: None,
signature: Vec::new(),
},
CommitSig::BlockIdFlagNil {
validator_address,
timestamp,
signature,
} => RawCommitSig {
block_id_flag: BlockIdFlag::Nil.to_i32().unwrap(),
validator_address: validator_address.into(),
timestamp: Some(timestamp.into()),
signature: signature.into(),
},
CommitSig::BlockIdFlagCommit {
validator_address,
timestamp,
signature,
} => RawCommitSig {
block_id_flag: BlockIdFlag::Commit.to_i32().unwrap(),
validator_address: validator_address.into(),
timestamp: Some(timestamp.into()),
signature: signature.into(),
},
}
}
}