commonware_consensus/simplex/
config.rs

1use super::types::{Activity, Context, View};
2use crate::{Automaton, Relay, Reporter, Supervisor};
3use commonware_cryptography::{Digest, Signer};
4use commonware_runtime::buffer::PoolRef;
5use governor::Quota;
6use std::{num::NonZeroUsize, time::Duration};
7
8/// Configuration for the consensus engine.
9pub struct Config<
10    C: Signer,
11    D: Digest,
12    A: Automaton<Context = Context<D>, Digest = D>,
13    R: Relay<Digest = D>,
14    F: Reporter<Activity = Activity<C::Signature, D>>,
15    S: Supervisor<Index = View>,
16> {
17    /// Cryptographic primitives.
18    pub crypto: C,
19
20    /// Automaton for the consensus engine.
21    pub automaton: A,
22
23    /// Relay for the consensus engine.
24    pub relay: R,
25
26    /// Reporter for the consensus engine.
27    pub reporter: F,
28
29    /// Supervisor for the consensus engine.
30    pub supervisor: S,
31
32    /// Partition for consensus engine storage.
33    pub partition: String,
34
35    /// Compression level for consensus engine storage.
36    pub compression: Option<u8>,
37
38    /// Maximum number of messages to buffer on channels inside the consensus
39    /// engine before blocking.
40    pub mailbox_size: usize,
41
42    /// Prefix for all signed messages to prevent replay attacks.
43    pub namespace: Vec<u8>,
44
45    /// Number of bytes to buffer when replaying during startup.
46    pub replay_buffer: NonZeroUsize,
47
48    /// The size of the write buffer to use for each blob in the journal.
49    pub write_buffer: NonZeroUsize,
50
51    /// Buffer pool for the journal.
52    pub buffer_pool: PoolRef,
53
54    /// Amount of time to wait for a leader to propose a payload
55    /// in a view.
56    pub leader_timeout: Duration,
57
58    /// Amount of time to wait for a quorum of notarizations in a view
59    /// before attempting to skip the view.
60    pub notarization_timeout: Duration,
61
62    /// Amount of time to wait before retrying a nullify broadcast if
63    /// stuck in a view.
64    pub nullify_retry: Duration,
65
66    /// Number of views behind finalized tip to track
67    /// and persist activity derived from validator messages.
68    pub activity_timeout: View,
69
70    /// Maximum number of participants to track in a single round.
71    ///
72    /// This is used to limit the size of notarization, nullification, and finalization messages,
73    /// which include up to one signature per participant. This number can be set to a reasonably high
74    /// value that we never expect to reach (it is just how many signatures we are willing to parse, not verify).
75    pub max_participants: usize,
76
77    /// Move to nullify immediately if the selected leader has been inactive
78    /// for this many views.
79    ///
80    /// This number should be less than or equal to `activity_timeout` (how
81    /// many views we are tracking).
82    pub skip_timeout: View,
83
84    /// Timeout to wait for a peer to respond to a request.
85    pub fetch_timeout: Duration,
86
87    /// Maximum number of notarizations/nullifications to request/respond with at once.
88    pub max_fetch_count: usize,
89
90    /// Maximum rate of requests to send to a given peer.
91    ///
92    /// Inbound rate limiting is handled by `commonware-p2p`.
93    pub fetch_rate_per_peer: Quota,
94
95    /// Number of concurrent requests to make at once.
96    pub fetch_concurrent: usize,
97}
98
99impl<
100        C: Signer,
101        D: Digest,
102        A: Automaton<Context = Context<D>, Digest = D>,
103        R: Relay<Digest = D>,
104        F: Reporter<Activity = Activity<C::Signature, D>>,
105        S: Supervisor<Index = View>,
106    > Config<C, D, A, R, F, S>
107{
108    /// Assert enforces that all configuration values are valid.
109    pub fn assert(&self) {
110        assert!(
111            self.leader_timeout > Duration::default(),
112            "leader timeout must be greater than zero"
113        );
114        assert!(
115            self.notarization_timeout > Duration::default(),
116            "notarization timeout must be greater than zero"
117        );
118        assert!(
119            self.leader_timeout <= self.notarization_timeout,
120            "leader timeout must be less than or equal to notarization timeout"
121        );
122        assert!(
123            self.nullify_retry > Duration::default(),
124            "nullify retry broadcast must be greater than zero"
125        );
126        assert!(
127            self.activity_timeout > 0,
128            "activity timeout must be greater than zero"
129        );
130        assert!(
131            self.skip_timeout > 0,
132            "skip timeout must be greater than zero"
133        );
134        assert!(
135            self.skip_timeout <= self.activity_timeout,
136            "skip timeout must be less than or equal to activity timeout"
137        );
138        assert!(
139            self.fetch_timeout > Duration::default(),
140            "fetch timeout must be greater than zero"
141        );
142        assert!(
143            self.max_fetch_count > 0,
144            "it must be possible to fetch at least one container per request"
145        );
146        assert!(
147            self.fetch_concurrent > 0,
148            "it must be possible to fetch from at least one peer at a time"
149        );
150    }
151}