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