openethereum/parity/stratum.rs
Nikolay Volf 1acc8031ce Stratum up (#4233)
* flush work

* flush work

* flush work

* flush work

* generalized notifiers

* general setup with modules

* general setup with modules

* all binded

* catch up with master

* all dependencies injected

* stratum another up

* tcp update

* submitwork routine

* finalize & fix warnings

* merge bugs, review fixes

* merge bugs, review fixes

* new cli mess cleanup

* usage.txt swap

* flush work

* cli adopt

* compilation with new cli sorted

* subid space in json

* serialization issues

* grumbles addressed

* more grumbles

* remove last_work note for now

* fix compilation

* fix tests

* merge bugs

* no obliged ipc

* moving notifiers

* no optional feature now

* refactored again

* working on tests

* refactor to new tcp/ip

* stratum lib ok

* ethcore crate ok

* wip on tests

* final test working

* fix warnings, \n-terminated response

* new compatibility

* re-pushing work once anybody submitted

* various review and general fixes

* reviewe fixes

* remove redundant notifier

* one symbol -> huge bug

* ensure write lock isn't held when calling handlers

* extern declarations moved

* options to stratum mod, SocketAddr strongly-typed instantiation

* Minor style fix.

* Whitespace and phrasing

* Whitespace
2017-01-25 11:03:36 +01:00

120 lines
3.7 KiB
Rust

// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity 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.
// Parity 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 Parity. 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");
}