sui_config/
node.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3use crate::Config;
4use crate::certificate_deny_config::CertificateDenyConfig;
5use crate::genesis;
6use crate::object_storage_config::ObjectStoreConfig;
7use crate::p2p::P2pConfig;
8use crate::transaction_deny_config::TransactionDenyConfig;
9use crate::validator_client_monitor_config::ValidatorClientMonitorConfig;
10use crate::verifier_signing_config::VerifierSigningConfig;
11use anyhow::Result;
12use consensus_config::Parameters as ConsensusParameters;
13use mysten_common::fatal;
14use nonzero_ext::nonzero;
15use once_cell::sync::OnceCell;
16use rand::rngs::OsRng;
17use serde::{Deserialize, Serialize};
18use serde_with::serde_as;
19use std::collections::{BTreeMap, BTreeSet};
20use std::net::SocketAddr;
21use std::num::{NonZeroU32, NonZeroUsize};
22use std::path::{Path, PathBuf};
23use std::sync::Arc;
24use std::time::Duration;
25use sui_keys::keypair_file::{read_authority_keypair_from_file, read_keypair_from_file};
26use sui_types::base_types::{ObjectID, SuiAddress};
27use sui_types::committee::EpochId;
28use sui_types::crypto::AuthorityPublicKeyBytes;
29use sui_types::crypto::KeypairTraits;
30use sui_types::crypto::NetworkKeyPair;
31use sui_types::crypto::SuiKeyPair;
32use sui_types::messages_checkpoint::CheckpointSequenceNumber;
33use sui_types::supported_protocol_versions::{Chain, SupportedProtocolVersions};
34use sui_types::traffic_control::{PolicyConfig, RemoteFirewallConfig};
35
36use sui_types::crypto::{AccountKeyPair, AuthorityKeyPair, get_key_pair_from_rng};
37use sui_types::multiaddr::Multiaddr;
38use tracing::info;
39
40// Default max number of concurrent requests served
41pub const DEFAULT_GRPC_CONCURRENCY_LIMIT: usize = 20000000000;
42
43/// Default gas price of 100 Mist
44pub const DEFAULT_VALIDATOR_GAS_PRICE: u64 = sui_types::transaction::DEFAULT_VALIDATOR_GAS_PRICE;
45
46/// Default commission rate of 2%
47pub const DEFAULT_COMMISSION_RATE: u64 = 200;
48
49#[serde_as]
50#[derive(Clone, Debug, Deserialize, Serialize)]
51#[serde(rename_all = "kebab-case")]
52pub struct NodeConfig {
53    #[serde(default = "default_authority_key_pair")]
54    pub protocol_key_pair: AuthorityKeyPairWithPath,
55    #[serde(default = "default_key_pair")]
56    pub worker_key_pair: KeyPairWithPath,
57    #[serde(default = "default_key_pair")]
58    pub account_key_pair: KeyPairWithPath,
59    #[serde(default = "default_key_pair")]
60    pub network_key_pair: KeyPairWithPath,
61
62    pub db_path: PathBuf,
63    #[serde(default = "default_grpc_address")]
64    pub network_address: Multiaddr,
65    #[serde(default = "default_json_rpc_address")]
66    pub json_rpc_address: SocketAddr,
67
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub rpc: Option<crate::RpcConfig>,
70
71    #[serde(default = "default_metrics_address")]
72    pub metrics_address: SocketAddr,
73    #[serde(default = "default_admin_interface_port")]
74    pub admin_interface_port: u16,
75
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub consensus_config: Option<ConsensusConfig>,
78
79    #[serde(default = "default_enable_index_processing")]
80    pub enable_index_processing: bool,
81
82    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
83    pub remove_deprecated_tables: bool,
84
85    #[serde(default)]
86    /// Determines the jsonrpc server type as either:
87    /// - 'websocket' for a websocket based service (deprecated)
88    /// - 'http' for an http based service
89    /// - 'both' for both a websocket and http based service (deprecated)
90    pub jsonrpc_server_type: Option<ServerType>,
91
92    #[serde(default)]
93    pub grpc_load_shed: Option<bool>,
94
95    #[serde(default = "default_concurrency_limit")]
96    pub grpc_concurrency_limit: Option<usize>,
97
98    #[serde(default)]
99    pub p2p_config: P2pConfig,
100
101    pub genesis: Genesis,
102
103    #[serde(default = "default_authority_store_pruning_config")]
104    pub authority_store_pruning_config: AuthorityStorePruningConfig,
105
106    /// Size of the broadcast channel used for notifying other systems of end of epoch.
107    ///
108    /// If unspecified, this will default to `128`.
109    #[serde(default = "default_end_of_epoch_broadcast_channel_capacity")]
110    pub end_of_epoch_broadcast_channel_capacity: usize,
111
112    #[serde(default)]
113    pub checkpoint_executor_config: CheckpointExecutorConfig,
114
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub metrics: Option<MetricsConfig>,
117
118    /// In a `sui-node` binary, this is set to SupportedProtocolVersions::SYSTEM_DEFAULT
119    /// in sui-node/src/main.rs. It is present in the config so that it can be changed by tests in
120    /// order to test protocol upgrades.
121    #[serde(skip)]
122    pub supported_protocol_versions: Option<SupportedProtocolVersions>,
123
124    #[serde(default)]
125    pub db_checkpoint_config: DBCheckpointConfig,
126
127    #[serde(default)]
128    pub expensive_safety_check_config: ExpensiveSafetyCheckConfig,
129
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub name_service_package_address: Option<SuiAddress>,
132
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub name_service_registry_id: Option<ObjectID>,
135
136    #[serde(skip_serializing_if = "Option::is_none")]
137    pub name_service_reverse_registry_id: Option<ObjectID>,
138
139    #[serde(default)]
140    pub transaction_deny_config: TransactionDenyConfig,
141
142    #[serde(default)]
143    pub certificate_deny_config: CertificateDenyConfig,
144
145    #[serde(default)]
146    pub state_debug_dump_config: StateDebugDumpConfig,
147
148    #[serde(default)]
149    pub state_archive_read_config: Vec<StateArchiveConfig>,
150
151    #[serde(default)]
152    pub state_snapshot_write_config: StateSnapshotConfig,
153
154    #[serde(default)]
155    pub indexer_max_subscriptions: Option<usize>,
156
157    #[serde(default = "default_transaction_kv_store_config")]
158    pub transaction_kv_store_read_config: TransactionKeyValueStoreReadConfig,
159
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub transaction_kv_store_write_config: Option<TransactionKeyValueStoreWriteConfig>,
162
163    #[serde(default = "default_jwk_fetch_interval_seconds")]
164    pub jwk_fetch_interval_seconds: u64,
165
166    #[serde(default = "default_zklogin_oauth_providers")]
167    pub zklogin_oauth_providers: BTreeMap<Chain, BTreeSet<String>>,
168
169    #[serde(default = "default_authority_overload_config")]
170    pub authority_overload_config: AuthorityOverloadConfig,
171
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub run_with_range: Option<RunWithRange>,
174
175    // For killswitch use None
176    #[serde(
177        skip_serializing_if = "Option::is_none",
178        default = "default_traffic_controller_policy_config"
179    )]
180    pub policy_config: Option<PolicyConfig>,
181
182    #[serde(skip_serializing_if = "Option::is_none")]
183    pub firewall_config: Option<RemoteFirewallConfig>,
184
185    #[serde(default)]
186    pub execution_cache: ExecutionCacheConfig,
187
188    // step 1 in removing the old state accumulator
189    #[serde(skip)]
190    #[serde(default = "bool_true")]
191    pub state_accumulator_v2: bool,
192
193    #[serde(default = "bool_true")]
194    pub enable_soft_bundle: bool,
195
196    #[serde(default = "bool_true")]
197    pub enable_validator_tx_finalizer: bool,
198
199    #[serde(default)]
200    pub verifier_signing_config: VerifierSigningConfig,
201
202    /// If a value is set, it determines if writes to DB can stall, which can halt the whole process.
203    /// By default, write stall is enabled on validators but not on fullnodes.
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub enable_db_write_stall: Option<bool>,
206
207    #[serde(skip_serializing_if = "Option::is_none")]
208    pub execution_time_observer_config: Option<ExecutionTimeObserverConfig>,
209
210    /// Allow overriding the chain for testing purposes. For instance, it allows you to
211    /// create a test network that believes it is mainnet or testnet. Attempting to
212    /// override this value on production networks will result in an error.
213    #[serde(skip_serializing_if = "Option::is_none")]
214    pub chain_override_for_testing: Option<Chain>,
215
216    /// Configuration for validator client monitoring from the client perspective.
217    /// When enabled, tracks client-observed performance metrics for validators.
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub validator_client_monitor_config: Option<ValidatorClientMonitorConfig>,
220
221    /// Fork recovery configuration for handling validator equivocation after forks
222    #[serde(skip_serializing_if = "Option::is_none")]
223    pub fork_recovery: Option<ForkRecoveryConfig>,
224
225    /// Configuration for the transaction driver.
226    #[serde(skip_serializing_if = "Option::is_none")]
227    pub transaction_driver_config: Option<TransactionDriverConfig>,
228}
229
230#[derive(Clone, Debug, Deserialize, Serialize)]
231#[serde(rename_all = "kebab-case")]
232pub struct TransactionDriverConfig {
233    /// The list of validators that are allowed to submit MFP transactions to (via the transaction driver).
234    /// Each entry is a validator display name.
235    #[serde(default, skip_serializing_if = "Vec::is_empty")]
236    pub allowed_submission_validators: Vec<String>,
237
238    /// Enable early transaction validation before submission to consensus.
239    /// This checks for non-retriable errors (like old object versions) and rejects
240    /// transactions early to provide fast feedback to clients.
241    /// Note: Currently used in TransactionOrchestrator, but may be moved to TransactionDriver in future.
242    #[serde(default = "bool_true")]
243    pub enable_early_validation: bool,
244}
245
246impl Default for TransactionDriverConfig {
247    fn default() -> Self {
248        Self {
249            allowed_submission_validators: vec![],
250            enable_early_validation: true,
251        }
252    }
253}
254
255#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq)]
256#[serde(rename_all = "kebab-case")]
257pub enum ForkCrashBehavior {
258    #[serde(rename = "await-fork-recovery")]
259    #[default]
260    AwaitForkRecovery,
261    /// Return an error instead of blocking forever. This is primarily for testing.
262    #[serde(rename = "return-error")]
263    ReturnError,
264}
265
266#[derive(Clone, Debug, Default, Deserialize, Serialize)]
267#[serde(rename_all = "kebab-case")]
268pub struct ForkRecoveryConfig {
269    /// Map of transaction digest to effects digest overrides
270    /// Used to repoint transactions to correct effects after a fork
271    #[serde(default)]
272    pub transaction_overrides: BTreeMap<String, String>,
273
274    /// Map of checkpoint sequence number to checkpoint digest overrides
275    /// On node start, if we have a locally computed checkpoint with a
276    /// digest mismatch with this table, we will clear any associated local state.
277    #[serde(default)]
278    pub checkpoint_overrides: BTreeMap<u64, String>,
279
280    /// Behavior when a fork is detected after recovery attempts
281    #[serde(default)]
282    pub fork_crash_behavior: ForkCrashBehavior,
283}
284
285#[derive(Clone, Debug, Default, Deserialize, Serialize)]
286#[serde(rename_all = "kebab-case")]
287pub struct ExecutionTimeObserverConfig {
288    /// Size of the channel used for buffering local execution time observations.
289    ///
290    /// If unspecified, this will default to `1_024`.
291    pub observation_channel_capacity: Option<NonZeroUsize>,
292
293    /// Size of the LRU cache used for storing local execution time observations.
294    ///
295    /// If unspecified, this will default to `10_000`.
296    pub observation_cache_size: Option<NonZeroUsize>,
297
298    /// Size of the channel used for buffering object debt updates from consensus handler.
299    ///
300    /// If unspecified, this will default to `128`.
301    pub object_debt_channel_capacity: Option<NonZeroUsize>,
302
303    /// Size of the LRU cache used for tracking object utilization.
304    ///
305    /// If unspecified, this will default to `50_000`.
306    pub object_utilization_cache_size: Option<NonZeroUsize>,
307
308    /// If true, the execution time observer will report per-object utilization metrics
309    /// with full object IDs. When set, the metric can have a high cardinality, so this
310    /// should not be used except in controlled tests where there are a small number of
311    /// objects.
312    ///
313    /// If false, object utilization is reported using hash(object_id) % 32 as the key,
314    /// which still allows observation of utilization when there are small numbers of
315    /// over-utilized objects.
316    ///
317    /// If unspecified, this will default to `false`.
318    pub report_object_utilization_metric_with_full_id: Option<bool>,
319
320    /// Unless target object utilization is exceeded by at least this amount, no observation
321    /// will be shared with consensus.
322    ///
323    /// If unspecified, this will default to `500` milliseconds.
324    pub observation_sharing_object_utilization_threshold: Option<Duration>,
325
326    /// Unless the current local observation differs from the last one we shared by at least this
327    /// percentage, no observation will be shared with consensus.
328    ///
329    /// If unspecified, this will default to `0.1`.
330    pub observation_sharing_diff_threshold: Option<f64>,
331
332    /// Minimum interval between sharing multiple observations of the same key.
333    ///
334    /// If unspecified, this will default to `5` seconds.
335    pub observation_sharing_min_interval: Option<Duration>,
336
337    /// Global per-second rate limit for sharing observations. This is a safety valve and
338    /// should not trigger during normal operation.
339    ///
340    /// If unspecified, this will default to `10` observations per second.
341    pub observation_sharing_rate_limit: Option<NonZeroU32>,
342
343    /// Global burst limit for sharing observations.
344    ///
345    /// If unspecified, this will default to `100` observations.
346    pub observation_sharing_burst_limit: Option<NonZeroU32>,
347
348    /// Whether to use gas price weighting in execution time estimates.
349    /// When enabled, samples with higher gas prices have more influence on the
350    /// execution time estimates, providing protection against volume-based
351    /// manipulation attacks.
352    ///
353    /// If unspecified, this will default to `false`.
354    pub enable_gas_price_weighting: Option<bool>,
355
356    /// Size of the weighted moving average window for execution time observations.
357    /// This determines how many recent observations are kept in the weighted moving average
358    /// calculation for each execution time observation key.
359    /// Note that this is independent of the window size for the simple moving average.
360    ///
361    /// If unspecified, this will default to `20`.
362    pub weighted_moving_average_window_size: Option<usize>,
363
364    /// Whether to inject synthetic execution time for testing in simtest.
365    /// When enabled, synthetic timings will be generated for execution time observations
366    /// to enable deterministic testing of congestion control features.
367    ///
368    /// If unspecified, this will default to `false`.
369    #[cfg(msim)]
370    pub inject_synthetic_execution_time: Option<bool>,
371}
372
373impl ExecutionTimeObserverConfig {
374    pub fn observation_channel_capacity(&self) -> NonZeroUsize {
375        self.observation_channel_capacity
376            .unwrap_or(nonzero!(1_024usize))
377    }
378
379    pub fn observation_cache_size(&self) -> NonZeroUsize {
380        self.observation_cache_size.unwrap_or(nonzero!(10_000usize))
381    }
382
383    pub fn object_debt_channel_capacity(&self) -> NonZeroUsize {
384        self.object_debt_channel_capacity
385            .unwrap_or(nonzero!(128usize))
386    }
387
388    pub fn object_utilization_cache_size(&self) -> NonZeroUsize {
389        self.object_utilization_cache_size
390            .unwrap_or(nonzero!(50_000usize))
391    }
392
393    pub fn report_object_utilization_metric_with_full_id(&self) -> bool {
394        self.report_object_utilization_metric_with_full_id
395            .unwrap_or(false)
396    }
397
398    pub fn observation_sharing_object_utilization_threshold(&self) -> Duration {
399        self.observation_sharing_object_utilization_threshold
400            .unwrap_or(Duration::from_millis(500))
401    }
402
403    pub fn observation_sharing_diff_threshold(&self) -> f64 {
404        self.observation_sharing_diff_threshold.unwrap_or(0.1)
405    }
406
407    pub fn observation_sharing_min_interval(&self) -> Duration {
408        self.observation_sharing_min_interval
409            .unwrap_or(Duration::from_secs(5))
410    }
411
412    pub fn observation_sharing_rate_limit(&self) -> NonZeroU32 {
413        self.observation_sharing_rate_limit
414            .unwrap_or(nonzero!(10u32))
415    }
416
417    pub fn observation_sharing_burst_limit(&self) -> NonZeroU32 {
418        self.observation_sharing_burst_limit
419            .unwrap_or(nonzero!(100u32))
420    }
421
422    pub fn enable_gas_price_weighting(&self) -> bool {
423        self.enable_gas_price_weighting.unwrap_or(false)
424    }
425
426    pub fn weighted_moving_average_window_size(&self) -> usize {
427        self.weighted_moving_average_window_size.unwrap_or(20)
428    }
429
430    #[cfg(msim)]
431    pub fn inject_synthetic_execution_time(&self) -> bool {
432        self.inject_synthetic_execution_time.unwrap_or(false)
433    }
434}
435
436#[allow(clippy::large_enum_variant)]
437#[derive(Clone, Debug, Deserialize, Serialize)]
438#[serde(rename_all = "kebab-case")]
439pub enum ExecutionCacheConfig {
440    PassthroughCache,
441    WritebackCache {
442        /// Maximum number of entries in each cache. (There are several different caches).
443        /// If None, the default of 10000 is used.
444        max_cache_size: Option<u64>,
445
446        package_cache_size: Option<u64>, // defaults to 1000
447
448        object_cache_size: Option<u64>, // defaults to max_cache_size
449        marker_cache_size: Option<u64>, // defaults to object_cache_size
450        object_by_id_cache_size: Option<u64>, // defaults to object_cache_size
451
452        transaction_cache_size: Option<u64>, // defaults to max_cache_size
453        executed_effect_cache_size: Option<u64>, // defaults to transaction_cache_size
454        effect_cache_size: Option<u64>,      // defaults to executed_effect_cache_size
455
456        events_cache_size: Option<u64>, // defaults to transaction_cache_size
457
458        transaction_objects_cache_size: Option<u64>, // defaults to 1000
459
460        /// Number of uncommitted transactions at which to pause consensus handler.
461        backpressure_threshold: Option<u64>,
462
463        /// Number of uncommitted transactions at which to refuse new transaction
464        /// submissions. Defaults to backpressure_threshold if unset.
465        backpressure_threshold_for_rpc: Option<u64>,
466
467        fastpath_transaction_outputs_cache_size: Option<u64>,
468    },
469}
470
471impl Default for ExecutionCacheConfig {
472    fn default() -> Self {
473        ExecutionCacheConfig::WritebackCache {
474            max_cache_size: None,
475            backpressure_threshold: None,
476            backpressure_threshold_for_rpc: None,
477            package_cache_size: None,
478            object_cache_size: None,
479            marker_cache_size: None,
480            object_by_id_cache_size: None,
481            transaction_cache_size: None,
482            executed_effect_cache_size: None,
483            effect_cache_size: None,
484            events_cache_size: None,
485            transaction_objects_cache_size: None,
486            fastpath_transaction_outputs_cache_size: None,
487        }
488    }
489}
490
491impl ExecutionCacheConfig {
492    pub fn max_cache_size(&self) -> u64 {
493        std::env::var("SUI_MAX_CACHE_SIZE")
494            .ok()
495            .and_then(|s| s.parse().ok())
496            .unwrap_or_else(|| match self {
497                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
498                ExecutionCacheConfig::WritebackCache { max_cache_size, .. } => {
499                    max_cache_size.unwrap_or(100000)
500                }
501            })
502    }
503
504    pub fn package_cache_size(&self) -> u64 {
505        std::env::var("SUI_PACKAGE_CACHE_SIZE")
506            .ok()
507            .and_then(|s| s.parse().ok())
508            .unwrap_or_else(|| match self {
509                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
510                ExecutionCacheConfig::WritebackCache {
511                    package_cache_size, ..
512                } => package_cache_size.unwrap_or(1000),
513            })
514    }
515
516    pub fn object_cache_size(&self) -> u64 {
517        std::env::var("SUI_OBJECT_CACHE_SIZE")
518            .ok()
519            .and_then(|s| s.parse().ok())
520            .unwrap_or_else(|| match self {
521                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
522                ExecutionCacheConfig::WritebackCache {
523                    object_cache_size, ..
524                } => object_cache_size.unwrap_or(self.max_cache_size()),
525            })
526    }
527
528    pub fn marker_cache_size(&self) -> u64 {
529        std::env::var("SUI_MARKER_CACHE_SIZE")
530            .ok()
531            .and_then(|s| s.parse().ok())
532            .unwrap_or_else(|| match self {
533                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
534                ExecutionCacheConfig::WritebackCache {
535                    marker_cache_size, ..
536                } => marker_cache_size.unwrap_or(self.object_cache_size()),
537            })
538    }
539
540    pub fn object_by_id_cache_size(&self) -> u64 {
541        std::env::var("SUI_OBJECT_BY_ID_CACHE_SIZE")
542            .ok()
543            .and_then(|s| s.parse().ok())
544            .unwrap_or_else(|| match self {
545                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
546                ExecutionCacheConfig::WritebackCache {
547                    object_by_id_cache_size,
548                    ..
549                } => object_by_id_cache_size.unwrap_or(self.object_cache_size()),
550            })
551    }
552
553    pub fn transaction_cache_size(&self) -> u64 {
554        std::env::var("SUI_TRANSACTION_CACHE_SIZE")
555            .ok()
556            .and_then(|s| s.parse().ok())
557            .unwrap_or_else(|| match self {
558                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
559                ExecutionCacheConfig::WritebackCache {
560                    transaction_cache_size,
561                    ..
562                } => transaction_cache_size.unwrap_or(self.max_cache_size()),
563            })
564    }
565
566    pub fn executed_effect_cache_size(&self) -> u64 {
567        std::env::var("SUI_EXECUTED_EFFECT_CACHE_SIZE")
568            .ok()
569            .and_then(|s| s.parse().ok())
570            .unwrap_or_else(|| match self {
571                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
572                ExecutionCacheConfig::WritebackCache {
573                    executed_effect_cache_size,
574                    ..
575                } => executed_effect_cache_size.unwrap_or(self.transaction_cache_size()),
576            })
577    }
578
579    pub fn effect_cache_size(&self) -> u64 {
580        std::env::var("SUI_EFFECT_CACHE_SIZE")
581            .ok()
582            .and_then(|s| s.parse().ok())
583            .unwrap_or_else(|| match self {
584                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
585                ExecutionCacheConfig::WritebackCache {
586                    effect_cache_size, ..
587                } => effect_cache_size.unwrap_or(self.executed_effect_cache_size()),
588            })
589    }
590
591    pub fn events_cache_size(&self) -> u64 {
592        std::env::var("SUI_EVENTS_CACHE_SIZE")
593            .ok()
594            .and_then(|s| s.parse().ok())
595            .unwrap_or_else(|| match self {
596                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
597                ExecutionCacheConfig::WritebackCache {
598                    events_cache_size, ..
599                } => events_cache_size.unwrap_or(self.transaction_cache_size()),
600            })
601    }
602
603    pub fn transaction_objects_cache_size(&self) -> u64 {
604        std::env::var("SUI_TRANSACTION_OBJECTS_CACHE_SIZE")
605            .ok()
606            .and_then(|s| s.parse().ok())
607            .unwrap_or_else(|| match self {
608                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
609                ExecutionCacheConfig::WritebackCache {
610                    transaction_objects_cache_size,
611                    ..
612                } => transaction_objects_cache_size.unwrap_or(1000),
613            })
614    }
615
616    pub fn backpressure_threshold(&self) -> u64 {
617        std::env::var("SUI_BACKPRESSURE_THRESHOLD")
618            .ok()
619            .and_then(|s| s.parse().ok())
620            .unwrap_or_else(|| match self {
621                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
622                ExecutionCacheConfig::WritebackCache {
623                    backpressure_threshold,
624                    ..
625                } => backpressure_threshold.unwrap_or(100_000),
626            })
627    }
628
629    pub fn backpressure_threshold_for_rpc(&self) -> u64 {
630        std::env::var("SUI_BACKPRESSURE_THRESHOLD_FOR_RPC")
631            .ok()
632            .and_then(|s| s.parse().ok())
633            .unwrap_or_else(|| match self {
634                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
635                ExecutionCacheConfig::WritebackCache {
636                    backpressure_threshold_for_rpc,
637                    ..
638                } => backpressure_threshold_for_rpc.unwrap_or(self.backpressure_threshold()),
639            })
640    }
641
642    pub fn fastpath_transaction_outputs_cache_size(&self) -> u64 {
643        std::env::var("SUI_FASTPATH_TRANSACTION_OUTPUTS_CACHE_SIZE")
644            .ok()
645            .and_then(|s| s.parse().ok())
646            .unwrap_or_else(|| match self {
647                ExecutionCacheConfig::PassthroughCache => fatal!("invalid cache config"),
648                ExecutionCacheConfig::WritebackCache {
649                    fastpath_transaction_outputs_cache_size,
650                    ..
651                } => fastpath_transaction_outputs_cache_size.unwrap_or(10_000),
652            })
653    }
654}
655
656#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
657#[serde(rename_all = "lowercase")]
658pub enum ServerType {
659    WebSocket,
660    Http,
661    Both,
662}
663
664#[derive(Clone, Debug, Deserialize, Serialize)]
665#[serde(rename_all = "kebab-case")]
666pub struct TransactionKeyValueStoreReadConfig {
667    #[serde(default = "default_base_url")]
668    pub base_url: String,
669
670    #[serde(default = "default_cache_size")]
671    pub cache_size: u64,
672}
673
674impl Default for TransactionKeyValueStoreReadConfig {
675    fn default() -> Self {
676        Self {
677            base_url: default_base_url(),
678            cache_size: default_cache_size(),
679        }
680    }
681}
682
683fn default_base_url() -> String {
684    "https://transactions.sui.io/".to_string()
685}
686
687fn default_cache_size() -> u64 {
688    100_000
689}
690
691fn default_jwk_fetch_interval_seconds() -> u64 {
692    3600
693}
694
695pub fn default_zklogin_oauth_providers() -> BTreeMap<Chain, BTreeSet<String>> {
696    let mut map = BTreeMap::new();
697
698    // providers that are available on devnet only.
699    let experimental_providers = BTreeSet::from([
700        "Google".to_string(),
701        "Facebook".to_string(),
702        "Twitch".to_string(),
703        "Kakao".to_string(),
704        "Apple".to_string(),
705        "Slack".to_string(),
706        "TestIssuer".to_string(),
707        "Microsoft".to_string(),
708        "KarrierOne".to_string(),
709        "Credenza3".to_string(),
710        "Playtron".to_string(),
711        "Threedos".to_string(),
712        "Onefc".to_string(),
713        "FanTV".to_string(),
714        "AwsTenant-region:us-east-1-tenant_id:us-east-1_qPsZxYqd8".to_string(), // Ambrus, external partner
715        "Arden".to_string(),                                                    // Arden partner
716        "AwsTenant-region:eu-west-3-tenant_id:eu-west-3_gGVCx53Es".to_string(), // Trace, external partner
717        "EveFrontier".to_string(),
718    ]);
719
720    // providers that are available for mainnet and testnet.
721    let providers = BTreeSet::from([
722        "Google".to_string(),
723        "Facebook".to_string(),
724        "Twitch".to_string(),
725        "Apple".to_string(),
726        "AwsTenant-region:us-east-1-tenant_id:us-east-1_qPsZxYqd8".to_string(), // Ambrus, external partner
727        "KarrierOne".to_string(),
728        "Credenza3".to_string(),
729        "Playtron".to_string(),
730        "Onefc".to_string(),
731        "Threedos".to_string(),
732        "AwsTenant-region:eu-west-3-tenant_id:eu-west-3_gGVCx53Es".to_string(), // Trace, external partner
733        "Arden".to_string(),
734        "FanTV".to_string(),
735    ]);
736    map.insert(Chain::Mainnet, providers.clone());
737    map.insert(Chain::Testnet, providers);
738    map.insert(Chain::Unknown, experimental_providers);
739    map
740}
741
742fn default_transaction_kv_store_config() -> TransactionKeyValueStoreReadConfig {
743    TransactionKeyValueStoreReadConfig::default()
744}
745
746fn default_authority_store_pruning_config() -> AuthorityStorePruningConfig {
747    AuthorityStorePruningConfig::default()
748}
749
750pub fn default_enable_index_processing() -> bool {
751    true
752}
753
754fn default_grpc_address() -> Multiaddr {
755    "/ip4/0.0.0.0/tcp/8080".parse().unwrap()
756}
757fn default_authority_key_pair() -> AuthorityKeyPairWithPath {
758    AuthorityKeyPairWithPath::new(get_key_pair_from_rng::<AuthorityKeyPair, _>(&mut OsRng).1)
759}
760
761fn default_key_pair() -> KeyPairWithPath {
762    KeyPairWithPath::new(
763        get_key_pair_from_rng::<AccountKeyPair, _>(&mut OsRng)
764            .1
765            .into(),
766    )
767}
768
769fn default_metrics_address() -> SocketAddr {
770    use std::net::{IpAddr, Ipv4Addr};
771    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
772}
773
774pub fn default_admin_interface_port() -> u16 {
775    1337
776}
777
778pub fn default_json_rpc_address() -> SocketAddr {
779    use std::net::{IpAddr, Ipv4Addr};
780    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
781}
782
783pub fn default_concurrency_limit() -> Option<usize> {
784    Some(DEFAULT_GRPC_CONCURRENCY_LIMIT)
785}
786
787pub fn default_end_of_epoch_broadcast_channel_capacity() -> usize {
788    128
789}
790
791pub fn bool_true() -> bool {
792    true
793}
794
795fn is_true(value: &bool) -> bool {
796    *value
797}
798
799impl Config for NodeConfig {}
800
801impl NodeConfig {
802    pub fn protocol_key_pair(&self) -> &AuthorityKeyPair {
803        self.protocol_key_pair.authority_keypair()
804    }
805
806    pub fn worker_key_pair(&self) -> &NetworkKeyPair {
807        match self.worker_key_pair.keypair() {
808            SuiKeyPair::Ed25519(kp) => kp,
809            other => panic!(
810                "Invalid keypair type: {:?}, only Ed25519 is allowed for worker key",
811                other
812            ),
813        }
814    }
815
816    pub fn network_key_pair(&self) -> &NetworkKeyPair {
817        match self.network_key_pair.keypair() {
818            SuiKeyPair::Ed25519(kp) => kp,
819            other => panic!(
820                "Invalid keypair type: {:?}, only Ed25519 is allowed for network key",
821                other
822            ),
823        }
824    }
825
826    pub fn protocol_public_key(&self) -> AuthorityPublicKeyBytes {
827        self.protocol_key_pair().public().into()
828    }
829
830    pub fn db_path(&self) -> PathBuf {
831        self.db_path.join("live")
832    }
833
834    pub fn db_checkpoint_path(&self) -> PathBuf {
835        self.db_path.join("db_checkpoints")
836    }
837
838    pub fn archive_path(&self) -> PathBuf {
839        self.db_path.join("archive")
840    }
841
842    pub fn snapshot_path(&self) -> PathBuf {
843        self.db_path.join("snapshot")
844    }
845
846    pub fn network_address(&self) -> &Multiaddr {
847        &self.network_address
848    }
849
850    pub fn consensus_config(&self) -> Option<&ConsensusConfig> {
851        self.consensus_config.as_ref()
852    }
853
854    pub fn genesis(&self) -> Result<&genesis::Genesis> {
855        self.genesis.genesis()
856    }
857
858    pub fn sui_address(&self) -> SuiAddress {
859        (&self.account_key_pair.keypair().public()).into()
860    }
861
862    pub fn archive_reader_config(&self) -> Option<ArchiveReaderConfig> {
863        self.state_archive_read_config
864            .first()
865            .map(|config| ArchiveReaderConfig {
866                ingestion_url: config.ingestion_url.clone(),
867                remote_store_options: config.remote_store_options.clone(),
868                download_concurrency: NonZeroUsize::new(config.concurrency)
869                    .unwrap_or(NonZeroUsize::new(5).unwrap()),
870                remote_store_config: ObjectStoreConfig::default(),
871            })
872    }
873
874    pub fn jsonrpc_server_type(&self) -> ServerType {
875        self.jsonrpc_server_type.unwrap_or(ServerType::Http)
876    }
877
878    pub fn rpc(&self) -> Option<&crate::RpcConfig> {
879        self.rpc.as_ref()
880    }
881}
882
883#[derive(Debug, Clone, Deserialize, Serialize)]
884pub enum ConsensusProtocol {
885    #[serde(rename = "narwhal")]
886    Narwhal,
887    #[serde(rename = "mysticeti")]
888    Mysticeti,
889}
890
891#[derive(Debug, Clone, Deserialize, Serialize)]
892#[serde(rename_all = "kebab-case")]
893pub struct ConsensusConfig {
894    // Base consensus DB path for all epochs.
895    pub db_path: PathBuf,
896
897    // The number of epochs for which to retain the consensus DBs. Setting it to 0 will make a consensus DB getting
898    // dropped as soon as system is switched to a new epoch.
899    pub db_retention_epochs: Option<u64>,
900
901    // Pruner will run on every epoch change but it will also check periodically on every `db_pruner_period_secs`
902    // seconds to see if there are any epoch DBs to remove.
903    pub db_pruner_period_secs: Option<u64>,
904
905    /// Maximum number of pending transactions to submit to consensus, including those
906    /// in submission wait.
907    /// Default to 20_000 inflight limit, assuming 20_000 txn tps * 1 sec consensus latency.
908    pub max_pending_transactions: Option<usize>,
909
910    /// When defined caps the calculated submission position to the max_submit_position. Even if the
911    /// is elected to submit from a higher position than this, it will "reset" to the max_submit_position.
912    pub max_submit_position: Option<usize>,
913
914    /// The submit delay step to consensus defined in milliseconds. When provided it will
915    /// override the current back off logic otherwise the default backoff logic will be applied based
916    /// on consensus latency estimates.
917    pub submit_delay_step_override_millis: Option<u64>,
918
919    pub parameters: Option<ConsensusParameters>,
920}
921
922impl ConsensusConfig {
923    pub fn db_path(&self) -> &Path {
924        &self.db_path
925    }
926
927    pub fn max_pending_transactions(&self) -> usize {
928        self.max_pending_transactions.unwrap_or(20_000)
929    }
930
931    pub fn submit_delay_step_override(&self) -> Option<Duration> {
932        self.submit_delay_step_override_millis
933            .map(Duration::from_millis)
934    }
935
936    pub fn db_retention_epochs(&self) -> u64 {
937        self.db_retention_epochs.unwrap_or(0)
938    }
939
940    pub fn db_pruner_period(&self) -> Duration {
941        // Default to 1 hour
942        self.db_pruner_period_secs
943            .map(Duration::from_secs)
944            .unwrap_or(Duration::from_secs(3_600))
945    }
946}
947
948#[derive(Clone, Debug, Deserialize, Serialize)]
949#[serde(rename_all = "kebab-case")]
950pub struct CheckpointExecutorConfig {
951    /// Upper bound on the number of checkpoints that can be concurrently executed
952    ///
953    /// If unspecified, this will default to `200`
954    #[serde(default = "default_checkpoint_execution_max_concurrency")]
955    pub checkpoint_execution_max_concurrency: usize,
956
957    /// Number of seconds to wait for effects of a batch of transactions
958    /// before logging a warning. Note that we will continue to retry
959    /// indefinitely
960    ///
961    /// If unspecified, this will default to `10`.
962    #[serde(default = "default_local_execution_timeout_sec")]
963    pub local_execution_timeout_sec: u64,
964
965    /// Optional directory used for data ingestion pipeline
966    /// When specified, each executed checkpoint will be saved in a local directory for post processing
967    #[serde(default, skip_serializing_if = "Option::is_none")]
968    pub data_ingestion_dir: Option<PathBuf>,
969}
970
971#[derive(Clone, Debug, Default, Deserialize, Serialize)]
972#[serde(rename_all = "kebab-case")]
973pub struct ExpensiveSafetyCheckConfig {
974    /// If enabled, at epoch boundary, we will check that the storage
975    /// fund balance is always identical to the sum of the storage
976    /// rebate of all live objects, and that the total SUI in the network remains
977    /// the same.
978    #[serde(default)]
979    enable_epoch_sui_conservation_check: bool,
980
981    /// If enabled, we will check that the total SUI in all input objects of a tx
982    /// (both the Move part and the storage rebate) matches the total SUI in all
983    /// output objects of the tx + gas fees
984    #[serde(default)]
985    enable_deep_per_tx_sui_conservation_check: bool,
986
987    /// Disable epoch SUI conservation check even when we are running in debug mode.
988    #[serde(default)]
989    force_disable_epoch_sui_conservation_check: bool,
990
991    /// If enabled, at epoch boundary, we will check that the accumulated
992    /// live object state matches the end of epoch root state digest.
993    #[serde(default)]
994    enable_state_consistency_check: bool,
995
996    /// Disable state consistency check even when we are running in debug mode.
997    #[serde(default)]
998    force_disable_state_consistency_check: bool,
999
1000    #[serde(default)]
1001    enable_secondary_index_checks: bool,
1002    // TODO: Add more expensive checks here
1003}
1004
1005impl ExpensiveSafetyCheckConfig {
1006    pub fn new_enable_all() -> Self {
1007        Self {
1008            enable_epoch_sui_conservation_check: true,
1009            enable_deep_per_tx_sui_conservation_check: true,
1010            force_disable_epoch_sui_conservation_check: false,
1011            enable_state_consistency_check: true,
1012            force_disable_state_consistency_check: false,
1013            enable_secondary_index_checks: false, // Disable by default for now
1014        }
1015    }
1016
1017    pub fn new_disable_all() -> Self {
1018        Self {
1019            enable_epoch_sui_conservation_check: false,
1020            enable_deep_per_tx_sui_conservation_check: false,
1021            force_disable_epoch_sui_conservation_check: true,
1022            enable_state_consistency_check: false,
1023            force_disable_state_consistency_check: true,
1024            enable_secondary_index_checks: false,
1025        }
1026    }
1027
1028    pub fn force_disable_epoch_sui_conservation_check(&mut self) {
1029        self.force_disable_epoch_sui_conservation_check = true;
1030    }
1031
1032    pub fn enable_epoch_sui_conservation_check(&self) -> bool {
1033        (self.enable_epoch_sui_conservation_check || cfg!(debug_assertions))
1034            && !self.force_disable_epoch_sui_conservation_check
1035    }
1036
1037    pub fn force_disable_state_consistency_check(&mut self) {
1038        self.force_disable_state_consistency_check = true;
1039    }
1040
1041    pub fn enable_state_consistency_check(&self) -> bool {
1042        (self.enable_state_consistency_check || cfg!(debug_assertions))
1043            && !self.force_disable_state_consistency_check
1044    }
1045
1046    pub fn enable_deep_per_tx_sui_conservation_check(&self) -> bool {
1047        self.enable_deep_per_tx_sui_conservation_check || cfg!(debug_assertions)
1048    }
1049
1050    pub fn enable_secondary_index_checks(&self) -> bool {
1051        self.enable_secondary_index_checks
1052    }
1053}
1054
1055fn default_checkpoint_execution_max_concurrency() -> usize {
1056    4
1057}
1058
1059fn default_local_execution_timeout_sec() -> u64 {
1060    30
1061}
1062
1063impl Default for CheckpointExecutorConfig {
1064    fn default() -> Self {
1065        Self {
1066            checkpoint_execution_max_concurrency: default_checkpoint_execution_max_concurrency(),
1067            local_execution_timeout_sec: default_local_execution_timeout_sec(),
1068            data_ingestion_dir: None,
1069        }
1070    }
1071}
1072
1073#[derive(Debug, Clone, Deserialize, Serialize)]
1074#[serde(rename_all = "kebab-case")]
1075pub struct AuthorityStorePruningConfig {
1076    /// number of the latest epoch dbs to retain
1077    #[serde(default = "default_num_latest_epoch_dbs_to_retain")]
1078    pub num_latest_epoch_dbs_to_retain: usize,
1079    /// time interval used by the pruner to determine whether there are any epoch DBs to remove
1080    #[serde(default = "default_epoch_db_pruning_period_secs")]
1081    pub epoch_db_pruning_period_secs: u64,
1082    /// number of epochs to keep the latest version of objects for.
1083    /// Note that a zero value corresponds to an aggressive pruner.
1084    /// This mode is experimental and needs to be used with caution.
1085    /// Use `u64::MAX` to disable the pruner for the objects.
1086    #[serde(default)]
1087    pub num_epochs_to_retain: u64,
1088    /// pruner's runtime interval used for aggressive mode
1089    #[serde(skip_serializing_if = "Option::is_none")]
1090    pub pruning_run_delay_seconds: Option<u64>,
1091    /// maximum number of checkpoints in the pruning batch. Can be adjusted to increase performance
1092    #[serde(default = "default_max_checkpoints_in_batch")]
1093    pub max_checkpoints_in_batch: usize,
1094    /// maximum number of transaction in the pruning batch
1095    #[serde(default = "default_max_transactions_in_batch")]
1096    pub max_transactions_in_batch: usize,
1097    /// enables periodic background compaction for old SST files whose last modified time is
1098    /// older than `periodic_compaction_threshold_days` days.
1099    /// That ensures that all sst files eventually go through the compaction process
1100    #[serde(
1101        default = "default_periodic_compaction_threshold_days",
1102        skip_serializing_if = "Option::is_none"
1103    )]
1104    pub periodic_compaction_threshold_days: Option<usize>,
1105    /// number of epochs to keep the latest version of transactions and effects for
1106    #[serde(skip_serializing_if = "Option::is_none")]
1107    pub num_epochs_to_retain_for_checkpoints: Option<u64>,
1108    /// disables object tombstone pruning. We don't serialize it if it is the default value, false.
1109    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1110    pub killswitch_tombstone_pruning: bool,
1111    #[serde(default = "default_smoothing", skip_serializing_if = "is_true")]
1112    pub smooth: bool,
1113    /// Enables the compaction filter for pruning the objects table.
1114    /// If disabled, a range deletion approach is used instead.
1115    /// While it is generally safe to switch between the two modes,
1116    /// switching from the compaction filter approach back to range deletion
1117    /// may result in some old versions that will never be pruned.
1118    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1119    pub enable_compaction_filter: bool,
1120    #[serde(skip_serializing_if = "Option::is_none")]
1121    pub num_epochs_to_retain_for_indexes: Option<u64>,
1122}
1123
1124fn default_num_latest_epoch_dbs_to_retain() -> usize {
1125    3
1126}
1127
1128fn default_epoch_db_pruning_period_secs() -> u64 {
1129    3600
1130}
1131
1132fn default_max_transactions_in_batch() -> usize {
1133    1000
1134}
1135
1136fn default_max_checkpoints_in_batch() -> usize {
1137    10
1138}
1139
1140fn default_smoothing() -> bool {
1141    cfg!(not(test))
1142}
1143
1144fn default_periodic_compaction_threshold_days() -> Option<usize> {
1145    Some(1)
1146}
1147
1148impl Default for AuthorityStorePruningConfig {
1149    fn default() -> Self {
1150        Self {
1151            num_latest_epoch_dbs_to_retain: default_num_latest_epoch_dbs_to_retain(),
1152            epoch_db_pruning_period_secs: default_epoch_db_pruning_period_secs(),
1153            num_epochs_to_retain: 0,
1154            pruning_run_delay_seconds: if cfg!(msim) { Some(2) } else { None },
1155            max_checkpoints_in_batch: default_max_checkpoints_in_batch(),
1156            max_transactions_in_batch: default_max_transactions_in_batch(),
1157            periodic_compaction_threshold_days: None,
1158            num_epochs_to_retain_for_checkpoints: if cfg!(msim) { Some(2) } else { None },
1159            killswitch_tombstone_pruning: false,
1160            smooth: true,
1161            enable_compaction_filter: cfg!(test) || cfg!(msim),
1162            num_epochs_to_retain_for_indexes: None,
1163        }
1164    }
1165}
1166
1167impl AuthorityStorePruningConfig {
1168    pub fn set_num_epochs_to_retain(&mut self, num_epochs_to_retain: u64) {
1169        self.num_epochs_to_retain = num_epochs_to_retain;
1170    }
1171
1172    pub fn set_num_epochs_to_retain_for_checkpoints(&mut self, num_epochs_to_retain: Option<u64>) {
1173        self.num_epochs_to_retain_for_checkpoints = num_epochs_to_retain;
1174    }
1175
1176    pub fn num_epochs_to_retain_for_checkpoints(&self) -> Option<u64> {
1177        self.num_epochs_to_retain_for_checkpoints
1178            // if n less than 2, coerce to 2 and log
1179            .map(|n| {
1180                if n < 2 {
1181                    info!("num_epochs_to_retain_for_checkpoints must be at least 2, rounding up from {}", n);
1182                    2
1183                } else {
1184                    n
1185                }
1186            })
1187    }
1188
1189    pub fn set_killswitch_tombstone_pruning(&mut self, killswitch_tombstone_pruning: bool) {
1190        self.killswitch_tombstone_pruning = killswitch_tombstone_pruning;
1191    }
1192}
1193
1194#[derive(Debug, Clone, Deserialize, Serialize)]
1195#[serde(rename_all = "kebab-case")]
1196pub struct MetricsConfig {
1197    #[serde(skip_serializing_if = "Option::is_none")]
1198    pub push_interval_seconds: Option<u64>,
1199    #[serde(skip_serializing_if = "Option::is_none")]
1200    pub push_url: Option<String>,
1201}
1202
1203#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1204#[serde(rename_all = "kebab-case")]
1205pub struct DBCheckpointConfig {
1206    #[serde(default)]
1207    pub perform_db_checkpoints_at_epoch_end: bool,
1208    #[serde(skip_serializing_if = "Option::is_none")]
1209    pub checkpoint_path: Option<PathBuf>,
1210    #[serde(skip_serializing_if = "Option::is_none")]
1211    pub object_store_config: Option<ObjectStoreConfig>,
1212    #[serde(skip_serializing_if = "Option::is_none")]
1213    pub perform_index_db_checkpoints_at_epoch_end: Option<bool>,
1214    #[serde(skip_serializing_if = "Option::is_none")]
1215    pub prune_and_compact_before_upload: Option<bool>,
1216}
1217
1218#[derive(Debug, Clone)]
1219pub struct ArchiveReaderConfig {
1220    pub remote_store_config: ObjectStoreConfig,
1221    pub download_concurrency: NonZeroUsize,
1222    pub ingestion_url: Option<String>,
1223    pub remote_store_options: Vec<(String, String)>,
1224}
1225
1226#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1227#[serde(rename_all = "kebab-case")]
1228pub struct StateArchiveConfig {
1229    #[serde(skip_serializing_if = "Option::is_none")]
1230    pub object_store_config: Option<ObjectStoreConfig>,
1231    pub concurrency: usize,
1232    #[serde(skip_serializing_if = "Option::is_none")]
1233    pub ingestion_url: Option<String>,
1234    #[serde(
1235        skip_serializing_if = "Vec::is_empty",
1236        default,
1237        deserialize_with = "deserialize_remote_store_options"
1238    )]
1239    pub remote_store_options: Vec<(String, String)>,
1240}
1241
1242#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1243#[serde(rename_all = "kebab-case")]
1244pub struct StateSnapshotConfig {
1245    #[serde(skip_serializing_if = "Option::is_none")]
1246    pub object_store_config: Option<ObjectStoreConfig>,
1247    pub concurrency: usize,
1248}
1249
1250#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1251#[serde(rename_all = "kebab-case")]
1252pub struct TransactionKeyValueStoreWriteConfig {
1253    pub aws_access_key_id: String,
1254    pub aws_secret_access_key: String,
1255    pub aws_region: String,
1256    pub table_name: String,
1257    pub bucket_name: String,
1258    pub concurrency: usize,
1259}
1260
1261/// Configuration for the threshold(s) at which we consider the system
1262/// to be overloaded. When one of the threshold is passed, the node may
1263/// stop processing new transactions and/or certificates until the congestion
1264/// resolves.
1265#[derive(Clone, Debug, Deserialize, Serialize)]
1266#[serde(rename_all = "kebab-case")]
1267pub struct AuthorityOverloadConfig {
1268    #[serde(default = "default_max_txn_age_in_queue")]
1269    pub max_txn_age_in_queue: Duration,
1270
1271    // The interval of checking overload signal.
1272    #[serde(default = "default_overload_monitor_interval")]
1273    pub overload_monitor_interval: Duration,
1274
1275    // The execution queueing latency when entering load shedding mode.
1276    #[serde(default = "default_execution_queue_latency_soft_limit")]
1277    pub execution_queue_latency_soft_limit: Duration,
1278
1279    // The execution queueing latency when entering aggressive load shedding mode.
1280    #[serde(default = "default_execution_queue_latency_hard_limit")]
1281    pub execution_queue_latency_hard_limit: Duration,
1282
1283    // The maximum percentage of transactions to shed in load shedding mode.
1284    #[serde(default = "default_max_load_shedding_percentage")]
1285    pub max_load_shedding_percentage: u32,
1286
1287    // When in aggressive load shedding mode, the minimum percentage of
1288    // transactions to shed.
1289    #[serde(default = "default_min_load_shedding_percentage_above_hard_limit")]
1290    pub min_load_shedding_percentage_above_hard_limit: u32,
1291
1292    // If transaction ready rate is below this rate, we consider the validator
1293    // is well under used, and will not enter load shedding mode.
1294    #[serde(default = "default_safe_transaction_ready_rate")]
1295    pub safe_transaction_ready_rate: u32,
1296
1297    // When set to true, transaction signing may be rejected when the validator
1298    // is overloaded.
1299    #[serde(default = "default_check_system_overload_at_signing")]
1300    pub check_system_overload_at_signing: bool,
1301
1302    // When set to true, transaction execution may be rejected when the validator
1303    // is overloaded.
1304    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1305    pub check_system_overload_at_execution: bool,
1306
1307    // Reject a transaction if transaction manager queue length is above this threshold.
1308    // 100_000 = 10k TPS * 5s resident time in transaction manager (pending + executing) * 2.
1309    #[serde(default = "default_max_transaction_manager_queue_length")]
1310    pub max_transaction_manager_queue_length: usize,
1311
1312    // Reject a transaction if the number of pending transactions depending on the object
1313    // is above the threshold.
1314    #[serde(default = "default_max_transaction_manager_per_object_queue_length")]
1315    pub max_transaction_manager_per_object_queue_length: usize,
1316}
1317
1318fn default_max_txn_age_in_queue() -> Duration {
1319    Duration::from_millis(1000)
1320}
1321
1322fn default_overload_monitor_interval() -> Duration {
1323    Duration::from_secs(10)
1324}
1325
1326fn default_execution_queue_latency_soft_limit() -> Duration {
1327    Duration::from_secs(1)
1328}
1329
1330fn default_execution_queue_latency_hard_limit() -> Duration {
1331    Duration::from_secs(10)
1332}
1333
1334fn default_max_load_shedding_percentage() -> u32 {
1335    95
1336}
1337
1338fn default_min_load_shedding_percentage_above_hard_limit() -> u32 {
1339    50
1340}
1341
1342fn default_safe_transaction_ready_rate() -> u32 {
1343    100
1344}
1345
1346fn default_check_system_overload_at_signing() -> bool {
1347    true
1348}
1349
1350fn default_max_transaction_manager_queue_length() -> usize {
1351    100_000
1352}
1353
1354fn default_max_transaction_manager_per_object_queue_length() -> usize {
1355    2000
1356}
1357
1358impl Default for AuthorityOverloadConfig {
1359    fn default() -> Self {
1360        Self {
1361            max_txn_age_in_queue: default_max_txn_age_in_queue(),
1362            overload_monitor_interval: default_overload_monitor_interval(),
1363            execution_queue_latency_soft_limit: default_execution_queue_latency_soft_limit(),
1364            execution_queue_latency_hard_limit: default_execution_queue_latency_hard_limit(),
1365            max_load_shedding_percentage: default_max_load_shedding_percentage(),
1366            min_load_shedding_percentage_above_hard_limit:
1367                default_min_load_shedding_percentage_above_hard_limit(),
1368            safe_transaction_ready_rate: default_safe_transaction_ready_rate(),
1369            check_system_overload_at_signing: true,
1370            check_system_overload_at_execution: false,
1371            max_transaction_manager_queue_length: default_max_transaction_manager_queue_length(),
1372            max_transaction_manager_per_object_queue_length:
1373                default_max_transaction_manager_per_object_queue_length(),
1374        }
1375    }
1376}
1377
1378fn default_authority_overload_config() -> AuthorityOverloadConfig {
1379    AuthorityOverloadConfig::default()
1380}
1381
1382fn default_traffic_controller_policy_config() -> Option<PolicyConfig> {
1383    Some(PolicyConfig::default_dos_protection_policy())
1384}
1385
1386#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1387pub struct Genesis {
1388    #[serde(flatten)]
1389    location: GenesisLocation,
1390
1391    #[serde(skip)]
1392    genesis: once_cell::sync::OnceCell<genesis::Genesis>,
1393}
1394
1395impl Genesis {
1396    pub fn new(genesis: genesis::Genesis) -> Self {
1397        Self {
1398            location: GenesisLocation::InPlace { genesis },
1399            genesis: Default::default(),
1400        }
1401    }
1402
1403    pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
1404        Self {
1405            location: GenesisLocation::File {
1406                genesis_file_location: path.into(),
1407            },
1408            genesis: Default::default(),
1409        }
1410    }
1411
1412    pub fn genesis(&self) -> Result<&genesis::Genesis> {
1413        match &self.location {
1414            GenesisLocation::InPlace { genesis } => Ok(genesis),
1415            GenesisLocation::File {
1416                genesis_file_location,
1417            } => self
1418                .genesis
1419                .get_or_try_init(|| genesis::Genesis::load(genesis_file_location)),
1420        }
1421    }
1422}
1423
1424#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1425#[serde(untagged)]
1426#[allow(clippy::large_enum_variant)]
1427enum GenesisLocation {
1428    InPlace {
1429        genesis: genesis::Genesis,
1430    },
1431    File {
1432        #[serde(rename = "genesis-file-location")]
1433        genesis_file_location: PathBuf,
1434    },
1435}
1436
1437/// Wrapper struct for SuiKeyPair that can be deserialized from a file path. Used by network, worker, and account keypair.
1438#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1439pub struct KeyPairWithPath {
1440    #[serde(flatten)]
1441    location: KeyPairLocation,
1442
1443    #[serde(skip)]
1444    keypair: OnceCell<Arc<SuiKeyPair>>,
1445}
1446
1447#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1448#[serde_as]
1449#[serde(untagged)]
1450enum KeyPairLocation {
1451    InPlace {
1452        #[serde_as(as = "Arc<KeyPairBase64>")]
1453        value: Arc<SuiKeyPair>,
1454    },
1455    File {
1456        #[serde(rename = "path")]
1457        path: PathBuf,
1458    },
1459}
1460
1461impl KeyPairWithPath {
1462    pub fn new(kp: SuiKeyPair) -> Self {
1463        let cell: OnceCell<Arc<SuiKeyPair>> = OnceCell::new();
1464        let arc_kp = Arc::new(kp);
1465        // OK to unwrap panic because authority should not start without all keypairs loaded.
1466        cell.set(arc_kp.clone()).expect("Failed to set keypair");
1467        Self {
1468            location: KeyPairLocation::InPlace { value: arc_kp },
1469            keypair: cell,
1470        }
1471    }
1472
1473    pub fn new_from_path(path: PathBuf) -> Self {
1474        let cell: OnceCell<Arc<SuiKeyPair>> = OnceCell::new();
1475        // OK to unwrap panic because authority should not start without all keypairs loaded.
1476        cell.set(Arc::new(read_keypair_from_file(&path).unwrap_or_else(
1477            |e| panic!("Invalid keypair file at path {:?}: {e}", &path),
1478        )))
1479        .expect("Failed to set keypair");
1480        Self {
1481            location: KeyPairLocation::File { path },
1482            keypair: cell,
1483        }
1484    }
1485
1486    pub fn keypair(&self) -> &SuiKeyPair {
1487        self.keypair
1488            .get_or_init(|| match &self.location {
1489                KeyPairLocation::InPlace { value } => value.clone(),
1490                KeyPairLocation::File { path } => {
1491                    // OK to unwrap panic because authority should not start without all keypairs loaded.
1492                    Arc::new(
1493                        read_keypair_from_file(path).unwrap_or_else(|e| {
1494                            panic!("Invalid keypair file at path {:?}: {e}", path)
1495                        }),
1496                    )
1497                }
1498            })
1499            .as_ref()
1500    }
1501}
1502
1503/// Wrapper struct for AuthorityKeyPair that can be deserialized from a file path.
1504#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1505pub struct AuthorityKeyPairWithPath {
1506    #[serde(flatten)]
1507    location: AuthorityKeyPairLocation,
1508
1509    #[serde(skip)]
1510    keypair: OnceCell<Arc<AuthorityKeyPair>>,
1511}
1512
1513#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1514#[serde_as]
1515#[serde(untagged)]
1516enum AuthorityKeyPairLocation {
1517    InPlace { value: Arc<AuthorityKeyPair> },
1518    File { path: PathBuf },
1519}
1520
1521impl AuthorityKeyPairWithPath {
1522    pub fn new(kp: AuthorityKeyPair) -> Self {
1523        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1524        let arc_kp = Arc::new(kp);
1525        // OK to unwrap panic because authority should not start without all keypairs loaded.
1526        cell.set(arc_kp.clone())
1527            .expect("Failed to set authority keypair");
1528        Self {
1529            location: AuthorityKeyPairLocation::InPlace { value: arc_kp },
1530            keypair: cell,
1531        }
1532    }
1533
1534    pub fn new_from_path(path: PathBuf) -> Self {
1535        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1536        // OK to unwrap panic because authority should not start without all keypairs loaded.
1537        cell.set(Arc::new(
1538            read_authority_keypair_from_file(&path)
1539                .unwrap_or_else(|_| panic!("Invalid authority keypair file at path {:?}", &path)),
1540        ))
1541        .expect("Failed to set authority keypair");
1542        Self {
1543            location: AuthorityKeyPairLocation::File { path },
1544            keypair: cell,
1545        }
1546    }
1547
1548    pub fn authority_keypair(&self) -> &AuthorityKeyPair {
1549        self.keypair
1550            .get_or_init(|| match &self.location {
1551                AuthorityKeyPairLocation::InPlace { value } => value.clone(),
1552                AuthorityKeyPairLocation::File { path } => {
1553                    // OK to unwrap panic because authority should not start without all keypairs loaded.
1554                    Arc::new(
1555                        read_authority_keypair_from_file(path).unwrap_or_else(|_| {
1556                            panic!("Invalid authority keypair file {:?}", &path)
1557                        }),
1558                    )
1559                }
1560            })
1561            .as_ref()
1562    }
1563}
1564
1565/// Configurations which determine how we dump state debug info.
1566/// Debug info is dumped when a node forks.
1567#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1568#[serde(rename_all = "kebab-case")]
1569pub struct StateDebugDumpConfig {
1570    #[serde(skip_serializing_if = "Option::is_none")]
1571    pub dump_file_directory: Option<PathBuf>,
1572}
1573
1574fn read_credential_from_path_or_literal(value: &str) -> Result<String, std::io::Error> {
1575    let path = Path::new(value);
1576    if path.exists() && path.is_file() {
1577        std::fs::read_to_string(path).map(|content| content.trim().to_string())
1578    } else {
1579        Ok(value.to_string())
1580    }
1581}
1582
1583// Custom deserializer for remote store options that supports file paths or literal values
1584fn deserialize_remote_store_options<'de, D>(
1585    deserializer: D,
1586) -> Result<Vec<(String, String)>, D::Error>
1587where
1588    D: serde::Deserializer<'de>,
1589{
1590    use serde::de::Error;
1591
1592    let raw_options: Vec<(String, String)> = Vec::deserialize(deserializer)?;
1593    let mut processed_options = Vec::new();
1594
1595    for (key, value) in raw_options {
1596        // GCS service_account keys expect a file path, not the file content
1597        // All other keys (AWS credentials, service_account_key) should read file content
1598        let is_service_account_path = matches!(
1599            key.as_str(),
1600            "google_service_account"
1601                | "service_account"
1602                | "google_service_account_path"
1603                | "service_account_path"
1604        );
1605
1606        let processed_value = if is_service_account_path {
1607            value
1608        } else {
1609            match read_credential_from_path_or_literal(&value) {
1610                Ok(processed) => processed,
1611                Err(e) => {
1612                    return Err(D::Error::custom(format!(
1613                        "Failed to read credential for key '{}': {}",
1614                        key, e
1615                    )));
1616                }
1617            }
1618        };
1619
1620        processed_options.push((key, processed_value));
1621    }
1622
1623    Ok(processed_options)
1624}
1625
1626#[cfg(test)]
1627mod tests {
1628    use std::path::PathBuf;
1629
1630    use fastcrypto::traits::KeyPair;
1631    use rand::{SeedableRng, rngs::StdRng};
1632    use sui_keys::keypair_file::{write_authority_keypair_to_file, write_keypair_to_file};
1633    use sui_types::crypto::{AuthorityKeyPair, NetworkKeyPair, SuiKeyPair, get_key_pair_from_rng};
1634
1635    use super::{Genesis, StateArchiveConfig};
1636    use crate::NodeConfig;
1637
1638    #[test]
1639    fn serialize_genesis_from_file() {
1640        let g = Genesis::new_from_file("path/to/file");
1641
1642        let s = serde_yaml::to_string(&g).unwrap();
1643        assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
1644        let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
1645        assert_eq!(g, loaded_genesis);
1646    }
1647
1648    #[test]
1649    fn fullnode_template() {
1650        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1651
1652        let _template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1653    }
1654
1655    /// Tests that a legacy validator config (captured on 12/06/2024) can be parsed.
1656    #[test]
1657    fn legacy_validator_config() {
1658        const FILE: &str = include_str!("../data/sui-node-legacy.yaml");
1659
1660        let _template: NodeConfig = serde_yaml::from_str(FILE).unwrap();
1661    }
1662
1663    #[test]
1664    fn load_key_pairs_to_node_config() {
1665        let protocol_key_pair: AuthorityKeyPair =
1666            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1667        let worker_key_pair: NetworkKeyPair =
1668            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1669        let network_key_pair: NetworkKeyPair =
1670            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1671
1672        write_authority_keypair_to_file(&protocol_key_pair, PathBuf::from("protocol.key")).unwrap();
1673        write_keypair_to_file(
1674            &SuiKeyPair::Ed25519(worker_key_pair.copy()),
1675            PathBuf::from("worker.key"),
1676        )
1677        .unwrap();
1678        write_keypair_to_file(
1679            &SuiKeyPair::Ed25519(network_key_pair.copy()),
1680            PathBuf::from("network.key"),
1681        )
1682        .unwrap();
1683
1684        const TEMPLATE: &str = include_str!("../data/fullnode-template-with-path.yaml");
1685        let template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1686        assert_eq!(
1687            template.protocol_key_pair().public(),
1688            protocol_key_pair.public()
1689        );
1690        assert_eq!(
1691            template.network_key_pair().public(),
1692            network_key_pair.public()
1693        );
1694        assert_eq!(
1695            template.worker_key_pair().public(),
1696            worker_key_pair.public()
1697        );
1698    }
1699
1700    #[test]
1701    fn test_remote_store_options_file_path_support() {
1702        // Create temporary credential files
1703        let temp_dir = std::env::temp_dir();
1704        let access_key_file = temp_dir.join("test_access_key");
1705        let secret_key_file = temp_dir.join("test_secret_key");
1706
1707        std::fs::write(&access_key_file, "test_access_key_value").unwrap();
1708        std::fs::write(&secret_key_file, "test_secret_key_value\n").unwrap();
1709
1710        let yaml_config = format!(
1711            r#"
1712object-store-config: null
1713concurrency: 5
1714ingestion-url: "https://example.com"
1715remote-store-options:
1716  - ["aws_access_key_id", "{}"]
1717  - ["aws_secret_access_key", "{}"]
1718  - ["literal_key", "literal_value"]
1719"#,
1720            access_key_file.to_string_lossy(),
1721            secret_key_file.to_string_lossy()
1722        );
1723
1724        let config: StateArchiveConfig = serde_yaml::from_str(&yaml_config).unwrap();
1725
1726        // Verify that file paths were resolved and literal values preserved
1727        assert_eq!(config.remote_store_options.len(), 3);
1728
1729        let access_key_option = config
1730            .remote_store_options
1731            .iter()
1732            .find(|(key, _)| key == "aws_access_key_id")
1733            .unwrap();
1734        assert_eq!(access_key_option.1, "test_access_key_value");
1735
1736        let secret_key_option = config
1737            .remote_store_options
1738            .iter()
1739            .find(|(key, _)| key == "aws_secret_access_key")
1740            .unwrap();
1741        assert_eq!(secret_key_option.1, "test_secret_key_value");
1742
1743        let literal_option = config
1744            .remote_store_options
1745            .iter()
1746            .find(|(key, _)| key == "literal_key")
1747            .unwrap();
1748        assert_eq!(literal_option.1, "literal_value");
1749
1750        // Clean up
1751        std::fs::remove_file(&access_key_file).ok();
1752        std::fs::remove_file(&secret_key_file).ok();
1753    }
1754
1755    #[test]
1756    fn test_remote_store_options_literal_values_only() {
1757        let yaml_config = r#"
1758object-store-config: null
1759concurrency: 5
1760ingestion-url: "https://example.com"
1761remote-store-options:
1762  - ["aws_access_key_id", "literal_access_key"]
1763  - ["aws_secret_access_key", "literal_secret_key"]
1764"#;
1765
1766        let config: StateArchiveConfig = serde_yaml::from_str(yaml_config).unwrap();
1767
1768        assert_eq!(config.remote_store_options.len(), 2);
1769        assert_eq!(config.remote_store_options[0].1, "literal_access_key");
1770        assert_eq!(config.remote_store_options[1].1, "literal_secret_key");
1771    }
1772
1773    #[test]
1774    fn test_remote_store_options_gcs_service_account_path_preserved() {
1775        let temp_dir = std::env::temp_dir();
1776        let service_account_file = temp_dir.join("test_service_account.json");
1777        let aws_key_file = temp_dir.join("test_aws_key");
1778
1779        std::fs::write(&service_account_file, r#"{"type": "service_account"}"#).unwrap();
1780        std::fs::write(&aws_key_file, "aws_key_value").unwrap();
1781
1782        let yaml_config = format!(
1783            r#"
1784object-store-config: null
1785concurrency: 5
1786ingestion-url: "gs://my-bucket"
1787remote-store-options:
1788  - ["service_account", "{}"]
1789  - ["google_service_account_path", "{}"]
1790  - ["aws_access_key_id", "{}"]
1791"#,
1792            service_account_file.to_string_lossy(),
1793            service_account_file.to_string_lossy(),
1794            aws_key_file.to_string_lossy()
1795        );
1796
1797        let config: StateArchiveConfig = serde_yaml::from_str(&yaml_config).unwrap();
1798
1799        assert_eq!(config.remote_store_options.len(), 3);
1800
1801        // service_account should preserve the file path, not read the content
1802        let service_account_option = config
1803            .remote_store_options
1804            .iter()
1805            .find(|(key, _)| key == "service_account")
1806            .unwrap();
1807        assert_eq!(
1808            service_account_option.1,
1809            service_account_file.to_string_lossy()
1810        );
1811
1812        // google_service_account_path should also preserve the file path
1813        let gcs_path_option = config
1814            .remote_store_options
1815            .iter()
1816            .find(|(key, _)| key == "google_service_account_path")
1817            .unwrap();
1818        assert_eq!(gcs_path_option.1, service_account_file.to_string_lossy());
1819
1820        // AWS key should read the file content
1821        let aws_option = config
1822            .remote_store_options
1823            .iter()
1824            .find(|(key, _)| key == "aws_access_key_id")
1825            .unwrap();
1826        assert_eq!(aws_option.1, "aws_key_value");
1827
1828        // Clean up
1829        std::fs::remove_file(&service_account_file).ok();
1830        std::fs::remove_file(&aws_key_file).ok();
1831    }
1832}
1833
1834// RunWithRange is used to specify the ending epoch/checkpoint to process.
1835// this is intended for use with disaster recovery debugging and verification workflows, never in normal operations
1836#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
1837pub enum RunWithRange {
1838    Epoch(EpochId),
1839    Checkpoint(CheckpointSequenceNumber),
1840}
1841
1842impl RunWithRange {
1843    // is epoch_id > RunWithRange::Epoch
1844    pub fn is_epoch_gt(&self, epoch_id: EpochId) -> bool {
1845        matches!(self, RunWithRange::Epoch(e) if epoch_id > *e)
1846    }
1847
1848    pub fn matches_checkpoint(&self, seq_num: CheckpointSequenceNumber) -> bool {
1849        matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
1850    }
1851
1852    pub fn into_checkpoint_bound(self) -> Option<CheckpointSequenceNumber> {
1853        match self {
1854            RunWithRange::Epoch(_) => None,
1855            RunWithRange::Checkpoint(seq) => Some(seq),
1856        }
1857    }
1858}