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