forked from CodeChain-io/codechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstratum.rs
151 lines (129 loc) · 4.57 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Client-side stratum job dispatcher and mining notifier handler
use std::net::{AddrParseError, SocketAddr};
use std::sync::Arc;
use crate::error::Error as MinerError;
use cstratum::{Error as StratumServiceError, JobDispatcher, PushWorkHandler, Stratum as StratumService};
use primitives::{Bytes, H256, U256};
use crate::client::Client;
use crate::miner::work_notify::NotifyWork;
use crate::miner::{Miner, MinerService};
/// Configures stratum server options.
#[derive(Debug, PartialEq, Clone)]
pub struct Config {
/// Network address
pub listen_addr: String,
/// Port
pub port: u16,
/// Secret for peers
pub secret: Option<H256>,
}
/// Job dispatcher for stratum service
pub struct StratumJobDispatcher {
client: Arc<Client>,
miner: Arc<Miner>,
}
impl JobDispatcher for StratumJobDispatcher {
fn initial(&self) -> Option<String> {
// initial payload may contain additional data, not in this case
self.job()
}
fn submit(&self, payload: (H256, Vec<Bytes>)) -> Result<(), StratumServiceError> {
let (pow_hash, seal) = payload;
ctrace!(STRATUM, "submit_work: Decoded: pow_hash={}, seal={:?}", pow_hash, seal);
if !self.miner.can_produce_work_package() {
cwarn!(STRATUM, "Cannot get work package - engine seals internally.");
return Err(StratumServiceError::InternalError)
}
match self.miner.submit_seal(&*self.client, pow_hash, seal) {
Ok(_) => Ok(()),
Err(e) => {
cwarn!(STRATUM, "submit_seal error: {:?}", e);
Err(StratumServiceError::from(e))
}
}
}
}
impl StratumJobDispatcher {
/// New stratum job dispatcher given the miner and client
fn new(miner: Arc<Miner>, client: Arc<Client>) -> StratumJobDispatcher {
StratumJobDispatcher {
client,
miner,
}
}
/// Serializes payload for stratum service
fn payload(&self, pow_hash: H256, target: U256) -> String {
format!(r#"["0x{:x}","0x{:x}"]"#, pow_hash, target)
}
}
/// Wrapper for dedicated stratum service
pub struct Stratum {
dispatcher: Arc<StratumJobDispatcher>,
service: Arc<StratumService>,
}
#[derive(Debug)]
/// Stratum error
pub enum Error {
/// IPC sockets error
Service(StratumServiceError),
/// Invalid network address
Address(AddrParseError),
}
impl From<MinerError> for StratumServiceError {
fn from(err: MinerError) -> Self {
match err {
MinerError::PowHashInvalid => StratumServiceError::PowHashInvalid,
MinerError::PowInvalid => StratumServiceError::PowInvalid,
_ => StratumServiceError::InternalError,
}
}
}
impl From<StratumServiceError> for Error {
fn from(service_err: StratumServiceError) -> Error {
Error::Service(service_err)
}
}
impl From<AddrParseError> for Error {
fn from(err: AddrParseError) -> Error {
Error::Address(err)
}
}
impl NotifyWork for Stratum {
fn notify(&self, pow_hash: H256, target: U256) {
ctrace!(STRATUM, "Notify work");
self.service
.push_work_all(self.dispatcher.payload(pow_hash, target))
.unwrap_or_else(|e| cwarn!(STRATUM, "Error while pushing work: {:?}", e));
}
}
impl Stratum {
/// New stratum job dispatcher, given the miner, client and dedicated stratum service
pub fn start(config: &Config, miner: Arc<Miner>, client: Arc<Client>) -> Result<Stratum, Error> {
use std::net::IpAddr;
let dispatcher = Arc::new(StratumJobDispatcher::new(miner, client));
let stratum_svc = StratumService::start(
&SocketAddr::new(config.listen_addr.parse::<IpAddr>()?, config.port),
dispatcher.clone(),
config.secret,
)?;
Ok(Stratum {
dispatcher,
service: stratum_svc,
})
}
}