diem_config/config/
safety_rules_config.rs1use crate::{
9 config::{LoggerConfig, SecureBackend},
10 keys::ConfigKey,
11};
12use cfx_types::U256;
13use diem_crypto::Uniform;
14use diem_types::{
15 network_address::NetworkAddress,
16 validator_config::{ConsensusPrivateKey, ConsensusVRFPrivateKey},
17 waypoint::Waypoint,
18 PeerId,
19};
20use rand::rngs::StdRng;
21use serde::{Deserialize, Serialize};
22use std::{
23 net::{SocketAddr, ToSocketAddrs},
24 path::PathBuf,
25};
26
27#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
28#[serde(default, deny_unknown_fields)]
29pub struct SafetyRulesConfig {
30 pub backend: SecureBackend,
31 pub logger: LoggerConfig,
32 pub service: SafetyRulesService,
33 pub test: Option<SafetyRulesTestConfig>,
34 pub verify_vote_proposal_signature: bool,
35 pub export_consensus_key: bool,
36 pub network_timeout_ms: u64,
38 pub enable_cached_safety_data: bool,
39
40 pub vrf_private_key: Option<ConfigKey<ConsensusVRFPrivateKey>>,
41 pub vrf_proposal_threshold: U256,
42}
43
44impl Default for SafetyRulesConfig {
45 fn default() -> Self {
46 Self {
47 backend: SecureBackend::OnDiskStorage(Default::default()),
48 logger: LoggerConfig::default(),
49 service: SafetyRulesService::Thread,
50 test: None,
51 verify_vote_proposal_signature: true,
52 export_consensus_key: false,
53 network_timeout_ms: 30_000,
55 enable_cached_safety_data: true,
56 vrf_private_key: None,
57 vrf_proposal_threshold: U256::MAX,
58 }
59 }
60}
61
62impl SafetyRulesConfig {
63 pub fn set_data_dir(&mut self, data_dir: PathBuf) {
64 if let SecureBackend::OnDiskStorage(backend) = &mut self.backend {
65 backend.set_data_dir(data_dir);
66 }
67 }
68}
69
70#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
72#[serde(rename_all = "snake_case", tag = "type")]
73pub enum SafetyRulesService {
74 Local,
76 Process(RemoteService),
78 Serializer,
81 Thread,
84}
85
86#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
87#[serde(deny_unknown_fields)]
88pub struct RemoteService {
89 pub server_address: NetworkAddress,
90}
91
92impl RemoteService {
93 pub fn server_address(&self) -> SocketAddr {
94 self.server_address
95 .to_socket_addrs()
96 .expect("server_address invalid")
97 .next()
98 .expect("server_address invalid")
99 }
100}
101
102#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
103pub struct SafetyRulesTestConfig {
104 pub author: PeerId,
105 pub consensus_key: Option<ConfigKey<ConsensusPrivateKey>>,
106 pub execution_key: Option<ConfigKey<ConsensusPrivateKey>>,
107 pub waypoint: Option<Waypoint>,
108}
109
110impl SafetyRulesTestConfig {
111 pub fn new(author: PeerId) -> Self {
112 Self {
113 author,
114 consensus_key: None,
115 execution_key: None,
116 waypoint: None,
117 }
118 }
119
120 pub fn consensus_key(&mut self, key: ConsensusPrivateKey) {
121 self.consensus_key = Some(ConfigKey::new(key));
122 }
123
124 pub fn execution_key(&mut self, key: ConsensusPrivateKey) {
125 self.execution_key = Some(ConfigKey::new(key));
126 }
127
128 pub fn random_consensus_key(&mut self, rng: &mut StdRng) {
129 let privkey = ConsensusPrivateKey::generate(rng);
130 self.consensus_key =
131 Some(ConfigKey::<ConsensusPrivateKey>::new(privkey));
132 }
133
134 pub fn random_execution_key(&mut self, rng: &mut StdRng) {
135 let privkey = ConsensusPrivateKey::generate(rng);
136 self.execution_key =
137 Some(ConfigKey::<ConsensusPrivateKey>::new(privkey));
138 }
139}