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
324fn default_grpc_api_address() -> SocketAddr {
325    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 50051)
326}
327
328fn default_grpc_api_broadcast_buffer_size() -> u32 {
329    100
330}
331
332fn default_grpc_api_max_message_size_bytes() -> u32 {
333    128 * 1024 * 1024 // 128MB
334}
335
336fn default_grpc_api_max_json_move_value_size() -> usize {
337    1024 * 1024 // 1 MB
338}
339
340impl Default for GrpcApiConfig {
341    fn default() -> Self {
342        Self {
343            address: default_grpc_api_address(),
344            tls: None,
345            max_message_size_bytes: default_grpc_api_max_message_size_bytes(),
346            broadcast_buffer_size: default_grpc_api_broadcast_buffer_size(),
347            max_json_move_value_size: default_grpc_api_max_json_move_value_size(),
348        }
349    }
350}
351
352impl GrpcApiConfig {
353    // The default maximum uncompressed size in bytes for a message, based on
354    // tonic's default.
355    const GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE: u32 = 4 * 1024 * 1024; // 4MB
356    const GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES: u32 =
357        Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE;
358
359    pub fn tls_config(&self) -> Option<&TlsConfig> {
360        self.tls.as_ref()
361    }
362
363    pub fn max_message_size_bytes(&self) -> u32 {
364        // Ensure max message size is at least the minimum allowed
365        self.max_message_size_bytes
366            .max(Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES)
367    }
368
369    /// Calculate the maximum size for a message that can be
370    /// sent to a client, taking into account the client's max message size
371    /// preference.
372    pub fn max_message_size_client_bytes(&self, client_max_message_size_bytes: Option<u32>) -> u32 {
373        client_max_message_size_bytes
374            // if the client did not specify a max message size, use the tonic default receive
375            // message size
376            .unwrap_or(Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE)
377            // clamp the value between the tonic default and the service max message size
378            .clamp(
379                Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES,
380                self.max_message_size_bytes(),
381            )
382    }
383}
384
385#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
386#[serde(rename_all = "kebab-case")]
387pub enum ExecutionCacheType {
388    #[default]
389    WritebackCache,
390    PassthroughCache,
391}
392
393impl From<ExecutionCacheType> for u8 {
394    fn from(cache_type: ExecutionCacheType) -> Self {
395        match cache_type {
396            ExecutionCacheType::WritebackCache => 0,
397            ExecutionCacheType::PassthroughCache => 1,
398        }
399    }
400}
401
402impl From<&u8> for ExecutionCacheType {
403    fn from(cache_type: &u8) -> Self {
404        match cache_type {
405            0 => ExecutionCacheType::WritebackCache,
406            1 => ExecutionCacheType::PassthroughCache,
407            _ => unreachable!("Invalid value for ExecutionCacheType"),
408        }
409    }
410}
411
412/// Type alias for atomic representation of ExecutionCacheType for lock-free
413/// operations
414pub type ExecutionCacheTypeAtomicU8 = std::sync::atomic::AtomicU8;
415
416impl From<ExecutionCacheType> for ExecutionCacheTypeAtomicU8 {
417    fn from(cache_type: ExecutionCacheType) -> Self {
418        ExecutionCacheTypeAtomicU8::new(u8::from(cache_type))
419    }
420}
421
422impl ExecutionCacheType {
423    pub fn cache_type(self) -> Self {
424        if std::env::var("DISABLE_WRITEBACK_CACHE").is_ok() {
425            Self::PassthroughCache
426        } else {
427            self
428        }
429    }
430}
431
432#[derive(Clone, Debug, Default, Deserialize, Serialize)]
433#[serde(rename_all = "kebab-case")]
434pub struct ExecutionCacheConfig {
435    #[serde(default)]
436    pub writeback_cache: WritebackCacheConfig,
437}
438
439#[derive(Clone, Debug, Default, Deserialize, Serialize)]
440#[serde(rename_all = "kebab-case")]
441pub struct WritebackCacheConfig {
442    /// Maximum number of entries in each cache. (There are several
443    /// different caches).
444    #[serde(default, skip_serializing_if = "Option::is_none")]
445    pub max_cache_size: Option<u64>, // defaults to 100000
446
447    #[serde(default, skip_serializing_if = "Option::is_none")]
448    pub package_cache_size: Option<u64>, // defaults to 1000
449
450    #[serde(default, skip_serializing_if = "Option::is_none")]
451    pub object_cache_size: Option<u64>, // defaults to max_cache_size
452    #[serde(default, skip_serializing_if = "Option::is_none")]
453    pub marker_cache_size: Option<u64>, // defaults to object_cache_size
454    #[serde(default, skip_serializing_if = "Option::is_none")]
455    pub object_by_id_cache_size: Option<u64>, // defaults to object_cache_size
456
457    #[serde(default, skip_serializing_if = "Option::is_none")]
458    pub transaction_cache_size: Option<u64>, // defaults to max_cache_size
459    #[serde(default, skip_serializing_if = "Option::is_none")]
460    pub executed_effect_cache_size: Option<u64>, // defaults to transaction_cache_size
461    #[serde(default, skip_serializing_if = "Option::is_none")]
462    pub effect_cache_size: Option<u64>, // defaults to executed_effect_cache_size
463
464    #[serde(default, skip_serializing_if = "Option::is_none")]
465    pub events_cache_size: Option<u64>, // defaults to transaction_cache_size
466
467    #[serde(default, skip_serializing_if = "Option::is_none")]
468    pub transaction_objects_cache_size: Option<u64>, // defaults to 1000
469
470    /// Number of uncommitted transactions at which to pause consensus
471    /// handler.
472    #[serde(default, skip_serializing_if = "Option::is_none")]
473    pub backpressure_threshold: Option<u64>, // defaults to 100_000
474
475    /// Number of uncommitted transactions at which to refuse new
476    /// transaction submissions. Defaults to backpressure_threshold
477    /// if unset.
478    #[serde(default, skip_serializing_if = "Option::is_none")]
479    pub backpressure_threshold_for_rpc: Option<u64>, // defaults to backpressure_threshold
480}
481
482impl WritebackCacheConfig {
483    pub fn max_cache_size(&self) -> u64 {
484        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MAX")
485            .ok()
486            .and_then(|s| s.parse().ok())
487            .or(self.max_cache_size)
488            .unwrap_or(100000)
489    }
490
491    pub fn package_cache_size(&self) -> u64 {
492        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_PACKAGE")
493            .ok()
494            .and_then(|s| s.parse().ok())
495            .or(self.package_cache_size)
496            .unwrap_or(1000)
497    }
498
499    pub fn object_cache_size(&self) -> u64 {
500        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT")
501            .ok()
502            .and_then(|s| s.parse().ok())
503            .or(self.object_cache_size)
504            .unwrap_or_else(|| self.max_cache_size())
505    }
506
507    pub fn marker_cache_size(&self) -> u64 {
508        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MARKER")
509            .ok()
510            .and_then(|s| s.parse().ok())
511            .or(self.marker_cache_size)
512            .unwrap_or_else(|| self.object_cache_size())
513    }
514
515    pub fn object_by_id_cache_size(&self) -> u64 {
516        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT_BY_ID")
517            .ok()
518            .and_then(|s| s.parse().ok())
519            .or(self.object_by_id_cache_size)
520            .unwrap_or_else(|| self.object_cache_size())
521    }
522
523    pub fn transaction_cache_size(&self) -> u64 {
524        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION")
525            .ok()
526            .and_then(|s| s.parse().ok())
527            .or(self.transaction_cache_size)
528            .unwrap_or_else(|| self.max_cache_size())
529    }
530
531    pub fn executed_effect_cache_size(&self) -> u64 {
532        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EXECUTED_EFFECT")
533            .ok()
534            .and_then(|s| s.parse().ok())
535            .or(self.executed_effect_cache_size)
536            .unwrap_or_else(|| self.transaction_cache_size())
537    }
538
539    pub fn effect_cache_size(&self) -> u64 {
540        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EFFECT")
541            .ok()
542            .and_then(|s| s.parse().ok())
543            .or(self.effect_cache_size)
544            .unwrap_or_else(|| self.executed_effect_cache_size())
545    }
546
547    pub fn events_cache_size(&self) -> u64 {
548        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EVENTS")
549            .ok()
550            .and_then(|s| s.parse().ok())
551            .or(self.events_cache_size)
552            .unwrap_or_else(|| self.transaction_cache_size())
553    }
554
555    pub fn transaction_objects_cache_size(&self) -> u64 {
556        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION_OBJECTS")
557            .ok()
558            .and_then(|s| s.parse().ok())
559            .or(self.transaction_objects_cache_size)
560            .unwrap_or(1000)
561    }
562
563    pub fn backpressure_threshold(&self) -> u64 {
564        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD")
565            .ok()
566            .and_then(|s| s.parse().ok())
567            .or(self.backpressure_threshold)
568            .unwrap_or(100_000)
569    }
570
571    pub fn backpressure_threshold_for_rpc(&self) -> u64 {
572        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD_FOR_RPC")
573            .ok()
574            .and_then(|s| s.parse().ok())
575            .or(self.backpressure_threshold_for_rpc)
576            .unwrap_or(self.backpressure_threshold())
577    }
578}
579
580#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
581#[serde(rename_all = "lowercase")]
582pub enum ServerType {
583    WebSocket,
584    Http,
585    Both,
586}
587
588#[derive(Clone, Debug, Deserialize, Serialize)]
589#[serde(rename_all = "kebab-case")]
590pub struct TransactionKeyValueStoreReadConfig {
591    #[serde(default = "default_base_url")]
592    pub base_url: String,
593
594    #[serde(default = "default_cache_size")]
595    pub cache_size: u64,
596}
597
598impl Default for TransactionKeyValueStoreReadConfig {
599    fn default() -> Self {
600        Self {
601            base_url: default_base_url(),
602            cache_size: default_cache_size(),
603        }
604    }
605}
606
607fn default_base_url() -> String {
608    "".to_string()
609}
610
611fn default_cache_size() -> u64 {
612    100_000
613}
614
615fn default_jwk_fetch_interval_seconds() -> u64 {
616    3600
617}
618
619pub fn default_zklogin_oauth_providers() -> BTreeMap<Chain, BTreeSet<String>> {
620    let mut map = BTreeMap::new();
621
622    // providers that are available on devnet only.
623    let experimental_providers = BTreeSet::from([
624        "Google".to_string(),
625        "Facebook".to_string(),
626        "Twitch".to_string(),
627        "Kakao".to_string(),
628        "Apple".to_string(),
629        "Slack".to_string(),
630        "TestIssuer".to_string(),
631        "Microsoft".to_string(),
632        "KarrierOne".to_string(),
633        "Credenza3".to_string(),
634    ]);
635
636    // providers that are available for mainnet and testnet.
637    let providers = BTreeSet::from([
638        "Google".to_string(),
639        "Facebook".to_string(),
640        "Twitch".to_string(),
641        "Apple".to_string(),
642        "KarrierOne".to_string(),
643        "Credenza3".to_string(),
644    ]);
645    map.insert(Chain::Mainnet, providers.clone());
646    map.insert(Chain::Testnet, providers);
647    map.insert(Chain::Unknown, experimental_providers);
648    map
649}
650
651fn default_transaction_kv_store_config() -> TransactionKeyValueStoreReadConfig {
652    TransactionKeyValueStoreReadConfig::default()
653}
654
655fn default_authority_store_pruning_config() -> AuthorityStorePruningConfig {
656    AuthorityStorePruningConfig::default()
657}
658
659pub fn default_enable_index_processing() -> bool {
660    true
661}
662
663fn default_grpc_address() -> Multiaddr {
664    "/ip4/0.0.0.0/tcp/8080".parse().unwrap()
665}
666fn default_authority_key_pair() -> AuthorityKeyPairWithPath {
667    AuthorityKeyPairWithPath::new(get_key_pair_from_rng::<AuthorityKeyPair, _>(&mut OsRng).1)
668}
669
670fn default_key_pair() -> KeyPairWithPath {
671    KeyPairWithPath::new(
672        get_key_pair_from_rng::<AccountKeyPair, _>(&mut OsRng)
673            .1
674            .into(),
675    )
676}
677
678fn default_metrics_address() -> SocketAddr {
679    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
680}
681
682pub fn default_admin_interface_address() -> SocketAddr {
683    SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1337)
684}
685
686pub fn default_json_rpc_address() -> SocketAddr {
687    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
688}
689
690pub fn default_grpc_api_config() -> Option<GrpcApiConfig> {
691    Some(GrpcApiConfig::default())
692}
693
694pub fn default_concurrency_limit() -> Option<usize> {
695    Some(DEFAULT_GRPC_CONCURRENCY_LIMIT)
696}
697
698pub fn default_end_of_epoch_broadcast_channel_capacity() -> usize {
699    128
700}
701
702pub fn bool_true() -> bool {
703    true
704}
705
706fn is_true(value: &bool) -> bool {
707    *value
708}
709
710impl Config for NodeConfig {}
711
712impl NodeConfig {
713    pub fn authority_key_pair(&self) -> &AuthorityKeyPair {
714        self.authority_key_pair.authority_keypair()
715    }
716
717    pub fn protocol_key_pair(&self) -> &NetworkKeyPair {
718        match self.protocol_key_pair.keypair() {
719            IotaKeyPair::Ed25519(kp) => kp,
720            other => {
721                panic!("invalid keypair type: {other:?}, only Ed25519 is allowed for protocol key")
722            }
723        }
724    }
725
726    pub fn network_key_pair(&self) -> &NetworkKeyPair {
727        match self.network_key_pair.keypair() {
728            IotaKeyPair::Ed25519(kp) => kp,
729            other => {
730                panic!("invalid keypair type: {other:?}, only Ed25519 is allowed for network key")
731            }
732        }
733    }
734
735    pub fn authority_public_key(&self) -> AuthorityPublicKeyBytes {
736        self.authority_key_pair().public().into()
737    }
738
739    pub fn db_path(&self) -> PathBuf {
740        self.db_path.join("live")
741    }
742
743    pub fn db_checkpoint_path(&self) -> PathBuf {
744        self.db_path.join("db_checkpoints")
745    }
746
747    pub fn archive_path(&self) -> PathBuf {
748        self.db_path.join("archive")
749    }
750
751    pub fn snapshot_path(&self) -> PathBuf {
752        self.db_path.join("snapshot")
753    }
754
755    pub fn network_address(&self) -> &Multiaddr {
756        &self.network_address
757    }
758
759    pub fn consensus_config(&self) -> Option<&ConsensusConfig> {
760        self.consensus_config.as_ref()
761    }
762
763    pub fn genesis(&self) -> Result<&genesis::Genesis> {
764        self.genesis.genesis()
765    }
766
767    pub fn load_migration_tx_data(&self) -> Result<MigrationTxData> {
768        let Some(location) = &self.migration_tx_data_path else {
769            anyhow::bail!("no file location set");
770        };
771
772        // Load from file
773        let migration_tx_data = MigrationTxData::load(location)?;
774
775        // Validate migration content in order to avoid corrupted or malicious data
776        migration_tx_data.validate_from_genesis(self.genesis.genesis()?)?;
777        Ok(migration_tx_data)
778    }
779
780    pub fn iota_address(&self) -> IotaAddress {
781        (&self.account_key_pair.keypair().public()).into()
782    }
783
784    pub fn archive_reader_config(&self) -> Vec<ArchiveReaderConfig> {
785        self.state_archive_read_config
786            .iter()
787            .flat_map(|config| {
788                config
789                    .object_store_config
790                    .as_ref()
791                    .map(|remote_store_config| ArchiveReaderConfig {
792                        remote_store_config: remote_store_config.clone(),
793                        download_concurrency: NonZeroUsize::new(config.concurrency)
794                            .unwrap_or(NonZeroUsize::new(5).unwrap()),
795                        use_for_pruning_watermark: config.use_for_pruning_watermark,
796                    })
797            })
798            .collect()
799    }
800
801    pub fn jsonrpc_server_type(&self) -> ServerType {
802        self.jsonrpc_server_type.unwrap_or(ServerType::Http)
803    }
804}
805
806#[derive(Debug, Clone, Deserialize, Serialize)]
807pub enum ConsensusProtocol {
808    #[serde(rename = "mysticeti")]
809    Mysticeti,
810    #[serde(rename = "starfish")]
811    Starfish,
812}
813
814#[derive(Debug, Clone, Deserialize, Serialize)]
815#[serde(rename_all = "kebab-case")]
816pub struct ConsensusConfig {
817    // Base consensus DB path for all epochs.
818    pub db_path: PathBuf,
819
820    // The number of epochs for which to retain the consensus DBs. Setting it to 0 will make a
821    // consensus DB getting dropped as soon as system is switched to a new epoch.
822    pub db_retention_epochs: Option<u64>,
823
824    // Pruner will run on every epoch change but it will also check periodically on every
825    // `db_pruner_period_secs` seconds to see if there are any epoch DBs to remove.
826    pub db_pruner_period_secs: Option<u64>,
827
828    /// Maximum number of pending transactions to submit to consensus, including
829    /// those in submission wait.
830    ///
831    /// Default to 20_000 inflight limit, assuming 20_000 txn tps * 1 sec
832    /// consensus latency.
833    pub max_pending_transactions: Option<usize>,
834
835    /// When defined caps the calculated submission position to the
836    /// max_submit_position.
837    ///
838    /// Even if the is elected to submit from a higher
839    /// position than this, it will "reset" to the max_submit_position.
840    pub max_submit_position: Option<usize>,
841
842    /// The submit delay step to consensus defined in milliseconds.
843    ///
844    /// When provided it will override the current back off logic otherwise the
845    /// default backoff logic will be applied based on consensus latency
846    /// estimates.
847    pub submit_delay_step_override_millis: Option<u64>,
848
849    /// Parameters for Mysticeti consensus
850    pub parameters: Option<ConsensusParameters>,
851
852    /// Parameters for Starfish consensus
853    #[serde(skip_serializing_if = "Option::is_none")]
854    pub starfish_parameters: Option<StarfishParameters>,
855}
856
857impl ConsensusConfig {
858    pub fn db_path(&self) -> &Path {
859        &self.db_path
860    }
861
862    pub fn max_pending_transactions(&self) -> usize {
863        self.max_pending_transactions.unwrap_or(20_000)
864    }
865
866    pub fn submit_delay_step_override(&self) -> Option<Duration> {
867        self.submit_delay_step_override_millis
868            .map(Duration::from_millis)
869    }
870
871    pub fn db_retention_epochs(&self) -> u64 {
872        self.db_retention_epochs.unwrap_or(0)
873    }
874
875    pub fn db_pruner_period(&self) -> Duration {
876        // Default to 1 hour
877        self.db_pruner_period_secs
878            .map(Duration::from_secs)
879            .unwrap_or(Duration::from_secs(3_600))
880    }
881}
882
883#[derive(Clone, Debug, Deserialize, Serialize)]
884#[serde(rename_all = "kebab-case")]
885pub struct CheckpointExecutorConfig {
886    /// Upper bound on the number of checkpoints that can be concurrently
887    /// executed.
888    ///
889    /// If unspecified, this will default to `200`
890    #[serde(default = "default_checkpoint_execution_max_concurrency")]
891    pub checkpoint_execution_max_concurrency: usize,
892
893    /// Number of seconds to wait for effects of a batch of transactions
894    /// before logging a warning. Note that we will continue to retry
895    /// indefinitely.
896    ///
897    /// If unspecified, this will default to `10`.
898    #[serde(default = "default_local_execution_timeout_sec")]
899    pub local_execution_timeout_sec: u64,
900
901    /// Optional directory used for data ingestion pipeline.
902    ///
903    /// When specified, each executed checkpoint will be saved in a local
904    /// directory for post-processing
905    #[serde(default, skip_serializing_if = "Option::is_none")]
906    pub data_ingestion_dir: Option<PathBuf>,
907}
908
909#[derive(Clone, Debug, Default, Deserialize, Serialize)]
910#[serde(rename_all = "kebab-case")]
911pub struct ExpensiveSafetyCheckConfig {
912    /// If enabled, at epoch boundary, we will check that the storage
913    /// fund balance is always identical to the sum of the storage
914    /// rebate of all live objects, and that the total IOTA in the network
915    /// remains the same.
916    #[serde(default)]
917    enable_epoch_iota_conservation_check: bool,
918
919    /// If enabled, we will check that the total IOTA in all input objects of a
920    /// tx (both the Move part and the storage rebate) matches the total IOTA
921    /// in all output objects of the tx + gas fees.
922    #[serde(default)]
923    enable_deep_per_tx_iota_conservation_check: bool,
924
925    /// Disable epoch IOTA conservation check even when we are running in debug
926    /// mode.
927    #[serde(default)]
928    force_disable_epoch_iota_conservation_check: bool,
929
930    /// If enabled, at epoch boundary, we will check that the accumulated
931    /// live object state matches the end of epoch root state digest.
932    #[serde(default)]
933    enable_state_consistency_check: bool,
934
935    /// Disable state consistency check even when we are running in debug mode.
936    #[serde(default)]
937    force_disable_state_consistency_check: bool,
938
939    #[serde(default)]
940    enable_secondary_index_checks: bool,
941    // TODO: Add more expensive checks here
942}
943
944impl ExpensiveSafetyCheckConfig {
945    pub fn new_enable_all() -> Self {
946        Self {
947            enable_epoch_iota_conservation_check: true,
948            enable_deep_per_tx_iota_conservation_check: true,
949            force_disable_epoch_iota_conservation_check: false,
950            enable_state_consistency_check: true,
951            force_disable_state_consistency_check: false,
952            enable_secondary_index_checks: false, // Disable by default for now
953        }
954    }
955
956    pub fn new_disable_all() -> Self {
957        Self {
958            enable_epoch_iota_conservation_check: false,
959            enable_deep_per_tx_iota_conservation_check: false,
960            force_disable_epoch_iota_conservation_check: true,
961            enable_state_consistency_check: false,
962            force_disable_state_consistency_check: true,
963            enable_secondary_index_checks: false,
964        }
965    }
966
967    pub fn force_disable_epoch_iota_conservation_check(&mut self) {
968        self.force_disable_epoch_iota_conservation_check = true;
969    }
970
971    pub fn enable_epoch_iota_conservation_check(&self) -> bool {
972        (self.enable_epoch_iota_conservation_check || cfg!(debug_assertions))
973            && !self.force_disable_epoch_iota_conservation_check
974    }
975
976    pub fn force_disable_state_consistency_check(&mut self) {
977        self.force_disable_state_consistency_check = true;
978    }
979
980    pub fn enable_state_consistency_check(&self) -> bool {
981        (self.enable_state_consistency_check || cfg!(debug_assertions))
982            && !self.force_disable_state_consistency_check
983    }
984
985    pub fn enable_deep_per_tx_iota_conservation_check(&self) -> bool {
986        self.enable_deep_per_tx_iota_conservation_check || cfg!(debug_assertions)
987    }
988
989    pub fn enable_secondary_index_checks(&self) -> bool {
990        self.enable_secondary_index_checks
991    }
992}
993
994fn default_checkpoint_execution_max_concurrency() -> usize {
995    40
996}
997
998fn default_local_execution_timeout_sec() -> u64 {
999    30
1000}
1001
1002impl Default for CheckpointExecutorConfig {
1003    fn default() -> Self {
1004        Self {
1005            checkpoint_execution_max_concurrency: default_checkpoint_execution_max_concurrency(),
1006            local_execution_timeout_sec: default_local_execution_timeout_sec(),
1007            data_ingestion_dir: None,
1008        }
1009    }
1010}
1011
1012#[derive(Debug, Clone, Deserialize, Serialize)]
1013#[serde(rename_all = "kebab-case")]
1014pub struct AuthorityStorePruningConfig {
1015    /// number of the latest epoch dbs to retain
1016    #[serde(default = "default_num_latest_epoch_dbs_to_retain")]
1017    pub num_latest_epoch_dbs_to_retain: usize,
1018    /// time interval used by the pruner to determine whether there are any
1019    /// epoch DBs to remove
1020    #[serde(default = "default_epoch_db_pruning_period_secs")]
1021    pub epoch_db_pruning_period_secs: u64,
1022    /// number of epochs to keep the latest version of objects for.
1023    /// Note that a zero value corresponds to an aggressive pruner.
1024    /// This mode is experimental and needs to be used with caution.
1025    /// Use `u64::MAX` to disable the pruner for the objects.
1026    #[serde(default)]
1027    pub num_epochs_to_retain: u64,
1028    /// pruner's runtime interval used for aggressive mode
1029    #[serde(skip_serializing_if = "Option::is_none")]
1030    pub pruning_run_delay_seconds: Option<u64>,
1031    /// maximum number of checkpoints in the pruning batch. Can be adjusted to
1032    /// increase performance
1033    #[serde(default = "default_max_checkpoints_in_batch")]
1034    pub max_checkpoints_in_batch: usize,
1035    /// maximum number of transaction in the pruning batch
1036    #[serde(default = "default_max_transactions_in_batch")]
1037    pub max_transactions_in_batch: usize,
1038    /// enables periodic background compaction for old SST files whose last
1039    /// modified time is older than `periodic_compaction_threshold_days`
1040    /// days. That ensures that all sst files eventually go through the
1041    /// compaction process
1042    #[serde(
1043        default = "default_periodic_compaction_threshold_days",
1044        skip_serializing_if = "Option::is_none"
1045    )]
1046    pub periodic_compaction_threshold_days: Option<usize>,
1047    /// number of epochs to keep the latest version of transactions and effects
1048    /// for
1049    #[serde(skip_serializing_if = "Option::is_none")]
1050    pub num_epochs_to_retain_for_checkpoints: Option<u64>,
1051    #[serde(default = "default_smoothing", skip_serializing_if = "is_true")]
1052    pub smooth: bool,
1053    /// Enables the compaction filter for pruning the objects table.
1054    /// If disabled, a range deletion approach is used instead.
1055    /// While it is generally safe to switch between the two modes,
1056    /// switching from the compaction filter approach back to range deletion
1057    /// may result in some old versions that will never be pruned.
1058    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1059    pub enable_compaction_filter: bool,
1060    #[serde(skip_serializing_if = "Option::is_none")]
1061    pub num_epochs_to_retain_for_indexes: Option<u64>,
1062}
1063
1064fn default_num_latest_epoch_dbs_to_retain() -> usize {
1065    3
1066}
1067
1068fn default_epoch_db_pruning_period_secs() -> u64 {
1069    3600
1070}
1071
1072fn default_max_transactions_in_batch() -> usize {
1073    1000
1074}
1075
1076fn default_max_checkpoints_in_batch() -> usize {
1077    10
1078}
1079
1080fn default_smoothing() -> bool {
1081    cfg!(not(test))
1082}
1083
1084fn default_periodic_compaction_threshold_days() -> Option<usize> {
1085    Some(1)
1086}
1087
1088impl Default for AuthorityStorePruningConfig {
1089    fn default() -> Self {
1090        Self {
1091            num_latest_epoch_dbs_to_retain: default_num_latest_epoch_dbs_to_retain(),
1092            epoch_db_pruning_period_secs: default_epoch_db_pruning_period_secs(),
1093            num_epochs_to_retain: 0,
1094            pruning_run_delay_seconds: if cfg!(msim) { Some(2) } else { None },
1095            max_checkpoints_in_batch: default_max_checkpoints_in_batch(),
1096            max_transactions_in_batch: default_max_transactions_in_batch(),
1097            periodic_compaction_threshold_days: None,
1098            num_epochs_to_retain_for_checkpoints: if cfg!(msim) { Some(2) } else { None },
1099            smooth: true,
1100            enable_compaction_filter: cfg!(test) || cfg!(msim),
1101            num_epochs_to_retain_for_indexes: None,
1102        }
1103    }
1104}
1105
1106impl AuthorityStorePruningConfig {
1107    pub fn set_num_epochs_to_retain(&mut self, num_epochs_to_retain: u64) {
1108        self.num_epochs_to_retain = num_epochs_to_retain;
1109    }
1110
1111    pub fn set_num_epochs_to_retain_for_checkpoints(&mut self, num_epochs_to_retain: Option<u64>) {
1112        self.num_epochs_to_retain_for_checkpoints = num_epochs_to_retain;
1113    }
1114
1115    pub fn num_epochs_to_retain_for_checkpoints(&self) -> Option<u64> {
1116        self.num_epochs_to_retain_for_checkpoints
1117            // if n less than 2, coerce to 2 and log
1118            .map(|n| {
1119                if n < 2 {
1120                    info!("num_epochs_to_retain_for_checkpoints must be at least 2, rounding up from {}", n);
1121                    2
1122                } else {
1123                    n
1124                }
1125            })
1126    }
1127}
1128
1129#[derive(Debug, Clone, Deserialize, Serialize)]
1130#[serde(rename_all = "kebab-case")]
1131pub struct MetricsConfig {
1132    #[serde(skip_serializing_if = "Option::is_none")]
1133    pub push_interval_seconds: Option<u64>,
1134    #[serde(skip_serializing_if = "Option::is_none")]
1135    pub push_url: Option<String>,
1136}
1137
1138#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1139#[serde(rename_all = "kebab-case")]
1140pub struct DBCheckpointConfig {
1141    #[serde(default)]
1142    pub perform_db_checkpoints_at_epoch_end: bool,
1143    #[serde(skip_serializing_if = "Option::is_none")]
1144    pub checkpoint_path: Option<PathBuf>,
1145    #[serde(skip_serializing_if = "Option::is_none")]
1146    pub object_store_config: Option<ObjectStoreConfig>,
1147    #[serde(skip_serializing_if = "Option::is_none")]
1148    pub perform_index_db_checkpoints_at_epoch_end: Option<bool>,
1149    #[serde(skip_serializing_if = "Option::is_none")]
1150    pub prune_and_compact_before_upload: Option<bool>,
1151}
1152
1153#[derive(Debug, Clone)]
1154pub struct ArchiveReaderConfig {
1155    pub remote_store_config: ObjectStoreConfig,
1156    pub download_concurrency: NonZeroUsize,
1157    pub use_for_pruning_watermark: bool,
1158}
1159
1160#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1161#[serde(rename_all = "kebab-case")]
1162pub struct StateArchiveConfig {
1163    #[serde(skip_serializing_if = "Option::is_none")]
1164    pub object_store_config: Option<ObjectStoreConfig>,
1165    pub concurrency: usize,
1166    pub use_for_pruning_watermark: bool,
1167}
1168
1169#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1170#[serde(rename_all = "kebab-case")]
1171pub struct StateSnapshotConfig {
1172    #[serde(skip_serializing_if = "Option::is_none")]
1173    pub object_store_config: Option<ObjectStoreConfig>,
1174    pub concurrency: usize,
1175}
1176
1177#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1178#[serde(rename_all = "kebab-case")]
1179pub struct TransactionKeyValueStoreWriteConfig {
1180    pub aws_access_key_id: String,
1181    pub aws_secret_access_key: String,
1182    pub aws_region: String,
1183    pub table_name: String,
1184    pub bucket_name: String,
1185    pub concurrency: usize,
1186}
1187
1188/// Configuration for the threshold(s) at which we consider the system
1189/// to be overloaded. When one of the threshold is passed, the node may
1190/// stop processing new transactions and/or certificates until the congestion
1191/// resolves.
1192#[derive(Clone, Debug, Deserialize, Serialize)]
1193#[serde(rename_all = "kebab-case")]
1194pub struct AuthorityOverloadConfig {
1195    #[serde(default = "default_max_txn_age_in_queue")]
1196    pub max_txn_age_in_queue: Duration,
1197
1198    // The interval of checking overload signal.
1199    #[serde(default = "default_overload_monitor_interval")]
1200    pub overload_monitor_interval: Duration,
1201
1202    // The execution queueing latency when entering load shedding mode.
1203    #[serde(default = "default_execution_queue_latency_soft_limit")]
1204    pub execution_queue_latency_soft_limit: Duration,
1205
1206    // The execution queueing latency when entering aggressive load shedding mode.
1207    #[serde(default = "default_execution_queue_latency_hard_limit")]
1208    pub execution_queue_latency_hard_limit: Duration,
1209
1210    // The maximum percentage of transactions to shed in load shedding mode.
1211    #[serde(default = "default_max_load_shedding_percentage")]
1212    pub max_load_shedding_percentage: u32,
1213
1214    // When in aggressive load shedding mode, the minimum percentage of
1215    // transactions to shed.
1216    #[serde(default = "default_min_load_shedding_percentage_above_hard_limit")]
1217    pub min_load_shedding_percentage_above_hard_limit: u32,
1218
1219    // If transaction ready rate is below this rate, we consider the validator
1220    // is well under used, and will not enter load shedding mode.
1221    #[serde(default = "default_safe_transaction_ready_rate")]
1222    pub safe_transaction_ready_rate: u32,
1223
1224    // When set to true, transaction signing may be rejected when the validator
1225    // is overloaded.
1226    #[serde(default = "default_check_system_overload_at_signing")]
1227    pub check_system_overload_at_signing: bool,
1228
1229    // When set to true, transaction execution may be rejected when the validator
1230    // is overloaded.
1231    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1232    pub check_system_overload_at_execution: bool,
1233
1234    // Reject a transaction if transaction manager queue length is above this threshold.
1235    // 100_000 = 10k TPS * 5s resident time in transaction manager (pending + executing) * 2.
1236    #[serde(default = "default_max_transaction_manager_queue_length")]
1237    pub max_transaction_manager_queue_length: usize,
1238
1239    // Reject a transaction if the number of pending transactions depending on the object
1240    // is above the threshold.
1241    #[serde(default = "default_max_transaction_manager_per_object_queue_length")]
1242    pub max_transaction_manager_per_object_queue_length: usize,
1243}
1244
1245fn default_max_txn_age_in_queue() -> Duration {
1246    Duration::from_millis(500)
1247}
1248
1249fn default_overload_monitor_interval() -> Duration {
1250    Duration::from_secs(10)
1251}
1252
1253fn default_execution_queue_latency_soft_limit() -> Duration {
1254    Duration::from_secs(1)
1255}
1256
1257fn default_execution_queue_latency_hard_limit() -> Duration {
1258    Duration::from_secs(10)
1259}
1260
1261fn default_max_load_shedding_percentage() -> u32 {
1262    95
1263}
1264
1265fn default_min_load_shedding_percentage_above_hard_limit() -> u32 {
1266    50
1267}
1268
1269fn default_safe_transaction_ready_rate() -> u32 {
1270    100
1271}
1272
1273fn default_check_system_overload_at_signing() -> bool {
1274    true
1275}
1276
1277fn default_max_transaction_manager_queue_length() -> usize {
1278    100_000
1279}
1280
1281fn default_max_transaction_manager_per_object_queue_length() -> usize {
1282    20
1283}
1284
1285impl Default for AuthorityOverloadConfig {
1286    fn default() -> Self {
1287        Self {
1288            max_txn_age_in_queue: default_max_txn_age_in_queue(),
1289            overload_monitor_interval: default_overload_monitor_interval(),
1290            execution_queue_latency_soft_limit: default_execution_queue_latency_soft_limit(),
1291            execution_queue_latency_hard_limit: default_execution_queue_latency_hard_limit(),
1292            max_load_shedding_percentage: default_max_load_shedding_percentage(),
1293            min_load_shedding_percentage_above_hard_limit:
1294                default_min_load_shedding_percentage_above_hard_limit(),
1295            safe_transaction_ready_rate: default_safe_transaction_ready_rate(),
1296            check_system_overload_at_signing: true,
1297            check_system_overload_at_execution: false,
1298            max_transaction_manager_queue_length: default_max_transaction_manager_queue_length(),
1299            max_transaction_manager_per_object_queue_length:
1300                default_max_transaction_manager_per_object_queue_length(),
1301        }
1302    }
1303}
1304
1305fn default_authority_overload_config() -> AuthorityOverloadConfig {
1306    AuthorityOverloadConfig::default()
1307}
1308
1309#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1310pub struct Genesis {
1311    #[serde(flatten)]
1312    location: Option<GenesisLocation>,
1313
1314    #[serde(skip)]
1315    genesis: once_cell::sync::OnceCell<genesis::Genesis>,
1316}
1317
1318impl Genesis {
1319    pub fn new(genesis: genesis::Genesis) -> Self {
1320        Self {
1321            location: Some(GenesisLocation::InPlace {
1322                genesis: Box::new(genesis),
1323            }),
1324            genesis: Default::default(),
1325        }
1326    }
1327
1328    pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
1329        Self {
1330            location: Some(GenesisLocation::File {
1331                genesis_file_location: path.into(),
1332            }),
1333            genesis: Default::default(),
1334        }
1335    }
1336
1337    pub fn new_empty() -> Self {
1338        Self {
1339            location: None,
1340            genesis: Default::default(),
1341        }
1342    }
1343
1344    pub fn genesis(&self) -> Result<&genesis::Genesis> {
1345        match &self.location {
1346            Some(GenesisLocation::InPlace { genesis }) => Ok(genesis),
1347            Some(GenesisLocation::File {
1348                genesis_file_location,
1349            }) => self
1350                .genesis
1351                .get_or_try_init(|| genesis::Genesis::load(genesis_file_location)),
1352            None => anyhow::bail!("no genesis location set"),
1353        }
1354    }
1355}
1356
1357#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1358#[serde(untagged)]
1359enum GenesisLocation {
1360    InPlace {
1361        genesis: Box<genesis::Genesis>,
1362    },
1363    File {
1364        #[serde(rename = "genesis-file-location")]
1365        genesis_file_location: PathBuf,
1366    },
1367}
1368
1369/// Wrapper struct for IotaKeyPair that can be deserialized from a file path.
1370/// Used by network, worker, and account keypair.
1371#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1372pub struct KeyPairWithPath {
1373    #[serde(flatten)]
1374    location: KeyPairLocation,
1375
1376    #[serde(skip)]
1377    keypair: OnceCell<Arc<IotaKeyPair>>,
1378}
1379
1380#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1381#[serde(untagged)]
1382enum KeyPairLocation {
1383    InPlace {
1384        #[serde(with = "bech32_formatted_keypair")]
1385        value: Arc<IotaKeyPair>,
1386    },
1387    File {
1388        path: PathBuf,
1389    },
1390}
1391
1392impl KeyPairWithPath {
1393    pub fn new(kp: IotaKeyPair) -> Self {
1394        let cell: OnceCell<Arc<IotaKeyPair>> = OnceCell::new();
1395        let arc_kp = Arc::new(kp);
1396        // OK to unwrap panic because authority should not start without all keypairs
1397        // loaded.
1398        cell.set(arc_kp.clone()).expect("failed to set keypair");
1399        Self {
1400            location: KeyPairLocation::InPlace { value: arc_kp },
1401            keypair: cell,
1402        }
1403    }
1404
1405    pub fn new_from_path(path: PathBuf) -> Self {
1406        let cell: OnceCell<Arc<IotaKeyPair>> = OnceCell::new();
1407        // OK to unwrap panic because authority should not start without all keypairs
1408        // loaded.
1409        cell.set(Arc::new(read_keypair_from_file(&path).unwrap_or_else(
1410            |e| panic!("invalid keypair file at path {:?}: {e}", &path),
1411        )))
1412        .expect("failed to set keypair");
1413        Self {
1414            location: KeyPairLocation::File { path },
1415            keypair: cell,
1416        }
1417    }
1418
1419    pub fn keypair(&self) -> &IotaKeyPair {
1420        self.keypair
1421            .get_or_init(|| match &self.location {
1422                KeyPairLocation::InPlace { value } => value.clone(),
1423                KeyPairLocation::File { path } => {
1424                    // OK to unwrap panic because authority should not start without all keypairs
1425                    // loaded.
1426                    Arc::new(
1427                        read_keypair_from_file(path).unwrap_or_else(|e| {
1428                            panic!("invalid keypair file at path {path:?}: {e}")
1429                        }),
1430                    )
1431                }
1432            })
1433            .as_ref()
1434    }
1435}
1436
1437/// Wrapper struct for AuthorityKeyPair that can be deserialized from a file
1438/// path.
1439#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1440pub struct AuthorityKeyPairWithPath {
1441    #[serde(flatten)]
1442    location: AuthorityKeyPairLocation,
1443
1444    #[serde(skip)]
1445    keypair: OnceCell<Arc<AuthorityKeyPair>>,
1446}
1447
1448#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1449#[serde(untagged)]
1450enum AuthorityKeyPairLocation {
1451    InPlace { value: Arc<AuthorityKeyPair> },
1452    File { path: PathBuf },
1453}
1454
1455impl AuthorityKeyPairWithPath {
1456    pub fn new(kp: AuthorityKeyPair) -> Self {
1457        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1458        let arc_kp = Arc::new(kp);
1459        // OK to unwrap panic because authority should not start without all keypairs
1460        // loaded.
1461        cell.set(arc_kp.clone())
1462            .expect("failed to set authority keypair");
1463        Self {
1464            location: AuthorityKeyPairLocation::InPlace { value: arc_kp },
1465            keypair: cell,
1466        }
1467    }
1468
1469    pub fn new_from_path(path: PathBuf) -> Self {
1470        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1471        // OK to unwrap panic because authority should not start without all keypairs
1472        // loaded.
1473        cell.set(Arc::new(
1474            read_authority_keypair_from_file(&path)
1475                .unwrap_or_else(|_| panic!("invalid authority keypair file at path {:?}", &path)),
1476        ))
1477        .expect("failed to set authority keypair");
1478        Self {
1479            location: AuthorityKeyPairLocation::File { path },
1480            keypair: cell,
1481        }
1482    }
1483
1484    pub fn authority_keypair(&self) -> &AuthorityKeyPair {
1485        self.keypair
1486            .get_or_init(|| match &self.location {
1487                AuthorityKeyPairLocation::InPlace { value } => value.clone(),
1488                AuthorityKeyPairLocation::File { path } => {
1489                    // OK to unwrap panic because authority should not start without all keypairs
1490                    // loaded.
1491                    Arc::new(
1492                        read_authority_keypair_from_file(path).unwrap_or_else(|_| {
1493                            panic!("invalid authority keypair file {:?}", &path)
1494                        }),
1495                    )
1496                }
1497            })
1498            .as_ref()
1499    }
1500}
1501
1502/// Configurations which determine how we dump state debug info.
1503/// Debug info is dumped when a node forks.
1504#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1505#[serde(rename_all = "kebab-case")]
1506pub struct StateDebugDumpConfig {
1507    #[serde(skip_serializing_if = "Option::is_none")]
1508    pub dump_file_directory: Option<PathBuf>,
1509}
1510
1511#[cfg(test)]
1512mod tests {
1513    use std::path::PathBuf;
1514
1515    use fastcrypto::traits::KeyPair;
1516    use iota_keys::keypair_file::{write_authority_keypair_to_file, write_keypair_to_file};
1517    use iota_types::crypto::{
1518        AuthorityKeyPair, IotaKeyPair, NetworkKeyPair, get_key_pair_from_rng,
1519    };
1520    use rand::{SeedableRng, rngs::StdRng};
1521
1522    use super::Genesis;
1523    use crate::NodeConfig;
1524
1525    #[test]
1526    fn serialize_genesis_from_file() {
1527        let g = Genesis::new_from_file("path/to/file");
1528
1529        let s = serde_yaml::to_string(&g).unwrap();
1530        assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
1531        let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
1532        assert_eq!(g, loaded_genesis);
1533    }
1534
1535    #[test]
1536    fn fullnode_template() {
1537        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1538
1539        let _template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1540    }
1541
1542    #[test]
1543    fn load_key_pairs_to_node_config() {
1544        let authority_key_pair: AuthorityKeyPair =
1545            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1546        let protocol_key_pair: NetworkKeyPair =
1547            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1548        let network_key_pair: NetworkKeyPair =
1549            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1550
1551        write_authority_keypair_to_file(&authority_key_pair, PathBuf::from("authority.key"))
1552            .unwrap();
1553        write_keypair_to_file(
1554            &IotaKeyPair::Ed25519(protocol_key_pair.copy()),
1555            PathBuf::from("protocol.key"),
1556        )
1557        .unwrap();
1558        write_keypair_to_file(
1559            &IotaKeyPair::Ed25519(network_key_pair.copy()),
1560            PathBuf::from("network.key"),
1561        )
1562        .unwrap();
1563
1564        const TEMPLATE: &str = include_str!("../data/fullnode-template-with-path.yaml");
1565        let template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1566        assert_eq!(
1567            template.authority_key_pair().public(),
1568            authority_key_pair.public()
1569        );
1570        assert_eq!(
1571            template.network_key_pair().public(),
1572            network_key_pair.public()
1573        );
1574        assert_eq!(
1575            template.protocol_key_pair().public(),
1576            protocol_key_pair.public()
1577        );
1578    }
1579}
1580
1581// RunWithRange is used to specify the ending epoch/checkpoint to process.
1582// this is intended for use with disaster recovery debugging and verification
1583// workflows, never in normal operations
1584#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
1585pub enum RunWithRange {
1586    Epoch(EpochId),
1587    Checkpoint(CheckpointSequenceNumber),
1588}
1589
1590impl RunWithRange {
1591    // is epoch_id > RunWithRange::Epoch
1592    pub fn is_epoch_gt(&self, epoch_id: EpochId) -> bool {
1593        matches!(self, RunWithRange::Epoch(e) if epoch_id > *e)
1594    }
1595
1596    pub fn matches_checkpoint(&self, seq_num: CheckpointSequenceNumber) -> bool {
1597        matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
1598    }
1599
1600    pub fn into_checkpoint_bound(self) -> Option<CheckpointSequenceNumber> {
1601        match self {
1602            RunWithRange::Epoch(_) => None,
1603            RunWithRange::Checkpoint(seq) => Some(seq),
1604        }
1605    }
1606}
1607
1608/// A serde helper module used with #[serde(with = "...")] to change the
1609/// de/serialization format of an `IotaKeyPair` to Bech32 when written to or
1610/// read from a node config.
1611mod bech32_formatted_keypair {
1612    use std::ops::Deref;
1613
1614    use iota_types::crypto::{EncodeDecodeBase64, IotaKeyPair};
1615    use serde::{Deserialize, Deserializer, Serializer};
1616
1617    pub fn serialize<S, T>(kp: &T, serializer: S) -> Result<S::Ok, S::Error>
1618    where
1619        S: Serializer,
1620        T: Deref<Target = IotaKeyPair>,
1621    {
1622        use serde::ser::Error;
1623
1624        // Serialize the keypair to a Bech32 string
1625        let s = kp.encode().map_err(Error::custom)?;
1626
1627        serializer.serialize_str(&s)
1628    }
1629
1630    pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
1631    where
1632        D: Deserializer<'de>,
1633        T: From<IotaKeyPair>,
1634    {
1635        use serde::de::Error;
1636
1637        let s = String::deserialize(deserializer)?;
1638
1639        // Try to deserialize the keypair from a Bech32 formatted string
1640        IotaKeyPair::decode(&s)
1641            .or_else(|_| {
1642                // For backwards compatibility try Base64 if Bech32 failed
1643                IotaKeyPair::decode_base64(&s)
1644            })
1645            .map(Into::into)
1646            .map_err(Error::custom)
1647    }
1648}