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    pub validator: ValidatorConfig,
34    pub validator_secret: ValidatorSecretKey,
35    pub internal_network: ValidatorInternalNetworkConfig,
36}
37
38/// The (public) configuration for all validators.
39#[derive(Debug, Default, Clone, Deserialize, Serialize)]
40pub struct CommitteeConfig {
41    pub validators: Vec<ValidatorConfig>,
42}
43
44impl CommitteeConfig {
45    pub fn into_committee(
46        self,
47        policy: ResourceControlPolicy,
48    ) -> Result<Committee, ArithmeticError> {
49        let validators = self
50            .validators
51            .into_iter()
52            .map(|v| {
53                (
54                    v.public_key,
55                    ValidatorState {
56                        network_address: v.network.to_string(),
57                        votes: 100,
58                        account_public_key: v.account_key,
59                    },
60                )
61            })
62            .collect();
63        Committee::new(validators, policy)
64    }
65
66    /// Creates a `GenesisConfig` from this committee config with the first chain being the admin chain.
67    pub fn into_genesis(
68        self,
69        timestamp: Timestamp,
70        policy: ResourceControlPolicy,
71        network_name: String,
72        admin_public_key: AccountPublicKey,
73        admin_balance: Amount,
74    ) -> Result<GenesisConfig, ArithmeticError> {
75        let committee = self.into_committee(policy)?;
76        Ok(GenesisConfig::new(
77            committee,
78            timestamp,
79            network_name,
80            admin_public_key,
81            admin_balance,
82        ))
83    }
84}