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