1use crate::*;
2use std::fmt::Debug;
3use std::time::Duration;
4
5#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Hash)]
6pub enum Timer {
7 Nomination = 0,
8 Ballot = 1,
9}
10
11#[derive(Debug, PartialEq, PartialOrd, Clone)]
12pub enum DriverCallback {
13 BallotTimerExpired {
14 slot_index: SlotIndex,
15 },
16 NominationTimerExpired {
17 slot_index: SlotIndex,
18 value: thruput_scp::SCPValue,
19 prev_value: thruput_scp::SCPValue,
20 },
21}
22
23impl std::fmt::Display for DriverCallback {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 match self {
26 DriverCallback::BallotTimerExpired { slot_index } => {
27 write!(f, "Ballot{{ i: {} }}", slot_index)
28 }
29 DriverCallback::NominationTimerExpired {
30 slot_index,
31 value,
32 prev_value,
33 } => {
34 write!(
35 f,
36 "Nomination{{ i: {} | X: {} | X': {} }}",
37 slot_index,
38 hah(value),
39 hah(prev_value)
40 )
41 }
42 }
43 }
45}
46#[allow(clippy::wrong_self_convention)]
47pub trait Driver: Clone + Debug {
48 fn get_qset(
49 &self,
50 h: &thruput_scp::SCPHash,
51 ) -> Option<thruput_scp::SCPQuorumSet>;
52 fn to_short_string(&self, pk: &thruput_scp::SCPNodeID) -> String;
53 fn to_str_key(&self, pk: &thruput_scp::SCPNodeID, full_key: bool)
54 -> String;
55 fn from_str_key(&self, s: &str) -> Result<thruput_scp::SCPNodeID, String>; fn get_value_string(&self, value: &thruput_scp::SCPValue) -> String;
57 fn get_hash_of(&self, opaque: &[u8]) -> thruput_scp::SCPHash;
58
59 fn get_node_weight(
60 &self,
61 node_id: &thruput_scp::SCPNodeID,
62 qset: &thruput_scp::SCPQuorumSet,
63 is_local_node: bool,
64 ) -> u64;
65 fn compute_hash_node(
66 &self,
67 slot_index: thruput_scp::SCPSlotIndex,
68 prev: &thruput_scp::SCPValue,
69 is_priority: bool,
70 round_number: thruput_scp::SCPCount,
71 node_id: &thruput_scp::SCPNodeID,
72 ) -> u64;
73 fn compute_value_hash(
74 &self,
75 slot_index: thruput_scp::SCPSlotIndex,
76 prev: &thruput_scp::SCPValue,
77 round_number: thruput_scp::SCPCount,
78 value: &thruput_scp::SCPValue,
79 ) -> u64;
80 fn sign_envelope(&self, env: &mut thruput_scp::SCPEnvelope);
81
82 fn emit_envelope(&self, env: thruput_scp::SCPEnvelope);
83 fn compute_timeout(
84 &self,
85 round_number: thruput_scp::SCPCount,
86 is_nomination: bool,
87 ) -> Duration;
88 fn start_timer(
89 &self,
90 slot_index: SlotIndex,
91 timer: Timer,
92 timeout: Duration,
93 cb: DriverCallback,
94 );
95 fn stop_timer(&self, slot_index: SlotIndex, timer: Timer);
96}
97
98pub trait DriverCb {
120 fn received_envelope(&mut self, env: &thruput_scp::SCPEnvelope);
123 fn driver_calling_back(&mut self, cb: DriverCallback);
126 fn set_state_from_envelope(
130 &mut self,
131 slot_index: SlotIndex,
132 envelope: &thruput_scp::SCPEnvelope,
133 );
134}
135
136pub const HASH_N: u32 = 1;
138pub const HASH_P: u32 = 2;
139pub const HASH_K: u32 = 3;