diem_types/
validator_info.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    account_address::AccountAddress,
10    validator_config::{
11        ConsensusPublicKey, ConsensusVRFPublicKey, ValidatorConfig,
12    },
13};
14#[cfg(any(test, feature = "fuzzing"))]
15use proptest_derive::Arbitrary;
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19/// After executing a special transaction indicates a change to the next epoch,
20/// consensus and networking get the new list of validators, their keys, and
21/// their voting power.  Consensus has a public key to validate signed messages
22/// and networking will has public identity keys for creating secure channels of
23/// communication between validators.  The validators and their public keys and
24/// voting power may or may not change between epochs.
25#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
26#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
27pub struct ValidatorInfo {
28    // The validator's account address. AccountAddresses are initially derived
29    // from the account auth pubkey; however, the auth key can be rotated,
30    // so one should not rely on this initial property.
31    account_address: AccountAddress,
32    // Voting power of this validator
33    consensus_voting_power: u64,
34    // Validator config
35    config: ValidatorConfig,
36    // The time of last recofiguration invoked by this validator
37    // in microseconds
38    last_config_update_time: u64,
39}
40
41impl fmt::Display for ValidatorInfo {
42    fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
43        write!(
44            f,
45            "account_address: {}",
46            self.account_address.short_str_lossless()
47        )
48    }
49}
50
51impl ValidatorInfo {
52    pub fn new(
53        account_address: AccountAddress, consensus_voting_power: u64,
54        config: ValidatorConfig,
55    ) -> Self {
56        ValidatorInfo {
57            account_address,
58            consensus_voting_power,
59            config,
60            last_config_update_time: 0,
61        }
62    }
63
64    #[cfg(any(test, feature = "fuzzing"))]
65    pub fn new_with_test_network_keys(
66        account_address: AccountAddress,
67        consensus_public_key: ConsensusPublicKey,
68        vrf_public_key: Option<ConsensusVRFPublicKey>,
69        consensus_voting_power: u64,
70    ) -> Self {
71        let config = ValidatorConfig::new(
72            consensus_public_key,
73            vrf_public_key,
74            vec![],
75            vec![],
76        );
77
78        Self {
79            account_address,
80            consensus_voting_power,
81            config,
82            last_config_update_time: 0,
83        }
84    }
85
86    /// Returns the id of this validator (hash of the current public key of the
87    /// validator associated account address)
88    pub fn account_address(&self) -> &AccountAddress { &self.account_address }
89
90    /// Returns the key for validating signed messages from this validator
91    pub fn consensus_public_key(&self) -> &ConsensusPublicKey {
92        &self.config.consensus_public_key
93    }
94
95    pub fn vrf_public_key(&self) -> &Option<ConsensusVRFPublicKey> {
96        &self.config.vrf_public_key
97    }
98
99    /// Returns the voting power for this validator
100    pub fn consensus_voting_power(&self) -> u64 { self.consensus_voting_power }
101
102    /// Returns the validator's config
103    pub fn config(&self) -> &ValidatorConfig { &self.config }
104
105    /// Returns the validator's config, consuming self
106    pub fn into_config(self) -> ValidatorConfig { self.config }
107}