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
use aptos_crypto_derive::{BCSCryptoHash, CryptoHasher};
use aptos_types::block_info::BlockInfo;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, CryptoHasher, BCSCryptoHash)]
pub struct VoteData {
proposed: BlockInfo,
parent: BlockInfo,
}
impl Display for VoteData {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"VoteData: [block id: {}, epoch: {}, round: {:02}, timestamp: {},\
parent_block_id: {}, parent_block_round: {:02}, parent_timestamp: {}]",
self.proposed().id(),
self.proposed().epoch(),
self.proposed().round(),
self.proposed().timestamp_usecs(),
self.parent().id(),
self.parent().round(),
self.parent.timestamp_usecs()
)
}
}
impl VoteData {
pub fn new(proposed: BlockInfo, parent: BlockInfo) -> Self {
Self { proposed, parent }
}
pub fn parent(&self) -> &BlockInfo {
&self.parent
}
pub fn proposed(&self) -> &BlockInfo {
&self.proposed
}
pub fn verify(&self) -> anyhow::Result<()> {
anyhow::ensure!(
self.parent.epoch() == self.proposed.epoch(),
"Parent and proposed epochs do not match",
);
anyhow::ensure!(
self.parent.round() < self.proposed.round(),
"Proposed round is less than parent round",
);
anyhow::ensure!(
self.parent.timestamp_usecs() <= self.proposed.timestamp_usecs(),
"Proposed happened before parent",
);
anyhow::ensure!(
self.proposed.version() == 0 || self.parent.version() <= self.proposed.version(),
"Proposed version is less than parent version",
);
Ok(())
}
pub fn is_for_nil(&self) -> bool {
self.proposed().timestamp_usecs() == self.parent().timestamp_usecs()
}
}