iota_config/
node.rs

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