Skip to main content

linera_client/
config.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2// Copyright (c) Zefchain Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5use std::iter::IntoIterator;
6
7use linera_base::{
8    crypto::{AccountPublicKey, ValidatorPublicKey, ValidatorSecretKey},
9    data_types::Timestamp,
10};
11pub use linera_core::genesis_config::{Error as GenesisConfigError, GenesisConfig};
12use linera_execution::{
13    committee::{Committee, ValidatorState},
14    ResourceControlPolicy,
15};
16use linera_rpc::config::{ValidatorInternalNetworkConfig, ValidatorPublicNetworkConfig};
17use serde::{Deserialize, Serialize};
18
19/// The public configuration of a validator.
20#[derive(Clone, Debug, Serialize, Deserialize)]
21pub struct ValidatorConfig {
22    /// The public key of the validator.
23    pub public_key: ValidatorPublicKey,
24    /// The account key of the validator.
25    pub account_key: AccountPublicKey,
26    /// The network configuration for the validator.
27    pub network: ValidatorPublicNetworkConfig,
28}
29
30/// The private configuration of a validator service.
31#[derive(Serialize, Deserialize)]
32pub struct ValidatorServerConfig {
33    /// The public configuration of the validator.
34    pub validator: ValidatorConfig,
35    /// The secret key of the validator.
36    pub validator_secret: ValidatorSecretKey,
37    /// The internal network configuration of the validator.
38    pub internal_network: ValidatorInternalNetworkConfig,
39}
40
41/// The (public) configuration for all validators.
42#[derive(Debug, Default, Clone, Deserialize, Serialize)]
43pub struct CommitteeConfig {
44    /// The public configurations of all validators in the committee.
45    pub validators: Vec<ValidatorConfig>,
46}
47
48impl CommitteeConfig {
49    /// Converts this committee config into a `Committee` using the given resource control policy.
50    pub fn into_committee(self, policy: ResourceControlPolicy) -> Committee {
51        let validators = self
52            .validators
53            .into_iter()
54            .map(|v| {
55                (
56                    v.public_key,
57                    ValidatorState {
58                        network_address: v.network.to_string(),
59                        votes: 100,
60                        account_public_key: v.account_key,
61                    },
62                )
63            })
64            .collect();
65        Committee::new(validators, policy)
66    }
67}
68
69impl CommitteeConfig {
70    /// Creates a `GenesisConfig` from this committee config with the first chain being the admin chain.
71    pub fn into_genesis(
72        self,
73        timestamp: Timestamp,
74        policy: ResourceControlPolicy,
75        network_name: String,
76        admin_public_key: AccountPublicKey,
77        admin_balance: linera_base::data_types::Amount,
78    ) -> GenesisConfig {
79        let committee = self.into_committee(policy);
80        GenesisConfig::new(
81            committee,
82            timestamp,
83            network_name,
84            admin_public_key,
85            admin_balance,
86        )
87    }
88}