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::{Amount, ArithmeticError, 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(
51        self,
52        policy: ResourceControlPolicy,
53    ) -> Result<Committee, ArithmeticError> {
54        let validators = self
55            .validators
56            .into_iter()
57            .map(|v| {
58                (
59                    v.public_key,
60                    ValidatorState {
61                        network_address: v.network.to_string(),
62                        votes: 100,
63                        account_public_key: v.account_key,
64                    },
65                )
66            })
67            .collect();
68        Committee::new(validators, policy)
69    }
70
71    /// Creates a `GenesisConfig` from this committee config with the first chain being the admin chain.
72    pub fn into_genesis(
73        self,
74        timestamp: Timestamp,
75        policy: ResourceControlPolicy,
76        network_name: String,
77        admin_public_key: AccountPublicKey,
78        admin_balance: Amount,
79    ) -> Result<GenesisConfig, ArithmeticError> {
80        let committee = self.into_committee(policy)?;
81        Ok(GenesisConfig::new(
82            committee,
83            timestamp,
84            network_name,
85            admin_public_key,
86            admin_balance,
87        ))
88    }
89}