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::{NonZeroU32, 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_metrics::MetricGroups;
16use iota_multiaddr::Multiaddr;
17use iota_names::config::IotaNamesConfig;
18use iota_sdk_crypto::simple::SimpleKeypair;
19use iota_sdk_types::Address;
20use iota_types::{
21    committee::EpochId,
22    crypto::{
23        AccountPrivateKey, AuthorityKeyPair, AuthorityPublicKeyBytes, KeypairTraits,
24        NetworkPrivateKey, get_key_pair_from_rng,
25    },
26    messages_checkpoint::CheckpointSequenceNumber,
27    supported_protocol_versions::{Chain, SupportedProtocolVersions},
28    traffic_control::PolicyConfig,
29};
30use once_cell::sync::OnceCell;
31use rand::{rand_core::UnwrapErr, rngs::SysRng};
32use serde::{Deserialize, Serialize};
33use starfish_config::Parameters as StarfishParameters;
34
35use crate::{
36    Config, certificate_deny_config::CertificateDenyConfig, genesis,
37    migration_tx_data::MigrationTxData, object_storage_config::ObjectStoreConfig, p2p::P2pConfig,
38    setting::Setting, transaction_deny_config::TransactionDenyConfig,
39    verifier_signing_config::VerifierSigningConfig,
40};
41
42/// Default gas price of 1000 Nanos
43pub const DEFAULT_VALIDATOR_GAS_PRICE: u64 = iota_types::transaction::DEFAULT_VALIDATOR_GAS_PRICE;
44
45/// Default commission rate of 2%
46pub const DEFAULT_COMMISSION_RATE: u64 = 200;
47
48/// Default budget in MiB for the in-memory full-checkpoint-contents cache.
49pub const DEFAULT_FULL_CHECKPOINT_CONTENTS_CACHE_SIZE_MB: usize = 1024;
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    /// The address for Prometheus metrics.
79    #[serde(default = "default_metrics_address")]
80    pub metrics_address: SocketAddr,
81
82    /// The address for the admin interface that is
83    /// run in the metrics separate runtime and provides access to
84    /// admin node commands such as logging and tracing options.
85    #[serde(default = "default_admin_interface_address")]
86    pub admin_interface_address: SocketAddr,
87
88    /// Configuration struct for the consensus.
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub consensus_config: Option<ConsensusConfig>,
91
92    /// Flag to enable the JSON-RPC API. Default: `true`. When `false` a full
93    /// node serves only the `/health` endpoint on `json_rpc_address`.
94    #[serde(default = "default_enable_jsonrpc_api")]
95    pub enable_jsonrpc_api: bool,
96
97    /// Flag to enable gRPC load shedding: requests over a service's
98    /// concurrency limit are rejected immediately with `RESOURCE_EXHAUSTED`
99    /// instead of waiting for a slot.
100    #[serde(default)]
101    pub grpc_load_shed: Option<bool>,
102
103    /// Maximum number of in-flight requests per CPU core, applied to each
104    /// service of the validator gRPC server separately (`Validator`,
105    /// `ValidatorV2`, `ValidatorPeer`), so a flood of client transaction
106    /// submissions cannot crowd validator-peer RPCs out of admission slots.
107    /// The per-service limit is this value times the CPU cores of the machine
108    /// at server startup.
109    ///
110    /// A request is in flight from admission until its response has been
111    /// sent, so a `SubmitTx` or `GetTxStatus` stream counts while it streams;
112    /// a `GetTxStatus` stream waits up to 30 s for its transactions. A
113    /// response not fully sent 60 s after its headers gives up its slot and
114    /// ends with `DEADLINE_EXCEEDED`, whether or not the client is still
115    /// reading. A request over the limit waits for a slot, or is rejected if
116    /// `grpc_load_shed` is set.
117    ///
118    /// A lower value must leave room for every `GetTxStatus` stream open at
119    /// peak.
120    ///
121    /// Zero is rejected at config load: it would block every request rather
122    /// than disable the limit.
123    #[serde(default = "default_grpc_concurrency_limit_per_core")]
124    pub grpc_concurrency_limit_per_core: NonZeroUsize,
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 for enabling/disabling expensive safety checks.
167    #[serde(default)]
168    pub expensive_safety_check_config: ExpensiveSafetyCheckConfig,
169
170    /// Configuration to specify rules for denying transactions
171    /// based on `objectsIDs`, `addresses`, or enable/disable many
172    /// features such as publishing new packages or using shared objects.
173    #[serde(default)]
174    pub transaction_deny_config: TransactionDenyConfig,
175
176    /// Config used to deny execution for certificate digests
177    /// know for crashing or hanging validator nodes.
178    ///
179    /// Should be used for a fast temporary fixes and
180    /// removed once the issue is fixed.
181    #[serde(default)]
182    pub certificate_deny_config: CertificateDenyConfig,
183
184    /// Used to determine how state debug information is dumped
185    /// when a node forks.
186    #[serde(default)]
187    pub state_debug_dump_config: StateDebugDumpConfig,
188
189    #[serde(default)]
190    pub checkpoint_archive_config: Option<CheckpointArchiveConfig>,
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 = "default_transaction_kv_store_config")]
197    pub transaction_kv_store_read_config: TransactionKeyValueStoreReadConfig,
198
199    // TODO: write config seem to be unused.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub transaction_kv_store_write_config: Option<TransactionKeyValueStoreWriteConfig>,
202
203    /// Configuration for defining thresholds and settings
204    /// for managing system overload conditions in a node.
205    #[serde(default = "default_authority_overload_config")]
206    pub authority_overload_config: AuthorityOverloadConfig,
207
208    /// Specifies the ending epoch for a node for debugging purposes.
209    ///
210    ///  Ignored if set by config, can be configured only by cli arguments.
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub run_with_range: Option<RunWithRange>,
213
214    /// Traffic control policy. For killswitch use None.
215    ///
216    /// With the key absent the default denial-of-service protection policy
217    /// applies, an explicit `null` turns traffic control off, and a value
218    /// configures it.
219    #[serde(
220        skip_serializing_if = "is_default_traffic_controller_policy_config",
221        default = "default_traffic_controller_policy_config"
222    )]
223    pub policy_config: Option<PolicyConfig>,
224
225    #[serde(default)]
226    pub execution_cache_config: ExecutionCacheConfig,
227
228    /// Memory budget in MiB for the in-memory cache of full checkpoint
229    /// contents, which serves the checkpoint executor's bulk transaction
230    /// loads and checkpoint-contents requests from state-sync peers. When
231    /// the budget is exceeded, the oldest checkpoints are evicted first.
232    /// Set to 0 to disable the cache; consumers then fall back to
233    /// reconstructing contents from the transaction and effects stores.
234    ///
235    /// The budget is accounted in serialized (BCS) bytes; the resident
236    /// memory of a full cache is somewhat higher than the configured value
237    /// due to in-memory representation overhead.
238    #[serde(default = "default_full_checkpoint_contents_cache_size_mb")]
239    pub full_checkpoint_contents_cache_size_mb: usize,
240
241    #[serde(default = "bool_true")]
242    pub enable_validator_tx_finalizer: bool,
243
244    /// Enables the pre-consensus soft-locking mechanism used by the
245    /// certificate-less (pcool) transaction flow (default: enabled).
246    ///
247    /// When disabled, post-consensus validation alone resolves owned-object
248    /// conflicts. Has no effect unless the pcool flow is enabled.
249    #[serde(default = "bool_true")]
250    pub enable_soft_locking: bool,
251
252    #[serde(default)]
253    pub verifier_signing_config: VerifierSigningConfig,
254
255    /// How many threads read the inputs of a consensus commit's transactions
256    /// at once in post-consensus validation, the consensus handler's own
257    /// thread included. One reads them one after another. By default a
258    /// third of the cores, at least one and at most sixteen. Has no effect
259    /// unless the pcool flow is enabled.
260    #[serde(default, skip_serializing_if = "Option::is_none")]
261    pub post_consensus_validation_readers: Option<NonZeroUsize>,
262
263    /// If a value is set, it determines if writes to DB can stall, which can
264    /// halt the whole process. By default, write stall is enabled on
265    /// validators but not on fullnodes.
266    #[serde(skip_serializing_if = "Option::is_none")]
267    pub enable_db_write_stall: Option<bool>,
268
269    #[serde(default, skip_serializing_if = "Option::is_none")]
270    pub iota_names_config: Option<IotaNamesConfig>,
271
272    /// Flag to enable the gRPC API.
273    #[serde(default)]
274    pub enable_grpc_api: bool,
275    /// Configuration of the gRPC API, read when `enable_grpc_api` is set.
276    ///
277    /// With the key absent the default configuration applies, an explicit
278    /// `null` leaves the API unconfigured — a node with `enable-grpc-api` set
279    /// then fails to start — and a value configures it.
280    #[serde(
281        default = "default_grpc_api_config",
282        skip_serializing_if = "is_default_grpc_api_config"
283    )]
284    pub grpc_api_config: Option<GrpcApiConfig>,
285
286    /// Allow overriding the chain for testing purposes. For instance, it allows
287    /// you to create a test network that believes it is mainnet or testnet.
288    /// Attempting to override this value on production networks will result
289    /// in an error.
290    #[serde(skip_serializing_if = "Option::is_none")]
291    pub chain_override_for_testing: Option<Chain>,
292
293    /// Configuration for the validator client monitor that tracks
294    /// client-observed performance metrics for validators.
295    #[serde(default, skip_serializing_if = "Option::is_none")]
296    pub validator_client_monitor_config:
297        Option<crate::validator_client_monitor_config::ValidatorClientMonitorConfig>,
298
299    /// Limits of the validator's gRPC listener on `network_address`.
300    #[serde(default, skip_serializing_if = "ListenerLimitsConfig::is_unset")]
301    pub validator_grpc_listener_limits: ListenerLimitsConfig,
302
303    /// Limits of the JSON-RPC listener on `json_rpc_address`.
304    #[serde(default, skip_serializing_if = "ListenerLimitsConfig::is_unset")]
305    pub json_rpc_listener_limits: ListenerLimitsConfig,
306
307    /// Limits of the gRPC API listener on `grpc_api_config.address`.
308    #[serde(default, skip_serializing_if = "ListenerLimitsConfig::is_unset")]
309    pub grpc_api_listener_limits: ListenerLimitsConfig,
310}
311
312/// Limits a listener places on the clients that connect to it.
313///
314/// Each field names a setting of `iota_http::Config`, which documents what the
315/// listener does with it. A field left out takes the listener's default, and a
316/// field written as `null` turns that limit off.
317#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
318#[serde(rename_all = "kebab-case")]
319pub struct ListenerLimitsConfig {
320    /// How long an accepted connection may take to complete its TLS handshake.
321    /// Can be turned off only together with `max-pending-connections`.
322    #[serde(default, skip_serializing_if = "Setting::is_unset")]
323    pub handshake_timeout: Setting<Duration>,
324
325    /// How many accepted connections may be in their TLS handshake at the
326    /// same time.
327    #[serde(default, skip_serializing_if = "Setting::is_unset")]
328    pub max_pending_connections: Setting<NonZeroUsize>,
329
330    /// How many connections the listener may hold at once.
331    #[serde(default, skip_serializing_if = "Setting::is_unset")]
332    pub max_connections: Setting<NonZeroUsize>,
333
334    /// How many connections one client address may hold at once.
335    #[serde(default, skip_serializing_if = "Setting::is_unset")]
336    pub max_connections_per_client_ip: Setting<NonZeroUsize>,
337
338    /// How many leading bits of an IPv6 address identify one client.
339    /// Must be between 32 and 128.
340    #[serde(default, skip_serializing_if = "Option::is_none")]
341    pub ipv6_prefix_len: Option<u8>,
342
343    /// How long a connection may serve no request before the listener closes
344    /// it.
345    #[serde(default, skip_serializing_if = "Setting::is_unset")]
346    pub max_connection_idle: Setting<Duration>,
347
348    /// How many HTTP/2 streams one connection may have open at once.
349    #[serde(default, skip_serializing_if = "Setting::is_unset")]
350    pub max_concurrent_streams: Setting<NonZeroU32>,
351
352    /// How often the listener pings an HTTP/2 connection to find out whether
353    /// its peer is still there.
354    #[serde(default, skip_serializing_if = "Setting::is_unset")]
355    pub http2_keepalive_interval: Setting<Duration>,
356
357    /// How long an HTTP/1 connection may take to send a complete request
358    /// header block.
359    #[serde(default, skip_serializing_if = "Setting::is_unset")]
360    pub http1_header_read_timeout: Setting<Duration>,
361}
362
363impl ListenerLimitsConfig {
364    /// Whether every field was left out. Meant for `skip_serializing_if`.
365    pub fn is_unset(&self) -> bool {
366        *self == Self::default()
367    }
368
369    /// Rejects the limits the listener would refuse to start with. `key` is
370    /// the section's key in `NodeConfig`, used in the error.
371    fn validate(&self, key: &str) -> Result<()> {
372        for (name, duration) in [
373            ("handshake-timeout", self.handshake_timeout),
374            ("max-connection-idle", self.max_connection_idle),
375            ("http2-keepalive-interval", self.http2_keepalive_interval),
376            ("http1-header-read-timeout", self.http1_header_read_timeout),
377        ] {
378            if duration == Setting::Value(Duration::ZERO) {
379                anyhow::bail!(
380                    "`{key}.{name}` is 0; set it greater than zero, or to `null` to turn it off"
381                );
382            }
383        }
384        // Every listener defaults both settings to on, so an absent
385        // `max-pending-connections` is on too.
386        if self.handshake_timeout == Setting::Disabled
387            && self.max_pending_connections != Setting::Disabled
388        {
389            anyhow::bail!(
390                "`{key}.handshake-timeout` is `null`, but `max-pending-connections` is on, and \
391                 only the handshake timeout frees its slots; set a handshake timeout, or set \
392                 `max-pending-connections` to `null` too"
393            );
394        }
395        if let Some(len) = self
396            .ipv6_prefix_len
397            .filter(|len| !(MIN_IPV6_PREFIX_LEN..=128).contains(len))
398        {
399            anyhow::bail!(
400                "`{key}.ipv6-prefix-len` is {len}; set it between {MIN_IPV6_PREFIX_LEN} and 128"
401            );
402        }
403        Ok(())
404    }
405}
406
407/// The shortest IPv6 prefix length a listener accepts.
408const MIN_IPV6_PREFIX_LEN: u8 = 32;
409
410#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
411#[serde(rename_all = "kebab-case")]
412pub struct TlsConfig {
413    /// File path to a PEM formatted TLS certificate chain
414    cert: String,
415    /// File path to a PEM formatted TLS private key
416    key: String,
417}
418
419impl TlsConfig {
420    pub fn cert(&self) -> &str {
421        &self.cert
422    }
423
424    pub fn key(&self) -> &str {
425        &self.key
426    }
427}
428
429/// Configuration for the gRPC API service
430#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
431#[serde(rename_all = "kebab-case")]
432pub struct GrpcApiConfig {
433    /// The address to bind the gRPC server to
434    #[serde(default = "default_grpc_api_address")]
435    pub address: SocketAddr,
436
437    /// TLS configuration for the gRPC server.
438    ///
439    /// If not provided, the gRPC server will use plain TCP without TLS.
440    #[serde(skip_serializing_if = "Option::is_none")]
441    pub tls: Option<TlsConfig>,
442
443    /// Maximum message size for gRPC responses (in bytes)
444    #[serde(default = "default_grpc_api_max_message_size_bytes")]
445    pub max_message_size_bytes: u32,
446
447    /// Buffer size for broadcast channels used for streaming
448    #[serde(default = "default_grpc_api_broadcast_buffer_size")]
449    pub broadcast_buffer_size: u32,
450
451    /// Maximum number of concurrent subscribers to checkpoint streaming RPCs.
452    /// Once the cap is reached, additional subscribe requests are rejected
453    /// with `Unavailable` to protect the server from being overwhelmed by
454    /// unbounded streaming clients. Values below 1 are clamped to 1 at
455    /// server startup.
456    #[serde(default = "default_grpc_api_max_concurrent_stream_subscribers")]
457    pub max_concurrent_stream_subscribers: u32,
458
459    /// Maximum size for Move values when rendering to JSON
460    /// in bytes.
461    #[serde(default = "default_grpc_api_max_json_move_value_size")]
462    pub max_json_move_value_size: usize,
463
464    /// Maximum number of transactions allowed in a single ExecuteTransactions
465    /// batch request.
466    #[serde(default = "default_grpc_api_max_execute_transaction_batch_size")]
467    pub max_execute_transaction_batch_size: u32,
468
469    /// Maximum number of transactions allowed in a single SimulateTransactions
470    /// batch request.
471    #[serde(default = "default_grpc_api_max_simulate_transaction_batch_size")]
472    pub max_simulate_transaction_batch_size: u32,
473
474    /// Maximum number of objects allowed in a single GetObjects batch request.
475    #[serde(default = "default_grpc_api_max_get_objects_batch_size")]
476    pub max_get_objects_batch_size: u32,
477
478    /// Maximum number of transactions allowed in a single GetTransactions batch
479    /// request.
480    #[serde(default = "default_grpc_api_max_get_transactions_batch_size")]
481    pub max_get_transactions_batch_size: u32,
482
483    /// Maximum number of view function calls allowed in a single
484    /// ViewFunctionCalls batch request.
485    #[serde(default = "default_grpc_api_max_view_function_call_batch_size")]
486    pub max_view_function_call_batch_size: u32,
487
488    /// Maximum allowed timeout in milliseconds for waiting for checkpoint
489    /// inclusion in ExecuteTransactions requests. Client-specified timeouts
490    /// are clamped to this value.
491    #[serde(default = "default_grpc_api_max_checkpoint_inclusion_timeout_ms")]
492    pub max_checkpoint_inclusion_timeout_ms: u64,
493}
494
495fn default_grpc_api_address() -> SocketAddr {
496    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 50051)
497}
498
499fn default_grpc_api_broadcast_buffer_size() -> u32 {
500    100
501}
502
503fn default_grpc_api_max_concurrent_stream_subscribers() -> u32 {
504    1024
505}
506
507fn default_grpc_api_max_message_size_bytes() -> u32 {
508    128 * 1024 * 1024 // 128MB
509}
510
511fn default_grpc_api_max_json_move_value_size() -> usize {
512    1024 * 1024 // 1 MB
513}
514
515fn default_grpc_api_max_execute_transaction_batch_size() -> u32 {
516    20
517}
518
519fn default_grpc_api_max_simulate_transaction_batch_size() -> u32 {
520    20
521}
522
523fn default_grpc_api_max_get_objects_batch_size() -> u32 {
524    1000
525}
526
527fn default_grpc_api_max_get_transactions_batch_size() -> u32 {
528    1000
529}
530
531fn default_grpc_api_max_view_function_call_batch_size() -> u32 {
532    20
533}
534
535fn default_grpc_api_max_checkpoint_inclusion_timeout_ms() -> u64 {
536    60_000 // 60 seconds
537}
538
539impl Default for GrpcApiConfig {
540    fn default() -> Self {
541        Self {
542            address: default_grpc_api_address(),
543            tls: None,
544            max_message_size_bytes: default_grpc_api_max_message_size_bytes(),
545            broadcast_buffer_size: default_grpc_api_broadcast_buffer_size(),
546            max_concurrent_stream_subscribers: default_grpc_api_max_concurrent_stream_subscribers(),
547            max_json_move_value_size: default_grpc_api_max_json_move_value_size(),
548            max_execute_transaction_batch_size: default_grpc_api_max_execute_transaction_batch_size(
549            ),
550            max_simulate_transaction_batch_size:
551                default_grpc_api_max_simulate_transaction_batch_size(),
552            max_get_objects_batch_size: default_grpc_api_max_get_objects_batch_size(),
553            max_get_transactions_batch_size: default_grpc_api_max_get_transactions_batch_size(),
554            max_view_function_call_batch_size: default_grpc_api_max_view_function_call_batch_size(),
555            max_checkpoint_inclusion_timeout_ms:
556                default_grpc_api_max_checkpoint_inclusion_timeout_ms(),
557        }
558    }
559}
560
561impl GrpcApiConfig {
562    // The default maximum uncompressed size in bytes for a message, based on
563    // tonic's default.
564    const GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE: u32 = 4 * 1024 * 1024; // 4MB
565    const GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES: u32 =
566        Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE;
567
568    pub fn tls_config(&self) -> Option<&TlsConfig> {
569        self.tls.as_ref()
570    }
571
572    pub fn max_message_size_bytes(&self) -> u32 {
573        // Ensure max message size is at least the minimum allowed
574        self.max_message_size_bytes
575            .max(Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES)
576    }
577
578    /// Calculate the maximum size for a message that can be
579    /// sent to a client, taking into account the client's max message size
580    /// preference.
581    pub fn max_message_size_client_bytes(&self, client_max_message_size_bytes: Option<u32>) -> u32 {
582        client_max_message_size_bytes
583            // if the client did not specify a max message size, use the tonic default receive
584            // message size
585            .unwrap_or(Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE)
586            // clamp the value between the tonic default and the service max message size
587            .clamp(
588                Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES,
589                self.max_message_size_bytes(),
590            )
591    }
592}
593
594#[derive(Clone, Debug, Default, Deserialize, Serialize)]
595#[serde(rename_all = "kebab-case")]
596pub struct ExecutionCacheConfig {
597    #[serde(default)]
598    pub writeback_cache: WritebackCacheConfig,
599}
600
601#[derive(Clone, Debug, Default, Deserialize, Serialize)]
602#[serde(rename_all = "kebab-case")]
603pub struct WritebackCacheConfig {
604    /// Maximum number of entries in each cache. (There are several
605    /// different caches).
606    #[serde(default, skip_serializing_if = "Option::is_none")]
607    pub max_cache_size: Option<u64>, // defaults to 100000
608
609    #[serde(default, skip_serializing_if = "Option::is_none")]
610    pub package_cache_size: Option<u64>, // defaults to 1000
611
612    #[serde(default, skip_serializing_if = "Option::is_none")]
613    pub object_cache_size: Option<u64>, // defaults to max_cache_size
614    #[serde(default, skip_serializing_if = "Option::is_none")]
615    pub marker_cache_size: Option<u64>, // defaults to object_cache_size
616    #[serde(default, skip_serializing_if = "Option::is_none")]
617    pub object_by_id_cache_size: Option<u64>, // defaults to object_cache_size
618
619    #[serde(default, skip_serializing_if = "Option::is_none")]
620    pub transaction_cache_size: Option<u64>, // defaults to max_cache_size
621    #[serde(default, skip_serializing_if = "Option::is_none")]
622    pub executed_effect_cache_size: Option<u64>, // defaults to transaction_cache_size
623    #[serde(default, skip_serializing_if = "Option::is_none")]
624    pub effect_cache_size: Option<u64>, // defaults to executed_effect_cache_size
625
626    #[serde(default, skip_serializing_if = "Option::is_none")]
627    pub events_cache_size: Option<u64>, // defaults to transaction_cache_size
628
629    #[serde(default, skip_serializing_if = "Option::is_none")]
630    pub transaction_objects_cache_size: Option<u64>, // defaults to 1000
631
632    /// Number of uncommitted transactions at which to pause consensus
633    /// handler.
634    #[serde(default, skip_serializing_if = "Option::is_none")]
635    pub backpressure_threshold: Option<u64>, // defaults to 100_000
636
637    /// Number of uncommitted transactions at which to refuse new
638    /// transaction submissions. Defaults to backpressure_threshold
639    /// if unset.
640    #[serde(default, skip_serializing_if = "Option::is_none")]
641    pub backpressure_threshold_for_rpc: Option<u64>, // defaults to backpressure_threshold
642
643    /// Percentage of `backpressure_threshold` at which graduated load shedding
644    /// based on writeback-cache pending transaction count begins. The
645    /// locally-calculated shedding percentage increases linearly from 0% at
646    /// `backpressure_threshold * backpressure_soft_limit_pct / 100` up to
647    /// 100% at the `backpressure_threshold` if the cache size continues to
648    /// increase. The calculated shedding percentage is broadcast to other
649    /// validators for a coordinated response. Defaults to 50.
650    #[serde(default, skip_serializing_if = "Option::is_none")]
651    pub backpressure_soft_limit_pct: Option<u32>,
652}
653
654impl WritebackCacheConfig {
655    pub fn max_cache_size(&self) -> u64 {
656        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MAX")
657            .ok()
658            .and_then(|s| s.parse().ok())
659            .or(self.max_cache_size)
660            .unwrap_or(100000)
661    }
662
663    pub fn package_cache_size(&self) -> u64 {
664        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_PACKAGE")
665            .ok()
666            .and_then(|s| s.parse().ok())
667            .or(self.package_cache_size)
668            .unwrap_or(1000)
669    }
670
671    pub fn object_cache_size(&self) -> u64 {
672        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT")
673            .ok()
674            .and_then(|s| s.parse().ok())
675            .or(self.object_cache_size)
676            .unwrap_or_else(|| self.max_cache_size())
677    }
678
679    pub fn marker_cache_size(&self) -> u64 {
680        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MARKER")
681            .ok()
682            .and_then(|s| s.parse().ok())
683            .or(self.marker_cache_size)
684            .unwrap_or_else(|| self.object_cache_size())
685    }
686
687    pub fn object_by_id_cache_size(&self) -> u64 {
688        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT_BY_ID")
689            .ok()
690            .and_then(|s| s.parse().ok())
691            .or(self.object_by_id_cache_size)
692            .unwrap_or_else(|| self.object_cache_size())
693    }
694
695    pub fn transaction_cache_size(&self) -> u64 {
696        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION")
697            .ok()
698            .and_then(|s| s.parse().ok())
699            .or(self.transaction_cache_size)
700            .unwrap_or_else(|| self.max_cache_size())
701    }
702
703    pub fn executed_effect_cache_size(&self) -> u64 {
704        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EXECUTED_EFFECT")
705            .ok()
706            .and_then(|s| s.parse().ok())
707            .or(self.executed_effect_cache_size)
708            .unwrap_or_else(|| self.transaction_cache_size())
709    }
710
711    pub fn effect_cache_size(&self) -> u64 {
712        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EFFECT")
713            .ok()
714            .and_then(|s| s.parse().ok())
715            .or(self.effect_cache_size)
716            .unwrap_or_else(|| self.executed_effect_cache_size())
717    }
718
719    pub fn events_cache_size(&self) -> u64 {
720        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EVENTS")
721            .ok()
722            .and_then(|s| s.parse().ok())
723            .or(self.events_cache_size)
724            .unwrap_or_else(|| self.transaction_cache_size())
725    }
726
727    pub fn transaction_objects_cache_size(&self) -> u64 {
728        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION_OBJECTS")
729            .ok()
730            .and_then(|s| s.parse().ok())
731            .or(self.transaction_objects_cache_size)
732            .unwrap_or(1000)
733    }
734
735    pub fn backpressure_threshold(&self) -> u64 {
736        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD")
737            .ok()
738            .and_then(|s| s.parse().ok())
739            .or(self.backpressure_threshold)
740            .unwrap_or(100_000)
741    }
742
743    pub fn backpressure_threshold_for_rpc(&self) -> u64 {
744        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD_FOR_RPC")
745            .ok()
746            .and_then(|s| s.parse().ok())
747            .or(self.backpressure_threshold_for_rpc)
748            .unwrap_or(self.backpressure_threshold())
749    }
750
751    pub fn backpressure_soft_limit_pct(&self) -> u32 {
752        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_SOFT_LIMIT_PCT")
753            .ok()
754            .and_then(|s| s.parse().ok())
755            .or(self.backpressure_soft_limit_pct)
756            .unwrap_or(50)
757            .min(100)
758    }
759}
760
761#[derive(Clone, Debug, Deserialize, Serialize)]
762#[serde(rename_all = "kebab-case")]
763pub struct TransactionKeyValueStoreReadConfig {
764    #[serde(default = "default_base_url")]
765    pub base_url: String,
766
767    #[serde(default = "default_cache_size")]
768    pub cache_size: u64,
769}
770
771impl Default for TransactionKeyValueStoreReadConfig {
772    fn default() -> Self {
773        Self {
774            base_url: default_base_url(),
775            cache_size: default_cache_size(),
776        }
777    }
778}
779
780fn default_base_url() -> String {
781    "".to_string()
782}
783
784fn default_cache_size() -> u64 {
785    100_000
786}
787
788fn default_transaction_kv_store_config() -> TransactionKeyValueStoreReadConfig {
789    TransactionKeyValueStoreReadConfig::default()
790}
791
792fn default_authority_store_pruning_config() -> AuthorityStorePruningConfig {
793    AuthorityStorePruningConfig::default()
794}
795
796pub fn default_enable_jsonrpc_api() -> bool {
797    true
798}
799
800fn default_grpc_address() -> Multiaddr {
801    "/ip4/0.0.0.0/tcp/8080".parse().unwrap()
802}
803fn default_authority_key_pair() -> AuthorityKeyPairWithPath {
804    AuthorityKeyPairWithPath::new(
805        get_key_pair_from_rng::<AuthorityKeyPair, _>(&mut UnwrapErr(SysRng)).1,
806    )
807}
808
809fn default_key_pair() -> KeyPairWithPath {
810    KeyPairWithPath::new(AccountPrivateKey::random().into())
811}
812
813fn default_metrics_address() -> SocketAddr {
814    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
815}
816
817pub fn default_admin_interface_address() -> SocketAddr {
818    SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1337)
819}
820
821pub fn default_json_rpc_address() -> SocketAddr {
822    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
823}
824
825pub fn default_grpc_api_config() -> Option<GrpcApiConfig> {
826    Some(GrpcApiConfig::default())
827}
828
829fn is_default_grpc_api_config(grpc_api_config: &Option<GrpcApiConfig>) -> bool {
830    serializes_like(grpc_api_config, &default_grpc_api_config())
831}
832
833/// Returns whether `value` and `default` serialize to the same YAML.
834///
835/// Meant for `skip_serializing_if` predicates, which cannot report an error: a
836/// value that fails to serialize is reported as unlike the default, so the
837/// field is kept and the failure surfaces when serializing the field itself.
838fn serializes_like<T: Serialize>(value: &T, default: &T) -> bool {
839    match (serde_yaml::to_string(value), serde_yaml::to_string(default)) {
840        (Ok(value), Ok(default)) => value == default,
841        _ => false,
842    }
843}
844
845pub fn default_grpc_concurrency_limit_per_core() -> NonZeroUsize {
846    NonZeroUsize::new(1000).unwrap()
847}
848
849pub fn default_end_of_epoch_broadcast_channel_capacity() -> usize {
850    128
851}
852
853pub fn default_full_checkpoint_contents_cache_size_mb() -> usize {
854    DEFAULT_FULL_CHECKPOINT_CONTENTS_CACHE_SIZE_MB
855}
856
857pub fn bool_true() -> bool {
858    true
859}
860
861impl Config for NodeConfig {}
862
863impl NodeConfig {
864    pub fn authority_key_pair(&self) -> &AuthorityKeyPair {
865        self.authority_key_pair.authority_keypair()
866    }
867
868    pub fn protocol_private_key(&self) -> NetworkPrivateKey {
869        self.protocol_key_pair.network_private_key()
870    }
871
872    pub fn network_private_key(&self) -> NetworkPrivateKey {
873        self.network_key_pair.network_private_key()
874    }
875
876    pub fn authority_public_key(&self) -> AuthorityPublicKeyBytes {
877        self.authority_key_pair().public().into()
878    }
879
880    pub fn db_path(&self) -> PathBuf {
881        self.db_path.join("live")
882    }
883
884    pub fn snapshot_path(&self) -> PathBuf {
885        self.db_path.join("snapshot")
886    }
887
888    pub fn network_address(&self) -> &Multiaddr {
889        &self.network_address
890    }
891
892    pub fn consensus_config(&self) -> Option<&ConsensusConfig> {
893        self.consensus_config.as_ref()
894    }
895
896    /// Whether this node builds the RPC indexes: only a fullnode with the
897    /// JSON-RPC or gRPC API enabled does.
898    pub fn maintains_rpc_indexes(&self) -> bool {
899        self.consensus_config().is_none() && (self.enable_jsonrpc_api || self.enable_grpc_api)
900    }
901
902    pub fn genesis(&self) -> Result<&genesis::Genesis> {
903        self.genesis.genesis()
904    }
905
906    pub fn load_migration_tx_data(&self) -> Result<MigrationTxData> {
907        let Some(location) = &self.migration_tx_data_path else {
908            anyhow::bail!("no file location set");
909        };
910
911        // Load from file
912        let migration_tx_data = MigrationTxData::load(location)?;
913
914        // Validate migration content in order to avoid corrupted or malicious data
915        migration_tx_data.validate_from_genesis(self.genesis.genesis()?)?;
916        Ok(migration_tx_data)
917    }
918
919    pub fn iota_address(&self) -> Address {
920        self.account_key_pair
921            .keypair()
922            .public_key()
923            .derive_address()
924    }
925
926    pub fn checkpoint_archive_config(&self) -> Option<&CheckpointArchiveConfig> {
927        self.checkpoint_archive_config.as_ref()
928    }
929
930    /// A config selects the validator role exactly when it has a consensus
931    /// config.
932    pub fn is_validator(&self) -> bool {
933        self.consensus_config.is_some()
934    }
935
936    /// Validate the node config. The checks reject a config a node could
937    /// not start with. They also reject the known cases where a node would
938    /// start and ignore part of the config.
939    pub fn validate(&self) -> Result<()> {
940        // The settings a validator must not carry: it never starts the gRPC
941        // server, and snapshot publication is a fullnode role.
942        if self.is_validator() {
943            if self.enable_grpc_api {
944                anyhow::bail!(
945                    "`enable-grpc-api` is set, but validators do not expose the gRPC API; turn \
946                     it off, or move the API to a fullnode"
947                );
948            }
949            if self
950                .state_snapshot_write_config
951                .object_store_config
952                .is_some()
953            {
954                anyhow::bail!(
955                    "`state-snapshot-write-config.object-store-config` is set, but snapshot \
956                     upload is only supported on fullnodes; remove the setting or move the \
957                     upload to a fullnode"
958                );
959            }
960        }
961        // In a file, an absent key deserializes to the default config, and a
962        // config built in code carries it too. `None` here therefore means an
963        // explicit `null`.
964        if self.enable_grpc_api && self.grpc_api_config.is_none() {
965            anyhow::bail!(
966                "`enable-grpc-api` is set but `grpc-api-config` is `null`; give it a value, \
967                 remove the key to take the default config, or turn off `enable-grpc-api`"
968            );
969        }
970        // The uploader builds the store at startup, which needs a backend.
971        if self
972            .state_snapshot_write_config
973            .object_store_config
974            .as_ref()
975            .is_some_and(|store| store.object_store.is_none())
976        {
977            anyhow::bail!(
978                "`state-snapshot-write-config.object-store-config` has no `object-store`; \
979                 snapshot upload needs a storage backend"
980            );
981        }
982        // Right after an epoch boundary, peers still sync the end of the
983        // previous epoch, and a validator still decides whether a transaction
984        // re-proposed from it was already executed, from its checkpoints.
985        if self
986            .authority_store_pruning_config
987            .num_epochs_to_retain_for_checkpoints()
988            == Some(0)
989        {
990            anyhow::bail!(
991                "`num-epochs-to-retain-for-checkpoints` is 0, but the previous epoch's \
992                 checkpoints are still needed after an epoch boundary; set it to 1 or more"
993            );
994        }
995        // Checked on every node: a fullnode starts the validator listener
996        // only when it joins the committee, at an epoch change.
997        for (key, limits) in [
998            (
999                "validator-grpc-listener-limits",
1000                &self.validator_grpc_listener_limits,
1001            ),
1002            ("json-rpc-listener-limits", &self.json_rpc_listener_limits),
1003            ("grpc-api-listener-limits", &self.grpc_api_listener_limits),
1004        ] {
1005            limits.validate(key)?;
1006        }
1007        Ok(())
1008    }
1009}
1010
1011#[derive(Debug, Clone, Deserialize, Serialize)]
1012#[serde(rename_all = "kebab-case")]
1013pub struct ConsensusConfig {
1014    /// Base consensus DB path for all epochs.
1015    pub db_path: PathBuf,
1016
1017    /// The number of epochs for which to retain the consensus DBs.
1018    /// Setting it to 0 will make a consensus DB getting dropped
1019    /// as soon as system is switched to a new epoch.
1020    pub db_retention_epochs: Option<u64>,
1021
1022    /// Pruner will run on every epoch change but it will also check
1023    /// periodically on every `db_pruner_period_secs` seconds to see
1024    /// if there are any epoch DBs to remove.
1025    pub db_pruner_period_secs: Option<u64>,
1026
1027    /// Hard limit on the number of pending transactions to submit to
1028    /// consensus, including those in submission wait and, in the
1029    /// certificate-less (P-COOL) mode, user transactions the validator is
1030    /// still checking. Used as the upper
1031    /// bound for graduated pre-consensus load shedding
1032    /// (`graduated_load_shedding_soft_limit_pct`) in the certificate-less
1033    /// (P-COOL) mode, and as the threshold for the binary
1034    /// cutoff in `ConsensusAdapter::check_consensus_overload()` in both
1035    /// certificate-less and certificate-based flows.
1036    ///
1037    /// Default to 20_000 inflight limit, assuming 20_000 txn tps * 1 sec
1038    /// consensus latency. In the P-COOL mode, a user transaction counts from
1039    /// admission, so that second also includes the checks there and, with
1040    /// attestation, the dry run and the wait for it.
1041    pub max_pending_transactions: Option<usize>,
1042
1043    /// When defined caps the calculated submission position to the
1044    /// max_submit_position.
1045    ///
1046    /// Even if the is elected to submit from a higher
1047    /// position than this, it will "reset" to the max_submit_position.
1048    pub max_submit_position: Option<usize>,
1049
1050    /// The submit delay step to consensus defined in milliseconds.
1051    ///
1052    /// When provided it will override the current back off logic otherwise the
1053    /// default backoff logic will be applied based on consensus latency
1054    /// estimates.
1055    pub submit_delay_step_override_millis: Option<u64>,
1056
1057    /// Parameters for Starfish consensus
1058    #[serde(skip_serializing_if = "Option::is_none", alias = "starfish_parameters")]
1059    pub parameters: Option<StarfishParameters>,
1060
1061    /// Percentage of `max_pending_transactions` (hard limit) defining the soft
1062    /// limit at which graduated pre-consensus load shedding begins. When
1063    /// in-flight transactions are at or below the soft limit, no shedding
1064    /// occurs; above it, the shedding rate scales linearly from 0% to 100% at
1065    /// `max_pending_transactions`. Used in the certificate-less (P-COOL) mode.
1066    #[serde(skip_serializing_if = "Option::is_none")]
1067    pub graduated_load_shedding_soft_limit_pct: Option<u32>,
1068}
1069
1070impl ConsensusConfig {
1071    pub fn db_path(&self) -> &Path {
1072        &self.db_path
1073    }
1074
1075    /// Returns the hard limit on the number of pending transactions to submit
1076    /// to consensus, including those in submission wait and, in the
1077    /// certificate-less (P-COOL) mode, user transactions the validator is
1078    /// still checking. Defaults to 20_000 inflight limit, assuming 20_000 txn
1079    /// tps * 1 sec consensus latency. In the P-COOL mode, a user transaction
1080    /// counts from admission, so that second also includes the checks there
1081    /// and, with attestation, the dry run and the wait for it.
1082    pub fn max_pending_transactions(&self) -> usize {
1083        self.max_pending_transactions.unwrap_or(20_000)
1084    }
1085
1086    /// Returns the percentage of `max_pending_transactions` (hard limit)
1087    /// defining the soft limit at which graduated pre-consensus load
1088    /// shedding begins. Defaults to 50%. Used in the certificate-less
1089    /// (P-COOL) mode.
1090    pub fn graduated_load_shedding_soft_limit_pct(&self) -> u32 {
1091        self.graduated_load_shedding_soft_limit_pct
1092            .unwrap_or(50)
1093            .min(100)
1094    }
1095
1096    pub fn submit_delay_step_override(&self) -> Option<Duration> {
1097        self.submit_delay_step_override_millis
1098            .map(Duration::from_millis)
1099    }
1100
1101    pub fn db_retention_epochs(&self) -> u64 {
1102        self.db_retention_epochs.unwrap_or(0)
1103    }
1104
1105    pub fn db_pruner_period(&self) -> Duration {
1106        // Default to 1 hour
1107        self.db_pruner_period_secs
1108            .map(Duration::from_secs)
1109            .unwrap_or(Duration::from_secs(3_600))
1110    }
1111}
1112
1113#[derive(Clone, Debug, Deserialize, Serialize)]
1114#[serde(rename_all = "kebab-case")]
1115pub struct CheckpointExecutorConfig {
1116    /// Upper bound on the number of checkpoints that can be concurrently
1117    /// executed.
1118    ///
1119    /// If unspecified, this will default to `200`
1120    #[serde(default = "default_checkpoint_execution_max_concurrency")]
1121    pub checkpoint_execution_max_concurrency: usize,
1122
1123    /// Number of seconds to wait for effects of a batch of transactions
1124    /// before logging a warning. Note that we will continue to retry
1125    /// indefinitely.
1126    ///
1127    /// If unspecified, this will default to `10`.
1128    #[serde(default = "default_local_execution_timeout_sec")]
1129    pub local_execution_timeout_sec: u64,
1130
1131    /// Optional directory used for data ingestion pipeline.
1132    ///
1133    /// When specified, each executed checkpoint will be saved in a local
1134    /// directory for post-processing
1135    #[serde(default, skip_serializing_if = "Option::is_none")]
1136    pub data_ingestion_dir: Option<PathBuf>,
1137}
1138
1139#[derive(Clone, Debug, Default, Deserialize, Serialize)]
1140#[serde(rename_all = "kebab-case")]
1141pub struct ExpensiveSafetyCheckConfig {
1142    /// If enabled, at epoch boundary, we will check that the storage
1143    /// fund balance is always identical to the sum of the storage
1144    /// rebate of all live objects, and that the total IOTA in the network
1145    /// remains the same.
1146    #[serde(default)]
1147    enable_epoch_iota_conservation_check: bool,
1148
1149    /// If enabled, we will check that the total IOTA in all input objects of a
1150    /// tx (both the Move part and the storage rebate) matches the total IOTA
1151    /// in all output objects of the tx + gas fees.
1152    #[serde(default)]
1153    enable_deep_per_tx_iota_conservation_check: bool,
1154
1155    /// Disable epoch IOTA conservation check even when we are running in debug
1156    /// mode.
1157    #[serde(default)]
1158    force_disable_epoch_iota_conservation_check: bool,
1159
1160    /// If enabled, at epoch boundary, we will check that the accumulated
1161    /// live object state matches the end of epoch root state digest.
1162    #[serde(default)]
1163    enable_state_consistency_check: bool,
1164
1165    /// Disable state consistency check even when we are running in debug mode.
1166    #[serde(default)]
1167    force_disable_state_consistency_check: bool,
1168}
1169
1170impl ExpensiveSafetyCheckConfig {
1171    pub fn new_enable_all() -> Self {
1172        Self {
1173            enable_epoch_iota_conservation_check: true,
1174            enable_deep_per_tx_iota_conservation_check: true,
1175            force_disable_epoch_iota_conservation_check: false,
1176            enable_state_consistency_check: true,
1177            force_disable_state_consistency_check: false,
1178        }
1179    }
1180
1181    pub fn new_disable_all() -> Self {
1182        Self {
1183            enable_epoch_iota_conservation_check: false,
1184            enable_deep_per_tx_iota_conservation_check: false,
1185            force_disable_epoch_iota_conservation_check: true,
1186            enable_state_consistency_check: false,
1187            force_disable_state_consistency_check: true,
1188        }
1189    }
1190
1191    pub fn force_disable_epoch_iota_conservation_check(&mut self) {
1192        self.force_disable_epoch_iota_conservation_check = true;
1193    }
1194
1195    pub fn enable_epoch_iota_conservation_check(&self) -> bool {
1196        (self.enable_epoch_iota_conservation_check || cfg!(debug_assertions))
1197            && !self.force_disable_epoch_iota_conservation_check
1198    }
1199
1200    pub fn force_disable_state_consistency_check(&mut self) {
1201        self.force_disable_state_consistency_check = true;
1202    }
1203
1204    pub fn enable_state_consistency_check(&self) -> bool {
1205        (self.enable_state_consistency_check || cfg!(debug_assertions))
1206            && !self.force_disable_state_consistency_check
1207    }
1208
1209    pub fn enable_deep_per_tx_iota_conservation_check(&self) -> bool {
1210        self.enable_deep_per_tx_iota_conservation_check || cfg!(debug_assertions)
1211    }
1212}
1213
1214fn default_checkpoint_execution_max_concurrency() -> usize {
1215    4
1216}
1217
1218fn default_local_execution_timeout_sec() -> u64 {
1219    30
1220}
1221
1222impl Default for CheckpointExecutorConfig {
1223    fn default() -> Self {
1224        Self {
1225            checkpoint_execution_max_concurrency: default_checkpoint_execution_max_concurrency(),
1226            local_execution_timeout_sec: default_local_execution_timeout_sec(),
1227            data_ingestion_dir: None,
1228        }
1229    }
1230}
1231
1232#[derive(Debug, Clone, Deserialize, Serialize)]
1233#[serde(rename_all = "kebab-case")]
1234pub struct AuthorityStorePruningConfig {
1235    /// number of the latest epoch dbs to retain
1236    #[serde(default = "default_num_latest_epoch_dbs_to_retain")]
1237    pub num_latest_epoch_dbs_to_retain: usize,
1238    /// Number of historic epochs of object versions to keep, on top of the
1239    /// epoch the node is in. Read through [`Self::num_epochs_to_retain`].
1240    ///   0        — keep the epoch the node is in only (default).
1241    ///   N        — keep the epoch the node is in plus the N epochs before it.
1242    ///   u64::MAX — keep every epoch's bucket; object pruning is off.
1243    #[serde(default)]
1244    pub num_epochs_to_retain: u64,
1245    /// enables periodic background compaction for old SST files whose last
1246    /// modified time is older than `periodic_compaction_threshold_days`
1247    /// days. That ensures that all sst files eventually go through the
1248    /// compaction process
1249    ///
1250    /// With the key absent files older than a day are compacted, an explicit
1251    /// `null` turns periodic compaction off, and a value sets the threshold in
1252    /// days.
1253    #[serde(
1254        default = "default_periodic_compaction_threshold_days",
1255        skip_serializing_if = "is_default_periodic_compaction_threshold_days"
1256    )]
1257    pub periodic_compaction_threshold_days: Option<usize>,
1258    /// Number of historic epochs of transactions, effects, events, and
1259    /// checkpoint data to keep, on top of the epoch the node is in. Controls
1260    /// transaction pruning. Read through
1261    /// [`Self::num_epochs_to_retain_for_checkpoints`].
1262    ///   0        — refused by [`NodeConfig::validate`].
1263    ///   N        — keep the epoch the node is in plus the N epochs before it.
1264    ///   u64::MAX — keep every epoch's bucket; transaction pruning is off.
1265    ///   None     — [`DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS`].
1266    #[serde(skip_serializing_if = "Option::is_none")]
1267    pub num_epochs_to_retain_for_checkpoints: Option<u64>,
1268    /// Number of historic epochs of RPC index history (the transaction and
1269    /// event lookups behind `iotax_queryTransactionBlocks` and
1270    /// `iotax_queryEvents`) to keep, on top of the epoch the node is in. Index tables for live
1271    /// objects are not affected. Read through [`Self::num_epochs_to_retain_for_indexes`].
1272    ///   0        — keep the epoch the node is in only.
1273    ///   N        — keep the epoch the node is in plus the N epochs before it.
1274    ///   u64::MAX — keep every epoch's bucket; index pruning is off.
1275    ///   None     — follow `num_epochs_to_retain_for_checkpoints`.
1276    ///
1277    /// Must not exceed `num_epochs_to_retain_for_checkpoints`; the node
1278    /// refuses to start otherwise.
1279    #[serde(skip_serializing_if = "Option::is_none")]
1280    pub num_epochs_to_retain_for_indexes: Option<u64>,
1281}
1282
1283/// The historic epochs of transactions, effects, events and checkpoint data
1284/// kept when `num-epochs-to-retain-for-checkpoints` is not set: about a month
1285/// at the networks' one-day epochs.
1286pub const DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS: u64 = 30;
1287
1288fn default_num_latest_epoch_dbs_to_retain() -> usize {
1289    3
1290}
1291
1292fn default_periodic_compaction_threshold_days() -> Option<usize> {
1293    Some(1)
1294}
1295
1296fn is_default_periodic_compaction_threshold_days(days: &Option<usize>) -> bool {
1297    *days == default_periodic_compaction_threshold_days()
1298}
1299
1300impl Default for AuthorityStorePruningConfig {
1301    fn default() -> Self {
1302        Self {
1303            num_latest_epoch_dbs_to_retain: default_num_latest_epoch_dbs_to_retain(),
1304            num_epochs_to_retain: 0,
1305            periodic_compaction_threshold_days: default_periodic_compaction_threshold_days(),
1306            num_epochs_to_retain_for_checkpoints: if cfg!(msim) { Some(2) } else { None },
1307            num_epochs_to_retain_for_indexes: None,
1308        }
1309    }
1310}
1311
1312impl AuthorityStorePruningConfig {
1313    pub fn set_num_epochs_to_retain(&mut self, num_epochs_to_retain: u64) {
1314        self.num_epochs_to_retain = num_epochs_to_retain;
1315    }
1316
1317    pub fn set_num_epochs_to_retain_for_checkpoints(&mut self, num_epochs_to_retain: Option<u64>) {
1318        self.num_epochs_to_retain_for_checkpoints = num_epochs_to_retain;
1319    }
1320
1321    /// Historic epochs of object versions to keep on top of the epoch the
1322    /// node is in, `None` when object pruning is off.
1323    pub fn num_epochs_to_retain(&self) -> Option<u64> {
1324        (self.num_epochs_to_retain != u64::MAX).then_some(self.num_epochs_to_retain)
1325    }
1326
1327    /// Historic epochs of transactions, effects, events and checkpoint data
1328    /// to keep on top of the epoch the node is in, `None` when transaction
1329    /// pruning is off. Unset is
1330    /// [`DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS`].
1331    pub fn num_epochs_to_retain_for_checkpoints(&self) -> Option<u64> {
1332        match self.num_epochs_to_retain_for_checkpoints {
1333            Some(u64::MAX) => None,
1334            Some(n) => Some(n),
1335            None => Some(DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS),
1336        }
1337    }
1338
1339    /// Historic epochs of RPC index history to keep on top of the epoch the
1340    /// node is in, `None` when index pruning is off. Unset follows the
1341    /// transaction retention.
1342    pub fn num_epochs_to_retain_for_indexes(&self) -> Option<u64> {
1343        match self.num_epochs_to_retain_for_indexes {
1344            Some(n) if n == u64::MAX => None,
1345            Some(n) => Some(n),
1346            None => self.num_epochs_to_retain_for_checkpoints(),
1347        }
1348    }
1349
1350    /// Fails if the index retention exceeds the transaction retention, which
1351    /// would let `iotax_queryTransactionBlocks` return digests the node has
1352    /// already pruned.
1353    pub fn check_index_retention_within_ledger(&self) -> Result<()> {
1354        let Some(ledger) = self.num_epochs_to_retain_for_checkpoints() else {
1355            // An unbounded ledger outlives every index retention.
1356            return Ok(());
1357        };
1358        let index = self.num_epochs_to_retain_for_indexes();
1359        if index.is_none_or(|index| index > ledger) {
1360            let index = index.map_or("unlimited".to_owned(), |index| index.to_string());
1361            anyhow::bail!(
1362                "num-epochs-to-retain-for-indexes ({index}) must not exceed \
1363                 num-epochs-to-retain-for-checkpoints ({ledger}): a longer index retention \
1364                 lets iotax_queryTransactionBlocks return a transaction digest whose ledger \
1365                 data has already been pruned. Lower num-epochs-to-retain-for-indexes or \
1366                 raise num-epochs-to-retain-for-checkpoints."
1367            );
1368        }
1369        Ok(())
1370    }
1371}
1372
1373#[derive(Debug, Clone, Deserialize, Serialize)]
1374#[serde(rename_all = "kebab-case")]
1375pub struct MetricsConfig {
1376    #[serde(skip_serializing_if = "Option::is_none")]
1377    pub push_interval_seconds: Option<u64>,
1378    #[serde(skip_serializing_if = "Option::is_none")]
1379    pub push_url: Option<String>,
1380    #[serde(skip_serializing_if = "Option::is_none")]
1381    pub groups: Option<MetricGroups>,
1382}
1383
1384fn default_checkpoint_archive_download_concurrency() -> NonZeroUsize {
1385    NonZeroUsize::new(10).unwrap()
1386}
1387
1388fn default_checkpoint_archive_verify_concurrency() -> NonZeroUsize {
1389    std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap())
1390}
1391
1392/// Configuration for backfilling checkpoint contents from the
1393/// checkpoint archive when peers no longer serve the required range.
1394#[derive(Debug, Clone, Deserialize, Serialize)]
1395#[serde(rename_all = "kebab-case")]
1396pub struct CheckpointArchiveConfig {
1397    /// URL of the checkpoint archive to backfill from.
1398    pub url: String,
1399    /// Non-zero number of checkpoints to download in parallel.
1400    #[serde(default = "default_checkpoint_archive_download_concurrency")]
1401    pub download_concurrency: NonZeroUsize,
1402    /// Non-zero number of downloaded checkpoints to verify in parallel.
1403    /// Defaults to the number of CPU cores.
1404    #[serde(default = "default_checkpoint_archive_verify_concurrency")]
1405    pub verify_concurrency: NonZeroUsize,
1406}
1407
1408/// Configuration for the per-epoch state-snapshot publisher.
1409///
1410/// **Operator note (V2 snapshot publishing).** A node configured to publish
1411/// V2 snapshots must have a perpetual store containing no pre-V2
1412/// (`StoreObjectV1`) rows. This means a snapshot-publishing node must have
1413/// either synced from genesis under V2 or been restored from a V2 snapshot.
1414/// There is no on-disk backfill: a fresh sync is the only supported path.
1415#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1416#[serde(rename_all = "kebab-case")]
1417pub struct StateSnapshotConfig {
1418    #[serde(skip_serializing_if = "Option::is_none")]
1419    pub object_store_config: Option<ObjectStoreConfig>,
1420    pub concurrency: usize,
1421}
1422
1423#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1424#[serde(rename_all = "kebab-case")]
1425pub struct TransactionKeyValueStoreWriteConfig {
1426    pub aws_access_key_id: String,
1427    pub aws_secret_access_key: String,
1428    pub aws_region: String,
1429    pub table_name: String,
1430    pub bucket_name: String,
1431    pub concurrency: usize,
1432}
1433
1434/// Configuration for the threshold(s) at which we consider the system
1435/// to be overloaded. When one of the threshold is passed, the node may
1436/// stop processing new transactions and/or certificates until the congestion
1437/// resolves.
1438#[derive(Clone, Debug, Deserialize, Serialize)]
1439#[serde(rename_all = "kebab-case")]
1440pub struct AuthorityOverloadConfig {
1441    /// Maximum time a transaction can wait in the transaction manager execution
1442    /// queue before it triggers an overload detection on the object it depends
1443    /// on.
1444    #[serde(default = "default_max_txn_age_in_queue")]
1445    pub max_txn_age_in_queue: Duration,
1446
1447    /// The interval of checking overload signal.
1448    #[serde(default = "default_overload_monitor_interval")]
1449    pub overload_monitor_interval: Duration,
1450
1451    /// The execution queueing latency when entering load shedding mode.
1452    #[serde(default = "default_execution_queue_latency_soft_limit")]
1453    pub execution_queue_latency_soft_limit: Duration,
1454
1455    /// The execution queueing latency when entering aggressive load shedding
1456    /// mode.
1457    #[serde(default = "default_execution_queue_latency_hard_limit")]
1458    pub execution_queue_latency_hard_limit: Duration,
1459
1460    /// The maximum percentage of transactions to shed in load shedding mode.
1461    #[serde(default = "default_max_load_shedding_percentage")]
1462    pub max_load_shedding_percentage: u32,
1463
1464    /// When in aggressive load shedding mode, the minimum percentage of
1465    /// transactions to shed.
1466    #[serde(default = "default_min_load_shedding_percentage_above_hard_limit")]
1467    pub min_load_shedding_percentage_above_hard_limit: u32,
1468
1469    /// If transaction ready rate is below this rate, we consider the validator
1470    /// is well under used, and will not enter load shedding mode.
1471    #[serde(default = "default_safe_transaction_ready_rate")]
1472    pub safe_transaction_ready_rate: u32,
1473
1474    /// When set to true, transaction signing may be rejected when the validator
1475    /// is overloaded, and so may a submission in the P-COOL flow with validator
1476    /// attestation.
1477    #[serde(default = "default_check_system_overload_at_signing")]
1478    pub check_system_overload_at_signing: bool,
1479
1480    /// When set to true, transaction execution may be rejected when the
1481    /// validator is overloaded.
1482    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1483    pub check_system_overload_at_execution: bool,
1484
1485    /// Reject a transaction if transaction manager queue length is above this
1486    /// threshold. 100_000 = 10k TPS * 5s resident time in transaction
1487    /// manager (pending + executing) * 2.
1488    #[serde(default = "default_max_transaction_manager_queue_length")]
1489    pub max_transaction_manager_queue_length: usize,
1490
1491    /// Reject a transaction if the number of pending transactions depending on
1492    /// the object is above the threshold.
1493    #[serde(default = "default_max_transaction_manager_per_object_queue_length")]
1494    pub max_transaction_manager_per_object_queue_length: usize,
1495
1496    /// Percentage of `max_transaction_manager_queue_length` at which graduated
1497    /// load shedding begins in the certificate-less (P-COOL) mode. Read
1498    /// via the same-named accessor, which clamps the value to <=100.
1499    #[serde(default = "default_max_transaction_manager_queue_length_soft_limit_pct")]
1500    pub max_transaction_manager_queue_length_soft_limit_pct: u32,
1501
1502    /// Maximum number of attestation dry runs the validator runs at once.
1503    /// Unset means a quarter of the available cores, at least one.
1504    #[serde(default, skip_serializing_if = "Option::is_none")]
1505    pub max_concurrent_attestations: Option<NonZeroUsize>,
1506
1507    /// Maximum number of submissions that wait for a free attestation dry run,
1508    /// a bound on the memory they hold. A submission that finds every dry run
1509    /// taken and this many waiting is rejected with
1510    /// [`IotaError::ValidatorOverloadedRetryAfter`]. Unset means 1,024.
1511    ///
1512    /// [`IotaError::ValidatorOverloadedRetryAfter`]: iota_types::error::IotaError::ValidatorOverloadedRetryAfter
1513    #[serde(default, skip_serializing_if = "Option::is_none")]
1514    pub max_queued_attestations: Option<usize>,
1515
1516    /// Maximum time a submission waits for a free attestation dry run. A
1517    /// submission that has waited this long, or whose expected wait is longer
1518    /// when it arrives, is rejected with
1519    /// [`IotaError::ValidatorOverloadedRetryAfter`], which asks the client to
1520    /// retry after the first whole second past this wait. Unset means 500 ms.
1521    ///
1522    /// [`IotaError::ValidatorOverloadedRetryAfter`]: iota_types::error::IotaError::ValidatorOverloadedRetryAfter
1523    #[serde(default, skip_serializing_if = "Option::is_none")]
1524    pub max_attestation_wait: Option<Duration>,
1525}
1526
1527impl AuthorityOverloadConfig {
1528    /// Returns the soft-limit percentage, clamped to <=100 to guard against
1529    /// out-of-range operator-supplied values.
1530    pub fn max_transaction_manager_queue_length_soft_limit_pct(&self) -> u32 {
1531        self.max_transaction_manager_queue_length_soft_limit_pct
1532            .min(100)
1533    }
1534}
1535
1536fn default_max_txn_age_in_queue() -> Duration {
1537    Duration::from_millis(500)
1538}
1539
1540fn default_overload_monitor_interval() -> Duration {
1541    Duration::from_secs(10)
1542}
1543
1544fn default_execution_queue_latency_soft_limit() -> Duration {
1545    Duration::from_secs(1)
1546}
1547
1548fn default_execution_queue_latency_hard_limit() -> Duration {
1549    Duration::from_secs(10)
1550}
1551
1552fn default_max_load_shedding_percentage() -> u32 {
1553    95
1554}
1555
1556fn default_min_load_shedding_percentage_above_hard_limit() -> u32 {
1557    50
1558}
1559
1560fn default_safe_transaction_ready_rate() -> u32 {
1561    100
1562}
1563
1564fn default_check_system_overload_at_signing() -> bool {
1565    true
1566}
1567
1568fn default_max_transaction_manager_queue_length() -> usize {
1569    100_000
1570}
1571
1572fn default_max_transaction_manager_queue_length_soft_limit_pct() -> u32 {
1573    50
1574}
1575
1576fn default_max_transaction_manager_per_object_queue_length() -> usize {
1577    20
1578}
1579
1580impl Default for AuthorityOverloadConfig {
1581    fn default() -> Self {
1582        Self {
1583            max_txn_age_in_queue: default_max_txn_age_in_queue(),
1584            overload_monitor_interval: default_overload_monitor_interval(),
1585            execution_queue_latency_soft_limit: default_execution_queue_latency_soft_limit(),
1586            execution_queue_latency_hard_limit: default_execution_queue_latency_hard_limit(),
1587            max_load_shedding_percentage: default_max_load_shedding_percentage(),
1588            min_load_shedding_percentage_above_hard_limit:
1589                default_min_load_shedding_percentage_above_hard_limit(),
1590            safe_transaction_ready_rate: default_safe_transaction_ready_rate(),
1591            check_system_overload_at_signing: true,
1592            check_system_overload_at_execution: false,
1593            max_transaction_manager_queue_length: default_max_transaction_manager_queue_length(),
1594            max_transaction_manager_queue_length_soft_limit_pct:
1595                default_max_transaction_manager_queue_length_soft_limit_pct(),
1596            max_transaction_manager_per_object_queue_length:
1597                default_max_transaction_manager_per_object_queue_length(),
1598            max_concurrent_attestations: None,
1599            max_queued_attestations: None,
1600            max_attestation_wait: None,
1601        }
1602    }
1603}
1604
1605fn default_authority_overload_config() -> AuthorityOverloadConfig {
1606    AuthorityOverloadConfig::default()
1607}
1608
1609fn default_traffic_controller_policy_config() -> Option<PolicyConfig> {
1610    Some(PolicyConfig::default_dos_protection_policy())
1611}
1612
1613fn is_default_traffic_controller_policy_config(policy_config: &Option<PolicyConfig>) -> bool {
1614    serializes_like(policy_config, &default_traffic_controller_policy_config())
1615}
1616
1617#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1618pub struct Genesis {
1619    #[serde(flatten)]
1620    location: Option<GenesisLocation>,
1621
1622    #[serde(skip)]
1623    genesis: once_cell::sync::OnceCell<genesis::Genesis>,
1624}
1625
1626impl Genesis {
1627    pub fn new(genesis: genesis::Genesis) -> Self {
1628        Self {
1629            location: Some(GenesisLocation::InPlace {
1630                genesis: Box::new(genesis),
1631            }),
1632            genesis: Default::default(),
1633        }
1634    }
1635
1636    pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
1637        Self {
1638            location: Some(GenesisLocation::File {
1639                genesis_file_location: path.into(),
1640            }),
1641            genesis: Default::default(),
1642        }
1643    }
1644
1645    pub fn new_empty() -> Self {
1646        Self {
1647            location: None,
1648            genesis: Default::default(),
1649        }
1650    }
1651
1652    pub fn genesis(&self) -> Result<&genesis::Genesis> {
1653        match &self.location {
1654            Some(GenesisLocation::InPlace { genesis }) => Ok(genesis),
1655            Some(GenesisLocation::File {
1656                genesis_file_location,
1657            }) => self
1658                .genesis
1659                .get_or_try_init(|| genesis::Genesis::load(genesis_file_location)),
1660            None => anyhow::bail!("no genesis location set"),
1661        }
1662    }
1663}
1664
1665#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1666#[serde(untagged)]
1667enum GenesisLocation {
1668    InPlace {
1669        genesis: Box<genesis::Genesis>,
1670    },
1671    File {
1672        #[serde(rename = "genesis-file-location")]
1673        genesis_file_location: PathBuf,
1674    },
1675}
1676
1677/// Wrapper struct for SimpleKeypair that can be deserialized from a file path.
1678/// Used by network, worker, and account keypair.
1679#[derive(Clone, Debug, Deserialize, Serialize)]
1680pub struct KeyPairWithPath {
1681    #[serde(flatten)]
1682    location: KeyPairLocation,
1683
1684    #[serde(skip)]
1685    keypair: OnceCell<Arc<SimpleKeypair>>,
1686}
1687
1688impl PartialEq for KeyPairWithPath {
1689    fn eq(&self, other: &Self) -> bool {
1690        self.location == other.location
1691    }
1692}
1693
1694impl Eq for KeyPairWithPath {}
1695
1696#[derive(Debug, Clone, Deserialize, Serialize)]
1697#[serde(untagged)]
1698enum KeyPairLocation {
1699    InPlace {
1700        #[serde(with = "bech32_formatted_keypair")]
1701        value: Arc<SimpleKeypair>,
1702    },
1703    File {
1704        path: PathBuf,
1705    },
1706}
1707
1708impl PartialEq for KeyPairLocation {
1709    fn eq(&self, other: &Self) -> bool {
1710        match (self, other) {
1711            (Self::InPlace { value: a }, Self::InPlace { value: b }) => {
1712                a.to_bytes() == b.to_bytes()
1713            }
1714            (Self::File { path: a }, Self::File { path: b }) => a == b,
1715            _ => false,
1716        }
1717    }
1718}
1719
1720impl Eq for KeyPairLocation {}
1721
1722impl KeyPairWithPath {
1723    pub fn new(kp: SimpleKeypair) -> Self {
1724        let cell: OnceCell<Arc<SimpleKeypair>> = OnceCell::new();
1725        let arc_kp = Arc::new(kp);
1726        // OK to unwrap panic because authority should not start without all keypairs
1727        // loaded.
1728        cell.set(arc_kp.clone()).expect("failed to set keypair");
1729        Self {
1730            location: KeyPairLocation::InPlace { value: arc_kp },
1731            keypair: cell,
1732        }
1733    }
1734
1735    pub fn new_from_path(path: PathBuf) -> Self {
1736        let cell: OnceCell<Arc<SimpleKeypair>> = OnceCell::new();
1737        // OK to unwrap panic because authority should not start without all keypairs
1738        // loaded.
1739        cell.set(Arc::new(read_keypair_from_file(&path).unwrap_or_else(
1740            |e| panic!("invalid keypair file at path {path:?}: {e}"),
1741        )))
1742        .expect("failed to set keypair");
1743        Self {
1744            location: KeyPairLocation::File { path },
1745            keypair: cell,
1746        }
1747    }
1748
1749    pub fn keypair(&self) -> &SimpleKeypair {
1750        self.keypair
1751            .get_or_init(|| match &self.location {
1752                KeyPairLocation::InPlace { value } => value.clone(),
1753                KeyPairLocation::File { path } => {
1754                    // OK to unwrap panic because authority should not start without all keypairs
1755                    // loaded.
1756                    Arc::new(
1757                        read_keypair_from_file(path).unwrap_or_else(|e| {
1758                            panic!("invalid keypair file at path {path:?}: {e}")
1759                        }),
1760                    )
1761                }
1762            })
1763            .as_ref()
1764    }
1765
1766    /// The stored keypair as an ed25519 network private key, for the network
1767    /// stacks that consume that type directly. Panics if the stored keypair
1768    /// is not ed25519.
1769    pub fn network_private_key(&self) -> NetworkPrivateKey {
1770        self.keypair()
1771            .as_opt_ed25519()
1772            .cloned()
1773            .expect("only Ed25519 network keys are allowed")
1774    }
1775}
1776
1777/// Wrapper struct for AuthorityKeyPair that can be deserialized from a file
1778/// path.
1779#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1780pub struct AuthorityKeyPairWithPath {
1781    #[serde(flatten)]
1782    location: AuthorityKeyPairLocation,
1783
1784    #[serde(skip)]
1785    keypair: OnceCell<Arc<AuthorityKeyPair>>,
1786}
1787
1788#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1789#[serde(untagged)]
1790enum AuthorityKeyPairLocation {
1791    InPlace { value: Arc<AuthorityKeyPair> },
1792    File { path: PathBuf },
1793}
1794
1795impl AuthorityKeyPairWithPath {
1796    pub fn new(kp: AuthorityKeyPair) -> Self {
1797        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1798        let arc_kp = Arc::new(kp);
1799        // OK to unwrap panic because authority should not start without all keypairs
1800        // loaded.
1801        cell.set(arc_kp.clone())
1802            .expect("failed to set authority keypair");
1803        Self {
1804            location: AuthorityKeyPairLocation::InPlace { value: arc_kp },
1805            keypair: cell,
1806        }
1807    }
1808
1809    pub fn new_from_path(path: PathBuf) -> Self {
1810        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1811        // OK to unwrap panic because authority should not start without all keypairs
1812        // loaded.
1813        cell.set(Arc::new(
1814            read_authority_keypair_from_file(&path)
1815                .unwrap_or_else(|_| panic!("invalid authority keypair file at path {path:?}")),
1816        ))
1817        .expect("failed to set authority keypair");
1818        Self {
1819            location: AuthorityKeyPairLocation::File { path },
1820            keypair: cell,
1821        }
1822    }
1823
1824    pub fn authority_keypair(&self) -> &AuthorityKeyPair {
1825        self.keypair
1826            .get_or_init(|| match &self.location {
1827                AuthorityKeyPairLocation::InPlace { value } => value.clone(),
1828                AuthorityKeyPairLocation::File { path } => {
1829                    // OK to unwrap panic because authority should not start without all keypairs
1830                    // loaded.
1831                    Arc::new(
1832                        read_authority_keypair_from_file(path)
1833                            .unwrap_or_else(|_| panic!("invalid authority keypair file {path:?}")),
1834                    )
1835                }
1836            })
1837            .as_ref()
1838    }
1839}
1840
1841/// Configurations which determine how we dump state debug info.
1842/// Debug info is dumped when a node forks.
1843#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1844#[serde(rename_all = "kebab-case")]
1845pub struct StateDebugDumpConfig {
1846    #[serde(skip_serializing_if = "Option::is_none")]
1847    pub dump_file_directory: Option<PathBuf>,
1848}
1849
1850#[cfg(test)]
1851mod tests {
1852    use std::{
1853        num::{NonZeroU32, NonZeroUsize},
1854        path::PathBuf,
1855        time::Duration,
1856    };
1857
1858    use fastcrypto::traits::KeyPair;
1859    use iota_keys::keypair_file::{write_authority_keypair_to_file, write_keypair_to_file};
1860    use iota_sdk_crypto::simple::SimpleKeypair;
1861    use iota_types::{
1862        crypto::{AuthorityKeyPair, NetworkPrivateKey, get_key_pair_from_rng},
1863        traffic_control::PolicyConfig,
1864    };
1865    use rand::{SeedableRng, rngs::StdRng};
1866    use serde::Serialize;
1867    use serde_yaml::Value;
1868
1869    use super::{
1870        AuthorityStorePruningConfig, DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS, Genesis,
1871        GrpcApiConfig, ListenerLimitsConfig, ObjectStoreConfig, default_grpc_api_config,
1872        default_periodic_compaction_threshold_days, default_traffic_controller_policy_config,
1873    };
1874    use crate::{
1875        ConsensusConfig, NodeConfig, object_storage_config::ObjectStoreType, setting::Setting,
1876    };
1877
1878    const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1879
1880    const POLICY_CONFIG: &[&str] = &["policy-config"];
1881    const GRPC_API_CONFIG: &[&str] = &["grpc-api-config"];
1882    const COMPACTION_THRESHOLD: &[&str] = &[
1883        "authority-store-pruning-config",
1884        "periodic-compaction-threshold-days",
1885    ];
1886
1887    fn template_config() -> NodeConfig {
1888        serde_yaml::from_str(TEMPLATE).unwrap()
1889    }
1890
1891    fn consensus_config() -> super::ConsensusConfig {
1892        serde_yaml::from_str("db-path: /opt/iota/consensus-db").unwrap()
1893    }
1894
1895    fn object_store_config() -> ObjectStoreConfig {
1896        ObjectStoreConfig {
1897            object_store: Some(ObjectStoreType::File),
1898            directory: Some(PathBuf::from("/opt/iota/snapshots")),
1899            ..Default::default()
1900        }
1901    }
1902
1903    fn round_trip(config: &NodeConfig) -> NodeConfig {
1904        serde_yaml::from_str(&serde_yaml::to_string(config).unwrap()).unwrap()
1905    }
1906
1907    fn as_yaml<T: Serialize>(value: &T) -> String {
1908        serde_yaml::to_string(value).unwrap()
1909    }
1910
1911    /// Reads `path` out of a serialized value, returning `None` when the last
1912    /// key is absent.
1913    fn written_at(value: &Value, path: &[&str]) -> Option<Value> {
1914        let (last, parents) = path.split_last().unwrap();
1915        let mut current = value;
1916        for name in parents {
1917            current = current
1918                .as_mapping()
1919                .unwrap()
1920                .get(&Value::String((*name).to_owned()))
1921                .unwrap();
1922        }
1923        current
1924            .as_mapping()
1925            .unwrap()
1926            .get(&Value::String((*last).to_owned()))
1927            .cloned()
1928    }
1929
1930    #[test]
1931    fn a_retention_below_two_is_no_longer_rounded_up() {
1932        let config = AuthorityStorePruningConfig {
1933            num_epochs_to_retain_for_checkpoints: Some(1),
1934            ..Default::default()
1935        };
1936        assert_eq!(config.num_epochs_to_retain_for_checkpoints(), Some(1));
1937    }
1938
1939    #[test]
1940    fn index_retention_within_ledger_retention_is_accepted() {
1941        let config = AuthorityStorePruningConfig {
1942            num_epochs_to_retain_for_checkpoints: Some(2),
1943            num_epochs_to_retain_for_indexes: Some(2),
1944            ..Default::default()
1945        };
1946        assert!(config.check_index_retention_within_ledger().is_ok());
1947    }
1948
1949    #[test]
1950    fn index_retention_beyond_ledger_retention_is_refused() {
1951        let config = AuthorityStorePruningConfig {
1952            num_epochs_to_retain_for_checkpoints: Some(2),
1953            num_epochs_to_retain_for_indexes: Some(3),
1954            ..Default::default()
1955        };
1956        let err = config
1957            .check_index_retention_within_ledger()
1958            .unwrap_err()
1959            .to_string();
1960        assert!(err.contains("num-epochs-to-retain-for-indexes"), "{err}");
1961        assert!(
1962            err.contains("num-epochs-to-retain-for-checkpoints"),
1963            "{err}"
1964        );
1965        assert!(err.contains('3'), "{err}");
1966        assert!(err.contains('2'), "{err}");
1967    }
1968
1969    #[test]
1970    fn the_narrowest_ledger_retention_refuses_a_longer_index_retention() {
1971        let config = AuthorityStorePruningConfig {
1972            num_epochs_to_retain_for_checkpoints: Some(1),
1973            num_epochs_to_retain_for_indexes: Some(2),
1974            ..Default::default()
1975        };
1976        assert!(config.check_index_retention_within_ledger().is_err());
1977    }
1978
1979    #[test]
1980    fn equal_narrowest_retentions_are_accepted() {
1981        let config = AuthorityStorePruningConfig {
1982            num_epochs_to_retain_for_checkpoints: Some(1),
1983            num_epochs_to_retain_for_indexes: Some(1),
1984            ..Default::default()
1985        };
1986        assert!(config.check_index_retention_within_ledger().is_ok());
1987    }
1988
1989    #[test]
1990    fn unbounded_ledger_retention_accepts_any_index_retention() {
1991        let config = AuthorityStorePruningConfig {
1992            num_epochs_to_retain_for_checkpoints: Some(u64::MAX),
1993            num_epochs_to_retain_for_indexes: Some(1000),
1994            ..Default::default()
1995        };
1996        assert!(config.check_index_retention_within_ledger().is_ok());
1997    }
1998
1999    #[test]
2000    fn unset_ledger_retention_keeps_the_default_and_the_index_follows_it() {
2001        let config = AuthorityStorePruningConfig {
2002            num_epochs_to_retain_for_checkpoints: None,
2003            num_epochs_to_retain_for_indexes: None,
2004            ..Default::default()
2005        };
2006        assert_eq!(
2007            config.num_epochs_to_retain_for_checkpoints(),
2008            Some(DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS)
2009        );
2010        assert_eq!(
2011            config.num_epochs_to_retain_for_indexes(),
2012            Some(DEFAULT_NUM_EPOCHS_TO_RETAIN_FOR_CHECKPOINTS)
2013        );
2014        assert!(config.check_index_retention_within_ledger().is_ok());
2015    }
2016
2017    #[test]
2018    fn unset_index_retention_follows_the_ledger() {
2019        let config = AuthorityStorePruningConfig {
2020            num_epochs_to_retain_for_checkpoints: Some(3),
2021            num_epochs_to_retain_for_indexes: None,
2022            ..Default::default()
2023        };
2024        assert_eq!(config.num_epochs_to_retain_for_indexes(), Some(3));
2025        assert!(config.check_index_retention_within_ledger().is_ok());
2026    }
2027
2028    #[test]
2029    fn index_pruning_turned_off_against_a_bounded_ledger_is_refused() {
2030        // `u64::MAX` is index pruning off, which outlives any bounded ledger.
2031        let config = AuthorityStorePruningConfig {
2032            num_epochs_to_retain_for_checkpoints: Some(2),
2033            num_epochs_to_retain_for_indexes: Some(u64::MAX),
2034            ..Default::default()
2035        };
2036        let err = config
2037            .check_index_retention_within_ledger()
2038            .unwrap_err()
2039            .to_string();
2040        assert!(err.contains("unlimited"), "{err}");
2041    }
2042
2043    #[test]
2044    fn max_retention_means_pruning_is_off_for_every_knob() {
2045        let config = AuthorityStorePruningConfig {
2046            num_epochs_to_retain: u64::MAX,
2047            num_epochs_to_retain_for_checkpoints: Some(u64::MAX),
2048            num_epochs_to_retain_for_indexes: Some(u64::MAX),
2049            ..Default::default()
2050        };
2051        assert_eq!(config.num_epochs_to_retain(), None);
2052        assert_eq!(config.num_epochs_to_retain_for_checkpoints(), None);
2053        assert_eq!(config.num_epochs_to_retain_for_indexes(), None);
2054    }
2055
2056    #[test]
2057    fn a_fullnode_serving_an_api_builds_the_indexes() {
2058        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
2059
2060        let config: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2061        assert!(config.maintains_rpc_indexes());
2062    }
2063
2064    #[test]
2065    fn a_fullnode_with_both_apis_off_builds_no_indexes() {
2066        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
2067
2068        let mut config: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2069        config.enable_jsonrpc_api = false;
2070        config.enable_grpc_api = false;
2071        assert!(!config.maintains_rpc_indexes());
2072    }
2073
2074    #[test]
2075    fn a_validator_builds_no_indexes_even_with_an_api_enabled() {
2076        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
2077
2078        let mut config: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2079        config.enable_jsonrpc_api = true;
2080        config.consensus_config = Some(ConsensusConfig {
2081            db_path: PathBuf::from("/tmp/consensus"),
2082            db_retention_epochs: None,
2083            db_pruner_period_secs: None,
2084            max_pending_transactions: None,
2085            max_submit_position: None,
2086            submit_delay_step_override_millis: None,
2087            parameters: None,
2088            graduated_load_shedding_soft_limit_pct: None,
2089        });
2090        assert!(!config.maintains_rpc_indexes());
2091    }
2092
2093    #[test]
2094    fn serialize_genesis_from_file() {
2095        let g = Genesis::new_from_file("path/to/file");
2096
2097        let s = serde_yaml::to_string(&g).unwrap();
2098        assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
2099        let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
2100        assert_eq!(g, loaded_genesis);
2101    }
2102
2103    #[test]
2104    fn fullnode_template() {
2105        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
2106
2107        let _template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2108    }
2109
2110    #[test]
2111    fn validate_requires_a_grpc_config_when_the_api_is_enabled() {
2112        // An absent key gives the default config, so only an explicit `null`
2113        // reaches this rule.
2114        let mut config = template_config();
2115        config.grpc_api_config = None;
2116        config.validate().unwrap();
2117
2118        config.enable_grpc_api = true;
2119        let err = config.validate().unwrap_err().to_string();
2120        assert!(err.contains("`grpc-api-config` is `null`"), "{err}");
2121
2122        config.grpc_api_config = Some(GrpcApiConfig::default());
2123        config.validate().unwrap();
2124
2125        // Validators do not expose the gRPC API, so the config check does
2126        // not apply to them and the API itself is rejected instead.
2127        config.grpc_api_config = None;
2128        config.consensus_config = Some(consensus_config());
2129        let err = config.validate().unwrap_err().to_string();
2130        assert!(err.contains("validators do not expose"), "{err}");
2131    }
2132
2133    #[test]
2134    fn validate_rejects_the_grpc_api_on_a_validator() {
2135        let mut config = template_config();
2136        config.consensus_config = Some(consensus_config());
2137        config.validate().unwrap();
2138
2139        config.enable_grpc_api = true;
2140        let err = config.validate().unwrap_err().to_string();
2141        assert!(err.contains("validators do not expose"), "{err}");
2142
2143        // The same config is valid for a fullnode.
2144        config.consensus_config = None;
2145        config.validate().unwrap();
2146    }
2147
2148    #[test]
2149    fn validate_rejects_snapshot_upload_on_a_validator() {
2150        let mut config = template_config();
2151        config.state_snapshot_write_config.object_store_config = Some(object_store_config());
2152        config.validate().unwrap();
2153
2154        config.consensus_config = Some(consensus_config());
2155        let err = config.validate().unwrap_err().to_string();
2156        assert!(err.contains("snapshot upload"), "{err}");
2157    }
2158
2159    #[test]
2160    fn validate_rejects_a_snapshot_store_without_a_backend() {
2161        let mut config = template_config();
2162        config.state_snapshot_write_config.object_store_config = Some(ObjectStoreConfig::default());
2163
2164        let err = config.validate().unwrap_err().to_string();
2165        assert!(err.contains("storage backend"), "{err}");
2166    }
2167
2168    #[test]
2169    fn validate_rejects_a_zero_checkpoint_retention() {
2170        let mut config = template_config();
2171        config
2172            .authority_store_pruning_config
2173            .set_num_epochs_to_retain_for_checkpoints(Some(0));
2174        let err = config.validate().unwrap_err().to_string();
2175        assert!(
2176            err.contains("num-epochs-to-retain-for-checkpoints"),
2177            "{err}"
2178        );
2179
2180        config
2181            .authority_store_pruning_config
2182            .set_num_epochs_to_retain_for_checkpoints(Some(1));
2183        config.validate().unwrap();
2184    }
2185
2186    /// The keys of the listeners' limits sections in a node config.
2187    const LISTENER_LIMITS_KEYS: [&str; 3] = [
2188        "validator-grpc-listener-limits",
2189        "json-rpc-listener-limits",
2190        "grpc-api-listener-limits",
2191    ];
2192
2193    /// The template config with `limits`, a YAML mapping, under `key`.
2194    fn config_with_listener_limits(key: &str, limits: &str) -> NodeConfig {
2195        serde_yaml::from_str(&format!("{TEMPLATE}\n{key}: {limits}\n")).unwrap()
2196    }
2197
2198    /// The pending-connection limit is on unless turned off, and only the
2199    /// handshake timeout frees its slots, so the timeout can be turned off
2200    /// only together with it. The template is a fullnode's, so this also
2201    /// covers the validator listener it starts only once it joins the
2202    /// committee.
2203    #[test]
2204    fn validate_rejects_a_handshake_timeout_turned_off_alone() {
2205        for key in LISTENER_LIMITS_KEYS {
2206            for limits in [
2207                "{handshake-timeout: null}",
2208                "{handshake-timeout: null, max-pending-connections: 1}",
2209            ] {
2210                let err = config_with_listener_limits(key, limits)
2211                    .validate()
2212                    .unwrap_err()
2213                    .to_string();
2214                assert!(
2215                    err.contains(&format!("`{key}.handshake-timeout` is `null`")),
2216                    "{limits}: {err}"
2217                );
2218            }
2219            config_with_listener_limits(
2220                key,
2221                "{handshake-timeout: null, max-pending-connections: null}",
2222            )
2223            .validate()
2224            .unwrap();
2225        }
2226    }
2227
2228    /// A zero deadline is reached at once and a zero interval never pauses,
2229    /// so the listener refuses either.
2230    #[test]
2231    fn validate_rejects_a_zero_listener_duration() {
2232        for key in LISTENER_LIMITS_KEYS {
2233            for name in [
2234                "handshake-timeout",
2235                "max-connection-idle",
2236                "http2-keepalive-interval",
2237                "http1-header-read-timeout",
2238            ] {
2239                let err =
2240                    config_with_listener_limits(key, &format!("{{{name}: {{secs: 0, nanos: 0}}}}"))
2241                        .validate()
2242                        .unwrap_err()
2243                        .to_string();
2244                assert!(err.contains(&format!("`{key}.{name}` is 0")), "{err}");
2245
2246                config_with_listener_limits(key, &format!("{{{name}: {{secs: 1, nanos: 0}}}}"))
2247                    .validate()
2248                    .unwrap();
2249            }
2250        }
2251    }
2252
2253    /// The listener accepts IPv6 prefix lengths from 32 to 128 bits.
2254    #[test]
2255    fn validate_rejects_an_ipv6_prefix_len_out_of_range() {
2256        for key in LISTENER_LIMITS_KEYS {
2257            for len in [0, 31, 129] {
2258                let err = config_with_listener_limits(key, &format!("{{ipv6-prefix-len: {len}}}"))
2259                    .validate()
2260                    .unwrap_err()
2261                    .to_string();
2262                assert!(
2263                    err.contains(&format!("`{key}.ipv6-prefix-len` is {len}")),
2264                    "{err}"
2265                );
2266            }
2267            for len in [32, 128] {
2268                config_with_listener_limits(key, &format!("{{ipv6-prefix-len: {len}}}"))
2269                    .validate()
2270                    .unwrap();
2271            }
2272        }
2273    }
2274
2275    #[test]
2276    fn enable_soft_locking_defaults_to_enabled() {
2277        // The template omits `enable-soft-locking`, so this exercises the serde
2278        // default and pins the documented "default: enabled" contract.
2279        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
2280
2281        let config: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2282        assert!(config.enable_soft_locking);
2283    }
2284
2285    #[test]
2286    fn load_key_pairs_to_node_config() {
2287        let authority_key_pair: AuthorityKeyPair =
2288            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
2289        let protocol_private_key: NetworkPrivateKey =
2290            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
2291        let network_private_key: NetworkPrivateKey =
2292            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
2293
2294        write_authority_keypair_to_file(&authority_key_pair, PathBuf::from("authority.key"))
2295            .unwrap();
2296        write_keypair_to_file(
2297            &SimpleKeypair::from(protocol_private_key.clone()),
2298            PathBuf::from("protocol.key"),
2299        )
2300        .unwrap();
2301        write_keypair_to_file(
2302            &SimpleKeypair::from(network_private_key.clone()),
2303            PathBuf::from("network.key"),
2304        )
2305        .unwrap();
2306
2307        const TEMPLATE: &str = include_str!("../data/fullnode-template-with-path.yaml");
2308        let template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
2309        assert_eq!(
2310            template.authority_key_pair().public(),
2311            authority_key_pair.public()
2312        );
2313        assert_eq!(
2314            template.network_private_key().public_key(),
2315            network_private_key.public_key()
2316        );
2317        assert_eq!(
2318            template.protocol_private_key().public_key(),
2319            protocol_private_key.public_key()
2320        );
2321    }
2322
2323    #[test]
2324    fn a_policy_config_survives_a_round_trip_in_all_three_states() {
2325        let mut config = template_config();
2326
2327        config.policy_config = None;
2328        assert!(round_trip(&config).policy_config.is_none());
2329
2330        config.policy_config = default_traffic_controller_policy_config();
2331        assert_eq!(
2332            as_yaml(&round_trip(&config).policy_config),
2333            as_yaml(&default_traffic_controller_policy_config())
2334        );
2335
2336        let configured = PolicyConfig {
2337            dry_run: !PolicyConfig::default_dos_protection_policy().dry_run,
2338            ..PolicyConfig::default_dos_protection_policy()
2339        };
2340        config.policy_config = Some(configured.clone());
2341        assert_eq!(
2342            as_yaml(&round_trip(&config).policy_config),
2343            as_yaml(&Some(configured))
2344        );
2345    }
2346
2347    #[test]
2348    fn a_grpc_api_config_survives_a_round_trip_in_all_three_states() {
2349        let mut config = template_config();
2350
2351        config.grpc_api_config = None;
2352        assert!(round_trip(&config).grpc_api_config.is_none());
2353
2354        config.grpc_api_config = default_grpc_api_config();
2355        assert_eq!(
2356            as_yaml(&round_trip(&config).grpc_api_config),
2357            as_yaml(&default_grpc_api_config())
2358        );
2359
2360        let configured = GrpcApiConfig {
2361            max_message_size_bytes: 1234,
2362            ..GrpcApiConfig::default()
2363        };
2364        config.grpc_api_config = Some(configured.clone());
2365        assert_eq!(
2366            as_yaml(&round_trip(&config).grpc_api_config),
2367            as_yaml(&Some(configured))
2368        );
2369    }
2370
2371    #[test]
2372    fn the_default_pruning_config_agrees_with_the_serde_default() {
2373        assert_eq!(
2374            super::AuthorityStorePruningConfig::default().periodic_compaction_threshold_days,
2375            default_periodic_compaction_threshold_days()
2376        );
2377    }
2378
2379    #[test]
2380    fn a_compaction_threshold_survives_a_round_trip_in_all_three_states() {
2381        let mut config = template_config();
2382
2383        for state in [None, default_periodic_compaction_threshold_days(), Some(7)] {
2384            config
2385                .authority_store_pruning_config
2386                .periodic_compaction_threshold_days = state;
2387            assert_eq!(
2388                round_trip(&config)
2389                    .authority_store_pruning_config
2390                    .periodic_compaction_threshold_days,
2391                state
2392            );
2393        }
2394    }
2395
2396    #[test]
2397    fn a_default_value_is_omitted_and_a_disabled_one_is_written_as_null() {
2398        let mut config = template_config();
2399        config.policy_config = default_traffic_controller_policy_config();
2400        config.grpc_api_config = default_grpc_api_config();
2401        config
2402            .authority_store_pruning_config
2403            .periodic_compaction_threshold_days = default_periodic_compaction_threshold_days();
2404
2405        let written = serde_yaml::to_value(&config).unwrap();
2406        assert_eq!(written_at(&written, POLICY_CONFIG), None);
2407        assert_eq!(written_at(&written, GRPC_API_CONFIG), None);
2408        assert_eq!(written_at(&written, COMPACTION_THRESHOLD), None);
2409
2410        config.policy_config = None;
2411        config.grpc_api_config = None;
2412        config
2413            .authority_store_pruning_config
2414            .periodic_compaction_threshold_days = None;
2415
2416        let written = serde_yaml::to_value(&config).unwrap();
2417        assert_eq!(written_at(&written, POLICY_CONFIG), Some(Value::Null));
2418        assert_eq!(written_at(&written, GRPC_API_CONFIG), Some(Value::Null));
2419        assert_eq!(
2420            written_at(&written, COMPACTION_THRESHOLD),
2421            Some(Value::Null)
2422        );
2423    }
2424
2425    fn limits(yaml: &str) -> ListenerLimitsConfig {
2426        serde_yaml::from_str(yaml).unwrap()
2427    }
2428
2429    #[test]
2430    fn a_node_config_without_listener_limits_leaves_every_limit_unset() {
2431        let config = template_config();
2432        assert_eq!(
2433            config.validator_grpc_listener_limits,
2434            ListenerLimitsConfig::default()
2435        );
2436        assert_eq!(
2437            config.json_rpc_listener_limits,
2438            ListenerLimitsConfig::default()
2439        );
2440        assert_eq!(
2441            config.grpc_api_listener_limits,
2442            ListenerLimitsConfig::default()
2443        );
2444    }
2445
2446    #[test]
2447    fn a_listener_limit_left_out_is_unset_and_null_disables_it() {
2448        assert_eq!(
2449            limits("max-connections: 30\nmax-connection-idle: null"),
2450            ListenerLimitsConfig {
2451                max_connections: Setting::Value(NonZeroUsize::new(30).unwrap()),
2452                max_connection_idle: Setting::Disabled,
2453                ..Default::default()
2454            }
2455        );
2456    }
2457
2458    /// Every value is distinct, so a key read into the wrong field fails here.
2459    #[test]
2460    fn every_listener_limit_is_read_from_its_own_key() {
2461        let read = limits(
2462            "
2463            handshake-timeout: {secs: 1, nanos: 0}
2464            max-pending-connections: 2
2465            max-connections: 3
2466            max-connections-per-client-ip: 4
2467            ipv6-prefix-len: 48
2468            max-connection-idle: {secs: 5, nanos: 0}
2469            max-concurrent-streams: 6
2470            http2-keepalive-interval: {secs: 7, nanos: 0}
2471            http1-header-read-timeout: {secs: 8, nanos: 0}
2472            ",
2473        );
2474        assert_eq!(
2475            read,
2476            ListenerLimitsConfig {
2477                handshake_timeout: Setting::Value(Duration::from_secs(1)),
2478                max_pending_connections: Setting::Value(NonZeroUsize::new(2).unwrap()),
2479                max_connections: Setting::Value(NonZeroUsize::new(3).unwrap()),
2480                max_connections_per_client_ip: Setting::Value(NonZeroUsize::new(4).unwrap()),
2481                ipv6_prefix_len: Some(48),
2482                max_connection_idle: Setting::Value(Duration::from_secs(5)),
2483                max_concurrent_streams: Setting::Value(NonZeroU32::new(6).unwrap()),
2484                http2_keepalive_interval: Setting::Value(Duration::from_secs(7)),
2485                http1_header_read_timeout: Setting::Value(Duration::from_secs(8)),
2486            }
2487        );
2488    }
2489
2490    #[test]
2491    fn every_listener_limit_can_be_disabled() {
2492        let read = limits(
2493            "
2494            handshake-timeout: null
2495            max-pending-connections: null
2496            max-connections: null
2497            max-connections-per-client-ip: null
2498            ipv6-prefix-len: null
2499            max-connection-idle: null
2500            max-concurrent-streams: null
2501            http2-keepalive-interval: null
2502            http1-header-read-timeout: null
2503            ",
2504        );
2505        assert_eq!(
2506            read,
2507            ListenerLimitsConfig {
2508                handshake_timeout: Setting::Disabled,
2509                max_pending_connections: Setting::Disabled,
2510                max_connections: Setting::Disabled,
2511                max_connections_per_client_ip: Setting::Disabled,
2512                ipv6_prefix_len: None,
2513                max_connection_idle: Setting::Disabled,
2514                max_concurrent_streams: Setting::Disabled,
2515                http2_keepalive_interval: Setting::Disabled,
2516                http1_header_read_timeout: Setting::Disabled,
2517            }
2518        );
2519    }
2520
2521    #[test]
2522    fn listener_limits_are_written_only_when_set() {
2523        let mut config = template_config();
2524        let written = serde_yaml::to_value(&config).unwrap();
2525        for key in [
2526            "validator-grpc-listener-limits",
2527            "json-rpc-listener-limits",
2528            "grpc-api-listener-limits",
2529        ] {
2530            assert_eq!(written_at(&written, &[key]), None, "{key} was written");
2531        }
2532
2533        config.json_rpc_listener_limits.max_connections = Setting::Value(NonZeroUsize::MIN);
2534        config.grpc_api_listener_limits.max_connection_idle = Setting::Disabled;
2535        let read_back = round_trip(&config);
2536        assert_eq!(
2537            read_back.json_rpc_listener_limits,
2538            config.json_rpc_listener_limits
2539        );
2540        assert_eq!(
2541            read_back.grpc_api_listener_limits,
2542            config.grpc_api_listener_limits
2543        );
2544        assert!(read_back.validator_grpc_listener_limits.is_unset());
2545    }
2546
2547    /// With a limit of zero attestation dry runs at once, a validator could
2548    /// attest no transaction, so a config with that limit fails to load.
2549    #[test]
2550    fn config_with_zero_concurrent_attestations_fails_to_load() {
2551        let zero = serde_yaml::from_str::<super::AuthorityOverloadConfig>(
2552            "max-concurrent-attestations: 0",
2553        );
2554        assert!(zero.is_err(), "{zero:?}");
2555
2556        let one: super::AuthorityOverloadConfig =
2557            serde_yaml::from_str("max-concurrent-attestations: 1").unwrap();
2558        assert_eq!(one.max_concurrent_attestations, NonZeroUsize::new(1));
2559    }
2560}
2561
2562// RunWithRange is used to specify the ending epoch/checkpoint to process.
2563// this is intended for use with disaster recovery debugging and verification
2564// workflows, never in normal operations
2565#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
2566pub enum RunWithRange {
2567    Epoch(EpochId),
2568    Checkpoint(CheckpointSequenceNumber),
2569}
2570
2571impl RunWithRange {
2572    // is epoch_id > RunWithRange::Epoch
2573    pub fn is_epoch_gt(&self, epoch_id: EpochId) -> bool {
2574        matches!(self, RunWithRange::Epoch(e) if epoch_id > *e)
2575    }
2576
2577    pub fn matches_checkpoint(&self, seq_num: CheckpointSequenceNumber) -> bool {
2578        matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
2579    }
2580
2581    pub fn into_checkpoint_bound(self) -> Option<CheckpointSequenceNumber> {
2582        match self {
2583            RunWithRange::Epoch(_) => None,
2584            RunWithRange::Checkpoint(seq) => Some(seq),
2585        }
2586    }
2587}
2588
2589/// A serde helper module used with #[serde(with = "...")] to change the
2590/// de/serialization format of an `SimpleKeypair` to Bech32 when written to or
2591/// read from a node config.
2592mod bech32_formatted_keypair {
2593    use std::ops::Deref;
2594
2595    use fastcrypto::encoding::{Base64, Encoding};
2596    use iota_sdk_crypto::{ToFromBech32, simple::SimpleKeypair};
2597    use serde::{Deserialize, Deserializer, Serializer};
2598
2599    pub fn serialize<S, T>(kp: &T, serializer: S) -> Result<S::Ok, S::Error>
2600    where
2601        S: Serializer,
2602        T: Deref<Target = SimpleKeypair>,
2603    {
2604        use serde::ser::Error;
2605
2606        // Serialize the keypair to a Bech32 string
2607        let s = kp.to_bech32().map_err(Error::custom)?;
2608
2609        serializer.serialize_str(&s)
2610    }
2611
2612    pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
2613    where
2614        D: Deserializer<'de>,
2615        T: From<SimpleKeypair>,
2616    {
2617        use serde::de::Error;
2618
2619        let s = String::deserialize(deserializer)?;
2620
2621        // Try to deserialize the keypair from a Bech32 formatted string
2622        SimpleKeypair::from_bech32(&s)
2623            .map_err(Error::custom)
2624            .or_else(|_: D::Error| {
2625                // For backwards compatibility try Base64 if Bech32 failed
2626                let bytes = Base64::decode(&s).map_err(Error::custom)?;
2627                SimpleKeypair::from_bytes(&bytes).map_err(Error::custom)
2628            })
2629            .map(Into::into)
2630    }
2631}