iota_swarm_config/
network_config.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_config::{Config, NodeConfig, genesis, node};
6use iota_types::{
7    committee::CommitteeWithNetworkMetadata, crypto::AccountKeyPair, multiaddr::Multiaddr,
8};
9use serde::{Deserialize, Serialize};
10use serde_with::serde_as;
11
12/// This is a config that is used for testing or local use as it contains the
13/// config and keys for all validators
14#[serde_as]
15#[derive(Debug, Deserialize, Serialize)]
16pub struct NetworkConfig {
17    pub validator_configs: Vec<NodeConfig>,
18    pub account_keys: Vec<AccountKeyPair>,
19    pub genesis: genesis::Genesis,
20}
21
22impl Config for NetworkConfig {}
23
24impl NetworkConfig {
25    pub fn validator_configs(&self) -> &[NodeConfig] {
26        &self.validator_configs
27    }
28
29    pub fn net_addresses(&self) -> Vec<Multiaddr> {
30        self.genesis
31            .committee_with_network()
32            .validators()
33            .values()
34            .map(|(_, n)| n.network_address.clone())
35            .collect()
36    }
37
38    pub fn committee_with_network(&self) -> CommitteeWithNetworkMetadata {
39        self.genesis.committee_with_network()
40    }
41
42    pub fn into_validator_configs(self) -> Vec<NodeConfig> {
43        self.validator_configs
44    }
45
46    /// Retrieve genesis information that might be present in the configured
47    /// validators.
48    pub fn get_validator_genesis(&self) -> Option<&node::Genesis> {
49        self.validator_configs
50            .first()
51            .as_ref()
52            .map(|validator| &validator.genesis)
53    }
54}
55
56/// This is the light version of [`NetworkConfig`] that does not
57/// contain the entire [`genesis::Genesis`].
58#[serde_as]
59#[derive(Debug, Deserialize, Serialize)]
60pub struct NetworkConfigLight {
61    pub validator_configs: Vec<NodeConfig>,
62    pub account_keys: Vec<AccountKeyPair>,
63    pub committee_with_network: CommitteeWithNetworkMetadata,
64}
65
66impl Config for NetworkConfigLight {}
67
68impl NetworkConfigLight {
69    pub fn new(
70        validator_configs: Vec<NodeConfig>,
71        account_keys: Vec<AccountKeyPair>,
72        genesis: &genesis::Genesis,
73    ) -> Self {
74        Self {
75            validator_configs,
76            account_keys,
77            committee_with_network: genesis.committee_with_network(),
78        }
79    }
80
81    pub fn validator_configs(&self) -> &[NodeConfig] {
82        &self.validator_configs
83    }
84
85    pub fn net_addresses(&self) -> Vec<Multiaddr> {
86        self.committee_with_network
87            .validators()
88            .values()
89            .map(|(_, n)| n.network_address.clone())
90            .collect()
91    }
92
93    pub fn into_validator_configs(self) -> Vec<NodeConfig> {
94        self.validator_configs
95    }
96
97    /// Retrieve genesis information that might be present in the configured
98    /// validators.
99    pub fn get_validator_genesis(&self) -> Option<&node::Genesis> {
100        self.validator_configs
101            .first()
102            .as_ref()
103            .map(|validator| &validator.genesis)
104    }
105}