forked from openethereum/parity-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstratum.rs
119 lines (100 loc) · 3.7 KB
/
stratum.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Open Ethereum.
// Open Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Open Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Open Ethereum. If not, see <http://www.gnu.org/licenses/>.
//! Parity sync service
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use ethcore_stratum::{Stratum as StratumServer, PushWorkHandler, RemoteJobDispatcher, ServiceConfiguration};
use modules::service_urls;
use boot;
use hypervisor::service::IpcModuleId;
use hypervisor::{HYPERVISOR_IPC_URL, ControlService};
use std::net::{SocketAddr, IpAddr};
use std::str::FromStr;
use nanoipc;
use std::thread;
use ethcore::miner::stratum::{STRATUM_SOCKET_NAME, JOB_DISPATCHER_SOCKET_NAME};
pub const MODULE_ID: IpcModuleId = 8000;
#[derive(Default)]
struct StratumControlService {
pub stop: Arc<AtomicBool>,
}
impl ControlService for StratumControlService {
fn shutdown(&self) -> bool {
trace!(target: "hypervisor", "Received shutdown from control service");
self.stop.store(true, ::std::sync::atomic::Ordering::Relaxed);
true
}
}
pub fn main() {
boot::setup_cli_logger("stratum");
let service_config: ServiceConfiguration = boot::payload()
.unwrap_or_else(|e| {
println!("Fatal: error reading boot arguments ({:?})", e);
std::process::exit(1)
});
let job_dispatcher = dependency!(
RemoteJobDispatcher,
&service_urls::with_base(&service_config.io_path, JOB_DISPATCHER_SOCKET_NAME)
);
let _ = boot::main_thread();
let service_stop = Arc::new(AtomicBool::new(false));
let server =
StratumServer::start(
&SocketAddr::new(
IpAddr::from_str(&service_config.listen_addr)
.unwrap_or_else(|e| {
println!("Fatal: invalid listen address: '{}' ({:?})", &service_config.listen_addr, e);
std::process::exit(1)
}),
service_config.port,
),
job_dispatcher.service().clone(),
service_config.secret
).unwrap_or_else(
|e| {
println!("Fatal: cannot start stratum server({:?})", e);
std::process::exit(1)
}
);
boot::host_service(
&service_urls::with_base(&service_config.io_path, STRATUM_SOCKET_NAME),
service_stop.clone(),
server.clone() as Arc<PushWorkHandler>
);
let hypervisor = boot::register(
&service_urls::with_base(&service_config.io_path, HYPERVISOR_IPC_URL),
&service_urls::with_base(&service_config.io_path, service_urls::STRATUM_CONTROL),
MODULE_ID
);
let timer_svc = server.clone();
let timer_stop = service_stop.clone();
thread::spawn(move || {
while !timer_stop.load(Ordering::SeqCst) {
thread::park_timeout(::std::time::Duration::from_millis(2000));
// It almost always not doing anything, only greets new peers with a job
timer_svc.maintain();
}
});
let control_service = Arc::new(StratumControlService::default());
let as_control = control_service.clone() as Arc<ControlService>;
let mut worker = nanoipc::Worker::<ControlService>::new(&as_control);
worker.add_reqrep(
&service_urls::with_base(&service_config.io_path, service_urls::STRATUM_CONTROL)
).unwrap();
while !control_service.stop.load(Ordering::SeqCst) {
worker.poll();
}
service_stop.store(true, Ordering::SeqCst);
hypervisor.module_shutdown(MODULE_ID);
trace!(target: "hypervisor", "Stratum process terminated gracefully");
}