iota_config/
node.rs

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