iota_config/
node.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{
6    collections::{BTreeMap, BTreeSet},
7    net::{IpAddr, Ipv4Addr, SocketAddr},
8    num::NonZeroUsize,
9    path::{Path, PathBuf},
10    sync::Arc,
11    time::Duration,
12};
13
14use anyhow::Result;
15use consensus_config::Parameters as ConsensusParameters;
16use iota_keys::keypair_file::{read_authority_keypair_from_file, read_keypair_from_file};
17use iota_names::config::IotaNamesConfig;
18use iota_types::{
19    base_types::IotaAddress,
20    committee::EpochId,
21    crypto::{
22        AccountKeyPair, AuthorityKeyPair, AuthorityPublicKeyBytes, IotaKeyPair, KeypairTraits,
23        NetworkKeyPair, get_key_pair_from_rng,
24    },
25    messages_checkpoint::CheckpointSequenceNumber,
26    multiaddr::Multiaddr,
27    supported_protocol_versions::{Chain, SupportedProtocolVersions},
28    traffic_control::{PolicyConfig, RemoteFirewallConfig},
29};
30use once_cell::sync::OnceCell;
31use rand::rngs::OsRng;
32use serde::{Deserialize, Serialize};
33use starfish_config::Parameters as StarfishParameters;
34use tracing::info;
35
36use crate::{
37    Config, certificate_deny_config::CertificateDenyConfig, genesis,
38    migration_tx_data::MigrationTxData, object_storage_config::ObjectStoreConfig, p2p::P2pConfig,
39    transaction_deny_config::TransactionDenyConfig, verifier_signing_config::VerifierSigningConfig,
40};
41
42// Default max number of concurrent requests served
43pub const DEFAULT_GRPC_CONCURRENCY_LIMIT: usize = 20000000000;
44
45/// Default gas price of 1000 Nanos
46pub const DEFAULT_VALIDATOR_GAS_PRICE: u64 = iota_types::transaction::DEFAULT_VALIDATOR_GAS_PRICE;
47
48/// Default commission rate of 2%
49pub const DEFAULT_COMMISSION_RATE: u64 = 200;
50
51#[derive(Clone, Debug, Deserialize, Serialize)]
52#[serde(rename_all = "kebab-case")]
53pub struct NodeConfig {
54    /// The public key bytes corresponding to the private key that the validator
55    /// holds to sign transactions.
56    #[serde(default = "default_authority_key_pair")]
57    pub authority_key_pair: AuthorityKeyPairWithPath,
58    /// The public key bytes corresponding to the private key that the validator
59    /// holds to sign consensus blocks.
60    #[serde(default = "default_key_pair")]
61    pub protocol_key_pair: KeyPairWithPath,
62    #[serde(default = "default_key_pair")]
63    pub account_key_pair: KeyPairWithPath,
64    /// The public key bytes corresponding to the private key that the validator
65    /// uses to establish TLS connections.
66    #[serde(default = "default_key_pair")]
67    pub network_key_pair: KeyPairWithPath,
68    pub db_path: PathBuf,
69
70    /// The network address for gRPC communication.
71    ///
72    /// Can be overwritten with args `listen-address` parameters.
73    #[serde(default = "default_grpc_address")]
74    pub network_address: Multiaddr,
75    #[serde(default = "default_json_rpc_address")]
76    pub json_rpc_address: SocketAddr,
77
78    /// Flag to enable the REST API under `/api/v1`
79    /// endpoint on the same interface as `json` `rpc` server.
80    #[serde(default)]
81    pub enable_rest_api: bool,
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub rest: Option<iota_rest_api::Config>,
84
85    /// The address for Prometheus metrics.
86    #[serde(default = "default_metrics_address")]
87    pub metrics_address: SocketAddr,
88
89    /// The address for the admin interface that is
90    /// run in the metrics separate runtime and provides access to
91    /// admin node commands such as logging and tracing options.
92    #[serde(default = "default_admin_interface_address")]
93    pub admin_interface_address: SocketAddr,
94
95    /// Configuration struct for the consensus.
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub consensus_config: Option<ConsensusConfig>,
98
99    /// Flag to enable index processing for a full node.
100    ///
101    /// If set to true, node creates `IndexStore` for transaction
102    /// data including ownership and balance information.
103    #[serde(default = "default_enable_index_processing")]
104    pub enable_index_processing: bool,
105
106    // only allow websocket connections for jsonrpc traffic
107    #[serde(default)]
108    /// Determines the jsonrpc server type as either:
109    /// - 'websocket' for a websocket based service (deprecated)
110    /// - 'http' for an http based service
111    /// - 'both' for both a websocket and http based service (deprecated)
112    pub jsonrpc_server_type: Option<ServerType>,
113
114    /// Flag to enable gRPC load shedding to manage and
115    /// mitigate overload conditions by shedding excess
116    /// load with `LoadShedLayer` middleware.
117    #[serde(default)]
118    pub grpc_load_shed: Option<bool>,
119
120    #[serde(default = "default_concurrency_limit")]
121    pub grpc_concurrency_limit: Option<usize>,
122
123    /// Configuration struct for P2P.
124    #[serde(default)]
125    pub p2p_config: P2pConfig,
126
127    /// Contains genesis location that might be `InPlace`
128    /// for reading all genesis data to memory or `InFile`,
129    /// and `OnceCell` pointer to a genesis struct.
130    pub genesis: Genesis,
131
132    /// Contains the path where to find the migration blob.
133    pub migration_tx_data_path: Option<PathBuf>,
134
135    /// Configuration for pruning of the authority store, to define when
136    /// an old data is removed from the storage space.
137    #[serde(default = "default_authority_store_pruning_config")]
138    pub authority_store_pruning_config: AuthorityStorePruningConfig,
139
140    /// Size of the broadcast channel used for notifying other systems of end of
141    /// epoch.
142    ///
143    /// If unspecified, this will default to `128`.
144    #[serde(default = "default_end_of_epoch_broadcast_channel_capacity")]
145    pub end_of_epoch_broadcast_channel_capacity: usize,
146
147    /// Configuration for the checkpoint executor for limiting
148    /// the number of checkpoints to execute concurrently,
149    /// and to allow for checkpoint post-processing.
150    #[serde(default)]
151    pub checkpoint_executor_config: CheckpointExecutorConfig,
152
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub metrics: Option<MetricsConfig>,
155
156    /// In a `iota-node` binary, this is set to
157    /// SupportedProtocolVersions::SYSTEM_DEFAULT in iota-node/src/main.rs.
158    /// It is present in the config so that it can be changed by tests in
159    /// order to test protocol upgrades.
160    #[serde(skip)]
161    pub supported_protocol_versions: Option<SupportedProtocolVersions>,
162
163    /// Configuration to manage database checkpoints,
164    /// including whether to perform checkpoints at the end of an epoch,
165    /// the path for storing checkpoints, and other related settings.
166    #[serde(default)]
167    pub db_checkpoint_config: DBCheckpointConfig,
168
169    /// Configuration for enabling/disabling expensive safety checks.
170    #[serde(default)]
171    pub expensive_safety_check_config: ExpensiveSafetyCheckConfig,
172
173    /// Configuration to specify rules for denying transactions
174    /// based on `objectsIDs`, `addresses`, or enable/disable many
175    /// features such as publishing new packages or using shared objects.
176    #[serde(default)]
177    pub transaction_deny_config: TransactionDenyConfig,
178
179    /// Config used to deny execution for certificate digests
180    /// know for crashing or hanging validator nodes.
181    ///
182    /// Should be used for a fast temporary fixes and
183    /// removed once the issue is fixed.
184    #[serde(default)]
185    pub certificate_deny_config: CertificateDenyConfig,
186
187    /// Used to determine how state debug information is dumped
188    /// when a node forks.
189    #[serde(default)]
190    pub state_debug_dump_config: StateDebugDumpConfig,
191
192    /// Configuration for writing state archive. If `ObjectStorage`
193    /// config is provided, `ArchiveWriter` will be created
194    /// for checkpoints archival.
195    #[serde(default)]
196    pub state_archive_write_config: StateArchiveConfig,
197
198    #[serde(default)]
199    pub state_archive_read_config: Vec<StateArchiveConfig>,
200
201    /// Determines if snapshot should be uploaded to the remote storage.
202    #[serde(default)]
203    pub state_snapshot_write_config: StateSnapshotConfig,
204
205    #[serde(default)]
206    pub indexer_max_subscriptions: Option<usize>,
207
208    #[serde(default = "default_transaction_kv_store_config")]
209    pub transaction_kv_store_read_config: TransactionKeyValueStoreReadConfig,
210
211    // TODO: write config seem to be unused.
212    #[serde(skip_serializing_if = "Option::is_none")]
213    pub transaction_kv_store_write_config: Option<TransactionKeyValueStoreWriteConfig>,
214
215    #[serde(default = "default_jwk_fetch_interval_seconds")]
216    pub jwk_fetch_interval_seconds: u64,
217
218    #[serde(default = "default_zklogin_oauth_providers")]
219    pub zklogin_oauth_providers: BTreeMap<Chain, BTreeSet<String>>,
220
221    /// Configuration for defining thresholds and settings
222    /// for managing system overload conditions in a node.
223    #[serde(default = "default_authority_overload_config")]
224    pub authority_overload_config: AuthorityOverloadConfig,
225
226    /// Specifies the ending epoch for a node for debugging purposes.
227    ///
228    ///  Ignored if set by config, can be configured only by cli arguments.
229    #[serde(skip_serializing_if = "Option::is_none")]
230    pub run_with_range: Option<RunWithRange>,
231
232    // For killswitch use None
233    #[serde(skip_serializing_if = "Option::is_none")]
234    pub policy_config: Option<PolicyConfig>,
235
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub firewall_config: Option<RemoteFirewallConfig>,
238
239    #[serde(default)]
240    pub execution_cache: ExecutionCacheType,
241
242    #[serde(default)]
243    pub execution_cache_config: ExecutionCacheConfig,
244
245    #[serde(default = "bool_true")]
246    pub enable_validator_tx_finalizer: bool,
247
248    #[serde(default)]
249    pub verifier_signing_config: VerifierSigningConfig,
250
251    /// If a value is set, it determines if writes to DB can stall, which can
252    /// halt the whole process. By default, write stall is enabled on
253    /// validators but not on fullnodes.
254    #[serde(skip_serializing_if = "Option::is_none")]
255    pub enable_db_write_stall: Option<bool>,
256
257    #[serde(default, skip_serializing_if = "Option::is_none")]
258    pub iota_names_config: Option<IotaNamesConfig>,
259
260    /// Flag to enable the gRPC API.
261    #[serde(default)]
262    pub enable_grpc_api: bool,
263    #[serde(
264        default = "default_grpc_api_config",
265        skip_serializing_if = "Option::is_none"
266    )]
267    pub grpc_api_config: Option<GrpcApiConfig>,
268
269    /// Allow overriding the chain for testing purposes. For instance, it allows
270    /// you to create a test network that believes it is mainnet or testnet.
271    /// Attempting to override this value on production networks will result
272    /// in an error.
273    #[serde(skip_serializing_if = "Option::is_none")]
274    pub chain_override_for_testing: Option<Chain>,
275}
276
277#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
278#[serde(rename_all = "kebab-case")]
279pub struct TlsConfig {
280    /// File path to a PEM formatted TLS certificate chain
281    cert: String,
282    /// File path to a PEM formatted TLS private key
283    key: String,
284}
285
286impl TlsConfig {
287    pub fn cert(&self) -> &str {
288        &self.cert
289    }
290
291    pub fn key(&self) -> &str {
292        &self.key
293    }
294}
295
296/// Configuration for the gRPC API service
297#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
298#[serde(rename_all = "kebab-case")]
299pub struct GrpcApiConfig {
300    /// The address to bind the gRPC server to
301    #[serde(default = "default_grpc_api_address")]
302    pub address: SocketAddr,
303
304    /// TLS configuration for the gRPC server.
305    ///
306    /// If not provided, the gRPC server will use plain TCP without TLS.
307    #[serde(skip_serializing_if = "Option::is_none")]
308    pub tls: Option<TlsConfig>,
309
310    /// Maximum message size for gRPC responses (in bytes)
311    #[serde(default = "default_grpc_api_max_message_size_bytes")]
312    pub max_message_size_bytes: u32,
313
314    /// Buffer size for broadcast channels used for streaming
315    #[serde(default = "default_grpc_api_broadcast_buffer_size")]
316    pub broadcast_buffer_size: u32,
317
318    /// Maximum size for Move values when rendering to JSON
319    /// in bytes.
320    #[serde(default = "default_grpc_api_max_json_move_value_size")]
321    pub max_json_move_value_size: usize,
322
323    /// Maximum number of transactions allowed in a single ExecuteTransactions
324    /// batch request.
325    #[serde(default = "default_grpc_api_max_execute_transaction_batch_size")]
326    pub max_execute_transaction_batch_size: u32,
327
328    /// Maximum number of transactions allowed in a single SimulateTransactions
329    /// batch request.
330    #[serde(default = "default_grpc_api_max_simulate_transaction_batch_size")]
331    pub max_simulate_transaction_batch_size: u32,
332
333    /// Maximum allowed timeout in milliseconds for waiting for checkpoint
334    /// inclusion in ExecuteTransactions requests. Client-specified timeouts
335    /// are clamped to this value.
336    #[serde(default = "default_grpc_api_max_checkpoint_inclusion_timeout_ms")]
337    pub max_checkpoint_inclusion_timeout_ms: u64,
338}
339
340fn default_grpc_api_address() -> SocketAddr {
341    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 50051)
342}
343
344fn default_grpc_api_broadcast_buffer_size() -> u32 {
345    100
346}
347
348fn default_grpc_api_max_message_size_bytes() -> u32 {
349    128 * 1024 * 1024 // 128MB
350}
351
352fn default_grpc_api_max_json_move_value_size() -> usize {
353    1024 * 1024 // 1 MB
354}
355
356fn default_grpc_api_max_execute_transaction_batch_size() -> u32 {
357    20
358}
359
360fn default_grpc_api_max_simulate_transaction_batch_size() -> u32 {
361    20
362}
363
364fn default_grpc_api_max_checkpoint_inclusion_timeout_ms() -> u64 {
365    60_000 // 60 seconds
366}
367
368impl Default for GrpcApiConfig {
369    fn default() -> Self {
370        Self {
371            address: default_grpc_api_address(),
372            tls: None,
373            max_message_size_bytes: default_grpc_api_max_message_size_bytes(),
374            broadcast_buffer_size: default_grpc_api_broadcast_buffer_size(),
375            max_json_move_value_size: default_grpc_api_max_json_move_value_size(),
376            max_execute_transaction_batch_size: default_grpc_api_max_execute_transaction_batch_size(
377            ),
378            max_simulate_transaction_batch_size:
379                default_grpc_api_max_simulate_transaction_batch_size(),
380            max_checkpoint_inclusion_timeout_ms:
381                default_grpc_api_max_checkpoint_inclusion_timeout_ms(),
382        }
383    }
384}
385
386impl GrpcApiConfig {
387    // The default maximum uncompressed size in bytes for a message, based on
388    // tonic's default.
389    const GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE: u32 = 4 * 1024 * 1024; // 4MB
390    const GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES: u32 =
391        Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE;
392
393    pub fn tls_config(&self) -> Option<&TlsConfig> {
394        self.tls.as_ref()
395    }
396
397    pub fn max_message_size_bytes(&self) -> u32 {
398        // Ensure max message size is at least the minimum allowed
399        self.max_message_size_bytes
400            .max(Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES)
401    }
402
403    /// Calculate the maximum size for a message that can be
404    /// sent to a client, taking into account the client's max message size
405    /// preference.
406    pub fn max_message_size_client_bytes(&self, client_max_message_size_bytes: Option<u32>) -> u32 {
407        client_max_message_size_bytes
408            // if the client did not specify a max message size, use the tonic default receive
409            // message size
410            .unwrap_or(Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE)
411            // clamp the value between the tonic default and the service max message size
412            .clamp(
413                Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES,
414                self.max_message_size_bytes(),
415            )
416    }
417}
418
419#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
420#[serde(rename_all = "kebab-case")]
421pub enum ExecutionCacheType {
422    #[default]
423    WritebackCache,
424    PassthroughCache,
425}
426
427impl From<ExecutionCacheType> for u8 {
428    fn from(cache_type: ExecutionCacheType) -> Self {
429        match cache_type {
430            ExecutionCacheType::WritebackCache => 0,
431            ExecutionCacheType::PassthroughCache => 1,
432        }
433    }
434}
435
436impl From<&u8> for ExecutionCacheType {
437    fn from(cache_type: &u8) -> Self {
438        match cache_type {
439            0 => ExecutionCacheType::WritebackCache,
440            1 => ExecutionCacheType::PassthroughCache,
441            _ => unreachable!("Invalid value for ExecutionCacheType"),
442        }
443    }
444}
445
446/// Type alias for atomic representation of ExecutionCacheType for lock-free
447/// operations
448pub type ExecutionCacheTypeAtomicU8 = std::sync::atomic::AtomicU8;
449
450impl From<ExecutionCacheType> for ExecutionCacheTypeAtomicU8 {
451    fn from(cache_type: ExecutionCacheType) -> Self {
452        ExecutionCacheTypeAtomicU8::new(u8::from(cache_type))
453    }
454}
455
456impl ExecutionCacheType {
457    pub fn cache_type(self) -> Self {
458        if std::env::var("DISABLE_WRITEBACK_CACHE").is_ok() {
459            Self::PassthroughCache
460        } else {
461            self
462        }
463    }
464}
465
466#[derive(Clone, Debug, Default, Deserialize, Serialize)]
467#[serde(rename_all = "kebab-case")]
468pub struct ExecutionCacheConfig {
469    #[serde(default)]
470    pub writeback_cache: WritebackCacheConfig,
471}
472
473#[derive(Clone, Debug, Default, Deserialize, Serialize)]
474#[serde(rename_all = "kebab-case")]
475pub struct WritebackCacheConfig {
476    /// Maximum number of entries in each cache. (There are several
477    /// different caches).
478    #[serde(default, skip_serializing_if = "Option::is_none")]
479    pub max_cache_size: Option<u64>, // defaults to 100000
480
481    #[serde(default, skip_serializing_if = "Option::is_none")]
482    pub package_cache_size: Option<u64>, // defaults to 1000
483
484    #[serde(default, skip_serializing_if = "Option::is_none")]
485    pub object_cache_size: Option<u64>, // defaults to max_cache_size
486    #[serde(default, skip_serializing_if = "Option::is_none")]
487    pub marker_cache_size: Option<u64>, // defaults to object_cache_size
488    #[serde(default, skip_serializing_if = "Option::is_none")]
489    pub object_by_id_cache_size: Option<u64>, // defaults to object_cache_size
490
491    #[serde(default, skip_serializing_if = "Option::is_none")]
492    pub transaction_cache_size: Option<u64>, // defaults to max_cache_size
493    #[serde(default, skip_serializing_if = "Option::is_none")]
494    pub executed_effect_cache_size: Option<u64>, // defaults to transaction_cache_size
495    #[serde(default, skip_serializing_if = "Option::is_none")]
496    pub effect_cache_size: Option<u64>, // defaults to executed_effect_cache_size
497
498    #[serde(default, skip_serializing_if = "Option::is_none")]
499    pub events_cache_size: Option<u64>, // defaults to transaction_cache_size
500
501    #[serde(default, skip_serializing_if = "Option::is_none")]
502    pub transaction_objects_cache_size: Option<u64>, // defaults to 1000
503
504    /// Number of uncommitted transactions at which to pause consensus
505    /// handler.
506    #[serde(default, skip_serializing_if = "Option::is_none")]
507    pub backpressure_threshold: Option<u64>, // defaults to 100_000
508
509    /// Number of uncommitted transactions at which to refuse new
510    /// transaction submissions. Defaults to backpressure_threshold
511    /// if unset.
512    #[serde(default, skip_serializing_if = "Option::is_none")]
513    pub backpressure_threshold_for_rpc: Option<u64>, // defaults to backpressure_threshold
514}
515
516impl WritebackCacheConfig {
517    pub fn max_cache_size(&self) -> u64 {
518        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MAX")
519            .ok()
520            .and_then(|s| s.parse().ok())
521            .or(self.max_cache_size)
522            .unwrap_or(100000)
523    }
524
525    pub fn package_cache_size(&self) -> u64 {
526        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_PACKAGE")
527            .ok()
528            .and_then(|s| s.parse().ok())
529            .or(self.package_cache_size)
530            .unwrap_or(1000)
531    }
532
533    pub fn object_cache_size(&self) -> u64 {
534        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT")
535            .ok()
536            .and_then(|s| s.parse().ok())
537            .or(self.object_cache_size)
538            .unwrap_or_else(|| self.max_cache_size())
539    }
540
541    pub fn marker_cache_size(&self) -> u64 {
542        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MARKER")
543            .ok()
544            .and_then(|s| s.parse().ok())
545            .or(self.marker_cache_size)
546            .unwrap_or_else(|| self.object_cache_size())
547    }
548
549    pub fn object_by_id_cache_size(&self) -> u64 {
550        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT_BY_ID")
551            .ok()
552            .and_then(|s| s.parse().ok())
553            .or(self.object_by_id_cache_size)
554            .unwrap_or_else(|| self.object_cache_size())
555    }
556
557    pub fn transaction_cache_size(&self) -> u64 {
558        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION")
559            .ok()
560            .and_then(|s| s.parse().ok())
561            .or(self.transaction_cache_size)
562            .unwrap_or_else(|| self.max_cache_size())
563    }
564
565    pub fn executed_effect_cache_size(&self) -> u64 {
566        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EXECUTED_EFFECT")
567            .ok()
568            .and_then(|s| s.parse().ok())
569            .or(self.executed_effect_cache_size)
570            .unwrap_or_else(|| self.transaction_cache_size())
571    }
572
573    pub fn effect_cache_size(&self) -> u64 {
574        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EFFECT")
575            .ok()
576            .and_then(|s| s.parse().ok())
577            .or(self.effect_cache_size)
578            .unwrap_or_else(|| self.executed_effect_cache_size())
579    }
580
581    pub fn events_cache_size(&self) -> u64 {
582        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EVENTS")
583            .ok()
584            .and_then(|s| s.parse().ok())
585            .or(self.events_cache_size)
586            .unwrap_or_else(|| self.transaction_cache_size())
587    }
588
589    pub fn transaction_objects_cache_size(&self) -> u64 {
590        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION_OBJECTS")
591            .ok()
592            .and_then(|s| s.parse().ok())
593            .or(self.transaction_objects_cache_size)
594            .unwrap_or(1000)
595    }
596
597    pub fn backpressure_threshold(&self) -> u64 {
598        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD")
599            .ok()
600            .and_then(|s| s.parse().ok())
601            .or(self.backpressure_threshold)
602            .unwrap_or(100_000)
603    }
604
605    pub fn backpressure_threshold_for_rpc(&self) -> u64 {
606        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD_FOR_RPC")
607            .ok()
608            .and_then(|s| s.parse().ok())
609            .or(self.backpressure_threshold_for_rpc)
610            .unwrap_or(self.backpressure_threshold())
611    }
612}
613
614#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
615#[serde(rename_all = "lowercase")]
616pub enum ServerType {
617    WebSocket,
618    Http,
619    Both,
620}
621
622#[derive(Clone, Debug, Deserialize, Serialize)]
623#[serde(rename_all = "kebab-case")]
624pub struct TransactionKeyValueStoreReadConfig {
625    #[serde(default = "default_base_url")]
626    pub base_url: String,
627
628    #[serde(default = "default_cache_size")]
629    pub cache_size: u64,
630}
631
632impl Default for TransactionKeyValueStoreReadConfig {
633    fn default() -> Self {
634        Self {
635            base_url: default_base_url(),
636            cache_size: default_cache_size(),
637        }
638    }
639}
640
641fn default_base_url() -> String {
642    "".to_string()
643}
644
645fn default_cache_size() -> u64 {
646    100_000
647}
648
649fn default_jwk_fetch_interval_seconds() -> u64 {
650    3600
651}
652
653pub fn default_zklogin_oauth_providers() -> BTreeMap<Chain, BTreeSet<String>> {
654    let mut map = BTreeMap::new();
655
656    // providers that are available on devnet only.
657    let experimental_providers = BTreeSet::from([
658        "Google".to_string(),
659        "Facebook".to_string(),
660        "Twitch".to_string(),
661        "Kakao".to_string(),
662        "Apple".to_string(),
663        "Slack".to_string(),
664        "TestIssuer".to_string(),
665        "Microsoft".to_string(),
666        "KarrierOne".to_string(),
667        "Credenza3".to_string(),
668    ]);
669
670    // providers that are available for mainnet and testnet.
671    let providers = BTreeSet::from([
672        "Google".to_string(),
673        "Facebook".to_string(),
674        "Twitch".to_string(),
675        "Apple".to_string(),
676        "KarrierOne".to_string(),
677        "Credenza3".to_string(),
678    ]);
679    map.insert(Chain::Mainnet, providers.clone());
680    map.insert(Chain::Testnet, providers);
681    map.insert(Chain::Unknown, experimental_providers);
682    map
683}
684
685fn default_transaction_kv_store_config() -> TransactionKeyValueStoreReadConfig {
686    TransactionKeyValueStoreReadConfig::default()
687}
688
689fn default_authority_store_pruning_config() -> AuthorityStorePruningConfig {
690    AuthorityStorePruningConfig::default()
691}
692
693pub fn default_enable_index_processing() -> bool {
694    true
695}
696
697fn default_grpc_address() -> Multiaddr {
698    "/ip4/0.0.0.0/tcp/8080".parse().unwrap()
699}
700fn default_authority_key_pair() -> AuthorityKeyPairWithPath {
701    AuthorityKeyPairWithPath::new(get_key_pair_from_rng::<AuthorityKeyPair, _>(&mut OsRng).1)
702}
703
704fn default_key_pair() -> KeyPairWithPath {
705    KeyPairWithPath::new(
706        get_key_pair_from_rng::<AccountKeyPair, _>(&mut OsRng)
707            .1
708            .into(),
709    )
710}
711
712fn default_metrics_address() -> SocketAddr {
713    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
714}
715
716pub fn default_admin_interface_address() -> SocketAddr {
717    SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1337)
718}
719
720pub fn default_json_rpc_address() -> SocketAddr {
721    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
722}
723
724pub fn default_grpc_api_config() -> Option<GrpcApiConfig> {
725    Some(GrpcApiConfig::default())
726}
727
728pub fn default_concurrency_limit() -> Option<usize> {
729    Some(DEFAULT_GRPC_CONCURRENCY_LIMIT)
730}
731
732pub fn default_end_of_epoch_broadcast_channel_capacity() -> usize {
733    128
734}
735
736pub fn bool_true() -> bool {
737    true
738}
739
740fn is_true(value: &bool) -> bool {
741    *value
742}
743
744impl Config for NodeConfig {}
745
746impl NodeConfig {
747    pub fn authority_key_pair(&self) -> &AuthorityKeyPair {
748        self.authority_key_pair.authority_keypair()
749    }
750
751    pub fn protocol_key_pair(&self) -> &NetworkKeyPair {
752        match self.protocol_key_pair.keypair() {
753            IotaKeyPair::Ed25519(kp) => kp,
754            other => {
755                panic!("invalid keypair type: {other:?}, only Ed25519 is allowed for protocol key")
756            }
757        }
758    }
759
760    pub fn network_key_pair(&self) -> &NetworkKeyPair {
761        match self.network_key_pair.keypair() {
762            IotaKeyPair::Ed25519(kp) => kp,
763            other => {
764                panic!("invalid keypair type: {other:?}, only Ed25519 is allowed for network key")
765            }
766        }
767    }
768
769    pub fn authority_public_key(&self) -> AuthorityPublicKeyBytes {
770        self.authority_key_pair().public().into()
771    }
772
773    pub fn db_path(&self) -> PathBuf {
774        self.db_path.join("live")
775    }
776
777    pub fn db_checkpoint_path(&self) -> PathBuf {
778        self.db_path.join("db_checkpoints")
779    }
780
781    pub fn archive_path(&self) -> PathBuf {
782        self.db_path.join("archive")
783    }
784
785    pub fn snapshot_path(&self) -> PathBuf {
786        self.db_path.join("snapshot")
787    }
788
789    pub fn network_address(&self) -> &Multiaddr {
790        &self.network_address
791    }
792
793    pub fn consensus_config(&self) -> Option<&ConsensusConfig> {
794        self.consensus_config.as_ref()
795    }
796
797    pub fn genesis(&self) -> Result<&genesis::Genesis> {
798        self.genesis.genesis()
799    }
800
801    pub fn load_migration_tx_data(&self) -> Result<MigrationTxData> {
802        let Some(location) = &self.migration_tx_data_path else {
803            anyhow::bail!("no file location set");
804        };
805
806        // Load from file
807        let migration_tx_data = MigrationTxData::load(location)?;
808
809        // Validate migration content in order to avoid corrupted or malicious data
810        migration_tx_data.validate_from_genesis(self.genesis.genesis()?)?;
811        Ok(migration_tx_data)
812    }
813
814    pub fn iota_address(&self) -> IotaAddress {
815        (&self.account_key_pair.keypair().public()).into()
816    }
817
818    pub fn archive_reader_config(&self) -> Vec<ArchiveReaderConfig> {
819        self.state_archive_read_config
820            .iter()
821            .flat_map(|config| {
822                config
823                    .object_store_config
824                    .as_ref()
825                    .map(|remote_store_config| ArchiveReaderConfig {
826                        remote_store_config: remote_store_config.clone(),
827                        download_concurrency: NonZeroUsize::new(config.concurrency)
828                            .unwrap_or(NonZeroUsize::new(5).unwrap()),
829                        use_for_pruning_watermark: config.use_for_pruning_watermark,
830                    })
831            })
832            .collect()
833    }
834
835    pub fn jsonrpc_server_type(&self) -> ServerType {
836        self.jsonrpc_server_type.unwrap_or(ServerType::Http)
837    }
838}
839
840#[derive(Debug, Clone, Deserialize, Serialize)]
841pub enum ConsensusProtocol {
842    #[serde(rename = "mysticeti")]
843    Mysticeti,
844    #[serde(rename = "starfish")]
845    Starfish,
846}
847
848#[derive(Debug, Clone, Deserialize, Serialize)]
849#[serde(rename_all = "kebab-case")]
850pub struct ConsensusConfig {
851    // Base consensus DB path for all epochs.
852    pub db_path: PathBuf,
853
854    // The number of epochs for which to retain the consensus DBs. Setting it to 0 will make a
855    // consensus DB getting dropped as soon as system is switched to a new epoch.
856    pub db_retention_epochs: Option<u64>,
857
858    // Pruner will run on every epoch change but it will also check periodically on every
859    // `db_pruner_period_secs` seconds to see if there are any epoch DBs to remove.
860    pub db_pruner_period_secs: Option<u64>,
861
862    /// Maximum number of pending transactions to submit to consensus, including
863    /// those in submission wait.
864    ///
865    /// Default to 20_000 inflight limit, assuming 20_000 txn tps * 1 sec
866    /// consensus latency.
867    pub max_pending_transactions: Option<usize>,
868
869    /// When defined caps the calculated submission position to the
870    /// max_submit_position.
871    ///
872    /// Even if the is elected to submit from a higher
873    /// position than this, it will "reset" to the max_submit_position.
874    pub max_submit_position: Option<usize>,
875
876    /// The submit delay step to consensus defined in milliseconds.
877    ///
878    /// When provided it will override the current back off logic otherwise the
879    /// default backoff logic will be applied based on consensus latency
880    /// estimates.
881    pub submit_delay_step_override_millis: Option<u64>,
882
883    /// Parameters for Mysticeti consensus
884    pub parameters: Option<ConsensusParameters>,
885
886    /// Parameters for Starfish consensus
887    #[serde(skip_serializing_if = "Option::is_none")]
888    pub starfish_parameters: Option<StarfishParameters>,
889}
890
891impl ConsensusConfig {
892    pub fn db_path(&self) -> &Path {
893        &self.db_path
894    }
895
896    pub fn max_pending_transactions(&self) -> usize {
897        self.max_pending_transactions.unwrap_or(20_000)
898    }
899
900    pub fn submit_delay_step_override(&self) -> Option<Duration> {
901        self.submit_delay_step_override_millis
902            .map(Duration::from_millis)
903    }
904
905    pub fn db_retention_epochs(&self) -> u64 {
906        self.db_retention_epochs.unwrap_or(0)
907    }
908
909    pub fn db_pruner_period(&self) -> Duration {
910        // Default to 1 hour
911        self.db_pruner_period_secs
912            .map(Duration::from_secs)
913            .unwrap_or(Duration::from_secs(3_600))
914    }
915}
916
917#[derive(Clone, Debug, Deserialize, Serialize)]
918#[serde(rename_all = "kebab-case")]
919pub struct CheckpointExecutorConfig {
920    /// Upper bound on the number of checkpoints that can be concurrently
921    /// executed.
922    ///
923    /// If unspecified, this will default to `200`
924    #[serde(default = "default_checkpoint_execution_max_concurrency")]
925    pub checkpoint_execution_max_concurrency: usize,
926
927    /// Number of seconds to wait for effects of a batch of transactions
928    /// before logging a warning. Note that we will continue to retry
929    /// indefinitely.
930    ///
931    /// If unspecified, this will default to `10`.
932    #[serde(default = "default_local_execution_timeout_sec")]
933    pub local_execution_timeout_sec: u64,
934
935    /// Optional directory used for data ingestion pipeline.
936    ///
937    /// When specified, each executed checkpoint will be saved in a local
938    /// directory for post-processing
939    #[serde(default, skip_serializing_if = "Option::is_none")]
940    pub data_ingestion_dir: Option<PathBuf>,
941}
942
943#[derive(Clone, Debug, Default, Deserialize, Serialize)]
944#[serde(rename_all = "kebab-case")]
945pub struct ExpensiveSafetyCheckConfig {
946    /// If enabled, at epoch boundary, we will check that the storage
947    /// fund balance is always identical to the sum of the storage
948    /// rebate of all live objects, and that the total IOTA in the network
949    /// remains the same.
950    #[serde(default)]
951    enable_epoch_iota_conservation_check: bool,
952
953    /// If enabled, we will check that the total IOTA in all input objects of a
954    /// tx (both the Move part and the storage rebate) matches the total IOTA
955    /// in all output objects of the tx + gas fees.
956    #[serde(default)]
957    enable_deep_per_tx_iota_conservation_check: bool,
958
959    /// Disable epoch IOTA conservation check even when we are running in debug
960    /// mode.
961    #[serde(default)]
962    force_disable_epoch_iota_conservation_check: bool,
963
964    /// If enabled, at epoch boundary, we will check that the accumulated
965    /// live object state matches the end of epoch root state digest.
966    #[serde(default)]
967    enable_state_consistency_check: bool,
968
969    /// Disable state consistency check even when we are running in debug mode.
970    #[serde(default)]
971    force_disable_state_consistency_check: bool,
972
973    #[serde(default)]
974    enable_secondary_index_checks: bool,
975    // TODO: Add more expensive checks here
976}
977
978impl ExpensiveSafetyCheckConfig {
979    pub fn new_enable_all() -> Self {
980        Self {
981            enable_epoch_iota_conservation_check: true,
982            enable_deep_per_tx_iota_conservation_check: true,
983            force_disable_epoch_iota_conservation_check: false,
984            enable_state_consistency_check: true,
985            force_disable_state_consistency_check: false,
986            enable_secondary_index_checks: false, // Disable by default for now
987        }
988    }
989
990    pub fn new_disable_all() -> Self {
991        Self {
992            enable_epoch_iota_conservation_check: false,
993            enable_deep_per_tx_iota_conservation_check: false,
994            force_disable_epoch_iota_conservation_check: true,
995            enable_state_consistency_check: false,
996            force_disable_state_consistency_check: true,
997            enable_secondary_index_checks: false,
998        }
999    }
1000
1001    pub fn force_disable_epoch_iota_conservation_check(&mut self) {
1002        self.force_disable_epoch_iota_conservation_check = true;
1003    }
1004
1005    pub fn enable_epoch_iota_conservation_check(&self) -> bool {
1006        (self.enable_epoch_iota_conservation_check || cfg!(debug_assertions))
1007            && !self.force_disable_epoch_iota_conservation_check
1008    }
1009
1010    pub fn force_disable_state_consistency_check(&mut self) {
1011        self.force_disable_state_consistency_check = true;
1012    }
1013
1014    pub fn enable_state_consistency_check(&self) -> bool {
1015        (self.enable_state_consistency_check || cfg!(debug_assertions))
1016            && !self.force_disable_state_consistency_check
1017    }
1018
1019    pub fn enable_deep_per_tx_iota_conservation_check(&self) -> bool {
1020        self.enable_deep_per_tx_iota_conservation_check || cfg!(debug_assertions)
1021    }
1022
1023    pub fn enable_secondary_index_checks(&self) -> bool {
1024        self.enable_secondary_index_checks
1025    }
1026}
1027
1028fn default_checkpoint_execution_max_concurrency() -> usize {
1029    4
1030}
1031
1032fn default_local_execution_timeout_sec() -> u64 {
1033    30
1034}
1035
1036impl Default for CheckpointExecutorConfig {
1037    fn default() -> Self {
1038        Self {
1039            checkpoint_execution_max_concurrency: default_checkpoint_execution_max_concurrency(),
1040            local_execution_timeout_sec: default_local_execution_timeout_sec(),
1041            data_ingestion_dir: None,
1042        }
1043    }
1044}
1045
1046#[derive(Debug, Clone, Deserialize, Serialize)]
1047#[serde(rename_all = "kebab-case")]
1048pub struct AuthorityStorePruningConfig {
1049    /// number of the latest epoch dbs to retain
1050    #[serde(default = "default_num_latest_epoch_dbs_to_retain")]
1051    pub num_latest_epoch_dbs_to_retain: usize,
1052    /// time interval used by the pruner to determine whether there are any
1053    /// epoch DBs to remove
1054    #[serde(default = "default_epoch_db_pruning_period_secs")]
1055    pub epoch_db_pruning_period_secs: u64,
1056    /// number of epochs to keep the latest version of objects for.
1057    /// Note that a zero value corresponds to an aggressive pruner.
1058    /// This mode is experimental and needs to be used with caution.
1059    /// Use `u64::MAX` to disable the pruner for the objects.
1060    #[serde(default)]
1061    pub num_epochs_to_retain: u64,
1062    /// pruner's runtime interval used for aggressive mode
1063    #[serde(skip_serializing_if = "Option::is_none")]
1064    pub pruning_run_delay_seconds: Option<u64>,
1065    /// maximum number of checkpoints in the pruning batch. Can be adjusted to
1066    /// increase performance
1067    #[serde(default = "default_max_checkpoints_in_batch")]
1068    pub max_checkpoints_in_batch: usize,
1069    /// maximum number of transaction in the pruning batch
1070    #[serde(default = "default_max_transactions_in_batch")]
1071    pub max_transactions_in_batch: usize,
1072    /// enables periodic background compaction for old SST files whose last
1073    /// modified time is older than `periodic_compaction_threshold_days`
1074    /// days. That ensures that all sst files eventually go through the
1075    /// compaction process
1076    #[serde(
1077        default = "default_periodic_compaction_threshold_days",
1078        skip_serializing_if = "Option::is_none"
1079    )]
1080    pub periodic_compaction_threshold_days: Option<usize>,
1081    /// number of epochs to keep the latest version of transactions and effects
1082    /// for
1083    #[serde(skip_serializing_if = "Option::is_none")]
1084    pub num_epochs_to_retain_for_checkpoints: Option<u64>,
1085    #[serde(default = "default_smoothing", skip_serializing_if = "is_true")]
1086    pub smooth: bool,
1087    /// Enables the compaction filter for pruning the objects table.
1088    /// If disabled, a range deletion approach is used instead.
1089    /// While it is generally safe to switch between the two modes,
1090    /// switching from the compaction filter approach back to range deletion
1091    /// may result in some old versions that will never be pruned.
1092    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1093    pub enable_compaction_filter: bool,
1094    #[serde(skip_serializing_if = "Option::is_none")]
1095    pub num_epochs_to_retain_for_indexes: Option<u64>,
1096}
1097
1098fn default_num_latest_epoch_dbs_to_retain() -> usize {
1099    3
1100}
1101
1102fn default_epoch_db_pruning_period_secs() -> u64 {
1103    3600
1104}
1105
1106fn default_max_transactions_in_batch() -> usize {
1107    1000
1108}
1109
1110fn default_max_checkpoints_in_batch() -> usize {
1111    10
1112}
1113
1114fn default_smoothing() -> bool {
1115    cfg!(not(test))
1116}
1117
1118fn default_periodic_compaction_threshold_days() -> Option<usize> {
1119    Some(1)
1120}
1121
1122impl Default for AuthorityStorePruningConfig {
1123    fn default() -> Self {
1124        Self {
1125            num_latest_epoch_dbs_to_retain: default_num_latest_epoch_dbs_to_retain(),
1126            epoch_db_pruning_period_secs: default_epoch_db_pruning_period_secs(),
1127            num_epochs_to_retain: 0,
1128            pruning_run_delay_seconds: if cfg!(msim) { Some(2) } else { None },
1129            max_checkpoints_in_batch: default_max_checkpoints_in_batch(),
1130            max_transactions_in_batch: default_max_transactions_in_batch(),
1131            periodic_compaction_threshold_days: None,
1132            num_epochs_to_retain_for_checkpoints: if cfg!(msim) { Some(2) } else { None },
1133            smooth: true,
1134            enable_compaction_filter: cfg!(test) || cfg!(msim),
1135            num_epochs_to_retain_for_indexes: None,
1136        }
1137    }
1138}
1139
1140impl AuthorityStorePruningConfig {
1141    pub fn set_num_epochs_to_retain(&mut self, num_epochs_to_retain: u64) {
1142        self.num_epochs_to_retain = num_epochs_to_retain;
1143    }
1144
1145    pub fn set_num_epochs_to_retain_for_checkpoints(&mut self, num_epochs_to_retain: Option<u64>) {
1146        self.num_epochs_to_retain_for_checkpoints = num_epochs_to_retain;
1147    }
1148
1149    pub fn num_epochs_to_retain_for_checkpoints(&self) -> Option<u64> {
1150        self.num_epochs_to_retain_for_checkpoints
1151            // if n less than 2, coerce to 2 and log
1152            .map(|n| {
1153                if n < 2 {
1154                    info!("num_epochs_to_retain_for_checkpoints must be at least 2, rounding up from {}", n);
1155                    2
1156                } else {
1157                    n
1158                }
1159            })
1160    }
1161}
1162
1163#[derive(Debug, Clone, Deserialize, Serialize)]
1164#[serde(rename_all = "kebab-case")]
1165pub struct MetricsConfig {
1166    #[serde(skip_serializing_if = "Option::is_none")]
1167    pub push_interval_seconds: Option<u64>,
1168    #[serde(skip_serializing_if = "Option::is_none")]
1169    pub push_url: Option<String>,
1170}
1171
1172#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1173#[serde(rename_all = "kebab-case")]
1174pub struct DBCheckpointConfig {
1175    #[serde(default)]
1176    pub perform_db_checkpoints_at_epoch_end: bool,
1177    #[serde(skip_serializing_if = "Option::is_none")]
1178    pub checkpoint_path: Option<PathBuf>,
1179    #[serde(skip_serializing_if = "Option::is_none")]
1180    pub object_store_config: Option<ObjectStoreConfig>,
1181    #[serde(skip_serializing_if = "Option::is_none")]
1182    pub perform_index_db_checkpoints_at_epoch_end: Option<bool>,
1183    #[serde(skip_serializing_if = "Option::is_none")]
1184    pub prune_and_compact_before_upload: Option<bool>,
1185}
1186
1187#[derive(Debug, Clone)]
1188pub struct ArchiveReaderConfig {
1189    pub remote_store_config: ObjectStoreConfig,
1190    pub download_concurrency: NonZeroUsize,
1191    pub use_for_pruning_watermark: bool,
1192}
1193
1194#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1195#[serde(rename_all = "kebab-case")]
1196pub struct StateArchiveConfig {
1197    #[serde(skip_serializing_if = "Option::is_none")]
1198    pub object_store_config: Option<ObjectStoreConfig>,
1199    pub concurrency: usize,
1200    pub use_for_pruning_watermark: bool,
1201}
1202
1203#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1204#[serde(rename_all = "kebab-case")]
1205pub struct StateSnapshotConfig {
1206    #[serde(skip_serializing_if = "Option::is_none")]
1207    pub object_store_config: Option<ObjectStoreConfig>,
1208    pub concurrency: usize,
1209}
1210
1211#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1212#[serde(rename_all = "kebab-case")]
1213pub struct TransactionKeyValueStoreWriteConfig {
1214    pub aws_access_key_id: String,
1215    pub aws_secret_access_key: String,
1216    pub aws_region: String,
1217    pub table_name: String,
1218    pub bucket_name: String,
1219    pub concurrency: usize,
1220}
1221
1222/// Configuration for the threshold(s) at which we consider the system
1223/// to be overloaded. When one of the threshold is passed, the node may
1224/// stop processing new transactions and/or certificates until the congestion
1225/// resolves.
1226#[derive(Clone, Debug, Deserialize, Serialize)]
1227#[serde(rename_all = "kebab-case")]
1228pub struct AuthorityOverloadConfig {
1229    #[serde(default = "default_max_txn_age_in_queue")]
1230    pub max_txn_age_in_queue: Duration,
1231
1232    // The interval of checking overload signal.
1233    #[serde(default = "default_overload_monitor_interval")]
1234    pub overload_monitor_interval: Duration,
1235
1236    // The execution queueing latency when entering load shedding mode.
1237    #[serde(default = "default_execution_queue_latency_soft_limit")]
1238    pub execution_queue_latency_soft_limit: Duration,
1239
1240    // The execution queueing latency when entering aggressive load shedding mode.
1241    #[serde(default = "default_execution_queue_latency_hard_limit")]
1242    pub execution_queue_latency_hard_limit: Duration,
1243
1244    // The maximum percentage of transactions to shed in load shedding mode.
1245    #[serde(default = "default_max_load_shedding_percentage")]
1246    pub max_load_shedding_percentage: u32,
1247
1248    // When in aggressive load shedding mode, the minimum percentage of
1249    // transactions to shed.
1250    #[serde(default = "default_min_load_shedding_percentage_above_hard_limit")]
1251    pub min_load_shedding_percentage_above_hard_limit: u32,
1252
1253    // If transaction ready rate is below this rate, we consider the validator
1254    // is well under used, and will not enter load shedding mode.
1255    #[serde(default = "default_safe_transaction_ready_rate")]
1256    pub safe_transaction_ready_rate: u32,
1257
1258    // When set to true, transaction signing may be rejected when the validator
1259    // is overloaded.
1260    #[serde(default = "default_check_system_overload_at_signing")]
1261    pub check_system_overload_at_signing: bool,
1262
1263    // When set to true, transaction execution may be rejected when the validator
1264    // is overloaded.
1265    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1266    pub check_system_overload_at_execution: bool,
1267
1268    // Reject a transaction if transaction manager queue length is above this threshold.
1269    // 100_000 = 10k TPS * 5s resident time in transaction manager (pending + executing) * 2.
1270    #[serde(default = "default_max_transaction_manager_queue_length")]
1271    pub max_transaction_manager_queue_length: usize,
1272
1273    // Reject a transaction if the number of pending transactions depending on the object
1274    // is above the threshold.
1275    #[serde(default = "default_max_transaction_manager_per_object_queue_length")]
1276    pub max_transaction_manager_per_object_queue_length: usize,
1277}
1278
1279fn default_max_txn_age_in_queue() -> Duration {
1280    Duration::from_millis(500)
1281}
1282
1283fn default_overload_monitor_interval() -> Duration {
1284    Duration::from_secs(10)
1285}
1286
1287fn default_execution_queue_latency_soft_limit() -> Duration {
1288    Duration::from_secs(1)
1289}
1290
1291fn default_execution_queue_latency_hard_limit() -> Duration {
1292    Duration::from_secs(10)
1293}
1294
1295fn default_max_load_shedding_percentage() -> u32 {
1296    95
1297}
1298
1299fn default_min_load_shedding_percentage_above_hard_limit() -> u32 {
1300    50
1301}
1302
1303fn default_safe_transaction_ready_rate() -> u32 {
1304    100
1305}
1306
1307fn default_check_system_overload_at_signing() -> bool {
1308    true
1309}
1310
1311fn default_max_transaction_manager_queue_length() -> usize {
1312    100_000
1313}
1314
1315fn default_max_transaction_manager_per_object_queue_length() -> usize {
1316    20
1317}
1318
1319impl Default for AuthorityOverloadConfig {
1320    fn default() -> Self {
1321        Self {
1322            max_txn_age_in_queue: default_max_txn_age_in_queue(),
1323            overload_monitor_interval: default_overload_monitor_interval(),
1324            execution_queue_latency_soft_limit: default_execution_queue_latency_soft_limit(),
1325            execution_queue_latency_hard_limit: default_execution_queue_latency_hard_limit(),
1326            max_load_shedding_percentage: default_max_load_shedding_percentage(),
1327            min_load_shedding_percentage_above_hard_limit:
1328                default_min_load_shedding_percentage_above_hard_limit(),
1329            safe_transaction_ready_rate: default_safe_transaction_ready_rate(),
1330            check_system_overload_at_signing: true,
1331            check_system_overload_at_execution: false,
1332            max_transaction_manager_queue_length: default_max_transaction_manager_queue_length(),
1333            max_transaction_manager_per_object_queue_length:
1334                default_max_transaction_manager_per_object_queue_length(),
1335        }
1336    }
1337}
1338
1339fn default_authority_overload_config() -> AuthorityOverloadConfig {
1340    AuthorityOverloadConfig::default()
1341}
1342
1343#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1344pub struct Genesis {
1345    #[serde(flatten)]
1346    location: Option<GenesisLocation>,
1347
1348    #[serde(skip)]
1349    genesis: once_cell::sync::OnceCell<genesis::Genesis>,
1350}
1351
1352impl Genesis {
1353    pub fn new(genesis: genesis::Genesis) -> Self {
1354        Self {
1355            location: Some(GenesisLocation::InPlace {
1356                genesis: Box::new(genesis),
1357            }),
1358            genesis: Default::default(),
1359        }
1360    }
1361
1362    pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
1363        Self {
1364            location: Some(GenesisLocation::File {
1365                genesis_file_location: path.into(),
1366            }),
1367            genesis: Default::default(),
1368        }
1369    }
1370
1371    pub fn new_empty() -> Self {
1372        Self {
1373            location: None,
1374            genesis: Default::default(),
1375        }
1376    }
1377
1378    pub fn genesis(&self) -> Result<&genesis::Genesis> {
1379        match &self.location {
1380            Some(GenesisLocation::InPlace { genesis }) => Ok(genesis),
1381            Some(GenesisLocation::File {
1382                genesis_file_location,
1383            }) => self
1384                .genesis
1385                .get_or_try_init(|| genesis::Genesis::load(genesis_file_location)),
1386            None => anyhow::bail!("no genesis location set"),
1387        }
1388    }
1389}
1390
1391#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1392#[serde(untagged)]
1393enum GenesisLocation {
1394    InPlace {
1395        genesis: Box<genesis::Genesis>,
1396    },
1397    File {
1398        #[serde(rename = "genesis-file-location")]
1399        genesis_file_location: PathBuf,
1400    },
1401}
1402
1403/// Wrapper struct for IotaKeyPair that can be deserialized from a file path.
1404/// Used by network, worker, and account keypair.
1405#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1406pub struct KeyPairWithPath {
1407    #[serde(flatten)]
1408    location: KeyPairLocation,
1409
1410    #[serde(skip)]
1411    keypair: OnceCell<Arc<IotaKeyPair>>,
1412}
1413
1414#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1415#[serde(untagged)]
1416enum KeyPairLocation {
1417    InPlace {
1418        #[serde(with = "bech32_formatted_keypair")]
1419        value: Arc<IotaKeyPair>,
1420    },
1421    File {
1422        path: PathBuf,
1423    },
1424}
1425
1426impl KeyPairWithPath {
1427    pub fn new(kp: IotaKeyPair) -> Self {
1428        let cell: OnceCell<Arc<IotaKeyPair>> = OnceCell::new();
1429        let arc_kp = Arc::new(kp);
1430        // OK to unwrap panic because authority should not start without all keypairs
1431        // loaded.
1432        cell.set(arc_kp.clone()).expect("failed to set keypair");
1433        Self {
1434            location: KeyPairLocation::InPlace { value: arc_kp },
1435            keypair: cell,
1436        }
1437    }
1438
1439    pub fn new_from_path(path: PathBuf) -> Self {
1440        let cell: OnceCell<Arc<IotaKeyPair>> = OnceCell::new();
1441        // OK to unwrap panic because authority should not start without all keypairs
1442        // loaded.
1443        cell.set(Arc::new(read_keypair_from_file(&path).unwrap_or_else(
1444            |e| panic!("invalid keypair file at path {:?}: {e}", &path),
1445        )))
1446        .expect("failed to set keypair");
1447        Self {
1448            location: KeyPairLocation::File { path },
1449            keypair: cell,
1450        }
1451    }
1452
1453    pub fn keypair(&self) -> &IotaKeyPair {
1454        self.keypair
1455            .get_or_init(|| match &self.location {
1456                KeyPairLocation::InPlace { value } => value.clone(),
1457                KeyPairLocation::File { path } => {
1458                    // OK to unwrap panic because authority should not start without all keypairs
1459                    // loaded.
1460                    Arc::new(
1461                        read_keypair_from_file(path).unwrap_or_else(|e| {
1462                            panic!("invalid keypair file at path {path:?}: {e}")
1463                        }),
1464                    )
1465                }
1466            })
1467            .as_ref()
1468    }
1469}
1470
1471/// Wrapper struct for AuthorityKeyPair that can be deserialized from a file
1472/// path.
1473#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1474pub struct AuthorityKeyPairWithPath {
1475    #[serde(flatten)]
1476    location: AuthorityKeyPairLocation,
1477
1478    #[serde(skip)]
1479    keypair: OnceCell<Arc<AuthorityKeyPair>>,
1480}
1481
1482#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1483#[serde(untagged)]
1484enum AuthorityKeyPairLocation {
1485    InPlace { value: Arc<AuthorityKeyPair> },
1486    File { path: PathBuf },
1487}
1488
1489impl AuthorityKeyPairWithPath {
1490    pub fn new(kp: AuthorityKeyPair) -> Self {
1491        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1492        let arc_kp = Arc::new(kp);
1493        // OK to unwrap panic because authority should not start without all keypairs
1494        // loaded.
1495        cell.set(arc_kp.clone())
1496            .expect("failed to set authority keypair");
1497        Self {
1498            location: AuthorityKeyPairLocation::InPlace { value: arc_kp },
1499            keypair: cell,
1500        }
1501    }
1502
1503    pub fn new_from_path(path: PathBuf) -> Self {
1504        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1505        // OK to unwrap panic because authority should not start without all keypairs
1506        // loaded.
1507        cell.set(Arc::new(
1508            read_authority_keypair_from_file(&path)
1509                .unwrap_or_else(|_| panic!("invalid authority keypair file at path {:?}", &path)),
1510        ))
1511        .expect("failed to set authority keypair");
1512        Self {
1513            location: AuthorityKeyPairLocation::File { path },
1514            keypair: cell,
1515        }
1516    }
1517
1518    pub fn authority_keypair(&self) -> &AuthorityKeyPair {
1519        self.keypair
1520            .get_or_init(|| match &self.location {
1521                AuthorityKeyPairLocation::InPlace { value } => value.clone(),
1522                AuthorityKeyPairLocation::File { path } => {
1523                    // OK to unwrap panic because authority should not start without all keypairs
1524                    // loaded.
1525                    Arc::new(
1526                        read_authority_keypair_from_file(path).unwrap_or_else(|_| {
1527                            panic!("invalid authority keypair file {:?}", &path)
1528                        }),
1529                    )
1530                }
1531            })
1532            .as_ref()
1533    }
1534}
1535
1536/// Configurations which determine how we dump state debug info.
1537/// Debug info is dumped when a node forks.
1538#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1539#[serde(rename_all = "kebab-case")]
1540pub struct StateDebugDumpConfig {
1541    #[serde(skip_serializing_if = "Option::is_none")]
1542    pub dump_file_directory: Option<PathBuf>,
1543}
1544
1545#[cfg(test)]
1546mod tests {
1547    use std::path::PathBuf;
1548
1549    use fastcrypto::traits::KeyPair;
1550    use iota_keys::keypair_file::{write_authority_keypair_to_file, write_keypair_to_file};
1551    use iota_types::crypto::{
1552        AuthorityKeyPair, IotaKeyPair, NetworkKeyPair, get_key_pair_from_rng,
1553    };
1554    use rand::{SeedableRng, rngs::StdRng};
1555
1556    use super::Genesis;
1557    use crate::NodeConfig;
1558
1559    #[test]
1560    fn serialize_genesis_from_file() {
1561        let g = Genesis::new_from_file("path/to/file");
1562
1563        let s = serde_yaml::to_string(&g).unwrap();
1564        assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
1565        let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
1566        assert_eq!(g, loaded_genesis);
1567    }
1568
1569    #[test]
1570    fn fullnode_template() {
1571        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1572
1573        let _template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1574    }
1575
1576    #[test]
1577    fn load_key_pairs_to_node_config() {
1578        let authority_key_pair: AuthorityKeyPair =
1579            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1580        let protocol_key_pair: NetworkKeyPair =
1581            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1582        let network_key_pair: NetworkKeyPair =
1583            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1584
1585        write_authority_keypair_to_file(&authority_key_pair, PathBuf::from("authority.key"))
1586            .unwrap();
1587        write_keypair_to_file(
1588            &IotaKeyPair::Ed25519(protocol_key_pair.copy()),
1589            PathBuf::from("protocol.key"),
1590        )
1591        .unwrap();
1592        write_keypair_to_file(
1593            &IotaKeyPair::Ed25519(network_key_pair.copy()),
1594            PathBuf::from("network.key"),
1595        )
1596        .unwrap();
1597
1598        const TEMPLATE: &str = include_str!("../data/fullnode-template-with-path.yaml");
1599        let template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1600        assert_eq!(
1601            template.authority_key_pair().public(),
1602            authority_key_pair.public()
1603        );
1604        assert_eq!(
1605            template.network_key_pair().public(),
1606            network_key_pair.public()
1607        );
1608        assert_eq!(
1609            template.protocol_key_pair().public(),
1610            protocol_key_pair.public()
1611        );
1612    }
1613}
1614
1615// RunWithRange is used to specify the ending epoch/checkpoint to process.
1616// this is intended for use with disaster recovery debugging and verification
1617// workflows, never in normal operations
1618#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
1619pub enum RunWithRange {
1620    Epoch(EpochId),
1621    Checkpoint(CheckpointSequenceNumber),
1622}
1623
1624impl RunWithRange {
1625    // is epoch_id > RunWithRange::Epoch
1626    pub fn is_epoch_gt(&self, epoch_id: EpochId) -> bool {
1627        matches!(self, RunWithRange::Epoch(e) if epoch_id > *e)
1628    }
1629
1630    pub fn matches_checkpoint(&self, seq_num: CheckpointSequenceNumber) -> bool {
1631        matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
1632    }
1633
1634    pub fn into_checkpoint_bound(self) -> Option<CheckpointSequenceNumber> {
1635        match self {
1636            RunWithRange::Epoch(_) => None,
1637            RunWithRange::Checkpoint(seq) => Some(seq),
1638        }
1639    }
1640}
1641
1642/// A serde helper module used with #[serde(with = "...")] to change the
1643/// de/serialization format of an `IotaKeyPair` to Bech32 when written to or
1644/// read from a node config.
1645mod bech32_formatted_keypair {
1646    use std::ops::Deref;
1647
1648    use iota_types::crypto::{EncodeDecodeBase64, IotaKeyPair};
1649    use serde::{Deserialize, Deserializer, Serializer};
1650
1651    pub fn serialize<S, T>(kp: &T, serializer: S) -> Result<S::Ok, S::Error>
1652    where
1653        S: Serializer,
1654        T: Deref<Target = IotaKeyPair>,
1655    {
1656        use serde::ser::Error;
1657
1658        // Serialize the keypair to a Bech32 string
1659        let s = kp.encode().map_err(Error::custom)?;
1660
1661        serializer.serialize_str(&s)
1662    }
1663
1664    pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
1665    where
1666        D: Deserializer<'de>,
1667        T: From<IotaKeyPair>,
1668    {
1669        use serde::de::Error;
1670
1671        let s = String::deserialize(deserializer)?;
1672
1673        // Try to deserialize the keypair from a Bech32 formatted string
1674        IotaKeyPair::decode(&s)
1675            .or_else(|_| {
1676                // For backwards compatibility try Base64 if Bech32 failed
1677                IotaKeyPair::decode_base64(&s)
1678            })
1679            .map(Into::into)
1680            .map_err(Error::custom)
1681    }
1682}