pathfinder_consensus/
config.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4use malachite_types::{ThresholdParams, TimeoutKind};
5
6/// The configuration for the consensus engine.
7#[derive(Clone, Debug, Default)]
8pub struct Config<A> {
9    /// The address of the validator.
10    pub address: A,
11    /// The initial height.
12    pub initial_height: u64,
13    /// The timeout configuration.
14    pub timeout_values: TimeoutValues,
15    /// The number of completed heights to keep in memory.
16    pub history_depth: u64,
17    /// The directory to store the write-ahead log.
18    pub wal_dir: PathBuf,
19    /// The tendermint threshold parameters (not exposed to the outside, yet)
20    pub(crate) threshold_params: ThresholdParams,
21}
22
23impl<A: Default> Config<A> {
24    /// Create a new consensus config with the default values for a single
25    /// validator.
26    pub fn new(address: A) -> Self {
27        Self {
28            address,
29            history_depth: 10,
30            wal_dir: PathBuf::from("wal"),
31            ..Default::default()
32        }
33    }
34
35    /// Set the initial height.
36    pub fn with_initial_height(mut self, height: u64) -> Self {
37        self.initial_height = height;
38        self
39    }
40
41    /// Set the timeout values.
42    pub fn with_timeout_values(mut self, timeout_values: TimeoutValues) -> Self {
43        self.timeout_values = timeout_values;
44        self
45    }
46
47    /// Set the number of completed heights to keep in memory.
48    pub fn with_history_depth(mut self, history_depth: u64) -> Self {
49        self.history_depth = history_depth;
50        self
51    }
52
53    /// Set the WAL directory.
54    pub fn with_wal_dir(mut self, wal_dir: PathBuf) -> Self {
55        self.wal_dir = wal_dir;
56        self
57    }
58}
59
60/// The timeout values for the consensus engine.
61#[derive(Debug, Clone)]
62pub struct TimeoutValues {
63    /// The timeout for the propose step.
64    pub propose: Duration,
65    /// The timeout for the prevote step.
66    pub prevote: Duration,
67    /// The timeout for the precommit step.
68    pub precommit: Duration,
69    /// Timeout to rebroadcast the last prevote.
70    pub rebroadcast: Duration,
71}
72
73impl Default for TimeoutValues {
74    fn default() -> Self {
75        Self {
76            propose: Duration::from_secs(10),
77            prevote: Duration::from_secs(10),
78            precommit: Duration::from_secs(10),
79            rebroadcast: Duration::from_secs(10),
80        }
81    }
82}
83
84impl TimeoutValues {
85    /// Get the timeout for a given timeout kind.
86    pub fn get(&self, kind: TimeoutKind) -> Duration {
87        match kind {
88            TimeoutKind::Propose => self.propose,
89            TimeoutKind::Prevote => self.prevote,
90            TimeoutKind::Precommit => self.precommit,
91            TimeoutKind::Rebroadcast => self.rebroadcast,
92        }
93    }
94}