safety_rules/thread.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//! This provides a execution separation between SafetyRules and Consensus
9//! without requiring the use of processes. Rust does not support fork and so
10//! the mechanics to actually construct a SafetyRules that would run together
11//! and be started by Consensus requires a separate binary and making a call to
12//! start that via a command. This is a lightweight means of accomplishing a
13//! goal in testing correctness of the communication layer between Consensus and
14//! SafetyRules.
15
16use crate::{
17 persistent_safety_storage::PersistentSafetyStorage,
18 remote_service::{self, RemoteService},
19};
20use diem_config::utils;
21use std::{
22 net::{IpAddr, Ipv4Addr, SocketAddr},
23 thread::{self, JoinHandle},
24};
25
26/// ThreadClient is the actual owner of the thread but in the context of
27/// Consenus and SafetyRules is on the client side of the operations as it makes
28/// queries / requests to SafetyRules.
29pub struct ThreadService {
30 _child: JoinHandle<()>,
31 server_addr: SocketAddr,
32 network_timeout: u64,
33}
34
35impl ThreadService {
36 pub fn new(
37 storage: PersistentSafetyStorage, verify_vote_proposal_signature: bool,
38 export_consensus_key: bool, timeout: u64,
39 ) -> Self {
40 let listen_port = utils::get_available_port();
41 let listen_addr =
42 SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), listen_port);
43 let server_addr = listen_addr;
44
45 let child = thread::spawn(move || {
46 remote_service::execute(
47 storage,
48 listen_addr,
49 verify_vote_proposal_signature,
50 export_consensus_key,
51 timeout,
52 // TODO(lpl): Support this?
53 None,
54 )
55 });
56
57 Self {
58 _child: child,
59 server_addr,
60 network_timeout: timeout,
61 }
62 }
63}
64
65impl RemoteService for ThreadService {
66 fn server_address(&self) -> SocketAddr { self.server_addr }
67
68 fn network_timeout_ms(&self) -> u64 { self.network_timeout }
69}