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