commonware_consensus/simplex/
config.rs

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