Skip to main content

iota_config/
node.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{
6    net::{IpAddr, Ipv4Addr, SocketAddr},
7    num::NonZeroUsize,
8    path::{Path, PathBuf},
9    sync::Arc,
10    time::Duration,
11};
12
13use anyhow::Result;
14use fastcrypto::ed25519::Ed25519KeyPair;
15use iota_keys::keypair_file::{read_authority_keypair_from_file, read_keypair_from_file};
16use iota_metrics::MetricGroups;
17use iota_multiaddr::Multiaddr;
18use iota_names::config::IotaNamesConfig;
19use iota_sdk_crypto::simple::SimpleKeypair;
20use iota_sdk_types::Address;
21use iota_types::{
22    committee::EpochId,
23    crypto::{
24        AccountKeyPair, AuthorityKeyPair, AuthorityPublicKeyBytes, KeypairTraits, NetworkKeyPair,
25        get_key_pair_from_rng, simple_to_network_keypair,
26    },
27    messages_checkpoint::CheckpointSequenceNumber,
28    supported_protocol_versions::{Chain, SupportedProtocolVersions},
29    traffic_control::{PolicyConfig, RemoteFirewallConfig},
30};
31use once_cell::sync::OnceCell;
32use rand::rngs::OsRng;
33use serde::{Deserialize, Serialize};
34use starfish_config::Parameters as StarfishParameters;
35use tracing::info;
36
37use crate::{
38    Config, certificate_deny_config::CertificateDenyConfig, genesis,
39    migration_tx_data::MigrationTxData, object_storage_config::ObjectStoreConfig, p2p::P2pConfig,
40    transaction_deny_config::TransactionDenyConfig, verifier_signing_config::VerifierSigningConfig,
41};
42
43/// Default gas price of 1000 Nanos
44pub const DEFAULT_VALIDATOR_GAS_PRICE: u64 = iota_types::transaction::DEFAULT_VALIDATOR_GAS_PRICE;
45
46/// Default commission rate of 2%
47pub const DEFAULT_COMMISSION_RATE: u64 = 200;
48
49/// Default budget in MiB for the in-memory full-checkpoint-contents cache.
50pub const DEFAULT_FULL_CHECKPOINT_CONTENTS_CACHE_SIZE_MB: usize = 1024;
51
52#[derive(Clone, Debug, Deserialize, Serialize)]
53#[serde(rename_all = "kebab-case")]
54pub struct NodeConfig {
55    /// The public key bytes corresponding to the private key that the validator
56    /// holds to sign transactions.
57    #[serde(default = "default_authority_key_pair")]
58    pub authority_key_pair: AuthorityKeyPairWithPath,
59    /// The public key bytes corresponding to the private key that the validator
60    /// holds to sign consensus blocks.
61    #[serde(default = "default_key_pair")]
62    pub protocol_key_pair: KeyPairWithPath,
63    #[serde(default = "default_key_pair")]
64    pub account_key_pair: KeyPairWithPath,
65    /// The public key bytes corresponding to the private key that the validator
66    /// uses to establish TLS connections.
67    #[serde(default = "default_key_pair")]
68    pub network_key_pair: KeyPairWithPath,
69    pub db_path: PathBuf,
70
71    /// The network address for gRPC communication.
72    ///
73    /// Can be overwritten with args `listen-address` parameters.
74    #[serde(default = "default_grpc_address")]
75    pub network_address: Multiaddr,
76    #[serde(default = "default_json_rpc_address")]
77    pub json_rpc_address: SocketAddr,
78
79    /// The address for Prometheus metrics.
80    #[serde(default = "default_metrics_address")]
81    pub metrics_address: SocketAddr,
82
83    /// The address for the admin interface that is
84    /// run in the metrics separate runtime and provides access to
85    /// admin node commands such as logging and tracing options.
86    #[serde(default = "default_admin_interface_address")]
87    pub admin_interface_address: SocketAddr,
88
89    /// Configuration struct for the consensus.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub consensus_config: Option<ConsensusConfig>,
92
93    /// Flag to enable index processing for a full node.
94    ///
95    /// If set to true, node creates `IndexStore` for transaction
96    /// data including ownership and balance information.
97    #[serde(default = "default_enable_index_processing")]
98    pub enable_index_processing: bool,
99
100    // only allow websocket connections for jsonrpc traffic
101    #[serde(default)]
102    /// Determines the jsonrpc server type as either:
103    /// - 'websocket' for a websocket based service (deprecated)
104    /// - 'http' for an http based service
105    /// - 'both' for both a websocket and http based service (deprecated)
106    pub jsonrpc_server_type: Option<ServerType>,
107
108    /// Flag to enable gRPC load shedding: requests over a service's
109    /// concurrency limit are rejected immediately with `RESOURCE_EXHAUSTED`
110    /// instead of waiting for a slot.
111    #[serde(default)]
112    pub grpc_load_shed: Option<bool>,
113
114    /// Maximum number of concurrent in-flight requests per CPU core, applied
115    /// to each service of the validator gRPC server separately (`Validator`,
116    /// `ValidatorV2`, `ValidatorPeer`), so a flood of client transaction
117    /// submissions cannot crowd validator-peer RPCs out of admission slots.
118    /// The effective per-service limit is this value multiplied by the CPU
119    /// cores of the machine at server startup, so the same config file scales
120    /// with the hardware.
121    ///
122    /// Most request time is spent awaiting locks, I/O or consensus rather
123    /// than on-CPU, so the default ceiling is generous: it bounds total
124    /// in-flight work so a request flood cannot grow queues and memory
125    /// without limit, it does not throttle normal load. Operators wanting
126    /// hard load-shedding can lower it and set `grpc_load_shed`.
127    ///
128    /// A value of zero is rejected at config load: it would not disable the
129    /// limit, it would block every request.
130    #[serde(default = "default_grpc_concurrency_limit_per_core")]
131    pub grpc_concurrency_limit_per_core: NonZeroUsize,
132
133    /// Configuration struct for P2P.
134    #[serde(default)]
135    pub p2p_config: P2pConfig,
136
137    /// Contains genesis location that might be `InPlace`
138    /// for reading all genesis data to memory or `InFile`,
139    /// and `OnceCell` pointer to a genesis struct.
140    pub genesis: Genesis,
141
142    /// Contains the path where to find the migration blob.
143    pub migration_tx_data_path: Option<PathBuf>,
144
145    /// Configuration for pruning of the authority store, to define when
146    /// an old data is removed from the storage space.
147    #[serde(default = "default_authority_store_pruning_config")]
148    pub authority_store_pruning_config: AuthorityStorePruningConfig,
149
150    /// Size of the broadcast channel used for notifying other systems of end of
151    /// epoch.
152    ///
153    /// If unspecified, this will default to `128`.
154    #[serde(default = "default_end_of_epoch_broadcast_channel_capacity")]
155    pub end_of_epoch_broadcast_channel_capacity: usize,
156
157    /// Configuration for the checkpoint executor for limiting
158    /// the number of checkpoints to execute concurrently,
159    /// and to allow for checkpoint post-processing.
160    #[serde(default)]
161    pub checkpoint_executor_config: CheckpointExecutorConfig,
162
163    #[serde(skip_serializing_if = "Option::is_none")]
164    pub metrics: Option<MetricsConfig>,
165
166    /// In a `iota-node` binary, this is set to
167    /// SupportedProtocolVersions::SYSTEM_DEFAULT in iota-node/src/main.rs.
168    /// It is present in the config so that it can be changed by tests in
169    /// order to test protocol upgrades.
170    #[serde(skip)]
171    pub supported_protocol_versions: Option<SupportedProtocolVersions>,
172
173    /// Configuration for enabling/disabling expensive safety checks.
174    #[serde(default)]
175    pub expensive_safety_check_config: ExpensiveSafetyCheckConfig,
176
177    /// Configuration to specify rules for denying transactions
178    /// based on `objectsIDs`, `addresses`, or enable/disable many
179    /// features such as publishing new packages or using shared objects.
180    #[serde(default)]
181    pub transaction_deny_config: TransactionDenyConfig,
182
183    /// Config used to deny execution for certificate digests
184    /// know for crashing or hanging validator nodes.
185    ///
186    /// Should be used for a fast temporary fixes and
187    /// removed once the issue is fixed.
188    #[serde(default)]
189    pub certificate_deny_config: CertificateDenyConfig,
190
191    /// Used to determine how state debug information is dumped
192    /// when a node forks.
193    #[serde(default)]
194    pub state_debug_dump_config: StateDebugDumpConfig,
195
196    #[serde(default)]
197    pub checkpoint_archive_config: Option<CheckpointArchiveConfig>,
198
199    /// Determines if snapshot should be uploaded to the remote storage.
200    #[serde(default)]
201    pub state_snapshot_write_config: StateSnapshotConfig,
202
203    #[serde(default)]
204    pub indexer_max_subscriptions: Option<usize>,
205
206    #[serde(default = "default_transaction_kv_store_config")]
207    pub transaction_kv_store_read_config: TransactionKeyValueStoreReadConfig,
208
209    // TODO: write config seem to be unused.
210    #[serde(skip_serializing_if = "Option::is_none")]
211    pub transaction_kv_store_write_config: Option<TransactionKeyValueStoreWriteConfig>,
212
213    /// Configuration for defining thresholds and settings
214    /// for managing system overload conditions in a node.
215    #[serde(default = "default_authority_overload_config")]
216    pub authority_overload_config: AuthorityOverloadConfig,
217
218    /// Specifies the ending epoch for a node for debugging purposes.
219    ///
220    ///  Ignored if set by config, can be configured only by cli arguments.
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub run_with_range: Option<RunWithRange>,
223
224    /// Traffic control policy. For killswitch use None.
225    ///
226    /// With the key absent the default denial-of-service protection policy
227    /// applies, an explicit `null` turns traffic control off, and a value
228    /// configures it.
229    #[serde(
230        skip_serializing_if = "is_default_traffic_controller_policy_config",
231        default = "default_traffic_controller_policy_config"
232    )]
233    pub policy_config: Option<PolicyConfig>,
234
235    #[serde(skip_serializing_if = "Option::is_none")]
236    pub firewall_config: Option<RemoteFirewallConfig>,
237
238    #[serde(default)]
239    pub execution_cache_config: ExecutionCacheConfig,
240
241    /// Memory budget in MiB for the in-memory cache of full checkpoint
242    /// contents, which serves the checkpoint executor's bulk transaction
243    /// loads and checkpoint-contents requests from state-sync peers. When
244    /// the budget is exceeded, the oldest checkpoints are evicted first.
245    /// Set to 0 to disable the cache; consumers then fall back to
246    /// reconstructing contents from the transaction and effects stores.
247    ///
248    /// The budget is accounted in serialized (BCS) bytes; the resident
249    /// memory of a full cache is somewhat higher than the configured value
250    /// due to in-memory representation overhead.
251    #[serde(default = "default_full_checkpoint_contents_cache_size_mb")]
252    pub full_checkpoint_contents_cache_size_mb: usize,
253
254    #[serde(default = "bool_true")]
255    pub enable_validator_tx_finalizer: bool,
256
257    /// Enables the pre-consensus soft-locking mechanism used by the
258    /// certificate-less (pcool) transaction flow (default: enabled).
259    ///
260    /// When disabled, post-consensus validation alone resolves owned-object
261    /// conflicts. Has no effect unless the pcool flow is enabled.
262    #[serde(default = "bool_true")]
263    pub enable_soft_locking: bool,
264
265    #[serde(default)]
266    pub verifier_signing_config: VerifierSigningConfig,
267
268    /// If a value is set, it determines if writes to DB can stall, which can
269    /// halt the whole process. By default, write stall is enabled on
270    /// validators but not on fullnodes.
271    #[serde(skip_serializing_if = "Option::is_none")]
272    pub enable_db_write_stall: Option<bool>,
273
274    #[serde(default, skip_serializing_if = "Option::is_none")]
275    pub iota_names_config: Option<IotaNamesConfig>,
276
277    /// Flag to enable the gRPC API.
278    #[serde(default)]
279    pub enable_grpc_api: bool,
280    /// Configuration of the gRPC API, read when `enable_grpc_api` is set.
281    ///
282    /// With the key absent the default configuration applies, an explicit
283    /// `null` leaves the API unconfigured — a node with `enable-grpc-api` set
284    /// then fails to start — and a value configures it.
285    #[serde(
286        default = "default_grpc_api_config",
287        skip_serializing_if = "is_default_grpc_api_config"
288    )]
289    pub grpc_api_config: Option<GrpcApiConfig>,
290
291    /// Allow overriding the chain for testing purposes. For instance, it allows
292    /// you to create a test network that believes it is mainnet or testnet.
293    /// Attempting to override this value on production networks will result
294    /// in an error.
295    #[serde(skip_serializing_if = "Option::is_none")]
296    pub chain_override_for_testing: Option<Chain>,
297
298    /// Configuration for the validator client monitor that tracks
299    /// client-observed performance metrics for validators.
300    #[serde(default, skip_serializing_if = "Option::is_none")]
301    pub validator_client_monitor_config:
302        Option<crate::validator_client_monitor_config::ValidatorClientMonitorConfig>,
303}
304
305#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
306#[serde(rename_all = "kebab-case")]
307pub struct TlsConfig {
308    /// File path to a PEM formatted TLS certificate chain
309    cert: String,
310    /// File path to a PEM formatted TLS private key
311    key: String,
312}
313
314impl TlsConfig {
315    pub fn cert(&self) -> &str {
316        &self.cert
317    }
318
319    pub fn key(&self) -> &str {
320        &self.key
321    }
322}
323
324/// Configuration for the gRPC API service
325#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
326#[serde(rename_all = "kebab-case")]
327pub struct GrpcApiConfig {
328    /// The address to bind the gRPC server to
329    #[serde(default = "default_grpc_api_address")]
330    pub address: SocketAddr,
331
332    /// TLS configuration for the gRPC server.
333    ///
334    /// If not provided, the gRPC server will use plain TCP without TLS.
335    #[serde(skip_serializing_if = "Option::is_none")]
336    pub tls: Option<TlsConfig>,
337
338    /// Maximum message size for gRPC responses (in bytes)
339    #[serde(default = "default_grpc_api_max_message_size_bytes")]
340    pub max_message_size_bytes: u32,
341
342    /// Buffer size for broadcast channels used for streaming
343    #[serde(default = "default_grpc_api_broadcast_buffer_size")]
344    pub broadcast_buffer_size: u32,
345
346    /// Maximum number of concurrent subscribers to checkpoint streaming RPCs.
347    /// Once the cap is reached, additional subscribe requests are rejected
348    /// with `Unavailable` to protect the server from being overwhelmed by
349    /// unbounded streaming clients. Values below 1 are clamped to 1 at
350    /// server startup.
351    #[serde(default = "default_grpc_api_max_concurrent_stream_subscribers")]
352    pub max_concurrent_stream_subscribers: u32,
353
354    /// Maximum size for Move values when rendering to JSON
355    /// in bytes.
356    #[serde(default = "default_grpc_api_max_json_move_value_size")]
357    pub max_json_move_value_size: usize,
358
359    /// Maximum number of transactions allowed in a single ExecuteTransactions
360    /// batch request.
361    #[serde(default = "default_grpc_api_max_execute_transaction_batch_size")]
362    pub max_execute_transaction_batch_size: u32,
363
364    /// Maximum number of transactions allowed in a single SimulateTransactions
365    /// batch request.
366    #[serde(default = "default_grpc_api_max_simulate_transaction_batch_size")]
367    pub max_simulate_transaction_batch_size: u32,
368
369    /// Maximum number of objects allowed in a single GetObjects batch request.
370    #[serde(default = "default_grpc_api_max_get_objects_batch_size")]
371    pub max_get_objects_batch_size: u32,
372
373    /// Maximum number of transactions allowed in a single GetTransactions batch
374    /// request.
375    #[serde(default = "default_grpc_api_max_get_transactions_batch_size")]
376    pub max_get_transactions_batch_size: u32,
377
378    /// Maximum allowed timeout in milliseconds for waiting for checkpoint
379    /// inclusion in ExecuteTransactions requests. Client-specified timeouts
380    /// are clamped to this value.
381    #[serde(default = "default_grpc_api_max_checkpoint_inclusion_timeout_ms")]
382    pub max_checkpoint_inclusion_timeout_ms: u64,
383}
384
385fn default_grpc_api_address() -> SocketAddr {
386    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 50051)
387}
388
389fn default_grpc_api_broadcast_buffer_size() -> u32 {
390    100
391}
392
393fn default_grpc_api_max_concurrent_stream_subscribers() -> u32 {
394    1024
395}
396
397fn default_grpc_api_max_message_size_bytes() -> u32 {
398    128 * 1024 * 1024 // 128MB
399}
400
401fn default_grpc_api_max_json_move_value_size() -> usize {
402    1024 * 1024 // 1 MB
403}
404
405fn default_grpc_api_max_execute_transaction_batch_size() -> u32 {
406    20
407}
408
409fn default_grpc_api_max_simulate_transaction_batch_size() -> u32 {
410    20
411}
412
413fn default_grpc_api_max_get_objects_batch_size() -> u32 {
414    1000
415}
416
417fn default_grpc_api_max_get_transactions_batch_size() -> u32 {
418    1000
419}
420
421fn default_grpc_api_max_checkpoint_inclusion_timeout_ms() -> u64 {
422    60_000 // 60 seconds
423}
424
425impl Default for GrpcApiConfig {
426    fn default() -> Self {
427        Self {
428            address: default_grpc_api_address(),
429            tls: None,
430            max_message_size_bytes: default_grpc_api_max_message_size_bytes(),
431            broadcast_buffer_size: default_grpc_api_broadcast_buffer_size(),
432            max_concurrent_stream_subscribers: default_grpc_api_max_concurrent_stream_subscribers(),
433            max_json_move_value_size: default_grpc_api_max_json_move_value_size(),
434            max_execute_transaction_batch_size: default_grpc_api_max_execute_transaction_batch_size(
435            ),
436            max_simulate_transaction_batch_size:
437                default_grpc_api_max_simulate_transaction_batch_size(),
438            max_get_objects_batch_size: default_grpc_api_max_get_objects_batch_size(),
439            max_get_transactions_batch_size: default_grpc_api_max_get_transactions_batch_size(),
440            max_checkpoint_inclusion_timeout_ms:
441                default_grpc_api_max_checkpoint_inclusion_timeout_ms(),
442        }
443    }
444}
445
446impl GrpcApiConfig {
447    // The default maximum uncompressed size in bytes for a message, based on
448    // tonic's default.
449    const GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE: u32 = 4 * 1024 * 1024; // 4MB
450    const GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES: u32 =
451        Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE;
452
453    pub fn tls_config(&self) -> Option<&TlsConfig> {
454        self.tls.as_ref()
455    }
456
457    pub fn max_message_size_bytes(&self) -> u32 {
458        // Ensure max message size is at least the minimum allowed
459        self.max_message_size_bytes
460            .max(Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES)
461    }
462
463    /// Calculate the maximum size for a message that can be
464    /// sent to a client, taking into account the client's max message size
465    /// preference.
466    pub fn max_message_size_client_bytes(&self, client_max_message_size_bytes: Option<u32>) -> u32 {
467        client_max_message_size_bytes
468            // if the client did not specify a max message size, use the tonic default receive
469            // message size
470            .unwrap_or(Self::GRPC_TONIC_DEFAULT_MAX_RECV_MESSAGE_SIZE)
471            // clamp the value between the tonic default and the service max message size
472            .clamp(
473                Self::GRPC_MIN_CLIENT_MAX_MESSAGE_SIZE_BYTES,
474                self.max_message_size_bytes(),
475            )
476    }
477}
478
479#[derive(Clone, Debug, Default, Deserialize, Serialize)]
480#[serde(rename_all = "kebab-case")]
481pub struct ExecutionCacheConfig {
482    #[serde(default)]
483    pub writeback_cache: WritebackCacheConfig,
484}
485
486#[derive(Clone, Debug, Default, Deserialize, Serialize)]
487#[serde(rename_all = "kebab-case")]
488pub struct WritebackCacheConfig {
489    /// Maximum number of entries in each cache. (There are several
490    /// different caches).
491    #[serde(default, skip_serializing_if = "Option::is_none")]
492    pub max_cache_size: Option<u64>, // defaults to 100000
493
494    #[serde(default, skip_serializing_if = "Option::is_none")]
495    pub package_cache_size: Option<u64>, // defaults to 1000
496
497    #[serde(default, skip_serializing_if = "Option::is_none")]
498    pub object_cache_size: Option<u64>, // defaults to max_cache_size
499    #[serde(default, skip_serializing_if = "Option::is_none")]
500    pub marker_cache_size: Option<u64>, // defaults to object_cache_size
501    #[serde(default, skip_serializing_if = "Option::is_none")]
502    pub object_by_id_cache_size: Option<u64>, // defaults to object_cache_size
503
504    #[serde(default, skip_serializing_if = "Option::is_none")]
505    pub transaction_cache_size: Option<u64>, // defaults to max_cache_size
506    #[serde(default, skip_serializing_if = "Option::is_none")]
507    pub executed_effect_cache_size: Option<u64>, // defaults to transaction_cache_size
508    #[serde(default, skip_serializing_if = "Option::is_none")]
509    pub effect_cache_size: Option<u64>, // defaults to executed_effect_cache_size
510
511    #[serde(default, skip_serializing_if = "Option::is_none")]
512    pub events_cache_size: Option<u64>, // defaults to transaction_cache_size
513
514    #[serde(default, skip_serializing_if = "Option::is_none")]
515    pub transaction_objects_cache_size: Option<u64>, // defaults to 1000
516
517    /// Number of uncommitted transactions at which to pause consensus
518    /// handler.
519    #[serde(default, skip_serializing_if = "Option::is_none")]
520    pub backpressure_threshold: Option<u64>, // defaults to 100_000
521
522    /// Number of uncommitted transactions at which to refuse new
523    /// transaction submissions. Defaults to backpressure_threshold
524    /// if unset.
525    #[serde(default, skip_serializing_if = "Option::is_none")]
526    pub backpressure_threshold_for_rpc: Option<u64>, // defaults to backpressure_threshold
527
528    /// Percentage of `backpressure_threshold` at which graduated load shedding
529    /// based on writeback-cache pending transaction count begins. The
530    /// locally-calculated shedding percentage increases linearly from 0% at
531    /// `backpressure_threshold * backpressure_soft_limit_pct / 100` up to
532    /// 100% at the `backpressure_threshold` if the cache size continues to
533    /// increase. The calculated shedding percentage is broadcast to other
534    /// validators for a coordinated response. Defaults to 50.
535    #[serde(default, skip_serializing_if = "Option::is_none")]
536    pub backpressure_soft_limit_pct: Option<u32>,
537}
538
539impl WritebackCacheConfig {
540    pub fn max_cache_size(&self) -> u64 {
541        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MAX")
542            .ok()
543            .and_then(|s| s.parse().ok())
544            .or(self.max_cache_size)
545            .unwrap_or(100000)
546    }
547
548    pub fn package_cache_size(&self) -> u64 {
549        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_PACKAGE")
550            .ok()
551            .and_then(|s| s.parse().ok())
552            .or(self.package_cache_size)
553            .unwrap_or(1000)
554    }
555
556    pub fn object_cache_size(&self) -> u64 {
557        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT")
558            .ok()
559            .and_then(|s| s.parse().ok())
560            .or(self.object_cache_size)
561            .unwrap_or_else(|| self.max_cache_size())
562    }
563
564    pub fn marker_cache_size(&self) -> u64 {
565        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_MARKER")
566            .ok()
567            .and_then(|s| s.parse().ok())
568            .or(self.marker_cache_size)
569            .unwrap_or_else(|| self.object_cache_size())
570    }
571
572    pub fn object_by_id_cache_size(&self) -> u64 {
573        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_OBJECT_BY_ID")
574            .ok()
575            .and_then(|s| s.parse().ok())
576            .or(self.object_by_id_cache_size)
577            .unwrap_or_else(|| self.object_cache_size())
578    }
579
580    pub fn transaction_cache_size(&self) -> u64 {
581        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION")
582            .ok()
583            .and_then(|s| s.parse().ok())
584            .or(self.transaction_cache_size)
585            .unwrap_or_else(|| self.max_cache_size())
586    }
587
588    pub fn executed_effect_cache_size(&self) -> u64 {
589        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EXECUTED_EFFECT")
590            .ok()
591            .and_then(|s| s.parse().ok())
592            .or(self.executed_effect_cache_size)
593            .unwrap_or_else(|| self.transaction_cache_size())
594    }
595
596    pub fn effect_cache_size(&self) -> u64 {
597        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EFFECT")
598            .ok()
599            .and_then(|s| s.parse().ok())
600            .or(self.effect_cache_size)
601            .unwrap_or_else(|| self.executed_effect_cache_size())
602    }
603
604    pub fn events_cache_size(&self) -> u64 {
605        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_EVENTS")
606            .ok()
607            .and_then(|s| s.parse().ok())
608            .or(self.events_cache_size)
609            .unwrap_or_else(|| self.transaction_cache_size())
610    }
611
612    pub fn transaction_objects_cache_size(&self) -> u64 {
613        std::env::var("IOTA_CACHE_WRITEBACK_SIZE_TRANSACTION_OBJECTS")
614            .ok()
615            .and_then(|s| s.parse().ok())
616            .or(self.transaction_objects_cache_size)
617            .unwrap_or(1000)
618    }
619
620    pub fn backpressure_threshold(&self) -> u64 {
621        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD")
622            .ok()
623            .and_then(|s| s.parse().ok())
624            .or(self.backpressure_threshold)
625            .unwrap_or(100_000)
626    }
627
628    pub fn backpressure_threshold_for_rpc(&self) -> u64 {
629        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_THRESHOLD_FOR_RPC")
630            .ok()
631            .and_then(|s| s.parse().ok())
632            .or(self.backpressure_threshold_for_rpc)
633            .unwrap_or(self.backpressure_threshold())
634    }
635
636    pub fn backpressure_soft_limit_pct(&self) -> u32 {
637        std::env::var("IOTA_CACHE_WRITEBACK_BACKPRESSURE_SOFT_LIMIT_PCT")
638            .ok()
639            .and_then(|s| s.parse().ok())
640            .or(self.backpressure_soft_limit_pct)
641            .unwrap_or(50)
642            .min(100)
643    }
644}
645
646#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
647#[serde(rename_all = "lowercase")]
648pub enum ServerType {
649    WebSocket,
650    Http,
651    Both,
652}
653
654#[derive(Clone, Debug, Deserialize, Serialize)]
655#[serde(rename_all = "kebab-case")]
656pub struct TransactionKeyValueStoreReadConfig {
657    #[serde(default = "default_base_url")]
658    pub base_url: String,
659
660    #[serde(default = "default_cache_size")]
661    pub cache_size: u64,
662}
663
664impl Default for TransactionKeyValueStoreReadConfig {
665    fn default() -> Self {
666        Self {
667            base_url: default_base_url(),
668            cache_size: default_cache_size(),
669        }
670    }
671}
672
673fn default_base_url() -> String {
674    "".to_string()
675}
676
677fn default_cache_size() -> u64 {
678    100_000
679}
680
681fn default_transaction_kv_store_config() -> TransactionKeyValueStoreReadConfig {
682    TransactionKeyValueStoreReadConfig::default()
683}
684
685fn default_authority_store_pruning_config() -> AuthorityStorePruningConfig {
686    AuthorityStorePruningConfig::default()
687}
688
689pub fn default_enable_index_processing() -> bool {
690    true
691}
692
693fn default_grpc_address() -> Multiaddr {
694    "/ip4/0.0.0.0/tcp/8080".parse().unwrap()
695}
696fn default_authority_key_pair() -> AuthorityKeyPairWithPath {
697    AuthorityKeyPairWithPath::new(get_key_pair_from_rng::<AuthorityKeyPair, _>(&mut OsRng).1)
698}
699
700fn default_key_pair() -> KeyPairWithPath {
701    KeyPairWithPath::new(AccountKeyPair::random().into())
702}
703
704fn default_metrics_address() -> SocketAddr {
705    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
706}
707
708pub fn default_admin_interface_address() -> SocketAddr {
709    SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 1337)
710}
711
712pub fn default_json_rpc_address() -> SocketAddr {
713    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
714}
715
716pub fn default_grpc_api_config() -> Option<GrpcApiConfig> {
717    Some(GrpcApiConfig::default())
718}
719
720fn is_default_grpc_api_config(grpc_api_config: &Option<GrpcApiConfig>) -> bool {
721    serializes_like(grpc_api_config, &default_grpc_api_config())
722}
723
724/// Returns whether `value` and `default` serialize to the same YAML.
725///
726/// Meant for `skip_serializing_if` predicates, which cannot report an error: a
727/// value that fails to serialize is reported as unlike the default, so the
728/// field is kept and the failure surfaces when serializing the field itself.
729fn serializes_like<T: Serialize>(value: &T, default: &T) -> bool {
730    match (serde_yaml::to_string(value), serde_yaml::to_string(default)) {
731        (Ok(value), Ok(default)) => value == default,
732        _ => false,
733    }
734}
735
736pub fn default_grpc_concurrency_limit_per_core() -> NonZeroUsize {
737    NonZeroUsize::new(1000).unwrap()
738}
739
740pub fn default_end_of_epoch_broadcast_channel_capacity() -> usize {
741    128
742}
743
744pub fn default_full_checkpoint_contents_cache_size_mb() -> usize {
745    DEFAULT_FULL_CHECKPOINT_CONTENTS_CACHE_SIZE_MB
746}
747
748pub fn bool_true() -> bool {
749    true
750}
751
752impl Config for NodeConfig {}
753
754impl NodeConfig {
755    pub fn authority_key_pair(&self) -> &AuthorityKeyPair {
756        self.authority_key_pair.authority_keypair()
757    }
758
759    pub fn protocol_key_pair(&self) -> &NetworkKeyPair {
760        self.protocol_key_pair.ed25519_keypair()
761    }
762
763    pub fn network_key_pair(&self) -> &NetworkKeyPair {
764        self.network_key_pair.ed25519_keypair()
765    }
766
767    pub fn authority_public_key(&self) -> AuthorityPublicKeyBytes {
768        self.authority_key_pair().public().into()
769    }
770
771    pub fn db_path(&self) -> PathBuf {
772        self.db_path.join("live")
773    }
774
775    pub fn db_checkpoint_path(&self) -> PathBuf {
776        self.db_path.join("db_checkpoints")
777    }
778
779    pub fn snapshot_path(&self) -> PathBuf {
780        self.db_path.join("snapshot")
781    }
782
783    pub fn network_address(&self) -> &Multiaddr {
784        &self.network_address
785    }
786
787    pub fn consensus_config(&self) -> Option<&ConsensusConfig> {
788        self.consensus_config.as_ref()
789    }
790
791    pub fn genesis(&self) -> Result<&genesis::Genesis> {
792        self.genesis.genesis()
793    }
794
795    pub fn load_migration_tx_data(&self) -> Result<MigrationTxData> {
796        let Some(location) = &self.migration_tx_data_path else {
797            anyhow::bail!("no file location set");
798        };
799
800        // Load from file
801        let migration_tx_data = MigrationTxData::load(location)?;
802
803        // Validate migration content in order to avoid corrupted or malicious data
804        migration_tx_data.validate_from_genesis(self.genesis.genesis()?)?;
805        Ok(migration_tx_data)
806    }
807
808    pub fn iota_address(&self) -> Address {
809        self.account_key_pair
810            .keypair()
811            .public_key()
812            .derive_address()
813    }
814
815    pub fn checkpoint_archive_config(&self) -> Option<&CheckpointArchiveConfig> {
816        self.checkpoint_archive_config.as_ref()
817    }
818
819    pub fn jsonrpc_server_type(&self) -> ServerType {
820        self.jsonrpc_server_type.unwrap_or(ServerType::Http)
821    }
822}
823
824#[derive(Debug, Clone, Deserialize, Serialize)]
825#[serde(rename_all = "kebab-case")]
826pub struct ConsensusConfig {
827    /// Base consensus DB path for all epochs.
828    pub db_path: PathBuf,
829
830    /// The number of epochs for which to retain the consensus DBs.
831    /// Setting it to 0 will make a consensus DB getting dropped
832    /// as soon as system is switched to a new epoch.
833    pub db_retention_epochs: Option<u64>,
834
835    /// Pruner will run on every epoch change but it will also check
836    /// periodically on every `db_pruner_period_secs` seconds to see
837    /// if there are any epoch DBs to remove.
838    pub db_pruner_period_secs: Option<u64>,
839
840    /// Hard limit on the number of pending transactions to submit to
841    /// consensus, including those in submission wait. Used as the upper
842    /// bound for graduated pre-consensus load shedding
843    /// (`graduated_load_shedding_soft_limit_pct`) in the certificate-less
844    /// (P-COOL) mode, and as the threshold for the binary
845    /// cutoff in `ConsensusAdapter::check_consensus_overload()` in both
846    /// certificate-less and certificate-based flows.
847    ///
848    /// Default to 20_000 inflight limit, assuming 20_000 txn tps * 1 sec
849    /// consensus latency.
850    pub max_pending_transactions: Option<usize>,
851
852    /// When defined caps the calculated submission position to the
853    /// max_submit_position.
854    ///
855    /// Even if the is elected to submit from a higher
856    /// position than this, it will "reset" to the max_submit_position.
857    pub max_submit_position: Option<usize>,
858
859    /// The submit delay step to consensus defined in milliseconds.
860    ///
861    /// When provided it will override the current back off logic otherwise the
862    /// default backoff logic will be applied based on consensus latency
863    /// estimates.
864    pub submit_delay_step_override_millis: Option<u64>,
865
866    /// Parameters for Starfish consensus
867    #[serde(skip_serializing_if = "Option::is_none", alias = "starfish_parameters")]
868    pub parameters: Option<StarfishParameters>,
869
870    /// Percentage of `max_pending_transactions` (hard limit) defining the soft
871    /// limit at which graduated pre-consensus load shedding begins. When
872    /// in-flight transactions are at or below the soft limit, no shedding
873    /// occurs; above it, the shedding rate scales linearly from 0% to 100% at
874    /// `max_pending_transactions`. Used in the certificate-less (P-COOL) mode.
875    #[serde(skip_serializing_if = "Option::is_none")]
876    pub graduated_load_shedding_soft_limit_pct: Option<u32>,
877}
878
879impl ConsensusConfig {
880    pub fn db_path(&self) -> &Path {
881        &self.db_path
882    }
883
884    /// Returns the hard limit on the number of pending transactions to submit
885    /// to consensus, including those in submission wait. Defaults to 20_000
886    /// inflight limit, assuming 20_000 txn tps * 1 sec consensus latency.
887    pub fn max_pending_transactions(&self) -> usize {
888        self.max_pending_transactions.unwrap_or(20_000)
889    }
890
891    /// Returns the percentage of `max_pending_transactions` (hard limit)
892    /// defining the soft limit at which graduated pre-consensus load
893    /// shedding begins. Defaults to 50%. Used in the certificate-less
894    /// (P-COOL) mode.
895    pub fn graduated_load_shedding_soft_limit_pct(&self) -> u32 {
896        self.graduated_load_shedding_soft_limit_pct
897            .unwrap_or(50)
898            .min(100)
899    }
900
901    pub fn submit_delay_step_override(&self) -> Option<Duration> {
902        self.submit_delay_step_override_millis
903            .map(Duration::from_millis)
904    }
905
906    pub fn db_retention_epochs(&self) -> u64 {
907        self.db_retention_epochs.unwrap_or(0)
908    }
909
910    pub fn db_pruner_period(&self) -> Duration {
911        // Default to 1 hour
912        self.db_pruner_period_secs
913            .map(Duration::from_secs)
914            .unwrap_or(Duration::from_secs(3_600))
915    }
916}
917
918#[derive(Clone, Debug, Deserialize, Serialize)]
919#[serde(rename_all = "kebab-case")]
920pub struct CheckpointExecutorConfig {
921    /// Upper bound on the number of checkpoints that can be concurrently
922    /// executed.
923    ///
924    /// If unspecified, this will default to `200`
925    #[serde(default = "default_checkpoint_execution_max_concurrency")]
926    pub checkpoint_execution_max_concurrency: usize,
927
928    /// Number of seconds to wait for effects of a batch of transactions
929    /// before logging a warning. Note that we will continue to retry
930    /// indefinitely.
931    ///
932    /// If unspecified, this will default to `10`.
933    #[serde(default = "default_local_execution_timeout_sec")]
934    pub local_execution_timeout_sec: u64,
935
936    /// Optional directory used for data ingestion pipeline.
937    ///
938    /// When specified, each executed checkpoint will be saved in a local
939    /// directory for post-processing
940    #[serde(default, skip_serializing_if = "Option::is_none")]
941    pub data_ingestion_dir: Option<PathBuf>,
942}
943
944#[derive(Clone, Debug, Default, Deserialize, Serialize)]
945#[serde(rename_all = "kebab-case")]
946pub struct ExpensiveSafetyCheckConfig {
947    /// If enabled, at epoch boundary, we will check that the storage
948    /// fund balance is always identical to the sum of the storage
949    /// rebate of all live objects, and that the total IOTA in the network
950    /// remains the same.
951    #[serde(default)]
952    enable_epoch_iota_conservation_check: bool,
953
954    /// If enabled, we will check that the total IOTA in all input objects of a
955    /// tx (both the Move part and the storage rebate) matches the total IOTA
956    /// in all output objects of the tx + gas fees.
957    #[serde(default)]
958    enable_deep_per_tx_iota_conservation_check: bool,
959
960    /// Disable epoch IOTA conservation check even when we are running in debug
961    /// mode.
962    #[serde(default)]
963    force_disable_epoch_iota_conservation_check: bool,
964
965    /// If enabled, at epoch boundary, we will check that the accumulated
966    /// live object state matches the end of epoch root state digest.
967    #[serde(default)]
968    enable_state_consistency_check: bool,
969
970    /// Disable state consistency check even when we are running in debug mode.
971    #[serde(default)]
972    force_disable_state_consistency_check: bool,
973
974    #[serde(default)]
975    enable_secondary_index_checks: bool,
976    // TODO: Add more expensive checks here
977}
978
979impl ExpensiveSafetyCheckConfig {
980    pub fn new_enable_all() -> Self {
981        Self {
982            enable_epoch_iota_conservation_check: true,
983            enable_deep_per_tx_iota_conservation_check: true,
984            force_disable_epoch_iota_conservation_check: false,
985            enable_state_consistency_check: true,
986            force_disable_state_consistency_check: false,
987            enable_secondary_index_checks: false, // Disable by default for now
988        }
989    }
990
991    pub fn new_disable_all() -> Self {
992        Self {
993            enable_epoch_iota_conservation_check: false,
994            enable_deep_per_tx_iota_conservation_check: false,
995            force_disable_epoch_iota_conservation_check: true,
996            enable_state_consistency_check: false,
997            force_disable_state_consistency_check: true,
998            enable_secondary_index_checks: false,
999        }
1000    }
1001
1002    pub fn force_disable_epoch_iota_conservation_check(&mut self) {
1003        self.force_disable_epoch_iota_conservation_check = true;
1004    }
1005
1006    pub fn enable_epoch_iota_conservation_check(&self) -> bool {
1007        (self.enable_epoch_iota_conservation_check || cfg!(debug_assertions))
1008            && !self.force_disable_epoch_iota_conservation_check
1009    }
1010
1011    pub fn force_disable_state_consistency_check(&mut self) {
1012        self.force_disable_state_consistency_check = true;
1013    }
1014
1015    pub fn enable_state_consistency_check(&self) -> bool {
1016        (self.enable_state_consistency_check || cfg!(debug_assertions))
1017            && !self.force_disable_state_consistency_check
1018    }
1019
1020    pub fn enable_deep_per_tx_iota_conservation_check(&self) -> bool {
1021        self.enable_deep_per_tx_iota_conservation_check || cfg!(debug_assertions)
1022    }
1023
1024    pub fn enable_secondary_index_checks(&self) -> bool {
1025        self.enable_secondary_index_checks
1026    }
1027}
1028
1029fn default_checkpoint_execution_max_concurrency() -> usize {
1030    4
1031}
1032
1033fn default_local_execution_timeout_sec() -> u64 {
1034    30
1035}
1036
1037impl Default for CheckpointExecutorConfig {
1038    fn default() -> Self {
1039        Self {
1040            checkpoint_execution_max_concurrency: default_checkpoint_execution_max_concurrency(),
1041            local_execution_timeout_sec: default_local_execution_timeout_sec(),
1042            data_ingestion_dir: None,
1043        }
1044    }
1045}
1046
1047#[derive(Debug, Clone, Deserialize, Serialize)]
1048#[serde(rename_all = "kebab-case")]
1049pub struct AuthorityStorePruningConfig {
1050    /// number of the latest epoch dbs to retain
1051    #[serde(default = "default_num_latest_epoch_dbs_to_retain")]
1052    pub num_latest_epoch_dbs_to_retain: usize,
1053    /// number of epochs to keep the latest version of objects for.
1054    /// Note that a zero value corresponds to an aggressive pruner.
1055    /// This mode is experimental and needs to be used with caution.
1056    /// Use `u64::MAX` to disable the pruner for the objects.
1057    #[serde(default)]
1058    pub num_epochs_to_retain: u64,
1059    /// enables periodic background compaction for old SST files whose last
1060    /// modified time is older than `periodic_compaction_threshold_days`
1061    /// days. That ensures that all sst files eventually go through the
1062    /// compaction process
1063    ///
1064    /// With the key absent files older than a day are compacted, an explicit
1065    /// `null` turns periodic compaction off, and a value sets the threshold in
1066    /// days.
1067    #[serde(
1068        default = "default_periodic_compaction_threshold_days",
1069        skip_serializing_if = "is_default_periodic_compaction_threshold_days"
1070    )]
1071    pub periodic_compaction_threshold_days: Option<usize>,
1072    /// number of epochs to keep the latest version of transactions and effects
1073    /// for
1074    #[serde(skip_serializing_if = "Option::is_none")]
1075    pub num_epochs_to_retain_for_checkpoints: Option<u64>,
1076    /// Enables the compaction filter for pruning the objects table.
1077    /// If disabled, a range deletion approach is used instead.
1078    /// While it is generally safe to switch between the two modes,
1079    /// switching from the compaction filter approach back to range deletion
1080    /// may result in some old versions that will never be pruned.
1081    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1082    pub enable_compaction_filter: bool,
1083    #[serde(skip_serializing_if = "Option::is_none")]
1084    pub num_epochs_to_retain_for_indexes: Option<u64>,
1085}
1086
1087fn default_num_latest_epoch_dbs_to_retain() -> usize {
1088    3
1089}
1090
1091fn default_periodic_compaction_threshold_days() -> Option<usize> {
1092    Some(1)
1093}
1094
1095fn is_default_periodic_compaction_threshold_days(days: &Option<usize>) -> bool {
1096    *days == default_periodic_compaction_threshold_days()
1097}
1098
1099impl Default for AuthorityStorePruningConfig {
1100    fn default() -> Self {
1101        Self {
1102            num_latest_epoch_dbs_to_retain: default_num_latest_epoch_dbs_to_retain(),
1103            num_epochs_to_retain: 0,
1104            periodic_compaction_threshold_days: default_periodic_compaction_threshold_days(),
1105            num_epochs_to_retain_for_checkpoints: if cfg!(msim) { Some(2) } else { None },
1106            enable_compaction_filter: cfg!(test) || cfg!(msim),
1107            num_epochs_to_retain_for_indexes: None,
1108        }
1109    }
1110}
1111
1112impl AuthorityStorePruningConfig {
1113    pub fn set_num_epochs_to_retain(&mut self, num_epochs_to_retain: u64) {
1114        self.num_epochs_to_retain = num_epochs_to_retain;
1115    }
1116
1117    pub fn set_num_epochs_to_retain_for_checkpoints(&mut self, num_epochs_to_retain: Option<u64>) {
1118        self.num_epochs_to_retain_for_checkpoints = num_epochs_to_retain;
1119    }
1120
1121    pub fn num_epochs_to_retain_for_checkpoints(&self) -> Option<u64> {
1122        self.num_epochs_to_retain_for_checkpoints
1123            // if n less than 2, coerce to 2 and log
1124            .map(|n| {
1125                if n < 2 {
1126                    info!("num_epochs_to_retain_for_checkpoints must be at least 2, rounding up from {}", n);
1127                    2
1128                } else {
1129                    n
1130                }
1131            })
1132    }
1133}
1134
1135#[derive(Debug, Clone, Deserialize, Serialize)]
1136#[serde(rename_all = "kebab-case")]
1137pub struct MetricsConfig {
1138    #[serde(skip_serializing_if = "Option::is_none")]
1139    pub push_interval_seconds: Option<u64>,
1140    #[serde(skip_serializing_if = "Option::is_none")]
1141    pub push_url: Option<String>,
1142    #[serde(skip_serializing_if = "Option::is_none")]
1143    pub groups: Option<MetricGroups>,
1144}
1145
1146fn default_checkpoint_archive_download_concurrency() -> NonZeroUsize {
1147    NonZeroUsize::new(10).unwrap()
1148}
1149
1150fn default_checkpoint_archive_verify_concurrency() -> NonZeroUsize {
1151    std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(4).unwrap())
1152}
1153
1154fn default_checkpoint_archive_max_checkpoints_ahead_of_execution() -> NonZeroUsize {
1155    NonZeroUsize::new(100_000).unwrap()
1156}
1157
1158/// Configuration for backfilling checkpoint contents from the
1159/// checkpoint archive when peers no longer serve the required range.
1160#[derive(Debug, Clone, Deserialize, Serialize)]
1161#[serde(rename_all = "kebab-case")]
1162pub struct CheckpointArchiveConfig {
1163    /// URL of the checkpoint archive to backfill from.
1164    pub url: String,
1165    /// Non-zero number of checkpoints to download in parallel.
1166    #[serde(default = "default_checkpoint_archive_download_concurrency")]
1167    pub download_concurrency: NonZeroUsize,
1168    /// Non-zero number of downloaded checkpoints to verify in parallel.
1169    /// Defaults to the number of CPU cores.
1170    #[serde(default = "default_checkpoint_archive_verify_concurrency")]
1171    pub verify_concurrency: NonZeroUsize,
1172    /// Pause downloading from the archive while the synced watermark is this
1173    /// many checkpoints ahead of the executed watermark, and resume once
1174    /// execution catches up. Bounds the disk space held by checkpoints that
1175    /// are synced but not yet executed, since only executed checkpoints can
1176    /// be pruned.
1177    #[serde(default = "default_checkpoint_archive_max_checkpoints_ahead_of_execution")]
1178    pub max_checkpoints_ahead_of_execution: NonZeroUsize,
1179}
1180
1181/// Configuration for the per-epoch state-snapshot publisher.
1182///
1183/// **Operator note (V2 snapshot publishing).** A node configured to publish
1184/// V2 snapshots must have a perpetual store containing no pre-V2
1185/// (`StoreObjectV1`) rows. This means a snapshot-publishing node must have
1186/// either synced from genesis under V2 or been restored from a V2 snapshot.
1187/// There is no on-disk backfill: a fresh sync is the only supported path.
1188#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1189#[serde(rename_all = "kebab-case")]
1190pub struct StateSnapshotConfig {
1191    #[serde(skip_serializing_if = "Option::is_none")]
1192    pub object_store_config: Option<ObjectStoreConfig>,
1193    pub concurrency: usize,
1194}
1195
1196#[derive(Default, Debug, Clone, Deserialize, Serialize)]
1197#[serde(rename_all = "kebab-case")]
1198pub struct TransactionKeyValueStoreWriteConfig {
1199    pub aws_access_key_id: String,
1200    pub aws_secret_access_key: String,
1201    pub aws_region: String,
1202    pub table_name: String,
1203    pub bucket_name: String,
1204    pub concurrency: usize,
1205}
1206
1207/// Configuration for the threshold(s) at which we consider the system
1208/// to be overloaded. When one of the threshold is passed, the node may
1209/// stop processing new transactions and/or certificates until the congestion
1210/// resolves.
1211#[derive(Clone, Debug, Deserialize, Serialize)]
1212#[serde(rename_all = "kebab-case")]
1213pub struct AuthorityOverloadConfig {
1214    /// Maximum time a transaction can wait in the transaction manager execution
1215    /// queue before it triggers an overload detection on the object it depends
1216    /// on.
1217    #[serde(default = "default_max_txn_age_in_queue")]
1218    pub max_txn_age_in_queue: Duration,
1219
1220    /// The interval of checking overload signal.
1221    #[serde(default = "default_overload_monitor_interval")]
1222    pub overload_monitor_interval: Duration,
1223
1224    /// The execution queueing latency when entering load shedding mode.
1225    #[serde(default = "default_execution_queue_latency_soft_limit")]
1226    pub execution_queue_latency_soft_limit: Duration,
1227
1228    /// The execution queueing latency when entering aggressive load shedding
1229    /// mode.
1230    #[serde(default = "default_execution_queue_latency_hard_limit")]
1231    pub execution_queue_latency_hard_limit: Duration,
1232
1233    /// The maximum percentage of transactions to shed in load shedding mode.
1234    #[serde(default = "default_max_load_shedding_percentage")]
1235    pub max_load_shedding_percentage: u32,
1236
1237    /// When in aggressive load shedding mode, the minimum percentage of
1238    /// transactions to shed.
1239    #[serde(default = "default_min_load_shedding_percentage_above_hard_limit")]
1240    pub min_load_shedding_percentage_above_hard_limit: u32,
1241
1242    /// If transaction ready rate is below this rate, we consider the validator
1243    /// is well under used, and will not enter load shedding mode.
1244    #[serde(default = "default_safe_transaction_ready_rate")]
1245    pub safe_transaction_ready_rate: u32,
1246
1247    /// When set to true, transaction signing may be rejected when the validator
1248    /// is overloaded.
1249    #[serde(default = "default_check_system_overload_at_signing")]
1250    pub check_system_overload_at_signing: bool,
1251
1252    /// When set to true, transaction execution may be rejected when the
1253    /// validator is overloaded.
1254    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1255    pub check_system_overload_at_execution: bool,
1256
1257    /// Reject a transaction if transaction manager queue length is above this
1258    /// threshold. 100_000 = 10k TPS * 5s resident time in transaction
1259    /// manager (pending + executing) * 2.
1260    #[serde(default = "default_max_transaction_manager_queue_length")]
1261    pub max_transaction_manager_queue_length: usize,
1262
1263    /// Reject a transaction if the number of pending transactions depending on
1264    /// the object is above the threshold.
1265    #[serde(default = "default_max_transaction_manager_per_object_queue_length")]
1266    pub max_transaction_manager_per_object_queue_length: usize,
1267
1268    /// Percentage of `max_transaction_manager_queue_length` at which graduated
1269    /// load shedding begins in the certificate-less (P-COOL) mode. Read
1270    /// via the same-named accessor, which clamps the value to <=100.
1271    #[serde(default = "default_max_transaction_manager_queue_length_soft_limit_pct")]
1272    pub max_transaction_manager_queue_length_soft_limit_pct: u32,
1273}
1274
1275impl AuthorityOverloadConfig {
1276    /// Returns the soft-limit percentage, clamped to <=100 to guard against
1277    /// out-of-range operator-supplied values.
1278    pub fn max_transaction_manager_queue_length_soft_limit_pct(&self) -> u32 {
1279        self.max_transaction_manager_queue_length_soft_limit_pct
1280            .min(100)
1281    }
1282}
1283
1284fn default_max_txn_age_in_queue() -> Duration {
1285    Duration::from_millis(500)
1286}
1287
1288fn default_overload_monitor_interval() -> Duration {
1289    Duration::from_secs(10)
1290}
1291
1292fn default_execution_queue_latency_soft_limit() -> Duration {
1293    Duration::from_secs(1)
1294}
1295
1296fn default_execution_queue_latency_hard_limit() -> Duration {
1297    Duration::from_secs(10)
1298}
1299
1300fn default_max_load_shedding_percentage() -> u32 {
1301    95
1302}
1303
1304fn default_min_load_shedding_percentage_above_hard_limit() -> u32 {
1305    50
1306}
1307
1308fn default_safe_transaction_ready_rate() -> u32 {
1309    100
1310}
1311
1312fn default_check_system_overload_at_signing() -> bool {
1313    true
1314}
1315
1316fn default_max_transaction_manager_queue_length() -> usize {
1317    100_000
1318}
1319
1320fn default_max_transaction_manager_queue_length_soft_limit_pct() -> u32 {
1321    50
1322}
1323
1324fn default_max_transaction_manager_per_object_queue_length() -> usize {
1325    20
1326}
1327
1328impl Default for AuthorityOverloadConfig {
1329    fn default() -> Self {
1330        Self {
1331            max_txn_age_in_queue: default_max_txn_age_in_queue(),
1332            overload_monitor_interval: default_overload_monitor_interval(),
1333            execution_queue_latency_soft_limit: default_execution_queue_latency_soft_limit(),
1334            execution_queue_latency_hard_limit: default_execution_queue_latency_hard_limit(),
1335            max_load_shedding_percentage: default_max_load_shedding_percentage(),
1336            min_load_shedding_percentage_above_hard_limit:
1337                default_min_load_shedding_percentage_above_hard_limit(),
1338            safe_transaction_ready_rate: default_safe_transaction_ready_rate(),
1339            check_system_overload_at_signing: true,
1340            check_system_overload_at_execution: false,
1341            max_transaction_manager_queue_length: default_max_transaction_manager_queue_length(),
1342            max_transaction_manager_queue_length_soft_limit_pct:
1343                default_max_transaction_manager_queue_length_soft_limit_pct(),
1344            max_transaction_manager_per_object_queue_length:
1345                default_max_transaction_manager_per_object_queue_length(),
1346        }
1347    }
1348}
1349
1350fn default_authority_overload_config() -> AuthorityOverloadConfig {
1351    AuthorityOverloadConfig::default()
1352}
1353
1354fn default_traffic_controller_policy_config() -> Option<PolicyConfig> {
1355    Some(PolicyConfig::default_dos_protection_policy())
1356}
1357
1358fn is_default_traffic_controller_policy_config(policy_config: &Option<PolicyConfig>) -> bool {
1359    serializes_like(policy_config, &default_traffic_controller_policy_config())
1360}
1361
1362#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1363pub struct Genesis {
1364    #[serde(flatten)]
1365    location: Option<GenesisLocation>,
1366
1367    #[serde(skip)]
1368    genesis: once_cell::sync::OnceCell<genesis::Genesis>,
1369}
1370
1371impl Genesis {
1372    pub fn new(genesis: genesis::Genesis) -> Self {
1373        Self {
1374            location: Some(GenesisLocation::InPlace {
1375                genesis: Box::new(genesis),
1376            }),
1377            genesis: Default::default(),
1378        }
1379    }
1380
1381    pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
1382        Self {
1383            location: Some(GenesisLocation::File {
1384                genesis_file_location: path.into(),
1385            }),
1386            genesis: Default::default(),
1387        }
1388    }
1389
1390    pub fn new_empty() -> Self {
1391        Self {
1392            location: None,
1393            genesis: Default::default(),
1394        }
1395    }
1396
1397    pub fn genesis(&self) -> Result<&genesis::Genesis> {
1398        match &self.location {
1399            Some(GenesisLocation::InPlace { genesis }) => Ok(genesis),
1400            Some(GenesisLocation::File {
1401                genesis_file_location,
1402            }) => self
1403                .genesis
1404                .get_or_try_init(|| genesis::Genesis::load(genesis_file_location)),
1405            None => anyhow::bail!("no genesis location set"),
1406        }
1407    }
1408}
1409
1410#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1411#[serde(untagged)]
1412enum GenesisLocation {
1413    InPlace {
1414        genesis: Box<genesis::Genesis>,
1415    },
1416    File {
1417        #[serde(rename = "genesis-file-location")]
1418        genesis_file_location: PathBuf,
1419    },
1420}
1421
1422/// Wrapper struct for SimpleKeypair that can be deserialized from a file path.
1423/// Used by network, worker, and account keypair.
1424#[derive(Clone, Debug, Deserialize, Serialize)]
1425pub struct KeyPairWithPath {
1426    #[serde(flatten)]
1427    location: KeyPairLocation,
1428
1429    #[serde(skip)]
1430    keypair: OnceCell<Arc<SimpleKeypair>>,
1431
1432    // The consensus/network stacks borrow their key as `&Ed25519KeyPair`
1433    // (fastcrypto), while the key itself is stored as an SDK `SimpleKeypair`
1434    // above. Converting on each access would return an owned value, which
1435    // can't back the `&`-returning accessors, so the converted key is cached
1436    // here. Never populated for account keys.
1437    #[serde(skip)]
1438    ed25519_keypair: OnceCell<Arc<Ed25519KeyPair>>,
1439}
1440
1441impl PartialEq for KeyPairWithPath {
1442    fn eq(&self, other: &Self) -> bool {
1443        self.location == other.location
1444    }
1445}
1446
1447impl Eq for KeyPairWithPath {}
1448
1449#[derive(Debug, Clone, Deserialize, Serialize)]
1450#[serde(untagged)]
1451enum KeyPairLocation {
1452    InPlace {
1453        #[serde(with = "bech32_formatted_keypair")]
1454        value: Arc<SimpleKeypair>,
1455    },
1456    File {
1457        path: PathBuf,
1458    },
1459}
1460
1461impl PartialEq for KeyPairLocation {
1462    fn eq(&self, other: &Self) -> bool {
1463        match (self, other) {
1464            (Self::InPlace { value: a }, Self::InPlace { value: b }) => {
1465                a.to_bytes() == b.to_bytes()
1466            }
1467            (Self::File { path: a }, Self::File { path: b }) => a == b,
1468            _ => false,
1469        }
1470    }
1471}
1472
1473impl Eq for KeyPairLocation {}
1474
1475impl KeyPairWithPath {
1476    pub fn new(kp: SimpleKeypair) -> Self {
1477        let cell: OnceCell<Arc<SimpleKeypair>> = OnceCell::new();
1478        let arc_kp = Arc::new(kp);
1479        // OK to unwrap panic because authority should not start without all keypairs
1480        // loaded.
1481        cell.set(arc_kp.clone()).expect("failed to set keypair");
1482        Self {
1483            location: KeyPairLocation::InPlace { value: arc_kp },
1484            keypair: cell,
1485            ed25519_keypair: OnceCell::new(),
1486        }
1487    }
1488
1489    pub fn new_from_path(path: PathBuf) -> Self {
1490        let cell: OnceCell<Arc<SimpleKeypair>> = OnceCell::new();
1491        // OK to unwrap panic because authority should not start without all keypairs
1492        // loaded.
1493        cell.set(Arc::new(read_keypair_from_file(&path).unwrap_or_else(
1494            |e| panic!("invalid keypair file at path {path:?}: {e}"),
1495        )))
1496        .expect("failed to set keypair");
1497        Self {
1498            location: KeyPairLocation::File { path },
1499            keypair: cell,
1500            ed25519_keypair: OnceCell::new(),
1501        }
1502    }
1503
1504    pub fn keypair(&self) -> &SimpleKeypair {
1505        self.keypair
1506            .get_or_init(|| match &self.location {
1507                KeyPairLocation::InPlace { value } => value.clone(),
1508                KeyPairLocation::File { path } => {
1509                    // OK to unwrap panic because authority should not start without all keypairs
1510                    // loaded.
1511                    Arc::new(
1512                        read_keypair_from_file(path).unwrap_or_else(|e| {
1513                            panic!("invalid keypair file at path {path:?}: {e}")
1514                        }),
1515                    )
1516                }
1517            })
1518            .as_ref()
1519    }
1520
1521    /// The keypair as a fastcrypto ed25519 keypair, for the network stacks
1522    /// that consume that type directly. Panics if the stored keypair is not
1523    /// ed25519.
1524    pub fn ed25519_keypair(&self) -> &Ed25519KeyPair {
1525        self.ed25519_keypair
1526            .get_or_init(|| {
1527                Arc::new(
1528                    simple_to_network_keypair(self.keypair())
1529                        .expect("only Ed25519 network keys are allowed"),
1530                )
1531            })
1532            .as_ref()
1533    }
1534}
1535
1536/// Wrapper struct for AuthorityKeyPair that can be deserialized from a file
1537/// path.
1538#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
1539pub struct AuthorityKeyPairWithPath {
1540    #[serde(flatten)]
1541    location: AuthorityKeyPairLocation,
1542
1543    #[serde(skip)]
1544    keypair: OnceCell<Arc<AuthorityKeyPair>>,
1545}
1546
1547#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
1548#[serde(untagged)]
1549enum AuthorityKeyPairLocation {
1550    InPlace { value: Arc<AuthorityKeyPair> },
1551    File { path: PathBuf },
1552}
1553
1554impl AuthorityKeyPairWithPath {
1555    pub fn new(kp: AuthorityKeyPair) -> Self {
1556        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1557        let arc_kp = Arc::new(kp);
1558        // OK to unwrap panic because authority should not start without all keypairs
1559        // loaded.
1560        cell.set(arc_kp.clone())
1561            .expect("failed to set authority keypair");
1562        Self {
1563            location: AuthorityKeyPairLocation::InPlace { value: arc_kp },
1564            keypair: cell,
1565        }
1566    }
1567
1568    pub fn new_from_path(path: PathBuf) -> Self {
1569        let cell: OnceCell<Arc<AuthorityKeyPair>> = OnceCell::new();
1570        // OK to unwrap panic because authority should not start without all keypairs
1571        // loaded.
1572        cell.set(Arc::new(
1573            read_authority_keypair_from_file(&path)
1574                .unwrap_or_else(|_| panic!("invalid authority keypair file at path {path:?}")),
1575        ))
1576        .expect("failed to set authority keypair");
1577        Self {
1578            location: AuthorityKeyPairLocation::File { path },
1579            keypair: cell,
1580        }
1581    }
1582
1583    pub fn authority_keypair(&self) -> &AuthorityKeyPair {
1584        self.keypair
1585            .get_or_init(|| match &self.location {
1586                AuthorityKeyPairLocation::InPlace { value } => value.clone(),
1587                AuthorityKeyPairLocation::File { path } => {
1588                    // OK to unwrap panic because authority should not start without all keypairs
1589                    // loaded.
1590                    Arc::new(
1591                        read_authority_keypair_from_file(path)
1592                            .unwrap_or_else(|_| panic!("invalid authority keypair file {path:?}")),
1593                    )
1594                }
1595            })
1596            .as_ref()
1597    }
1598}
1599
1600/// Configurations which determine how we dump state debug info.
1601/// Debug info is dumped when a node forks.
1602#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1603#[serde(rename_all = "kebab-case")]
1604pub struct StateDebugDumpConfig {
1605    #[serde(skip_serializing_if = "Option::is_none")]
1606    pub dump_file_directory: Option<PathBuf>,
1607}
1608
1609#[cfg(test)]
1610mod tests {
1611    use std::path::PathBuf;
1612
1613    use fastcrypto::traits::KeyPair;
1614    use iota_keys::keypair_file::{write_authority_keypair_to_file, write_keypair_to_file};
1615    use iota_types::{
1616        crypto::{
1617            AuthorityKeyPair, NetworkKeyPair, get_key_pair_from_rng, network_to_simple_keypair,
1618        },
1619        traffic_control::PolicyConfig,
1620    };
1621    use rand::{SeedableRng, rngs::StdRng};
1622    use serde::Serialize;
1623    use serde_yaml::Value;
1624
1625    use super::{
1626        Genesis, GrpcApiConfig, default_grpc_api_config,
1627        default_periodic_compaction_threshold_days, default_traffic_controller_policy_config,
1628    };
1629    use crate::NodeConfig;
1630
1631    const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1632
1633    const POLICY_CONFIG: &[&str] = &["policy-config"];
1634    const GRPC_API_CONFIG: &[&str] = &["grpc-api-config"];
1635    const COMPACTION_THRESHOLD: &[&str] = &[
1636        "authority-store-pruning-config",
1637        "periodic-compaction-threshold-days",
1638    ];
1639
1640    fn template_config() -> NodeConfig {
1641        serde_yaml::from_str(TEMPLATE).unwrap()
1642    }
1643
1644    fn round_trip(config: &NodeConfig) -> NodeConfig {
1645        serde_yaml::from_str(&serde_yaml::to_string(config).unwrap()).unwrap()
1646    }
1647
1648    fn as_yaml<T: Serialize>(value: &T) -> String {
1649        serde_yaml::to_string(value).unwrap()
1650    }
1651
1652    /// Reads `path` out of a serialized value, returning `None` when the last
1653    /// key is absent.
1654    fn written_at(value: &Value, path: &[&str]) -> Option<Value> {
1655        let (last, parents) = path.split_last().unwrap();
1656        let mut current = value;
1657        for name in parents {
1658            current = current
1659                .as_mapping()
1660                .unwrap()
1661                .get(&Value::String((*name).to_owned()))
1662                .unwrap();
1663        }
1664        current
1665            .as_mapping()
1666            .unwrap()
1667            .get(&Value::String((*last).to_owned()))
1668            .cloned()
1669    }
1670
1671    #[test]
1672    fn serialize_genesis_from_file() {
1673        let g = Genesis::new_from_file("path/to/file");
1674
1675        let s = serde_yaml::to_string(&g).unwrap();
1676        assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
1677        let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
1678        assert_eq!(g, loaded_genesis);
1679    }
1680
1681    #[test]
1682    fn fullnode_template() {
1683        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1684
1685        let _template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1686    }
1687
1688    #[test]
1689    fn enable_soft_locking_defaults_to_enabled() {
1690        // The template omits `enable-soft-locking`, so this exercises the serde
1691        // default and pins the documented "default: enabled" contract.
1692        const TEMPLATE: &str = include_str!("../data/fullnode-template.yaml");
1693
1694        let config: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1695        assert!(config.enable_soft_locking);
1696    }
1697
1698    #[test]
1699    fn load_key_pairs_to_node_config() {
1700        let authority_key_pair: AuthorityKeyPair =
1701            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1702        let protocol_key_pair: NetworkKeyPair =
1703            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1704        let network_key_pair: NetworkKeyPair =
1705            get_key_pair_from_rng(&mut StdRng::from_seed([0; 32])).1;
1706
1707        write_authority_keypair_to_file(&authority_key_pair, PathBuf::from("authority.key"))
1708            .unwrap();
1709        write_keypair_to_file(
1710            &network_to_simple_keypair(&protocol_key_pair),
1711            PathBuf::from("protocol.key"),
1712        )
1713        .unwrap();
1714        write_keypair_to_file(
1715            &network_to_simple_keypair(&network_key_pair),
1716            PathBuf::from("network.key"),
1717        )
1718        .unwrap();
1719
1720        const TEMPLATE: &str = include_str!("../data/fullnode-template-with-path.yaml");
1721        let template: NodeConfig = serde_yaml::from_str(TEMPLATE).unwrap();
1722        assert_eq!(
1723            template.authority_key_pair().public(),
1724            authority_key_pair.public()
1725        );
1726        assert_eq!(
1727            template.network_key_pair().public(),
1728            network_key_pair.public()
1729        );
1730        assert_eq!(
1731            template.protocol_key_pair().public(),
1732            protocol_key_pair.public()
1733        );
1734    }
1735
1736    #[test]
1737    fn a_policy_config_survives_a_round_trip_in_all_three_states() {
1738        let mut config = template_config();
1739
1740        config.policy_config = None;
1741        assert!(round_trip(&config).policy_config.is_none());
1742
1743        config.policy_config = default_traffic_controller_policy_config();
1744        assert_eq!(
1745            as_yaml(&round_trip(&config).policy_config),
1746            as_yaml(&default_traffic_controller_policy_config())
1747        );
1748
1749        let configured = PolicyConfig {
1750            dry_run: !PolicyConfig::default_dos_protection_policy().dry_run,
1751            ..PolicyConfig::default_dos_protection_policy()
1752        };
1753        config.policy_config = Some(configured.clone());
1754        assert_eq!(
1755            as_yaml(&round_trip(&config).policy_config),
1756            as_yaml(&Some(configured))
1757        );
1758    }
1759
1760    #[test]
1761    fn a_grpc_api_config_survives_a_round_trip_in_all_three_states() {
1762        let mut config = template_config();
1763
1764        config.grpc_api_config = None;
1765        assert!(round_trip(&config).grpc_api_config.is_none());
1766
1767        config.grpc_api_config = default_grpc_api_config();
1768        assert_eq!(
1769            as_yaml(&round_trip(&config).grpc_api_config),
1770            as_yaml(&default_grpc_api_config())
1771        );
1772
1773        let configured = GrpcApiConfig {
1774            max_message_size_bytes: 1234,
1775            ..GrpcApiConfig::default()
1776        };
1777        config.grpc_api_config = Some(configured.clone());
1778        assert_eq!(
1779            as_yaml(&round_trip(&config).grpc_api_config),
1780            as_yaml(&Some(configured))
1781        );
1782    }
1783
1784    #[test]
1785    fn the_default_pruning_config_agrees_with_the_serde_default() {
1786        assert_eq!(
1787            super::AuthorityStorePruningConfig::default().periodic_compaction_threshold_days,
1788            default_periodic_compaction_threshold_days()
1789        );
1790    }
1791
1792    #[test]
1793    fn a_compaction_threshold_survives_a_round_trip_in_all_three_states() {
1794        let mut config = template_config();
1795
1796        for state in [None, default_periodic_compaction_threshold_days(), Some(7)] {
1797            config
1798                .authority_store_pruning_config
1799                .periodic_compaction_threshold_days = state;
1800            assert_eq!(
1801                round_trip(&config)
1802                    .authority_store_pruning_config
1803                    .periodic_compaction_threshold_days,
1804                state
1805            );
1806        }
1807    }
1808
1809    #[test]
1810    fn a_default_value_is_omitted_and_a_disabled_one_is_written_as_null() {
1811        let mut config = template_config();
1812        config.policy_config = default_traffic_controller_policy_config();
1813        config.grpc_api_config = default_grpc_api_config();
1814        config
1815            .authority_store_pruning_config
1816            .periodic_compaction_threshold_days = default_periodic_compaction_threshold_days();
1817
1818        let written = serde_yaml::to_value(&config).unwrap();
1819        assert_eq!(written_at(&written, POLICY_CONFIG), None);
1820        assert_eq!(written_at(&written, GRPC_API_CONFIG), None);
1821        assert_eq!(written_at(&written, COMPACTION_THRESHOLD), None);
1822
1823        config.policy_config = None;
1824        config.grpc_api_config = None;
1825        config
1826            .authority_store_pruning_config
1827            .periodic_compaction_threshold_days = None;
1828
1829        let written = serde_yaml::to_value(&config).unwrap();
1830        assert_eq!(written_at(&written, POLICY_CONFIG), Some(Value::Null));
1831        assert_eq!(written_at(&written, GRPC_API_CONFIG), Some(Value::Null));
1832        assert_eq!(
1833            written_at(&written, COMPACTION_THRESHOLD),
1834            Some(Value::Null)
1835        );
1836    }
1837}
1838
1839// RunWithRange is used to specify the ending epoch/checkpoint to process.
1840// this is intended for use with disaster recovery debugging and verification
1841// workflows, never in normal operations
1842#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
1843pub enum RunWithRange {
1844    Epoch(EpochId),
1845    Checkpoint(CheckpointSequenceNumber),
1846}
1847
1848impl RunWithRange {
1849    // is epoch_id > RunWithRange::Epoch
1850    pub fn is_epoch_gt(&self, epoch_id: EpochId) -> bool {
1851        matches!(self, RunWithRange::Epoch(e) if epoch_id > *e)
1852    }
1853
1854    pub fn matches_checkpoint(&self, seq_num: CheckpointSequenceNumber) -> bool {
1855        matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
1856    }
1857
1858    pub fn into_checkpoint_bound(self) -> Option<CheckpointSequenceNumber> {
1859        match self {
1860            RunWithRange::Epoch(_) => None,
1861            RunWithRange::Checkpoint(seq) => Some(seq),
1862        }
1863    }
1864}
1865
1866/// A serde helper module used with #[serde(with = "...")] to change the
1867/// de/serialization format of an `SimpleKeypair` to Bech32 when written to or
1868/// read from a node config.
1869mod bech32_formatted_keypair {
1870    use std::ops::Deref;
1871
1872    use fastcrypto::encoding::{Base64, Encoding};
1873    use iota_sdk_crypto::{ToFromBech32, simple::SimpleKeypair};
1874    use serde::{Deserialize, Deserializer, Serializer};
1875
1876    pub fn serialize<S, T>(kp: &T, serializer: S) -> Result<S::Ok, S::Error>
1877    where
1878        S: Serializer,
1879        T: Deref<Target = SimpleKeypair>,
1880    {
1881        use serde::ser::Error;
1882
1883        // Serialize the keypair to a Bech32 string
1884        let s = kp.to_bech32().map_err(Error::custom)?;
1885
1886        serializer.serialize_str(&s)
1887    }
1888
1889    pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
1890    where
1891        D: Deserializer<'de>,
1892        T: From<SimpleKeypair>,
1893    {
1894        use serde::de::Error;
1895
1896        let s = String::deserialize(deserializer)?;
1897
1898        // Try to deserialize the keypair from a Bech32 formatted string
1899        SimpleKeypair::from_bech32(&s)
1900            .map_err(Error::custom)
1901            .or_else(|_: D::Error| {
1902                // For backwards compatibility try Base64 if Bech32 failed
1903                let bytes = Base64::decode(&s).map_err(Error::custom)?;
1904                SimpleKeypair::from_bytes(&bytes).map_err(Error::custom)
1905            })
1906            .map(Into::into)
1907    }
1908}