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