Skip to main content

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