diem_config/config/
safety_rules_config.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    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    // Read/Write/Connect networking operation timeout in milliseconds.
37    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            // Default value of 30 seconds for a timeout
54            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/// Defines how safety rules should be executed
71#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
72#[serde(rename_all = "snake_case", tag = "type")]
73pub enum SafetyRulesService {
74    /// This runs safety rules in the same thread as event processor
75    Local,
76    /// This is the production, separate service approach
77    Process(RemoteService),
78    /// This runs safety rules in the same thread as event processor but data
79    /// is passed through the light weight RPC (serializer)
80    Serializer,
81    /// This creates a separate thread to run safety rules, it is similar to a
82    /// fork / exec style
83    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}