consensus_config/
parameters.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{path::PathBuf, time::Duration};
5
6use serde::{Deserialize, Serialize};
7
8/// Operational configurations of a consensus authority.
9///
10/// All fields should tolerate inconsistencies among authorities, without affecting safety of the
11/// protocol. Otherwise, they need to be part of Sui protocol config or epoch state on-chain.
12///
13/// NOTE: fields with default values are specified in the serde default functions. Most operators
14/// should not need to specify any field, except db_path.
15#[derive(Clone, Debug, Deserialize, Serialize)]
16pub struct Parameters {
17    /// Path to consensus DB for this epoch. Required when initializing consensus.
18    /// This is calculated based on user configuration for base directory.
19    #[serde(skip)]
20    pub db_path: PathBuf,
21
22    /// Time to wait for parent round leader before sealing a block, from when parent round
23    /// has a quorum.
24    #[serde(default = "Parameters::default_leader_timeout")]
25    pub leader_timeout: Duration,
26
27    /// Minimum delay between rounds, to avoid generating too many rounds when latency is low.
28    /// This is especially necessary for tests running locally.
29    /// If setting a non-default value, it should be set low enough to avoid reducing
30    /// round rate and increasing latency in realistic and distributed configurations.
31    #[serde(default = "Parameters::default_min_round_delay")]
32    pub min_round_delay: Duration,
33
34    /// Maximum forward time drift (how far in future) allowed for received blocks.
35    #[serde(default = "Parameters::default_max_forward_time_drift")]
36    pub max_forward_time_drift: Duration,
37
38    /// Max number of blocks to fetch per block sync request.
39    /// Block sync requests have very short (~2s) timeouts.
40    /// So this value should be limited to allow the requests
41    /// to finish on hosts with good network with this timeout.
42    /// Usually a host sends 14-16 blocks per sec to a peer, so
43    /// sending 32 blocks in 2 seconds should be reasonable.
44    #[serde(default = "Parameters::default_max_blocks_per_sync")]
45    pub max_blocks_per_sync: usize,
46
47    /// Max number of blocks to fetch per commit sync request.
48    #[serde(default = "Parameters::default_max_blocks_per_fetch")]
49    pub max_blocks_per_fetch: usize,
50
51    /// Time to wait during node start up until the node has synced the last proposed block via the
52    /// network peers. When set to `0` the sync mechanism is disabled. This property is meant to be
53    /// used for amnesia recovery.
54    #[serde(default = "Parameters::default_sync_last_known_own_block_timeout")]
55    pub sync_last_known_own_block_timeout: Duration,
56
57    /// Interval in milliseconds to probe highest received rounds of peers.
58    #[serde(default = "Parameters::default_round_prober_interval_ms")]
59    pub round_prober_interval_ms: u64,
60
61    /// Timeout in milliseconds for a round prober request.
62    #[serde(default = "Parameters::default_round_prober_request_timeout_ms")]
63    pub round_prober_request_timeout_ms: u64,
64
65    /// Proposing new block is stopped when the propagation delay is greater than this threshold.
66    /// Propagation delay is the difference between the round of the last proposed block and the
67    /// the highest round from this authority that is received by all validators in a quorum.
68    #[serde(default = "Parameters::default_propagation_delay_stop_proposal_threshold")]
69    pub propagation_delay_stop_proposal_threshold: u32,
70
71    /// The number of rounds of blocks to be kept in the Dag state cache per authority. The larger
72    /// the number the more the blocks that will be kept in memory allowing minimising any potential
73    /// disk access.
74    /// Value should be at minimum 50 rounds to ensure node performance, but being too large can be
75    /// expensive in memory usage.
76    #[serde(default = "Parameters::default_dag_state_cached_rounds")]
77    pub dag_state_cached_rounds: u32,
78
79    // Number of authorities commit syncer fetches in parallel.
80    // Both commits in a range and blocks referenced by the commits are fetched per authority.
81    #[serde(default = "Parameters::default_commit_sync_parallel_fetches")]
82    pub commit_sync_parallel_fetches: usize,
83
84    // Number of commits to fetch in a batch, also the maximum number of commits returned per fetch.
85    // If this value is set too small, fetching becomes inefficient.
86    // If this value is set too large, it can result in load imbalance and stragglers.
87    #[serde(default = "Parameters::default_commit_sync_batch_size")]
88    pub commit_sync_batch_size: u32,
89
90    // This affects the maximum number of commit batches being fetched, and those fetched but not
91    // processed as consensus output, before throttling of outgoing commit fetches starts.
92    #[serde(default = "Parameters::default_commit_sync_batches_ahead")]
93    pub commit_sync_batches_ahead: usize,
94
95    /// Tonic network settings.
96    #[serde(default = "TonicParameters::default")]
97    pub tonic: TonicParameters,
98
99    /// Internal consensus parameters.
100    #[serde(default = "InternalParameters::default")]
101    pub internal: InternalParameters,
102}
103
104impl Parameters {
105    pub(crate) fn default_leader_timeout() -> Duration {
106        Duration::from_millis(200)
107    }
108
109    pub(crate) fn default_min_round_delay() -> Duration {
110        if cfg!(msim) || std::env::var("__TEST_ONLY_CONSENSUS_USE_LONG_MIN_ROUND_DELAY").is_ok() {
111            // Checkpoint building and execution cannot keep up with high commit rate in simtests,
112            // leading to long reconfiguration delays. This is because simtest is single threaded,
113            // and spending too much time in consensus can lead to starvation elsewhere.
114            Duration::from_millis(400)
115        } else if cfg!(test) {
116            // Avoid excessive CPU, data and logs in tests.
117            Duration::from_millis(250)
118        } else {
119            Duration::from_millis(50)
120        }
121    }
122
123    pub(crate) fn default_max_forward_time_drift() -> Duration {
124        Duration::from_millis(500)
125    }
126
127    pub(crate) fn default_max_blocks_per_sync() -> usize {
128        if cfg!(msim) {
129            // Exercise hitting blocks per sync limit.
130            4
131        } else {
132            32
133        }
134    }
135
136    pub(crate) fn default_max_blocks_per_fetch() -> usize {
137        if cfg!(msim) {
138            // Exercise hitting blocks per fetch limit.
139            10
140        } else {
141            1000
142        }
143    }
144
145    pub(crate) fn default_sync_last_known_own_block_timeout() -> Duration {
146        if cfg!(msim) {
147            Duration::from_millis(500)
148        } else {
149            // Here we prioritise liveness over the complete de-risking of block equivocation. 5 seconds
150            // in the majority of cases should be good enough for this given a healthy network.
151            Duration::from_secs(5)
152        }
153    }
154
155    pub(crate) fn default_round_prober_interval_ms() -> u64 {
156        if cfg!(msim) { 1000 } else { 5000 }
157    }
158
159    pub(crate) fn default_round_prober_request_timeout_ms() -> u64 {
160        if cfg!(msim) { 800 } else { 4000 }
161    }
162
163    pub(crate) fn default_propagation_delay_stop_proposal_threshold() -> u32 {
164        // Propagation delay is usually 0 round in production.
165        if cfg!(msim) { 2 } else { 5 }
166    }
167
168    pub(crate) fn default_dag_state_cached_rounds() -> u32 {
169        if cfg!(msim) {
170            // Exercise reading blocks from store.
171            5
172        } else {
173            500
174        }
175    }
176
177    pub(crate) fn default_commit_sync_parallel_fetches() -> usize {
178        8
179    }
180
181    pub(crate) fn default_commit_sync_batch_size() -> u32 {
182        if cfg!(msim) {
183            // Exercise commit sync.
184            5
185        } else {
186            100
187        }
188    }
189
190    pub(crate) fn default_commit_sync_batches_ahead() -> usize {
191        // This is set to be a multiple of default commit_sync_parallel_fetches to allow fetching ahead,
192        // while keeping the total number of inflight fetches and unprocessed fetched commits limited.
193        32
194    }
195}
196
197impl Default for Parameters {
198    fn default() -> Self {
199        Self {
200            db_path: PathBuf::default(),
201            leader_timeout: Parameters::default_leader_timeout(),
202            min_round_delay: Parameters::default_min_round_delay(),
203            max_forward_time_drift: Parameters::default_max_forward_time_drift(),
204            max_blocks_per_sync: Parameters::default_max_blocks_per_sync(),
205            max_blocks_per_fetch: Parameters::default_max_blocks_per_fetch(),
206            sync_last_known_own_block_timeout:
207                Parameters::default_sync_last_known_own_block_timeout(),
208            round_prober_interval_ms: Parameters::default_round_prober_interval_ms(),
209            round_prober_request_timeout_ms: Parameters::default_round_prober_request_timeout_ms(),
210            propagation_delay_stop_proposal_threshold:
211                Parameters::default_propagation_delay_stop_proposal_threshold(),
212            dag_state_cached_rounds: Parameters::default_dag_state_cached_rounds(),
213            commit_sync_parallel_fetches: Parameters::default_commit_sync_parallel_fetches(),
214            commit_sync_batch_size: Parameters::default_commit_sync_batch_size(),
215            commit_sync_batches_ahead: Parameters::default_commit_sync_batches_ahead(),
216            tonic: TonicParameters::default(),
217            internal: InternalParameters::default(),
218        }
219    }
220}
221
222#[derive(Clone, Debug, Deserialize, Serialize)]
223pub struct TonicParameters {
224    /// Keepalive interval and timeouts for both client and server.
225    ///
226    /// If unspecified, this will default to 5s.
227    #[serde(default = "TonicParameters::default_keepalive_interval")]
228    pub keepalive_interval: Duration,
229
230    /// Size of various per-connection buffers.
231    ///
232    /// If unspecified, this will default to 32MiB.
233    #[serde(default = "TonicParameters::default_connection_buffer_size")]
234    pub connection_buffer_size: usize,
235
236    /// Messages over this size threshold will increment a counter.
237    ///
238    /// If unspecified, this will default to 16MiB.
239    #[serde(default = "TonicParameters::default_excessive_message_size")]
240    pub excessive_message_size: usize,
241
242    /// Hard message size limit for both requests and responses.
243    /// This value is higher than strictly necessary, to allow overheads.
244    /// Message size targets and soft limits are computed based on this value.
245    ///
246    /// If unspecified, this will default to 1GiB.
247    #[serde(default = "TonicParameters::default_message_size_limit")]
248    pub message_size_limit: usize,
249}
250
251impl TonicParameters {
252    fn default_keepalive_interval() -> Duration {
253        Duration::from_secs(10)
254    }
255
256    fn default_connection_buffer_size() -> usize {
257        32 << 20
258    }
259
260    fn default_excessive_message_size() -> usize {
261        16 << 20
262    }
263
264    fn default_message_size_limit() -> usize {
265        64 << 20
266    }
267}
268
269impl Default for TonicParameters {
270    fn default() -> Self {
271        Self {
272            keepalive_interval: TonicParameters::default_keepalive_interval(),
273            connection_buffer_size: TonicParameters::default_connection_buffer_size(),
274            excessive_message_size: TonicParameters::default_excessive_message_size(),
275            message_size_limit: TonicParameters::default_message_size_limit(),
276        }
277    }
278}
279
280/// Internal parameters unrelated to operating a consensus node in the real world.
281#[derive(Clone, Debug, Deserialize, Serialize)]
282pub struct InternalParameters {
283    /// Whether to skip equivocation validation, when testing with equivocators.
284    #[serde(default = "InternalParameters::default_skip_equivocation_validation")]
285    pub skip_equivocation_validation: bool,
286}
287
288impl InternalParameters {
289    fn default_skip_equivocation_validation() -> bool {
290        false
291    }
292}
293
294impl Default for InternalParameters {
295    fn default() -> Self {
296        Self {
297            skip_equivocation_validation: InternalParameters::default_skip_equivocation_validation(
298            ),
299        }
300    }
301}