diem_types/
validator_info.rs1#[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#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
33#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
34pub struct ValidatorInfo {
35 account_address: AccountAddress,
39 consensus_voting_power: u64,
41 config: ValidatorConfig,
43 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 pub fn account_address(&self) -> &AccountAddress { &self.account_address }
104
105 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 pub fn consensus_voting_power(&self) -> u64 { self.consensus_voting_power }
116
117 pub fn config(&self) -> &ValidatorConfig { &self.config }
119
120 pub fn into_config(self) -> ValidatorConfig { self.config }
122}