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
100impl Parameters {
101    pub(crate) fn default_leader_timeout() -> Duration {
102        Duration::from_millis(200)
103    }
104
105    pub(crate) fn default_min_round_delay() -> Duration {
106        if cfg!(msim) || std::env::var("__TEST_ONLY_CONSENSUS_USE_LONG_MIN_ROUND_DELAY").is_ok() {
107            // Checkpoint building and execution cannot keep up with high commit rate in simtests,
108            // leading to long reconfiguration delays. This is because simtest is single threaded,
109            // and spending too much time in consensus can lead to starvation elsewhere.
110            Duration::from_millis(400)
111        } else if cfg!(test) {
112            // Avoid excessive CPU, data and logs in tests.
113            Duration::from_millis(250)
114        } else {
115            Duration::from_millis(50)
116        }
117    }
118
119    pub(crate) fn default_max_forward_time_drift() -> Duration {
120        Duration::from_millis(500)
121    }
122
123    pub(crate) fn default_max_blocks_per_sync() -> usize {
124        if cfg!(msim) {
125            // Exercise hitting blocks per sync limit.
126            4
127        } else {
128            32
129        }
130    }
131
132    pub(crate) fn default_max_blocks_per_fetch() -> usize {
133        if cfg!(msim) {
134            // Exercise hitting blocks per fetch limit.
135            10
136        } else {
137            1000
138        }
139    }
140
141    pub(crate) fn default_sync_last_known_own_block_timeout() -> Duration {
142        if cfg!(msim) {
143            Duration::from_millis(500)
144        } else {
145            // Here we prioritise liveness over the complete de-risking of block equivocation. 5 seconds
146            // in the majority of cases should be good enough for this given a healthy network.
147            Duration::from_secs(5)
148        }
149    }
150
151    pub(crate) fn default_round_prober_interval_ms() -> u64 {
152        if cfg!(msim) { 1000 } else { 5000 }
153    }
154
155    pub(crate) fn default_round_prober_request_timeout_ms() -> u64 {
156        if cfg!(msim) { 800 } else { 4000 }
157    }
158
159    pub(crate) fn default_propagation_delay_stop_proposal_threshold() -> u32 {
160        // Propagation delay is usually 0 round in production.
161        if cfg!(msim) { 2 } else { 5 }
162    }
163
164    pub(crate) fn default_dag_state_cached_rounds() -> u32 {
165        if cfg!(msim) {
166            // Exercise reading blocks from store.
167            5
168        } else {
169            500
170        }
171    }
172
173    pub(crate) fn default_commit_sync_parallel_fetches() -> usize {
174        8
175    }
176
177    pub(crate) fn default_commit_sync_batch_size() -> u32 {
178        if cfg!(msim) {
179            // Exercise commit sync.
180            5
181        } else {
182            100
183        }
184    }
185
186    pub(crate) fn default_commit_sync_batches_ahead() -> usize {
187        // This is set to be a multiple of default commit_sync_parallel_fetches to allow fetching ahead,
188        // while keeping the total number of inflight fetches and unprocessed fetched commits limited.
189        32
190    }
191}
192
193impl Default for Parameters {
194    fn default() -> Self {
195        Self {
196            db_path: PathBuf::default(),
197            leader_timeout: Parameters::default_leader_timeout(),
198            min_round_delay: Parameters::default_min_round_delay(),
199            max_forward_time_drift: Parameters::default_max_forward_time_drift(),
200            max_blocks_per_sync: Parameters::default_max_blocks_per_sync(),
201            max_blocks_per_fetch: Parameters::default_max_blocks_per_fetch(),
202            sync_last_known_own_block_timeout:
203                Parameters::default_sync_last_known_own_block_timeout(),
204            round_prober_interval_ms: Parameters::default_round_prober_interval_ms(),
205            round_prober_request_timeout_ms: Parameters::default_round_prober_request_timeout_ms(),
206            propagation_delay_stop_proposal_threshold:
207                Parameters::default_propagation_delay_stop_proposal_threshold(),
208            dag_state_cached_rounds: Parameters::default_dag_state_cached_rounds(),
209            commit_sync_parallel_fetches: Parameters::default_commit_sync_parallel_fetches(),
210            commit_sync_batch_size: Parameters::default_commit_sync_batch_size(),
211            commit_sync_batches_ahead: Parameters::default_commit_sync_batches_ahead(),
212            tonic: TonicParameters::default(),
213        }
214    }
215}
216
217#[derive(Clone, Debug, Deserialize, Serialize)]
218pub struct TonicParameters {
219    /// Keepalive interval and timeouts for both client and server.
220    ///
221    /// If unspecified, this will default to 5s.
222    #[serde(default = "TonicParameters::default_keepalive_interval")]
223    pub keepalive_interval: Duration,
224
225    /// Size of various per-connection buffers.
226    ///
227    /// If unspecified, this will default to 32MiB.
228    #[serde(default = "TonicParameters::default_connection_buffer_size")]
229    pub connection_buffer_size: usize,
230
231    /// Messages over this size threshold will increment a counter.
232    ///
233    /// If unspecified, this will default to 16MiB.
234    #[serde(default = "TonicParameters::default_excessive_message_size")]
235    pub excessive_message_size: usize,
236
237    /// Hard message size limit for both requests and responses.
238    /// This value is higher than strictly necessary, to allow overheads.
239    /// Message size targets and soft limits are computed based on this value.
240    ///
241    /// If unspecified, this will default to 1GiB.
242    #[serde(default = "TonicParameters::default_message_size_limit")]
243    pub message_size_limit: usize,
244}
245
246impl TonicParameters {
247    fn default_keepalive_interval() -> Duration {
248        Duration::from_secs(10)
249    }
250
251    fn default_connection_buffer_size() -> usize {
252        32 << 20
253    }
254
255    fn default_excessive_message_size() -> usize {
256        16 << 20
257    }
258
259    fn default_message_size_limit() -> usize {
260        64 << 20
261    }
262}
263
264impl Default for TonicParameters {
265    fn default() -> Self {
266        Self {
267            keepalive_interval: TonicParameters::default_keepalive_interval(),
268            connection_buffer_size: TonicParameters::default_connection_buffer_size(),
269            excessive_message_size: TonicParameters::default_excessive_message_size(),
270            message_size_limit: TonicParameters::default_message_size_limit(),
271        }
272    }
273}