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
8#[cfg(any(test, feature = "fuzzing"))]
9use crate::network_address::{
10    encrypted::{
11        TEST_SHARED_VAL_NETADDR_KEY, TEST_SHARED_VAL_NETADDR_KEY_VERSION,
12    },
13    NetworkAddress,
14};
15use crate::{
16    account_address::AccountAddress,
17    validator_config::{
18        ConsensusPublicKey, ConsensusVRFPublicKey, ValidatorConfig,
19    },
20};
21#[cfg(any(test, feature = "fuzzing"))]
22use proptest_derive::Arbitrary;
23use serde::{Deserialize, Serialize};
24use std::fmt;
25
26/// After executing a special transaction indicates a change to the next epoch,
27/// consensus and networking get the new list of validators, their keys, and
28/// their voting power.  Consensus has a public key to validate signed messages
29/// and networking will has public identity keys for creating secure channels of
30/// communication between validators.  The validators and their public keys and
31/// voting power may or may not change between epochs.
32#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
33#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
34pub struct ValidatorInfo {
35    // The validator's account address. AccountAddresses are initially derived
36    // from the account auth pubkey; however, the auth key can be rotated,
37    // so one should not rely on this initial property.
38    account_address: AccountAddress,
39    // Voting power of this validator
40    consensus_voting_power: u64,
41    // Validator config
42    config: ValidatorConfig,
43    // The time of last recofiguration invoked by this validator
44    // in microseconds
45    last_config_update_time: u64,
46}
47
48impl fmt::Display for ValidatorInfo {
49    fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
50        write!(
51            f,
52            "account_address: {}",
53            self.account_address.short_str_lossless()
54        )
55    }
56}
57
58impl ValidatorInfo {
59    pub fn new(
60        account_address: AccountAddress, consensus_voting_power: u64,
61        config: ValidatorConfig,
62    ) -> Self {
63        ValidatorInfo {
64            account_address,
65            consensus_voting_power,
66            config,
67            last_config_update_time: 0,
68        }
69    }
70
71    #[cfg(any(test, feature = "fuzzing"))]
72    pub fn new_with_test_network_keys(
73        account_address: AccountAddress,
74        consensus_public_key: ConsensusPublicKey,
75        vrf_public_key: Option<ConsensusVRFPublicKey>,
76        consensus_voting_power: u64,
77    ) -> Self {
78        let addr = NetworkAddress::mock();
79        let enc_addr = addr.clone().encrypt(
80            &TEST_SHARED_VAL_NETADDR_KEY,
81            TEST_SHARED_VAL_NETADDR_KEY_VERSION,
82            &account_address,
83            0,
84            0,
85        );
86        let config = ValidatorConfig::new(
87            consensus_public_key,
88            vrf_public_key,
89            bcs::to_bytes(&vec![enc_addr.unwrap()]).unwrap(),
90            bcs::to_bytes(&vec![addr]).unwrap(),
91        );
92
93        Self {
94            account_address,
95            consensus_voting_power,
96            config,
97            last_config_update_time: 0,
98        }
99    }
100
101    /// Returns the id of this validator (hash of the current public key of the
102    /// validator associated account address)
103    pub fn account_address(&self) -> &AccountAddress { &self.account_address }
104
105    /// Returns the key for validating signed messages from this validator
106    pub fn consensus_public_key(&self) -> &ConsensusPublicKey {
107        &self.config.consensus_public_key
108    }
109
110    pub fn vrf_public_key(&self) -> &Option<ConsensusVRFPublicKey> {
111        &self.config.vrf_public_key
112    }
113
114    /// Returns the voting power for this validator
115    pub fn consensus_voting_power(&self) -> u64 { self.consensus_voting_power }
116
117    /// Returns the validator's config
118    pub fn config(&self) -> &ValidatorConfig { &self.config }
119
120    /// Returns the validator's config, consuming self
121    pub fn into_config(self) -> ValidatorConfig { self.config }
122}