ab_client_proof_of_time/source/
timekeeper.rs1#[cfg(test)]
2mod tests;
3
4use crate::PotNextSlotInput;
5use crate::source::state::PotState;
6use crate::verifier::PotVerifier;
7use ab_core_primitives::pot::{PotCheckpoints, PotSeed, SlotDuration, SlotNumber};
8use ab_proof_of_time::PotError;
9use futures::SinkExt;
10use futures::channel::mpsc;
11use futures::executor::block_on;
12use std::num::NonZeroU32;
13use std::thread::sleep;
14use std::time::Instant;
15use tracing::{debug, trace};
16use triomphe::Arc;
17
18#[derive(Debug, Copy, Clone)]
20pub struct TimekeeperProof {
21 pub slot: SlotNumber,
23 pub seed: PotSeed,
25 pub slot_iterations: NonZeroU32,
27 pub checkpoints: PotCheckpoints,
29}
30
31#[derive(Debug)]
33#[must_use = "Doesn't do anything unless run() method is called"]
34pub struct Timekeeper {
35 state: Arc<PotState>,
36 pot_verifier: PotVerifier,
37 proof_sender: mpsc::Sender<TimekeeperProof>,
38 slot_duration: SlotDuration,
39}
40
41impl Timekeeper {
42 pub fn new(
44 state: Arc<PotState>,
45 pot_verifier: PotVerifier,
46 slot_duration: SlotDuration,
47 ) -> (Self, mpsc::Receiver<TimekeeperProof>) {
48 let (proof_sender, proof_receiver) = mpsc::channel(1);
49
50 (
51 Self {
52 state,
53 pot_verifier,
54 proof_sender,
55 slot_duration,
56 },
57 proof_receiver,
58 )
59 }
60
61 pub fn run(self) -> Result<(), PotError> {
65 let Self {
66 state,
67 pot_verifier,
68 mut proof_sender,
69 slot_duration,
70 } = self;
71
72 Self::try_debug_run(&state, &pot_verifier, &mut proof_sender, slot_duration)?;
73
74 let mut next_slot_input = state.next_slot_input();
75
76 loop {
77 let Some(new_next_slot_input) =
78 Self::prove_slot(&state, &pot_verifier, &mut proof_sender, next_slot_input)?
79 else {
80 return Ok(());
81 };
82 next_slot_input = new_next_slot_input;
83 }
84 }
85
86 #[inline(never)]
92 fn try_debug_run(
93 state: &Arc<PotState>,
94 pot_verifier: &PotVerifier,
95 proof_sender: &mut mpsc::Sender<TimekeeperProof>,
96 slot_duration: SlotDuration,
97 ) -> Result<(), PotError> {
98 let mut next_slot_input = state.next_slot_input();
99
100 let delay = {
101 let start = Instant::now();
102 ab_proof_of_time::prove(next_slot_input.seed, next_slot_input.slot_iterations)?;
103 let duration = start.elapsed();
104
105 if duration.is_zero() || duration > slot_duration.as_duration() / 2 {
106 return Ok(());
108 }
109
110 slot_duration.as_duration().saturating_sub(duration)
111 };
112
113 loop {
114 let Some(new_next_slot_input) =
115 Self::prove_slot(state, pot_verifier, proof_sender, next_slot_input)?
116 else {
117 return Ok(());
118 };
119 next_slot_input = new_next_slot_input;
120
121 sleep(delay);
123 }
124 }
125
126 fn prove_slot(
131 state: &PotState,
132 pot_verifier: &PotVerifier,
133 proof_sender: &mut mpsc::Sender<TimekeeperProof>,
134 next_slot_input: PotNextSlotInput,
135 ) -> Result<Option<PotNextSlotInput>, PotError> {
136 trace!(
137 "Proving for slot {} with {} iterations",
138 next_slot_input.slot, next_slot_input.slot_iterations
139 );
140 let checkpoints =
141 ab_proof_of_time::prove(next_slot_input.seed, next_slot_input.slot_iterations)?;
142
143 let proof = TimekeeperProof {
144 seed: next_slot_input.seed,
145 slot_iterations: next_slot_input.slot_iterations,
146 slot: next_slot_input.slot,
147 checkpoints,
148 };
149
150 pot_verifier.inject_verified_checkpoints(
151 next_slot_input.seed,
152 next_slot_input.slot_iterations,
153 checkpoints,
154 );
155
156 match state.try_extend(
157 next_slot_input,
158 next_slot_input.slot,
159 checkpoints.output(),
160 None,
161 ) {
162 Ok(new_next_slot_input) => {
163 if let Err(error) = proof_sender.try_send(proof)
164 && let Err(error) = block_on(proof_sender.send(error.into_inner()))
165 {
166 debug!(%error, "Couldn't send proof, the channel is closed");
167 return Ok(None);
168 }
169
170 Ok(Some(new_next_slot_input))
171 }
172 Err(existing_next_slot_input) => {
173 trace!(
176 slot = %next_slot_input.slot,
177 "Proof of time chain was changed while proving, not sending the proof",
178 );
179
180 Ok(Some(existing_next_slot_input))
181 }
182 }
183 }
184}