heika_finalization/
signer.rs1use heika_block::finalization::message::{FinalizationMessage, SIGNATURE_SIZE};
8use heika_block::finalization::round::Candidate;
9use heika_block::finalization::{FinalizationRound, FinalizedBlockHeader};
10use heika_core::hash::Hash256;
11
12use crate::Settings;
13use crate::orchestrator::MessageFactory;
14use crate::prevote::prevote_range;
15
16pub trait VotingKeys {
18 fn sign(&mut self, epoch: u32, signed: &[u8]) -> Option<[u8; SIGNATURE_SIZE]>;
21}
22
23pub struct Signer<'a> {
25 pub keys: &'a mut dyn VotingKeys,
26 pub settings: Settings,
27 pub finalized: FinalizedBlockHeader,
29 pub chain_height: u64,
31 pub hashes: &'a dyn Fn(u64, u64) -> Vec<Hash256>,
33}
34
35impl MessageFactory for Signer<'_> {
36 fn prevote(&mut self, round: FinalizationRound) -> Option<FinalizationMessage> {
37 let (from, count) = prevote_range(
38 round,
39 self.finalized.height,
40 self.chain_height,
41 &self.settings,
42 );
43 let mut hashes = (self.hashes)(from, count);
44 if hashes.is_empty() {
45 hashes = vec![self.finalized.hash];
46 }
47 let keys = &mut *self.keys;
48 FinalizationMessage::sign(round, true, from, hashes, |signed| {
49 keys.sign(round.epoch, signed)
50 })
51 }
52
53 fn precommit(
54 &mut self,
55 round: FinalizationRound,
56 target: Candidate,
57 ) -> Option<FinalizationMessage> {
58 let keys = &mut *self.keys;
59 FinalizationMessage::sign(round, false, target.0, vec![target.1], |signed| {
60 keys.sign(round.epoch, signed)
61 })
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use heika_core::voting::VotingKeyTree;
68
69 use super::*;
70 use crate::stage::tests::block_hash;
71
72 const SETTINGS: Settings = Settings {
73 step_duration: 1000,
74 prevote_blocks_multiple: 4,
75 max_hashes_per_point: 256,
76 voting_set_grouping: 100,
77 };
78
79 struct Tree(VotingKeyTree);
81
82 impl VotingKeys for Tree {
83 fn sign(&mut self, epoch: u32, signed: &[u8]) -> Option<[u8; SIGNATURE_SIZE]> {
84 self.0.sign(epoch, &[signed])
85 }
86 }
87
88 fn tree() -> Tree {
89 Tree(VotingKeyTree::generate(&mut rand_core::UnwrapErr(getrandom::SysRng), 2, 4).unwrap())
90 }
91
92 fn chain_hashes(from: u64, count: u64) -> Vec<Hash256> {
93 (from..from + count)
95 .take_while(|height| *height <= 150)
96 .map(|height| block_hash(height, 0))
97 .collect()
98 }
99
100 fn signer<'a>(keys: &'a mut Tree, chain_height: u64) -> Signer<'a> {
101 Signer {
102 keys,
103 settings: SETTINGS,
104 finalized: FinalizedBlockHeader {
105 round: FinalizationRound { epoch: 3, point: 1 },
106 height: 140,
107 hash: block_hash(140, 0),
108 },
109 chain_height,
110 hashes: &chain_hashes,
111 }
112 }
113
114 #[test]
115 fn prevotes_from_the_finalized_block() {
116 let mut keys = tree();
117 let round = FinalizationRound { epoch: 3, point: 2 };
118 let message = signer(&mut keys, 150).prevote(round).unwrap();
119 assert!(message.is_prevote);
120 assert_eq!(message.round, round);
121 assert_eq!((message.height, message.last_height()), (140, 144));
123 assert_eq!(message.hashes[0], block_hash(140, 0));
124 assert!(message.verify_signature());
125 assert_eq!(message.root_key(), keys.0.public_key().0);
126 }
127
128 #[test]
129 fn prevotes_only_the_finalized_block_without_newer_ones() {
130 let mut keys = tree();
131 let round = FinalizationRound { epoch: 3, point: 2 };
132 let message = signer(&mut keys, 141).prevote(round).unwrap();
133 assert_eq!(message.hashes, [block_hash(140, 0)]);
134 }
135
136 #[test]
137 fn precommits_the_target() {
138 let mut keys = tree();
139 let round = FinalizationRound { epoch: 3, point: 2 };
140 let target = (143, block_hash(143, 0));
141 let message = signer(&mut keys, 150).precommit(round, target).unwrap();
142 assert!(!message.is_prevote);
143 assert_eq!(
144 (message.height, message.hashes.clone()),
145 (143, vec![target.1])
146 );
147 assert!(message.verify_signature());
148 }
149
150 #[test]
151 fn makes_nothing_without_a_key_for_the_epoch() {
152 let mut keys = tree();
153 let late = FinalizationRound { epoch: 5, point: 1 };
154 assert!(signer(&mut keys, 150).prevote(late).is_none());
155 let round = FinalizationRound { epoch: 4, point: 1 };
157 assert!(signer(&mut keys, 150).prevote(round).is_some());
158 let earlier = FinalizationRound { epoch: 3, point: 9 };
159 assert!(
160 signer(&mut keys, 150)
161 .precommit(earlier, (143, block_hash(143, 0)))
162 .is_none()
163 );
164 }
165}