Skip to main content

scp_kernel/
driver.rs

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        //
44    }
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>; // XXX: get from KeyUtils.cpp
56    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
98/// The entry points through which the application drives the protocol.
99///
100/// The kernel does not verify envelope signatures. Every envelope passed to
101/// [`received_envelope`](Self::received_envelope) or
102/// [`set_state_from_envelope`](Self::set_state_from_envelope) must already be
103/// authenticated by the caller, normally with [`scp_util::verify_envelope`],
104/// which checks the signature against the statement's `node_id`:
105///
106/// ```no_run
107/// # fn forward(
108/// #     kernel: &mut impl scp_kernel::DriverCb,
109/// #     env: &thruput_scp::SCPEnvelope,
110/// # ) {
111/// if scp_util::verify_envelope(env).is_ok() {
112///     kernel.received_envelope(env);
113/// }
114/// # }
115/// ```
116///
117/// An unverified envelope lets whoever sent it speak for any node in the
118/// quorum set.
119pub trait DriverCb {
120    /// Processes an envelope received from a peer. The envelope must be
121    /// authenticated; see the trait documentation.
122    fn received_envelope(&mut self, env: &thruput_scp::SCPEnvelope);
123    /// Delivers an expired nomination or ballot timer, with the callback the
124    /// driver was given in [`Driver::start_timer`].
125    fn driver_calling_back(&mut self, cb: DriverCallback);
126    /// Restores a slot's state from one of this node's own earlier envelopes,
127    /// for example after a restart. The envelope must be authenticated like
128    /// any other.
129    fn set_state_from_envelope(
130        &mut self,
131        slot_index: SlotIndex,
132        envelope: &thruput_scp::SCPEnvelope,
133    );
134}
135
136// values used to switch hash function between priority and neighborhood checks
137pub const HASH_N: u32 = 1;
138pub const HASH_P: u32 = 2;
139pub const HASH_K: u32 = 3;