commonware_consensus/threshold_simplex/
config.rs

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