2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-08-24 18:35:38 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-08-24 18:35:38 +02:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
//! OpenEthereum sync service
|
2016-08-24 18:35:38 +02:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
2017-01-25 11:03:36 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2016-08-24 18:35:38 +02:00
|
|
|
use ethcore_stratum::{Stratum as StratumServer, PushWorkHandler, RemoteJobDispatcher, ServiceConfiguration};
|
|
|
|
use modules::service_urls;
|
|
|
|
use boot;
|
|
|
|
use hypervisor::service::IpcModuleId;
|
2017-01-25 11:03:36 +01:00
|
|
|
use hypervisor::{HYPERVISOR_IPC_URL, ControlService};
|
|
|
|
use std::net::{SocketAddr, IpAddr};
|
2016-08-24 18:35:38 +02:00
|
|
|
use std::str::FromStr;
|
2017-01-25 11:03:36 +01:00
|
|
|
use nanoipc;
|
|
|
|
use std::thread;
|
|
|
|
use ethcore::miner::stratum::{STRATUM_SOCKET_NAME, JOB_DISPATCHER_SOCKET_NAME};
|
|
|
|
|
|
|
|
pub const MODULE_ID: IpcModuleId = 8000;
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2017-01-25 11:03:36 +01:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 18:35:38 +02:00
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
boot::setup_cli_logger("stratum");
|
|
|
|
|
|
|
|
let service_config: ServiceConfiguration = boot::payload()
|
2017-01-25 11:03:36 +01:00
|
|
|
.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)
|
|
|
|
);
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2017-01-25 11:03:36 +01:00
|
|
|
let _ = boot::main_thread();
|
|
|
|
let service_stop = Arc::new(AtomicBool::new(false));
|
2016-08-24 18:35:38 +02:00
|
|
|
|
|
|
|
let server =
|
|
|
|
StratumServer::start(
|
2017-01-25 11:03:36 +01:00
|
|
|
&SocketAddr::new(
|
|
|
|
IpAddr::from_str(&service_config.listen_addr)
|
2017-05-19 17:12:20 +02:00
|
|
|
.unwrap_or_else(|e| {
|
2017-01-25 11:03:36 +01:00
|
|
|
println!("Fatal: invalid listen address: '{}' ({:?})", &service_config.listen_addr, e);
|
|
|
|
std::process::exit(1)
|
2017-05-19 17:12:20 +02:00
|
|
|
}),
|
2017-01-25 11:03:36 +01:00
|
|
|
service_config.port,
|
|
|
|
),
|
2016-08-24 18:35:38 +02:00
|
|
|
job_dispatcher.service().clone(),
|
|
|
|
service_config.secret
|
|
|
|
).unwrap_or_else(
|
2017-01-25 11:03:36 +01:00
|
|
|
|e| {
|
|
|
|
println!("Fatal: cannot start stratum server({:?})", e);
|
|
|
|
std::process::exit(1)
|
|
|
|
}
|
2016-08-24 18:35:38 +02:00
|
|
|
);
|
|
|
|
|
2017-01-25 11:03:36 +01:00
|
|
|
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
|
|
|
|
);
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2017-01-25 11:03:36 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2016-08-24 18:35:38 +02:00
|
|
|
|
2017-01-25 11:03:36 +01:00
|
|
|
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();
|
2016-08-24 18:35:38 +02:00
|
|
|
}
|
2017-01-25 11:03:36 +01:00
|
|
|
service_stop.store(true, Ordering::SeqCst);
|
|
|
|
|
|
|
|
hypervisor.module_shutdown(MODULE_ID);
|
|
|
|
trace!(target: "hypervisor", "Stratum process terminated gracefully");
|
2016-08-24 18:35:38 +02:00
|
|
|
}
|