Skip to main content

iota_swarm_config/
node_config_builder.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{net::SocketAddr, path::PathBuf};
6
7use fastcrypto::{
8    encoding::{Encoding, Hex},
9    traits::KeyPair,
10};
11use iota_config::{
12    AUTHORITIES_DB_NAME, CONSENSUS_DB_NAME, ConsensusConfig, FULL_NODE_DB_PATH,
13    IOTA_GENESIS_MIGRATION_TX_DATA_FILENAME, NodeConfig, local_ip_utils,
14    node::{
15        AuthorityKeyPairWithPath, AuthorityOverloadConfig, AuthorityStorePruningConfig,
16        CheckpointExecutorConfig, DBCheckpointConfig, DEFAULT_GRPC_CONCURRENCY_LIMIT,
17        ExecutionCacheConfig, ExecutionCacheType, ExpensiveSafetyCheckConfig, Genesis,
18        GrpcApiConfig, KeyPairWithPath, RunWithRange, StateArchiveConfig, StateSnapshotConfig,
19        default_enable_index_processing, default_end_of_epoch_broadcast_channel_capacity,
20    },
21    p2p::{DiscoveryConfig, P2pConfig, SeedPeer, StateSyncConfig},
22    verifier_signing_config::VerifierSigningConfig,
23};
24use iota_names::config::IotaNamesConfig;
25use iota_protocol_config::Chain;
26use iota_types::{
27    crypto::{AuthorityKeyPair, AuthorityPublicKeyBytes, IotaKeyPair, NetworkKeyPair},
28    multiaddr::Multiaddr,
29    supported_protocol_versions::SupportedProtocolVersions,
30    traffic_control::{PolicyConfig, RemoteFirewallConfig},
31};
32
33use crate::{
34    genesis_config::{ValidatorGenesisConfig, ValidatorGenesisConfigBuilder},
35    network_config::NetworkConfig,
36};
37
38/// This builder contains information that's not included in
39/// ValidatorGenesisConfig for building a validator NodeConfig. It can be used
40/// to build either a genesis validator or a new validator.
41#[derive(Clone, Default)]
42pub struct ValidatorConfigBuilder {
43    config_directory: Option<PathBuf>,
44    supported_protocol_versions: Option<SupportedProtocolVersions>,
45    force_unpruned_checkpoints: bool,
46    authority_overload_config: Option<AuthorityOverloadConfig>,
47    execution_cache_type: Option<ExecutionCacheType>,
48    execution_cache_config: Option<ExecutionCacheConfig>,
49    data_ingestion_dir: Option<PathBuf>,
50    policy_config: Option<PolicyConfig>,
51    firewall_config: Option<RemoteFirewallConfig>,
52    max_submit_position: Option<usize>,
53    submit_delay_step_override_millis: Option<u64>,
54    discovery_config: Option<DiscoveryConfig>,
55    chain_override: Option<Chain>,
56}
57
58impl ValidatorConfigBuilder {
59    pub fn new() -> Self {
60        Self {
61            ..Default::default()
62        }
63    }
64
65    pub fn with_chain_override(mut self, chain: Chain) -> Self {
66        assert!(self.chain_override.is_none(), "Chain override already set");
67        self.chain_override = Some(chain);
68        self
69    }
70
71    pub fn with_config_directory(mut self, config_directory: PathBuf) -> Self {
72        assert!(self.config_directory.is_none());
73        self.config_directory = Some(config_directory);
74        self
75    }
76
77    pub fn with_supported_protocol_versions(
78        mut self,
79        supported_protocol_versions: SupportedProtocolVersions,
80    ) -> Self {
81        assert!(self.supported_protocol_versions.is_none());
82        self.supported_protocol_versions = Some(supported_protocol_versions);
83        self
84    }
85
86    pub fn with_unpruned_checkpoints(mut self) -> Self {
87        self.force_unpruned_checkpoints = true;
88        self
89    }
90
91    pub fn with_authority_overload_config(mut self, config: AuthorityOverloadConfig) -> Self {
92        self.authority_overload_config = Some(config);
93        self
94    }
95
96    pub fn with_execution_cache_type(mut self, execution_cache_type: ExecutionCacheType) -> Self {
97        self.execution_cache_type = Some(execution_cache_type);
98        self
99    }
100
101    pub fn with_execution_cache_config(mut self, config: ExecutionCacheConfig) -> Self {
102        self.execution_cache_config = Some(config);
103        self
104    }
105
106    pub fn with_data_ingestion_dir(mut self, path: PathBuf) -> Self {
107        self.data_ingestion_dir = Some(path);
108        self
109    }
110
111    pub fn with_policy_config(mut self, config: Option<PolicyConfig>) -> Self {
112        self.policy_config = config;
113        self
114    }
115
116    pub fn with_firewall_config(mut self, config: Option<RemoteFirewallConfig>) -> Self {
117        self.firewall_config = config;
118        self
119    }
120
121    pub fn with_max_submit_position(mut self, max_submit_position: usize) -> Self {
122        self.max_submit_position = Some(max_submit_position);
123        self
124    }
125
126    pub fn with_submit_delay_step_override_millis(
127        mut self,
128        submit_delay_step_override_millis: u64,
129    ) -> Self {
130        self.submit_delay_step_override_millis = Some(submit_delay_step_override_millis);
131        self
132    }
133
134    pub fn with_discovery_config(mut self, discovery_config: DiscoveryConfig) -> Self {
135        self.discovery_config = Some(discovery_config);
136        self
137    }
138
139    pub fn build_without_genesis(self, validator: ValidatorGenesisConfig) -> NodeConfig {
140        let key_path = get_key_path(&validator.authority_key_pair);
141        let config_directory = self
142            .config_directory
143            .unwrap_or_else(|| iota_common::tempdir().keep());
144        let migration_tx_data_path =
145            Some(config_directory.join(IOTA_GENESIS_MIGRATION_TX_DATA_FILENAME));
146        let db_path = config_directory
147            .join(AUTHORITIES_DB_NAME)
148            .join(key_path.clone());
149        let network_address = validator.network_address;
150        let consensus_db_path = config_directory.join(CONSENSUS_DB_NAME).join(key_path);
151        let localhost = local_ip_utils::localhost_for_testing();
152        let consensus_config = ConsensusConfig {
153            db_path: consensus_db_path,
154            db_retention_epochs: None,
155            db_pruner_period_secs: None,
156            max_pending_transactions: None,
157            max_submit_position: self.max_submit_position,
158            submit_delay_step_override_millis: self.submit_delay_step_override_millis,
159            parameters: Default::default(),
160        };
161
162        let p2p_config = P2pConfig {
163            listen_address: validator.p2p_listen_address.unwrap_or_else(|| {
164                validator
165                    .p2p_address
166                    .udp_multiaddr_to_listen_address()
167                    .unwrap()
168            }),
169            external_address: Some(validator.p2p_address),
170            // Set a shorter timeout for checkpoint content download in tests, since
171            // checkpoint pruning also happens much faster, and network is local.
172            state_sync: Some(StateSyncConfig {
173                checkpoint_content_timeout_ms: Some(10_000),
174                ..Default::default()
175            }),
176            // Use discovery config if provided
177            discovery: self.discovery_config,
178            ..Default::default()
179        };
180
181        let mut pruning_config = AuthorityStorePruningConfig::default();
182        if self.force_unpruned_checkpoints {
183            pruning_config.set_num_epochs_to_retain_for_checkpoints(None);
184        }
185        let pruning_config = pruning_config;
186        let checkpoint_executor_config = CheckpointExecutorConfig {
187            data_ingestion_dir: self.data_ingestion_dir,
188            ..Default::default()
189        };
190
191        NodeConfig {
192            authority_key_pair: AuthorityKeyPairWithPath::new(validator.authority_key_pair),
193            network_key_pair: KeyPairWithPath::new(IotaKeyPair::Ed25519(
194                validator.network_key_pair,
195            )),
196            account_key_pair: KeyPairWithPath::new(validator.account_key_pair),
197            protocol_key_pair: KeyPairWithPath::new(IotaKeyPair::Ed25519(
198                validator.protocol_key_pair,
199            )),
200            db_path,
201            network_address,
202            metrics_address: validator.metrics_address,
203            admin_interface_address: validator.admin_interface_address,
204            json_rpc_address: local_ip_utils::new_tcp_address_for_testing(&localhost)
205                .to_socket_addr()
206                .unwrap(),
207            consensus_config: Some(consensus_config),
208            enable_index_processing: default_enable_index_processing(),
209            genesis: Genesis::new_empty(),
210            migration_tx_data_path,
211            grpc_load_shed: None,
212            grpc_concurrency_limit: Some(DEFAULT_GRPC_CONCURRENCY_LIMIT),
213            p2p_config,
214            authority_store_pruning_config: pruning_config,
215            end_of_epoch_broadcast_channel_capacity:
216                default_end_of_epoch_broadcast_channel_capacity(),
217            checkpoint_executor_config,
218            metrics: None,
219            supported_protocol_versions: self.supported_protocol_versions,
220            db_checkpoint_config: Default::default(),
221            // By default, expensive checks will be enabled in debug build, but not in release
222            // build.
223            expensive_safety_check_config: ExpensiveSafetyCheckConfig::default(),
224            transaction_deny_config: Default::default(),
225            certificate_deny_config: Default::default(),
226            state_debug_dump_config: Default::default(),
227            state_archive_write_config: StateArchiveConfig::default(),
228            state_archive_read_config: vec![],
229            state_snapshot_write_config: StateSnapshotConfig::default(),
230            indexer_max_subscriptions: Default::default(),
231            transaction_kv_store_read_config: Default::default(),
232            transaction_kv_store_write_config: None,
233            authority_overload_config: self.authority_overload_config.unwrap_or_default(),
234            execution_cache: self.execution_cache_type.unwrap_or_default(),
235            execution_cache_config: self.execution_cache_config.unwrap_or_default(),
236            run_with_range: None,
237            jsonrpc_server_type: None,
238            policy_config: self.policy_config,
239            firewall_config: self.firewall_config,
240            enable_validator_tx_finalizer: true,
241            verifier_signing_config: VerifierSigningConfig::default(),
242            enable_db_write_stall: None,
243            iota_names_config: None,
244            enable_grpc_api: false,
245            grpc_api_config: None,
246            chain_override_for_testing: self.chain_override,
247        }
248    }
249
250    pub fn build(
251        self,
252        validator: ValidatorGenesisConfig,
253        genesis: iota_config::genesis::Genesis,
254    ) -> NodeConfig {
255        let mut config = self.build_without_genesis(validator);
256        config.genesis = iota_config::node::Genesis::new(genesis);
257        config
258    }
259
260    pub fn build_new_validator<R: rand::RngCore + rand::CryptoRng>(
261        self,
262        rng: &mut R,
263        network_config: &NetworkConfig,
264    ) -> NodeConfig {
265        let validator_config = ValidatorGenesisConfigBuilder::new().build(rng);
266        self.build(validator_config, network_config.genesis.clone())
267    }
268}
269
270#[derive(Clone, Debug, Default)]
271pub struct FullnodeConfigBuilder {
272    config_directory: Option<PathBuf>,
273    // port for json rpc api
274    rpc_port: Option<u16>,
275    rpc_addr: Option<SocketAddr>,
276    supported_protocol_versions: Option<SupportedProtocolVersions>,
277    db_checkpoint_config: Option<DBCheckpointConfig>,
278    expensive_safety_check_config: Option<ExpensiveSafetyCheckConfig>,
279    db_path: Option<PathBuf>,
280    network_address: Option<Multiaddr>,
281    json_rpc_address: Option<SocketAddr>,
282    metrics_address: Option<SocketAddr>,
283    admin_interface_address: Option<SocketAddr>,
284    genesis: Option<Genesis>,
285    p2p_external_address: Option<Multiaddr>,
286    p2p_listen_address: Option<SocketAddr>,
287    network_key_pair: Option<KeyPairWithPath>,
288    run_with_range: Option<RunWithRange>,
289    policy_config: Option<PolicyConfig>,
290    fw_config: Option<RemoteFirewallConfig>,
291    data_ingestion_dir: Option<PathBuf>,
292    disable_pruning: bool,
293    iota_names_config: Option<IotaNamesConfig>,
294    enable_grpc_api: bool,
295    grpc_api_config: Option<GrpcApiConfig>,
296    discovery_config: Option<DiscoveryConfig>,
297    chain_override: Option<Chain>,
298}
299
300impl FullnodeConfigBuilder {
301    pub fn new() -> Self {
302        Self::default()
303    }
304
305    pub fn with_chain_override(mut self, chain: Chain) -> Self {
306        assert!(self.chain_override.is_none(), "Chain override already set");
307        self.chain_override = Some(chain);
308        self
309    }
310
311    pub fn with_config_directory(mut self, config_directory: PathBuf) -> Self {
312        self.config_directory = Some(config_directory);
313        self
314    }
315
316    pub fn with_rpc_port(mut self, port: u16) -> Self {
317        assert!(self.rpc_addr.is_none() && self.rpc_port.is_none());
318        self.rpc_port = Some(port);
319        self
320    }
321
322    pub fn with_rpc_addr(mut self, addr: impl Into<SocketAddr>) -> Self {
323        assert!(self.rpc_addr.is_none() && self.rpc_port.is_none());
324        self.rpc_addr = Some(addr.into());
325        self
326    }
327
328    pub fn with_supported_protocol_versions(mut self, versions: SupportedProtocolVersions) -> Self {
329        self.supported_protocol_versions = Some(versions);
330        self
331    }
332
333    pub fn with_db_checkpoint_config(mut self, db_checkpoint_config: DBCheckpointConfig) -> Self {
334        self.db_checkpoint_config = Some(db_checkpoint_config);
335        self
336    }
337
338    pub fn with_disable_pruning(mut self, disable_pruning: bool) -> Self {
339        self.disable_pruning = disable_pruning;
340        self
341    }
342
343    pub fn with_expensive_safety_check_config(
344        mut self,
345        expensive_safety_check_config: ExpensiveSafetyCheckConfig,
346    ) -> Self {
347        self.expensive_safety_check_config = Some(expensive_safety_check_config);
348        self
349    }
350
351    pub fn with_db_path(mut self, db_path: PathBuf) -> Self {
352        self.db_path = Some(db_path);
353        self
354    }
355
356    pub fn with_network_address(mut self, network_address: Multiaddr) -> Self {
357        self.network_address = Some(network_address);
358        self
359    }
360
361    pub fn with_json_rpc_address(mut self, json_rpc_address: impl Into<SocketAddr>) -> Self {
362        self.json_rpc_address = Some(json_rpc_address.into());
363        self
364    }
365
366    pub fn with_metrics_address(mut self, metrics_address: impl Into<SocketAddr>) -> Self {
367        self.metrics_address = Some(metrics_address.into());
368        self
369    }
370
371    pub fn with_admin_interface_address(
372        mut self,
373        admin_interface_address: Option<impl Into<SocketAddr>>,
374    ) -> Self {
375        self.admin_interface_address = admin_interface_address.map(|addr| addr.into());
376        self
377    }
378
379    pub fn with_genesis(mut self, genesis: Genesis) -> Self {
380        self.genesis = Some(genesis);
381        self
382    }
383
384    pub fn with_p2p_external_address(mut self, p2p_external_address: Multiaddr) -> Self {
385        self.p2p_external_address = Some(p2p_external_address);
386        self
387    }
388
389    pub fn with_p2p_listen_address(mut self, p2p_listen_address: impl Into<SocketAddr>) -> Self {
390        self.p2p_listen_address = Some(p2p_listen_address.into());
391        self
392    }
393
394    pub fn with_network_key_pair(mut self, network_key_pair: Option<NetworkKeyPair>) -> Self {
395        if let Some(network_key_pair) = network_key_pair {
396            self.network_key_pair =
397                Some(KeyPairWithPath::new(IotaKeyPair::Ed25519(network_key_pair)));
398        }
399        self
400    }
401
402    pub fn with_run_with_range(mut self, run_with_range: Option<RunWithRange>) -> Self {
403        if let Some(run_with_range) = run_with_range {
404            self.run_with_range = Some(run_with_range);
405        }
406        self
407    }
408
409    pub fn with_policy_config(mut self, config: Option<PolicyConfig>) -> Self {
410        self.policy_config = config;
411        self
412    }
413
414    pub fn with_fw_config(mut self, config: Option<RemoteFirewallConfig>) -> Self {
415        self.fw_config = config;
416        self
417    }
418
419    pub fn with_data_ingestion_dir(mut self, path: Option<PathBuf>) -> Self {
420        self.data_ingestion_dir = path;
421        self
422    }
423
424    pub fn with_iota_names_config(mut self, config: Option<IotaNamesConfig>) -> Self {
425        self.iota_names_config = config;
426        self
427    }
428
429    pub fn with_enable_grpc_api(mut self, enable_grpc_api: bool) -> Self {
430        self.enable_grpc_api = enable_grpc_api;
431        self
432    }
433
434    pub fn with_grpc_api_config(mut self, config: GrpcApiConfig) -> Self {
435        self.grpc_api_config = Some(config);
436        self
437    }
438
439    pub fn with_discovery_config(mut self, discovery_config: DiscoveryConfig) -> Self {
440        self.discovery_config = Some(discovery_config);
441        self
442    }
443
444    pub fn build_from_parts<R: rand::RngCore + rand::CryptoRng>(
445        self,
446        rng: &mut R,
447        validator_configs: &[NodeConfig],
448        genesis: iota_config::node::Genesis,
449    ) -> NodeConfig {
450        // Take advantage of ValidatorGenesisConfigBuilder to build the keypairs and
451        // addresses, even though this is a fullnode.
452        let validator_config = ValidatorGenesisConfigBuilder::new().build(rng);
453        let ip = validator_config
454            .network_address
455            .to_socket_addr()
456            .unwrap()
457            .ip()
458            .to_string();
459
460        let key_path = get_key_path(&validator_config.authority_key_pair);
461        let config_directory = self
462            .config_directory
463            .unwrap_or_else(|| iota_common::tempdir().keep());
464
465        let migration_tx_data_path =
466            Some(config_directory.join(IOTA_GENESIS_MIGRATION_TX_DATA_FILENAME));
467
468        let p2p_config = {
469            let seed_peers = validator_configs
470                .iter()
471                .map(|config| SeedPeer {
472                    peer_id: Some(anemo::PeerId(
473                        config.network_key_pair().public().0.to_bytes(),
474                    )),
475                    address: config.p2p_config.external_address.clone().unwrap(),
476                })
477                .collect();
478
479            P2pConfig {
480                listen_address: self.p2p_listen_address.unwrap_or_else(|| {
481                    validator_config.p2p_listen_address.unwrap_or_else(|| {
482                        validator_config
483                            .p2p_address
484                            .udp_multiaddr_to_listen_address()
485                            .unwrap()
486                    })
487                }),
488                external_address: self
489                    .p2p_external_address
490                    .or(Some(validator_config.p2p_address.clone())),
491                seed_peers,
492                // Set a shorter timeout for checkpoint content download in tests, since
493                // checkpoint pruning also happens much faster, and network is local.
494                state_sync: Some(StateSyncConfig {
495                    checkpoint_content_timeout_ms: Some(10_000),
496                    ..Default::default()
497                }),
498                // Use discovery config if provided
499                discovery: self.discovery_config,
500                ..Default::default()
501            }
502        };
503
504        let json_rpc_address = self.rpc_addr.unwrap_or_else(|| {
505            let rpc_port = self
506                .rpc_port
507                .unwrap_or_else(|| local_ip_utils::get_available_port(&ip));
508            format!("{ip}:{rpc_port}").parse().unwrap()
509        });
510
511        let grpc_api_config = self.grpc_api_config.or_else(|| {
512            if self.enable_grpc_api {
513                Some(GrpcApiConfig {
514                    address: format!("{ip}:{}", local_ip_utils::get_available_port(&ip))
515                        .parse()
516                        .unwrap(),
517                    ..Default::default()
518                })
519            } else {
520                None
521            }
522        });
523
524        let checkpoint_executor_config = CheckpointExecutorConfig {
525            data_ingestion_dir: self.data_ingestion_dir,
526            ..Default::default()
527        };
528
529        let mut pruning_config = AuthorityStorePruningConfig::default();
530        if self.disable_pruning {
531            pruning_config.set_num_epochs_to_retain_for_checkpoints(None);
532            pruning_config.set_num_epochs_to_retain(u64::MAX);
533        };
534
535        NodeConfig {
536            authority_key_pair: AuthorityKeyPairWithPath::new(validator_config.authority_key_pair),
537            account_key_pair: KeyPairWithPath::new(validator_config.account_key_pair),
538            protocol_key_pair: KeyPairWithPath::new(IotaKeyPair::Ed25519(
539                validator_config.protocol_key_pair,
540            )),
541            network_key_pair: self.network_key_pair.unwrap_or(KeyPairWithPath::new(
542                IotaKeyPair::Ed25519(validator_config.network_key_pair),
543            )),
544            db_path: self
545                .db_path
546                .unwrap_or(config_directory.join(FULL_NODE_DB_PATH).join(key_path)),
547            network_address: self
548                .network_address
549                .unwrap_or(validator_config.network_address),
550            metrics_address: self
551                .metrics_address
552                .unwrap_or(local_ip_utils::new_local_tcp_socket_for_testing()),
553            admin_interface_address: self
554                .admin_interface_address
555                .unwrap_or(local_ip_utils::new_local_tcp_socket_for_testing()),
556            json_rpc_address: self.json_rpc_address.unwrap_or(json_rpc_address),
557            consensus_config: None,
558            enable_index_processing: default_enable_index_processing(),
559            genesis,
560            migration_tx_data_path,
561            grpc_load_shed: None,
562            grpc_concurrency_limit: None,
563            p2p_config,
564            authority_store_pruning_config: pruning_config,
565            end_of_epoch_broadcast_channel_capacity:
566                default_end_of_epoch_broadcast_channel_capacity(),
567            checkpoint_executor_config,
568            metrics: None,
569            supported_protocol_versions: self.supported_protocol_versions,
570            db_checkpoint_config: self.db_checkpoint_config.unwrap_or_default(),
571            expensive_safety_check_config: self
572                .expensive_safety_check_config
573                .unwrap_or_else(ExpensiveSafetyCheckConfig::new_enable_all),
574            transaction_deny_config: Default::default(),
575            certificate_deny_config: Default::default(),
576            state_debug_dump_config: Default::default(),
577            state_archive_write_config: StateArchiveConfig::default(),
578            state_archive_read_config: vec![],
579            state_snapshot_write_config: StateSnapshotConfig::default(),
580            indexer_max_subscriptions: Default::default(),
581            transaction_kv_store_read_config: Default::default(),
582            transaction_kv_store_write_config: Default::default(),
583            authority_overload_config: Default::default(),
584            run_with_range: self.run_with_range,
585            jsonrpc_server_type: None,
586            policy_config: self.policy_config,
587            firewall_config: self.fw_config,
588            execution_cache: ExecutionCacheType::default(),
589            execution_cache_config: ExecutionCacheConfig::default(),
590            // This is a validator specific feature.
591            enable_validator_tx_finalizer: false,
592            verifier_signing_config: VerifierSigningConfig::default(),
593            enable_db_write_stall: None,
594            iota_names_config: self.iota_names_config,
595            enable_grpc_api: self.enable_grpc_api,
596            grpc_api_config,
597            chain_override_for_testing: self.chain_override,
598        }
599    }
600
601    pub fn build<R: rand::RngCore + rand::CryptoRng>(
602        self,
603        rng: &mut R,
604        network_config: &NetworkConfig,
605    ) -> NodeConfig {
606        let genesis = self
607            .genesis
608            .as_ref()
609            .or_else(|| network_config.get_validator_genesis())
610            .cloned()
611            .unwrap_or_else(|| iota_config::node::Genesis::new(network_config.genesis.clone()));
612        self.build_from_parts(rng, network_config.validator_configs(), genesis)
613    }
614}
615
616/// Given a validator keypair, return a path that can be used to identify the
617/// validator.
618fn get_key_path(key_pair: &AuthorityKeyPair) -> String {
619    let public_key: AuthorityPublicKeyBytes = key_pair.public().into();
620    let mut key_path = Hex::encode(public_key);
621    // 12 is rather arbitrary here but it's a nice balance between being short and
622    // being unique.
623    key_path.truncate(12);
624    key_path
625}